A simple AVR RTC using built-in Timers

In my current project I am implementing a simple data logger. When the hardware was designed we did not implement a nice RTC module, such as the chip used in Sparkfun’s clock module here: http://www.sparkfun.com/commerce/product_info.php?products_id=99. In lieu of such a nice solution I have opted to implement a basic, semi-accurate clock using an 8-bit timer in the AVR. Here’s the steps:

  1. Calculate from the clockspeed the interval and width that timer needs to fire at to record a 1Hz measurement
  2. Enable timer interrupts
  3. Capture the timer interrupt and increment a 4-byte integer

In my case the microcontroller is running off of a 8Mhz crystal. Setting the timer prescaler to clk/256 produces 31,250 clock pulses. Setting OCRA to 250 and counting up to 124 before incrementing the second produces 31,250/250/124 = 1 pulse per second.

For this project I have decided to store the time as the number of seconds elapsed since January 1st, 2001. This is convenient because the host system can handle all the nasty stuff such as leap-years and other oddities in the calendar while the microcontroller just stores a number.

And without further ado I shall present some code!

AVR timer.c

In addition to this AVR code some host-side code is needed to set and read the time from the AVR accurately. For this I have used VB.Net.

Generating appropriate times

Testing has shown this clock to drag a millisecond or two for every minute of operation. Not the greatest performance but for a data logger it is adequate.

Posted

0 comments

Leave a comment...

? ?