
A while back we saw a logic clock that used the alternating current frequency from the power grid to keep time. We asked for information on your projects that use this method and we got a lot of comments and tips. Today we’re sharing [Doug Jackson's] method which he used in his word clock.
The schematic above is from that project and we’ve outlined the important part in green. [Doug] pulls a signal from the 9V AC power before it hits the bridge rectifier, using a 100K resistor and a zener diode to protect the microcontroller pin. The code for that project comes as a hex file but he sent us the C code pertaining to this timing circuit. It’s written for PIC but you’ll have no trouble adapting it to other microcontroller families. Take a look after the break.
// Set the frequency of the local mains - MUST BE SET OR THE CLOCK WILL BE USELESS//#define MAINS_FREQ 50 // the local mains supply frequency for Australia#define MAINS_FREQ 60 // the local mains supply frequency for the USAT1CON = 0b00000011; // Timer 1 control// Prescale - 00 - 1:1, Oscilator disabled, External Clock, Timer Enebledvoid incrementtime(void){// increment the time counters keeping care to rollover as required sec=0; if (++min >= 60) { min=0; if (++hour == 13) { hour=1; } }} void interrupt my_isr(void){ // test to see if it was a Timer 1 interrupt - External mains source if ((TMR1IE) && (TMR1IF) && (mode==0)){ sec++; // increment the seconds counter TMR1IF=0; TMR1H=0xff; TMR1L=MAINS_DLY; // reset TMR1H and TMR1L to go off in the // appropriate number of cycles based on the local // supply frequency }}void main(void){ init(); // initialise the hardware testleds(); // test the LED array version(); // Display the version number of the software displaytime(); // display the current time TMR1H=0xff; TMR1L=MAINS_DLY; // reset TMR1H and TMR1L to go off in the number of // mains cycles based on the local mains frequency ei(); while (1) { //test to see if we need to increment the time counters if (sec==60) { incrementtime(); displaytime(); } ....... etc etc etc - do the display of the time as required..... }}
