There are a number of ways to generate one second intervals. One way is to use a 32.768kHz crystal as your clock source. These are often used for this because 32768 is 2^15 power and so your clock is easily divided down to give one second intervals. Or put another way, you can use run this crystal into your AVR timer pin. If you use an 8- bit timer, then each time the timer rolls over (256 counts) is 1/128th of a second. Count 128 of those roll overs and you have a second. If you use a 16-bit counter, each roll over of the counter is exactly 2 seconds so you want set up the counter with a preloaded value of half the full count so you can get a one-second interval. Another way to do it is to use the utility company's clock. Not sure how the rest of the world is, but in the USA the 60Hz frequency is pretty accurate, certainly over a period of time. So one way to generate a clock is to use a step down isolation transformer and step down your 120V/60Hz signal to something like 5V/60Hz or whatever low voltage you like. You can then use basic circuitry (optocouplers, bridges, etc.) and generate "zero crossing" pulses from the line frequency. For the USA, those zero crossing pulses will be 120 pulses per second. With Europe and other places that use 50Hz your zero crossing will be 100 pulses per second. Count the pulses and every 120 (or 100) will be a second. As I said, you might get some variation from second to second, but over any period of time, you can get an accurate clock. By period of time I mean if you are designing a desk clock this will work great or if you are making a timer for cooking. If you need to be accurate down to the millisecond at all times, this method might give a tiny bit of variation from one millisecond to the next. Also, as a final note, some of the AVRs with internal oscillators can be calibrated to get a much more accurate internal clock. Atmel has a tech note on this, so if you want to use your internal oscillator, you can get a much better clock source by going through the calibration procedure. See the Atmel document "AVR054" for further details. Apparently this method can get you to within +/- 1% of the frequency stated on the data sheet rather than the factory specification of +/- 3% and within 2% for those devices where the internal RC oscillator is only +/- 10%. Patrick
Message
Re: Second Generating
2006-09-25 by Patrick Timlin
Attachments
- No local attachments were found for this message.