Real Timer Using Microcontroller ATME8535
Real Timer Using Microcontroller ATME8535
Real Timer Using Microcontroller ATME8535
Abstract— The sector of microcomputers is a constantly In principle, a timer is a simple counter. Its advantage
growing sector with unlimited applications. The subject of is that the input clock and operation of the timer is inde-
this application is the implementation of a REAL TIMER pendent of the program execution. The deterministic clock
AND TIME COUNTER with lcd display and joystick for makes it possible to measure time by counting the elapsed
continuous control in order to demonstrate the rich set of cycles and take the input frequency of the timer into ac-
capabilities of microcomputers. In the frame of this applica-
tion, the microcomputer ATmega8536 of Atmel, a large
count.
number of peripherals (display, keyboard, communication A) WHY DIFFERENT TIMERS. To have the right
ports, frequency generator), control integrated circuits (regu- timer for the right application and to have enough re-
lator LM7805, timer NE555, MAX232) as well as support sources to do this. Generally, the 90S- and megaAVRs
devices (ports, voltage regulators , circuit NE555, LCD
2x16)were selected and the hardware and software architec- have two 8-bit and one 16-bit timer[2]. A timer with 16-bit
tures were defined. Then a prototype board was constructed, resolution is certainly more flexible to use than one with 8-
software in assembly was developed (drivers for all the peri- bit resolution. For many applications, it is sufficient to
pherals, serial/parallel communications, interrupt service have 8-bit resolution. Using a higher resolution means a
routines, internal routines for access eeprom ) and after some larger program overhead, which costs processing time and
optimization, the final boards was constructed with design should be avoided in speed optimized code. It also means
suite OrCAD. higher device cost. Because of the flexibility of the AVR
timers, they can be used for different purposes. The num-
INTRODUCTION ber of timers determines the amount of independent confi-
This application note describes how to implement a gurations. In the following, the different configuration
Real Time Clock (RTC) on AVR microcontrollers that options will be described more closely.
features the RTC module. The implementation requires
B) TIMER EVENTS. The timer of the AVR can be
only one discrete component – a 32.768 kHz watch crystal.
specified to monitor several events. Status flags in the
The application has very low power consumption because
TIMSK register show if an event has occurred. The AT-
the microcontroller operates in Power-save mode most of
MEGA8535 can be configured to monitor up to three
the time. In Power-save mode the AVR controller is sleep-
events per timer Timer Overflow, Compare Match and
ing with only a Timer running. The Timer is clocked by
Input Capture[1].
the external crystal. On every Timer overflow the time,
date, month, and year are counted. This RTC implementa- C) TIMER OVERFLOW. A timer overflow means
tion is written for the ATmega8535, and can easily be that the counter has counted up to its maximum value and
ported to other AVRs with RTC Module. The advantages is reset to zero in the next timer clock cycle. The resolution
of implementing a RTC in software compared to an exter- of the timer determines the maximum value of that timer.
nal hardware RTC are obvious: There are two timers with 8-bit resolution and one timer
with 16-bit resolution on the ATMEGA8535. The maxi-
• Lower cost mum value a timer can count to can be calculated by Equa-
• Few external components tion 1. Res is here the resolution in bits. The timer over-
flow event causes the Timer Overflow Flag (TOVx) to be
• Lower power set in the Timer Interrupt Flag Register (TIFR)[2].
• Greater flexibility
MaxVal = 2Res – 1 (1)
II. GENERAL DESCRIPTION
D) CLOCK OPTIONS. The clock unit of the AVR
timers consists of a prescaler connected to a multiplexer. A
prescaler can be described as a clock divider. Generally, it by an asynchronous external clock. For this purpose a
is implemented as a counter with several output signals at crystal or a ceramic resonator can be connected to the on
different counting stages. In the case of the ATME- board oscillator via the pins TOSC1 and TOSC2. The os-
GA8535, a 10-bit counter is used to divide the input clock cillator is optimized for a watch crystal of 32.768 kHz.
in four (six in case of the Timer2) different prescaled This frequency is well suited for the implementation of
clocks. The multiplexer is used to select which prescaled Real Time Clocks (RTC)(1). There the main advantage of
clock signal to use as input signal for the Timer. Alterna- a separate clock is shown – it is independent of the system
tively, the multiplexer can be used to bypass the prescaler clock. This makes it possible to run the part at a high
and configure an external pin to be used as input for the processing frequency while the timer is clocked by an ex-
Timer. The fact that there are two prescalers available, but ternal clock with a frequency optimized for accurate tim-
three different Timers, has to be seen in context with ing. Additional power save mode support allows putting
which clock source the prescaled value is based on. Both the part in sleep mode while the asynchronous timer is still
Timer0 and Timer1 are synchronous timers and use the in duty. Note: The external clock frequency should be in
system clock (CPU clock) as input source. In this case the interval [0 Hz .. 256 kHz] and maximum CK/4.
there is no limitation if both use the same prescaler (as
long as each timer can be configured separately). Howev- Asynchronous operation requires some additional con-
er, the asynchronous clocked Timer2 needs its own presca- sideration. Because the clocking of Timer2 is asynchron-
ler to be independent of the system clock. Figure 1 shows ous, the timer events have to be synchronized by the CPU.
the prescaling and the configuration unit. The data sheets This requires a timer clock frequency which is at least four
contain more detailed drawings showing all prescalers and times lower than the system clock. On the other hand, con-
multiplexers. An overview of the possible clock settings is flicts between the synchronous and the asynchronous
given in Table 1. In the following sections these settings access have to be avoided. This is done by using tempo-
will be described more clearly. rary registers. Status bits signalize when an update of the
configuration registers is in process. See the description of
the Asynchronous Status Register (ASSR) in the data sheet
for details. The TOVCK is calculated according to equa-
Notes:
tion 2, but by using the oscillator frequency instead of the
1. The prescaler is constantly running during opera- system clock. The settings of TCCR2 are given in Table 1.
tion. In cases where the timer has to count very accurately The prescaler input clock PCK2 is a function of the AS2
it has to be ensured that the prescaler starts counting from bit in the ASSR register. If this bit is cleared, the timer
zero. On parts without prescaler reset this has to be done runs in synchronous mode with the system clock as input
by detecting the prescaler overflow by software and the frequency. If this bit is set, the asynchronous clock signal
initialization of the timer counter TCNTx register after- on the pins TOSC1 and TOSC2 is used as input signal of
wards. the prescaler[4].
2. On newer devices with shared prescaler, executing a
prescaler reset will affect all connected timers[4].
E) CLOCKING BY ASYNCHRONOUS CLOCK.
In contrast to the two other timers which do not support
this option, Timer2 of the ATMEGA8535 can be clocked
III. THEORY OF OPERATION tor. An example of a circuit that wakes up once every
The implementation of a RTC utilizes the asynchron- second to update the RTC will show the power consump-
ous operation of the RTC module. In this mode, Ti- tion for the two types of clock source[4]:
mer/Counter2 runs independently from the CPU clock.
Figure 2-1 shows that the AVR controller operates from
the 4 MHz main clock sourcein normal operation. When
low power operation is desired the AVR operates in Pow-
er-down mode, with only the asynchronous timer running
from an external 32.768 kHz crystal. The software Real
Time Clock (RTC) is implemented using a 8-bit Ti-
mer/Counter with Overflow Interrupt. The software con-
trols the Overflow Interrupt to count clock and calendar
variables. The Timer Overflow Interrupt is used to update
the software variables “second”, “minute”, “hour”, “date”,
“month” and “year” at the correct intervals[6].