[Circuitsville logo]
[ Home ] [ Services ] [ Anadigm ] Micro Controllers [ Contact ] [ About ] [ Links ] [ Atmel Microcontroller & Arduino Hardware  ] [ Applications for Arduino - Real Time Clock ] [ Applications for Arduino - Pseudo Random Numbers ] [ Applications for Arduino - Extended Input/Output ] [ Arduino Shield Starter Templates ] [ ] [ ]

        
           
		

Real Time Clocks in a Microcontroller

There are several places on the web that discuss Real Time Clocks. Real Time Clocks are ubiquitous applications for any microcontroller, whether it is running a remote antarctic weather station or a kitchen microwave oven. Any device that has to do a task at a specific time or for a specific time period needs such a clock.

The fundamental operation requires setting up a 16 bit counter/timer to interrupt when the counter overflows (goes from 9999 to 0000). For a precise count it is possible with some microcontrollers to use a 32.768kHz crystal and have the counter count the oscillations. The value 32,768 is 2 raised to the 15 power or half the count of a 16 bit counter. In this example I used the internal clock, divided down to something close to what I needed.

When the counter overflows it causes an interrupt. The Interupt Service Routine (or ISR) then resets and restarts the counter, Then it counts one second and determines if the minutes, hours, days, etc need to be incremented or reset. Then the microcontroller returns to its previously scheduled program, already in progress, and picks up from where it was interrupted.

For the Arduino there are some complications. The standard documentation does not discuss interrupt service routines and most of the registers that have to be set are hidden below the standard user interface. This is where the Arduino Forum comes in handy. The Forum can be searched for topics that might be similar to your project. In this case I learned how to do an ISR and how to set the registers.

I didn't do anything fancy with this. There is no output display, nor method of setting the original time or changing time zones or daylight time. What this is is the code for the heart of a Real Time Clock within an Arduino. The rest is up to you.

Code to Download is here (for Arduino 18 & and Arduio Mega) A print of the code as PDF is here





Back to top
Contact Us
©Copyright 2006 - 2016 Circuitsville Engineering,
revised 25 January 2016