For safety, and to keep the voltages more reasonable, the mains AC is stepped down to approximately 9 VAC, although really anything over 5 VAC would work just fine for this design.
***PARTS LIST***
(1) PIC16F688
(3) Rectifier Diode
(1) 5V Regulator
(1) Large Capacitor
(1) Smaller Capacitor
(2) 5.1V Zener Diode
(1) 9 VAC Transformer
(1) Transistor
Assorted Resistors
***SCHEMATIC***
To keep things simple and cheap, a diode is used as a half-wave rectifier. The signal is voltage regulated with a resistor and 5.1V Zener to create a 50/60Hz quasi-square wave that is fed to the PIC's interrupt.
The half wave rectified signal is further isolated from the DC side with another diode. The DC side is heavily filtered with a beefy capacitor, then sent through a 5V regulator to power the PIC.
***CODE***
I wrote the code initially very simply by polling the AC input for a high, then waiting 5ms, if the input was still high, the count was incremented. This worked just fine, but I figured using the rising edge as an interrupt would be more appropriate. This also freed up processing time to store the current count in the EEPROM, which, from what I read, can take 4-10ms. I scrapped this idea later on when I read that the EEPROM has a 1,000,000 write cycle lifetime.
I want this functionality so that the count isn't lost during a power outage. For this, I have yet to test it, but I plan to use another diode after the half-wave rectifying diode with a capacitor (much smaller than the DC filter capacitor) and 5.1V zener/resistor combo. The zener output is connected to another input on the PIC. The PIC will poll this input and once it detects a low due to power outage, the count is stored in the EEPROM while being powered from large DC filter capacitor.


Menu
Recent Articles



Using PDS with SPI GLCD based on ST7565R Controller
Graphic LCDs based on the ST7565 are cheaper then GLCDs with other controllers. SPI requires only four pins. If the circuit