Author Topic: Atmega8 Timer0Clock  (Read 3657 times)

0 Members and 1 Guest are viewing this topic.

Offline fyrewurxTopic starter

  • Contributor
  • Posts: 16
Atmega8 Timer0Clock
« on: March 02, 2013, 09:14:32 pm »
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
« Last Edit: March 02, 2013, 09:24:44 pm by fyrewurx »
 

Offline ve7xen

  • Super Contributor
  • ***
  • Posts: 1192
  • Country: ca
    • VE7XEN Blog
Re: Atmega8 Timer0Clock
« Reply #1 on: March 02, 2013, 09:48:35 pm »
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.
73 de VE7XEN
He/Him
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: Atmega8 Timer0Clock
« Reply #2 on: March 02, 2013, 09:53:46 pm »
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.

Offline firewalker

  • Super Contributor
  • ***
  • Posts: 2450
  • Country: gr
Re: Atmega8 Timer0Clock
« Reply #3 on: March 02, 2013, 10:03:38 pm »
The best way is to use timer/counter2 clocked by an a 32768 Hz crystal.

http://www.atmel.com/Images/doc1259.pdf

Alexander.
Become a realist, stay a dreamer.

 

Offline fyrewurxTopic starter

  • Contributor
  • Posts: 16
Re: Atmega8 Timer0Clock
« Reply #4 on: March 06, 2013, 10:10:50 am »
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
 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
Re: Atmega8 Timer0Clock
« Reply #5 on: March 06, 2013, 10:24:47 am »
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,
 

Offline ktulu

  • Regular Contributor
  • *
  • Posts: 52
Re: Atmega8 Timer0Clock
« Reply #6 on: March 11, 2013, 10:11:21 am »
And make one of the capacitors tunable, because I'm sure that if you wait a couple of days, you will get some drift.
 

Offline fyrewurxTopic starter

  • Contributor
  • Posts: 16
Re: Atmega8 Timer0Clock
« Reply #7 on: March 11, 2013, 10:25:52 am »
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.
 

Offline FrozenHaxor

  • Contributor
  • Posts: 30
  • Country: pl
    • My random stuff
Re: Atmega8 Timer0Clock
« Reply #8 on: March 11, 2013, 02:03:42 pm »
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 :)
 

Offline Len

  • Frequent Contributor
  • **
  • Posts: 547
  • Country: ca
Re: Atmega8 Timer0Clock
« Reply #9 on: March 12, 2013, 07:30:40 pm »
You can also trim it in the firmware, adding or skipping a few ticks each hour. I did that with my Adafruit Ice Tube clock and it keeps very accurate time.
DIY Eurorack Synth: https://len42.github.io/Synth/
 

Offline ve7xen

  • Super Contributor
  • ***
  • Posts: 1192
  • Country: ca
    • VE7XEN Blog
Re: Atmega8 Timer0Clock
« Reply #10 on: March 13, 2013, 03:52:20 am »
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).
Wouldn'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 minutes a week.
73 de VE7XEN
He/Him
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf