You are on page 1of 4

Real timer using microcontroller ATME8535

Zogopoulos A , Tigas O , Katsiris J , Pagiatakis G , Panetsos S


Department of electronics engineering educators
School of Pedagogical and Technological Education ASPETE
Athens , Greece

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].

Because of the amount time for the Timer/Counter to


complete one overflow is always the same, each of these
timer variables will be incremented by a fixed number
with every Timer Overflow. The Timer Overflow Interrupt
routine is used to perform this task. To reduce power con-
sumption, AVR enters Power-save mode, in which all On-
chip modules are disabled except for the RTC. As shown
in Table 2-1, the MCU typically consumes less than 4 µA
in this mode. The device will wake-up on the Timer Over-
flow Interrupt. The updates of the timer variables are per-
formed during the active period. Then the AVR re-enters
the Power-save mode until the next Timer Overflow oc-
curs. Figure 2-2 and Figure 2-3 shows the time the AVR
controller operates in Power-save mode versus that Active
mode. To calculate the total power consumption, the pow-
er consumption in Power-save mode must be added to the
power consumption in Active mode. The time it takes to
update the timer variables in the interrupt routine is less IV. CALCULATION
than 100 cycles, with a 4 MHz main clock this is 25 ms. Given the frequency of the watch crystal, the user can
The power consumption for this period is neglectable. determine the time for each tick in the Timer/Counter by
More important is the wake-up period for the controller. selecting the desired prescale factor. As shown in Table 3-
The wake-up time can be programmed to 35 ms for use 1, CS02, CS01, and CS00 in the TCCR2 (Timer/Counter2
with external crystal, or 1 ms for use with ceramic resona- Control Register) define the prescaling source of the Ti-
mer/Counter, where CK is the frequency of the watch crys- REFERENCES
tal. For example, if CK equals 32.768 kHz, the Ti- [1] Dhananjay V. Gadre (2001) Programming Microcontroller Avr,
mer/Counter will tick at a frequency of 256 Hz with a Thessalonica: Publications [Tziola].
prescaler of CK/128[5]. [2] Morton John (2002) Avr An Introductory Course, Oxford:
Publications Newnes
V. IMPLEMENTATION [3] Morton John (2002) Avr Risk Microcontroller Handbook Oxford:
The software consists of two subroutines. “counter” is Publications Newnes
the Timer/Counter Overflow service routine, which up- [4] WWW.atmel.com
dates all the timer variables whenever a Timer Overflow [5] WWW.avr-forum.com/
occurs. The other one, “not_leap”, corrects the date for [6] WWW.avrfreaks.com/
leap years. The main program sets up all the necessary I/O
Registers to enable the RTC module and controls the Pow-
er-down sequence. The AS0 bit in the ASSR (Asynchron-
ous Status Register) is set to configure Timer/Counter2 to
be clocked from an external clock source. Only this timer
can perform asynchronous operations. The start value for
the Timer is Reset and the desired prescaler value is se-

lected. To synchronize with the external clock signal the


program wait for the ASSR Register to be updated. TOIE0
bit in the TIMSK (Timer/Counter Interrupt Mask Register)
is then set to enable Timer/Counter0 Overflow Interrupt.
The Global Interrupt Enable bit in SREG (Status Register)
also has to be set to enable all interrupts. SM1 and SM0 bit
in MCUCR (MCU Control Register) are set to select Pow-
er-save mode. The SLEEP instruction will then place the
controller in sleep mode. A loop in the main program ex-
ecutes the SLEEP instruction[1].
E) ”COUNTER” OVERFLOW INTERRUPT
ROUTINE The interrupt routine is executed every time a
Timer Overflow occurs. It wakes up the MCU to update
the timer variables. An interrupt procedure cannot return
or accept any variables. A global structure with timer va-
riables are declared to keep track of time: “second”,
“minute”, “hour”, “date”, “month” and “year”. Since the
time required to complete one Timer Overflow is known,
“second” will be incremented by a fixed number every
time. Once it reaches 60, “minute” is incremented by “1”
and “second” is set to “0”[2,3].

You might also like