PDA

View Full Version : 60Hz timebase clock



astouffer
- 10th January 2014, 19:20
Hello all, I am in the planning stages for a simple clock that uses numitron tubes for the display. That part is pretty much worked out. I decided against using a realtime clock chip as they do need an initial date setting and battery backup. My idea is to base it from the 60Hz power. For isolation and protection against spikes I plan to drive the LED side of an optic isolator using a small step down transformer. That should give nice clean 60Hz pulses on the output side. So far so good.

Now for the timekeeping part. How about preloading an 8 bit counter with 195 so it will overflow on the 60th pulse? Then the interrupt goes to the timekeeping code where each overflow is counted as a second. The time between these periods should be plenty for the necessary math and writing to the display.

Any comments appreciated.


Adam

Demon
- 10th January 2014, 23:26
If you have a logic probe like this one:
http://www.saleae.com/logic
then it simplifies testing radically.

You get to measure hertzes down to 4 decimal places, as well as view several communication protocols like I2C, SPI, 1-wire, CAN, etc. I used it to get LEDs blinking at 120Hz and I have to say it works great (wish I could afford to upgrade to Logic 16).

About the preloading, that's what I did using Darrel's template. Details here:
http://www.darreltaylor.com/DT_INTS-14/TimerTemplate.html

The only limitation I found with the template is the 16 MHz cap, other than that it was easy to set up. It's too bad 'cause I was running the 18F44K22 at 64MHz until now. I wished I knew enough about interrupts to go without the template, but hey, it's the price I have to pay to be me.

Robert

astouffer
- 11th January 2014, 21:21
I don't have to measure the accuracy of my timebase, just count 60 pulses to give one second ticks. The power company makes small frequency adjustments over a 24 hour time so it always averages to 60Hz. Some quick testing with a transformer and a 4N25 optic isolator gave me a decent 60Hz square wave output. Not a 50% duty cycle but more like 60% on 40% off. Setting TMR0 to 195 and then having an interrupt when it rolls over looks like it will work.

Demon
- 11th January 2014, 22:31
... The power company makes small frequency adjustments over a 24 hour time so it always averages to 60Hz...

I didn't know they bothered.


If you're only interested in triggering an event every 60 Hz, have you looked at Darrel's interrupt I posted above? You can control the exact hertz you want a trigger without external parts. If you're not going faster than 16 MHz it's perfect for you.

Robert

Amoque
- 13th January 2014, 01:34
Well, since you promised to appreciate any comments...

I'd use an RTC anyway. Some of the DS series are cheap, do not require a backup battery (actually, even those that make the option available need not have it to run) and provide a 1 Hz output without setting the time explicitly. Not sure why you wouldn't want to have a battery backed up time at your disposal, but if you don't... [shrug] ... you don't have to use it.

There's also a good article here somewhere (http://www.picbasic.co.uk/forum/showthread.php?t=2129) about making a very accurate 1 second timer with no other time base than an external crystal... that would be my second choice.

On the other hand, Robert's suggestions are valid - as are your own.

astouffer
- 15th January 2014, 03:06
Amoque: Using the 60Hz power for a source will give me more accuracy than an RTC chip and be cheaper. I actually built a large clock based on the link you posted. It worked great but requires long term tweaking for accuracy. Even a 20ppm crystal it still gained a minute or two every month. Here is me testing one of the digits
https://www.youtube.com/watch?v=ACQd9gfyBRE

This code runs on a 16F684 and blinks an LED on pin 10 at 1Hz when fed a 60Hz input on pin 11. Of course the final version will be doing much more than blinking an led. I plan to use four IV-9 numitron tubes for the digits. http://hackadaycom.files.wordpress.com/2011/12/numitron.jpg


@ __CONFIG _FOSC_INTOSCIO & _WDTE_OFF & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOD_OFF & _BOREN_OFF & _IESO_OFF & _FCMEN_OFF


OSCCON = %01110101 'Internal OSC 8MHz
DEFINE OSC 8 '8MHz
PORTA = 0 'All low
TRISA = %00000100 'Porta.2 input
ANSEL = 0 'No analog
WPUA = 0 'Weak pull up off
CMCON0 = %00000111 'Comparators off and I/O is digital
ADCON0 = 0 'A/D is off
CCP1CON = 0 'PWM and CCP off
OPTION_REG = %11111000 'Timer0 on
TMR0 = 195 'Preload so it rolls over on the 60th pulse
INTCON = %11100000 'INTCON.2 overflow bit

on interrupt goto blink

main:
@ NOP
goto main

disable

blink:
toggle portc.0
TMR0=195
INTCON.2 = 0
resume
enable

end

Almost forgot the basic schematic. Use a scope to see what resistor values give you the best output. My meter read 3mA going into a 4N25 optic isolator. http://i.imgur.com/fHWGJHr.jpg

Amoque
- 15th January 2014, 13:06
Ok... I didn't mean to infer I didn't think it would work. I know LOTS of cheap kitchen clocks use that method for just the reasons you state (cheap and accurate). Probably an easy way to get the whole assembly is to hit up Goodwill and look through the clocks...

Me? I tend to overcomplicate things. I'd probably order up the DS3231 module from Banggood for $2.70, then display the date, day of the week, daily hi and low temperature... In the end, if I wanted to know the time, I'd end up having to look at my watch!

astouffer
- 18th January 2014, 06:38
Ok so apparently my math skills may be a bit rusty :o Doing some initial testing the timekeeping part seemed to be losing a few seconds pretty quickly. TMR0 should be 196!