Atmega32 timming

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I built a 4 digit clock that counts minutes and seconds using atmega32 and large 5 inch 7-segment display and it is working fine.
My problem is that it is not that accurate, for example I started it with my mobile stop watch and after 10 minutes there was 1 sec difference, after 30 minutes the difference increased to about 10 sec or so then after 1 hour it was like 25 sec(where the clock i built was leading i.e. count8ng faster). Each I reset the atmega32 the rate of difference change, like in the second try after 30 minutes the difference was 8 sec( not 10) .
I am using timer 1 in output compare mode and I set the compare value to 7812 where the cpu frequency is set to 8MHz using internal oscillator and used prescaler 1024 (8000000/1024=7812.5)
I tried increasing the compare value till it reached 8100,the difference did decrease but it is still there. So what could be going wrong here?

Last Edited: Sat. Feb 9, 2019 - 08:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also when I changed the 7-segments to smaller once the difference decreases, could it be related to power consumption or so?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The internal oscillator is not accurate. You will need to use an external crystal.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Seif_1999 wrote:
could it be related to power consumption or so?

It is.
Read the datasheet.
See "Calibrated 8MHz RC Oscillator Frequency vs. VCC"
and "Calibrated 8MHz RC Oscillator Frequency vs. Temperature"

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try subtracting 1 from the content of the OSCCAL register. That should slow down your clock a bit. If you have a debounced push button input you could use that to subtract 1 per push several times. The oscillator frequency varies with temperature and VCC. At 08:08 the speed will be different from 11:11.

This should get your frequency accurate enough for 59 minutes. For a clock that runs for days it will not be good enough using the RC oscillator. You can use the power line frequency or a crystal.

Each reset restores the OSCCAL register and the magic subtract number will be different for each chip. EEPROM could be used to store the correction so it could be applied at each restart.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I believe that a real-time clock is one instance where it would be best to use a external DS3231 module board for the timing circuitry.  They are cheap, very accurate, easy to use, and easy to obtain from eBay: https://www.ebay.com/itm/DS3231-...

 

I have initialized several of these modules to the correct time and date, put them on the shelf, and come back to them about a year later.  They were showing the correct time to within a minute.  They have internal 32768Hz crystals that are calibrated and compensated for the DS3231 circuitry.  Unless your CPU is running on a time base that controlled by 32768Hz crystal, then your clock is going to be inaccurate.   For about a dollar or a Euro, this is definitely a no-brainer.