AVR-RTC.pdf
(
66 KB
)
Pobierz
AVR134: Real Time Clock (RTC) using the
Asynchronous Timer
8-bit
Microcontrollers
Features
•
Real Time Clock with Very Low Power Consumption (4
μ
A @ 3.3V)
•
Very Low Cost Solution
•
Adjustable Prescaler to Adjust Precision
•
Counts Time, Date, Month, and Year with Auto Leap Year Configuration
•
Year 2000 Compliant Date Format
•
Can be used on all AVR Controllers with RTC Module
•
“C”-Code for ATMega103 Included
Application Note
1 Introduction
This application note describes how to implement a Real Time Clock (RTC) on
AVR
®
microcontrollers that features the RTC module. The implementation requires
only one discrete component – a 32.768 kHz watch crystal. The application has
very low power consumption because the microcontroller operates in Power-save
mode most of the time. In Power-save mode the AVR controller is sleeping with
only a Timer running. The Timer is clocked by the external crystal. On every Timer
overflow the time, date, month, and year are counted. This RTC implementation is
written for the ATmega103, and can easily be ported to other AVRs with RTC
Module. The advantages of implementing a RTC in software compared to an
external hardware RTC are obvious:
•
Lower cost
•
Few external components
•
Lower power
•
Greater flexibility
Rev. 1259G-AVR-04/09
2 Theory of Operation
The implementation of a RTC utilizes the asynchronous operation of the RTC module.
In this mode, Timer/Counter0 runs independently from the CPU clock.
Figure 2-1 shows that the AVR controller operates from the 4 MHz main clock source
in normal operation. When low power operation is desired the AVR operates in
Power-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 Timer/Counter with
Overflow Interrupt. The software controls 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.
Figure 2-1.
Oscillator Connection for Real Time Clock
XTAL1
4.000 MHz
V
CC
XTAL2
AVR
R
ATmega103
TOSC2
TIMER/COUNTER0
32.768 kHz
TOSC1
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 consumption, 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
Overflow Interrupt. The updates of the timer variables are performed during the active
period.
Then the AVR re-enters the Power-save mode until the next Timer Overflow occurs.
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 power 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 than 100 cycles, with a 4
MHz main clock this is 25 µs. The power consumption for this period is neglectable.
More important is the wake-up period for the controller. The wake-up time can be
programmed to 35 ms for use with external crystal, or 1 ms for use with ceramic
resonator. An example of a circuit that wakes up once every second to update the
RTC will show the power consumption for the two types of clock source:
AVR134
2
1259G-AVR-04/09
AVR134
Figure 2-2.
Current Figures for Crystal Oscillator, 35 ms Startup Time
Current
35m
s
1s
Active Mode
4
μ
A
6mA
Power-save
Time
Total current consumption per second:
= (1 sec * 4
μ
A) + (35 ms * 6 mA) = 4
μ
As + 210
μ
As = 214
μ
As
This shows that the dominating part of the current consumption is in Active mode.
Figure 2-3.
Current Figures for Ceramic Resonator, 1 ms Startup Time
Current
1 ms
1s
Active Mode
6mA
4
μ
A
Power-save
Time
Total current consumption per second:
= (1 sec * 4
μ
A) + (1 ms * 6 mA) = 4
μ
As + 6
μ
As = 10
μ
As
This shows that by reducing the startup time the current consumption is reduced from
214
μ
As to 10
μ
As.
Table 2-1.
Current Consumption by the
AVR Controller in Eac
h Mode
Mode
Typical
Max
Active 4 MHz, 3.3 V
CC
4 mA
6.0 mA
Idle 4 MHz, 3.3 V
CC
1.8 mA
2.0 mA
Power-down 4 MHz, 3.3 V
CC
< 1.0
μ
A
2.0
μ
A
Power-save 4 MHz, 3.3 V
CC
< 4.0
μ
A
6.0
μ
A
3 Calculation
Given the frequency of the watch crystal, the user can determine the time for each
tick in the Timer/Counter by selecting the desired prescale factor. As shown in Table
3-1, CS02, CS01, and CS00 in the TCCR0 (Timer/Counter0 Control Register) define
the prescaling source of the Timer/Counter, where CK is the frequency of the watch
crystal. For example, if CK equals 32.768 kHz, the Timer/Counter will tick at a
frequency of 256 Hz with a prescaler of CK/128.
Table 3-1.
Timer/Counter0 Prescale Select
CS02
Description
(1)
CS01
CS00
Overflow Period
0
0
0
Timer/Counter0 is stopped
–
0
0
1
CK
1/128s
3
1259G-AVR-04/09
Description
(1)
CS02
CS01
CS00
Overflow Period
0
1
0
CK/8
1/16s
0
1
1
CK/32
1/4s
1
0
0
CK/64
1/2s
1
0
1
CK/128
1s
1
1
0
CK/256
2s
1
1
1
CK/1024
8s
Notes:
1. CK = 32.768 kHz
4 Configuration Example
As shown in Figure 2-1, the crystal should be connected directly between pins
TOSC1 and TOSC2. Newer devices require external capacitors on these pins as they
have a different internal oscillator, please refer to the device datasheet for details on
crystal connections. The Oscillator is optimized for use with a 32.768 kHz watch
crystal, or an external clock signal in the interval of 0 Hz - 256 kHz. In this example,
the eight LEDs in port B are used to display the RTC. The LED on Port B pin 0 will
change state every second. The next six LEDs represents the minute in binary, and
the LED on pin 7 stays on for one hour and off for the next.
Considerations should be taken when clocking the Timer/Counter from an
asynchronous clock source. A 32.768 kHz crystal have a stabilization time up to one
second after Power-up. The controller must therefore not enter Power-save mode
less than a second after Power-up. Care must be taken when changing to
asynchronous operation. See the data sheet for detailed instructions. When updating
the Timer Register the data is transferred to a temporary register and latched after
two external clock cycles. The Asynchronous Status Register (ASSR) contains status
flags that can be checked to control that the written register is updated.
5 Implementation
The software consists of two subroutines. “counter” is the Timer/Counter Overflow
service routine, which updates all the timer variables whenever a Timer Overflow
occurs. The other one, “not_leap”, corrects the date for leap years. The main program
sets up all the necessary I/O Registers to enable the RTC module and controls the
Power-down sequence.
The AS0 bit in the ASSR (Asynchronous Status Register) is set to configure
Timer/Counter0 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 selected. 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 Power-save mode. The SLEEP instruction will then place
the controller in sleep mode. A loop in the main program executes the SLEEP
instruction.
AVR134
4
1259G-AVR-04/09
AVR134
5.1 “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 variables 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”.
5
1259G-AVR-04/09
Plik z chomika:
atlantis2
Inne pliki z tego folderu:
AVR-RTC.pdf
(66 KB)
Podstawy_AVRGCC.pdf
(638 KB)
c_crc16.zip
(2 KB)
USART.pdf
(303 KB)
EEPROM Memory.pdf
(369 KB)
Inne foldery tego chomika:
Pliki dostępne do 01.06.2025
Pliki dostępne do 19.01.2025
Datasheet
laptop_servicemanuals
NEOWARE
Zgłoś jeśli
naruszono regulamin