Atmega8 Timer0Clock - Page 1
Forum section: electronics / general

author: fyrewurx
author_rank: Contributor
timestamp: March 02, 2013, 09:14:32 pm
content: Hi everyone,First of all I want to apologize for my bad english second I hope I posted this were it's supposed to (yes I am new).I barely started to program some Atmega8, making some progress, still afraid to mess around with fuse bits.I scrached the surface of what timers can do, and I came up with this: clock with timer0.The code is in the attachments, didn't wanted to make a very long post. It actually works. Sort of. Now you can stop laugh.Problem is that is off by about 2 minutes every hour. I am using the internal RC oscillator at 1Mhz and Atmel Studio 6.I would like to know, is something about the code that sets it off, or the internal oscillator is way too off, because my next ideea is to use a cheap chinese clock on T0 on rising edge to make it tick. If the code is OK not causing the delay then using an "external clock source (literally)" should give me the precision of the cheap clock I use for this purpose.Thank you,Alex

author: ve7xen
author_rank: Super Contributor
timestamp: March 02, 2013, 09:48:35 pm
content: The 'calibrated' internal RC oscillator is only calibrated to something like +/-10%, which is nowhere near good enough for timekeeping. It's also quite temperature and voltage dependent.You need an external oscillator (or crystal), either replacing the RC oscillator as the main system clock or as a timer clock input. I don't use the ATmega8, but most AVRs have a 32768Hz oscillator option which can directly clock one of the timers. Just add a watch crystal and maybe a couple of capacitors.

author: andersm
author_rank: Super Contributor
timestamp: March 02, 2013, 09:53:46 pm
content: The internal RC oscillator is not very accurate at all. The datasheet provides characteristic curves for frequency vs. voltage and temperature. Elsewhere the figure of ±3% at 1MHz when running at 5V and 25 degrees C is given, which isn't too far off from what you're getting. Even the cheapest 32.768kHz watch crystal will be far more accurate.

author: firewalker
author_rank: Super Contributor
timestamp: March 02, 2013, 10:03:38 pm
content: The best way is to use timer/counter2 clocked by an a 32768 Hz crystal.http://www.atmel.com/Images/doc1259.pdfAlexander.

author: fyrewurx
author_rank: Contributor
timestamp: March 06, 2013, 10:10:50 am
content: Thank you all for your answers and sorry for the late response.After many failed attepts, I found some code and actually works like a charm. Only one extra component (32Khz crystal from a printer) and the clock is perfect, no observable delay in over 12 h. It's on a breadboard and I didn't had to use any caps, just the crystal between TOSC1 and TOSC2. I just have to add a couple of buttons and some code to be able to adjust the clock, maybe a calendar. Never thought that you can make your own Ultra-configurable clock with aprox 4 dolars.Cheers,Alex

author: Rerouter
author_rank: Super Contributor
timestamp: March 06, 2013, 10:24:47 am
content: you may want to use some capacitors if you ever go to move it off a breadboard, by nature they are quite high in parasitic capacitance, so could be keeping it in the threshold of what will work,

author: ktulu
author_rank: Regular Contributor
timestamp: March 11, 2013, 10:11:21 am
content: And make one of the capacitors tunable, because I'm sure that if you wait a couple of days, you will get some drift.

author: fyrewurx
author_rank: Contributor
timestamp: March 11, 2013, 10:25:52 am
content: Yes, that is exactly what happened. But in an strange way. I compare it to the clock from my pc (should be precise I guess). Yesterday was 6 minutes ahead (initially the difference was a few seconds), today is only 5. I will buy another LCD and move off breadboard and definetly use tunable cap.

author: FrozenHaxor
author_rank: Contributor
timestamp: March 11, 2013, 02:03:42 pm
content: Last time I did a clock on ATMega8, I've used a 32.768 kHz watch crystal and 2 trim caps.More accurate than my PC and Cell phone

author: Len
author_rank: Frequent Contributor
timestamp: March 12, 2013, 07:30:40 pm
content: You can also trim it in the firmware, adding or skipping a few ticks each hour. I did that with myAdafruit Ice Tubeclock and it keeps very accurate time.

author: ve7xen
author_rank: Super Contributor
timestamp: March 13, 2013, 03:52:20 am
content: Quote from: fyrewurx on March 11, 2013, 10:25:52 amWouldn't count on it unless it's properly synchronized via NTP. Most computer clocks are pretty crap at keeping time, compared to timekeeping devices. NTP on the nearest terminal says the attached machine is more than 50ppm out - that's a few minutes a month. One of my wonkier machines is over 300ppm off frequency - that's a few minutesa week.