APPLICATION NOTE. Atmel AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Atmel AVR 8-bit Microcontroller. Introduction.



Similar documents
AVR134: Real Time Clock (RTC) using the Asynchronous Timer. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

APPLICATION NOTE Atmel AT02509: In House Unit with Bluetooth Low Energy Module Hardware User Guide 8-bit Atmel Microcontroller Features Description

Application Note. Atmel ATSHA204 Authentication Modes. Prerequisites. Overview. Introduction

AVR1321: Using the Atmel AVR XMEGA 32-bit Real Time Counter and Battery Backup System. 8-bit Microcontrollers. Application Note.

How To Use An Atmel Atmel Avr32848 Demo For Android (32Bit) With A Microcontroller (32B) And An Android Accessory (32D) On A Microcontroller (32Gb) On An Android Phone Or

APPLICATION NOTE. Atmel AT04389: Connecting SAMD20E to the AT86RF233 Transceiver. Atmel SAMD20. Description. Features

Atmel AVR4921: ASF - USB Device Stack Differences between ASF V1 and V2. 8-bit Atmel Microcontrollers. Application Note. Features.

Atmel AVR4903: ASF - USB Device HID Mouse Application. Atmel Microcontrollers. Application Note. Features. 1 Introduction

APPLICATION NOTE. Atmel AVR443: Sensor-based Control of Three Phase Brushless DC Motor. Atmel AVR 8-bit Microcontrollers. Features.

Application Note. Atmel CryptoAuthentication Product Uses. Atmel ATSHA204. Abstract. Overview

APPLICATION NOTE. Atmel LF-RFID Kits Overview. Atmel LF-RFID Kit. LF-RFID Kit Introduction

AVR131: Using the AVR s High-speed PWM. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

Atmel AVR4920: ASF - USB Device Stack - Compliance and Performance Figures. Atmel Microcontrollers. Application Note. Features.

How To Design An Ism Band Antenna For 915Mhz/2.4Ghz Ism Bands On A Pbbb (Bcm) Board

General Porting Considerations. Memory EEPROM XRAM

AVR319: Using the USI module for SPI communication. 8-bit Microcontrollers. Application Note. Features. Introduction

AVR055: Using a 32kHz XTAL for run-time calibration of the internal RC. 8-bit Microcontrollers. Application Note. Features.

AVR125: ADC of tinyavr in Single Ended Mode. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR32138: How to optimize the ADC usage on AT32UC3A0/1, AT32UC3A3 and AT32UC3B0/1 series. 32-bit Microcontrollers. Application Note.

32-bit AVR UC3 Microcontrollers. 32-bit AtmelAVR Application Note. AVR32769: How to Compile the standalone AVR32 Software Framework in AVR32 Studio V2

Atmel AVR1017: XMEGA - USB Hardware Design Recommendations. 8-bit Atmel Microcontrollers. Application Note. Features.

APPLICATION NOTE. Atmel AT01095: Joystick Game Controller Reference Design. 8-/16-bit Atmel Microcontrollers. Features.

AVR115: Data Logging with Atmel File System on ATmega32U4. Microcontrollers. Application Note. 1 Introduction. Atmel

APPLICATION NOTE. AT03155: Real-Time-Clock Calibration and Compensation. SAM3 / SAM4 Series. Scope

AVR1510: Xplain training - XMEGA USART. 8-bit Microcontrollers. Application Note. Prerequisites. 1 Introduction

APPLICATION NOTE. Atmel AT02985: User s Guide for USB-CAN Demo on SAM4E-EK. Atmel AVR 32-bit Microcontroller. Features. Description.

AVR305: Half Duplex Compact Software UART. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR32788: AVR 32 How to use the SSC in I2S mode. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

USER GUIDE EDBG. Description

Application Note. 8-bit Microcontrollers. AVR270: USB Mouse Demonstration

AVR353: Voltage Reference Calibration and Voltage ADC Usage. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1922: Xplain Board Controller Firmware. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1003: Using the XMEGA Clock System. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR32701: AVR32AP7 USB Performance. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR151: Setup and Use of the SPI. Introduction. Features. Atmel AVR 8-bit Microcontroller APPLICATION NOTE

Introducing a platform to facilitate reliable and highly productive embedded developments

AVR317: Using the Master SPI Mode of the USART module. 8-bit Microcontrollers. Application Note. Features. Introduction

AVR033: Getting Started with the CodeVisionAVR C Compiler. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

APPLICATION NOTE. Atmel AT01180: Barcode and QR code scanner User Guide. Atmel 32-bit Microcontroller. Features. Introduction

AVR1309: Using the XMEGA SPI. 8-bit Microcontrollers. Application Note. Features. 1 Introduction SCK MOSI MISO SS

Software Real Time Clock Implementation on MC9S08LG32

APPLICATION NOTE. Atmel AVR600: STK600 Expansion, Routing and Socket Boards. Atmel Microcontrollers. Introduction

AVR1600: Using the XMEGA Quadrature Decoder. 8-bit Microcontrollers. Application Note. Features. 1 Introduction. Sensors

8-bit Microcontroller. Application Note. AVR400: Low Cost A/D Converter

AT91SAM ARM-based Flash MCU. Application Note

Software Prerequisites Linux Ubuntu LTS. Estimated completion time: 15min. The goal of this hands-on is to:

8051 Flash Microcontroller. Application Note. A Digital Thermometer Using the Atmel AT89LP2052 Microcontroller

AT15007: Differences between ATmega328/P and ATmega328PB. Introduction. Features. Atmel AVR 8-bit Microcontrollers APPLICATION NOTE

Using CryptoMemory in Full I 2 C Compliant Mode. Using CryptoMemory in Full I 2 C Compliant Mode AT88SC0104CA AT88SC0204CA AT88SC0404CA AT88SC0808CA

Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers. 8-bit Atmel Microcontrollers. Application Note.

8-bit. Application Note. Microcontrollers. AVR282: USB Firmware Upgrade for AT90USB

AVR1318: Using the XMEGA built-in AES accelerator. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1900: Getting started with ATxmega128A1 on STK bit Microcontrollers. Application Note. 1 Introduction

AVR287: USB Host HID and Mass Storage Demonstration. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Application Note. 1. Introduction. 2. Associated Documentation. 3. Gigabit Ethernet Implementation on SAMA5D3 Series. AT91SAM ARM-based Embedded MPU

AVR1301: Using the XMEGA DAC. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

APPLICATION NOTE. Authentication Counting. Atmel CryptoAuthentication. Features. Introduction

AVR106: C Functions for Reading and Writing to Flash Memory. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

Atmel AT32UC3A3256 microcontroller 64MBit SDRAM Analog input (to ADC) Temperature sensor RC filter

APPLICATION NOTE. Secure Personalization with Transport Key Authentication. ATSHA204A, ATECC108A, and ATECC508A. Introduction.

SMARTCARD XPRO. Preface. SMART ARM-based Microcontrollers USER GUIDE

AT88CK490 Evaluation Kit

AVR127: Understanding ADC Parameters. Introduction. Features. Atmel 8-bit and 32-bit Microcontrollers APPLICATION NOTE

APPLICATION NOTE. AT07175: SAM-BA Bootloader for SAM D21. Atmel SAM D21. Introduction. Features

AN3252 Application note

Atmel AVR ATxmega384C3 microcontroller OLED display with pixels resolution Analog sensors. Ambient light sensor Temperature sensor

8-bit Microcontroller. Application Note. AVR415: RC5 IR Remote Control Transmitter. Features. Introduction. Figure 1.

AVR2006: Design and characterization of the Radio Controller Board's 2.4GHz PCB Antenna. Application Note. Features.

APPLICATION NOTE. AT12405: Low Power Sensor Design with PTC. Atmel MCU Integrated Touch. Introduction

APPLICATION NOTE. AT16268: JD Smart Cloud Based Smart Plug Getting. Started Guide ATSAMW25. Introduction. Features

AN974 APPLICATION NOTE

AVR311: Using the TWI Module as I2C Slave. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

APPLICATION NOTE. Atmel AVR2033: SAM-ICE Adapter Hardware User Manual. 8-bit Atmel Microcontrollers. Features. Introduction

AN3265 Application note

AVR Timer/Counter. Prof Prabhat Ranjan DA-IICT, Gandhinagar

Application Note. C51 Bootloaders. C51 General Information about Bootloader and In System Programming. Overview. Abreviations

USER GUIDE. ZigBit USB Stick User Guide. Introduction

AVR315: Using the TWI Module as I2C Master. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

AVR126: ADC of megaavr in Single Ended Mode. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

Flexible Active Shutter Control Interface using the MC1323x

Two mechanical buttons Two user LEDs Four expansion headers. Board controller with USB interface. One power LED and one status LED

1Mb (64K x 16) One-time Programmable Read-only Memory

Real-Time Clock. * Real-Time Computing, edited by Duncan A. Mellichamp, Van Nostrand Reinhold

Fujitsu Microelectronics Europe Application Note MCU-AN E-V10 FR FAMILY 32-BIT MICROCONTROLLER MB91460 CLOCK SUPERVISOR APPLICATION NOTE

AN2604 Application note

AN3332 Application note

Application Note. 8-bit Microcontrollers. AVR272: USB CDC Demonstration UART to USB Bridge

3-output Laser Driver for HD-DVD/ Blu-ray/DVD/ CD-ROM ATR0885. Preliminary. Summary. Features. Applications. 1. Description

AN LPC1700 RTC hardware auto calibration. Document information. RTC, Hardware Auto Calibration, LPC1700, Graphic LCD

AVR32110: Using the AVR32 Timer/Counter. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

APPLICATION NOTE. Atmel AT02845: Coexistence between ZigBee and Other 2.4GHz Products. Atmel MCU Wireless. Description. Features

8-bit Atmel with 8KBytes In- System Programmable Flash. ATmega8 ATmega8L. Summary

8-bit Atmel Microcontrollers. Application Note. Atmel AVR211: Wafer Level Chip Scale Packages

APPLICATION NOTE. Atmel AVR911: AVR Open Source Programmer. 8-bit Atmel Microcontrollers. Features. Introduction

Atmel AVR4950: ASF - USB Host Stack. 8-bit Atmel Microcontrollers. Application Note. Features. 1 Introduction

Capacitive Touch Technology Opens the Door to a New Generation of Automotive User Interfaces

Implementing a Real-Time Clock on the MSP430

IRTC Compensation and 1 Hz Clock Generation

Section 1 Introduction to the AT91SAMD20 and the Development Environment

Transcription:

APPLICATION NOTE Atmel AVR134: Real Time Clock (RTC) Using the Asynchronous Timer Introduction Atmel AVR 8-bit Microcontroller This application note describes how to implement a real time counter (RTC) on Atmel AVR microcontrollers that features the RTC module. The implementation requires only one discrete component a 32.768kHz 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 Atmel ATmega128, and can easily be ported to other AVRs with RTC Module. The advantages of implementing an RTC in software compared to an external hardware RTC are obvious: Lower cost Few external components Lower power Greater flexibility Features Real Time Clock with Very Low Power Consumption (10µ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 ATmega128 Included

Table of Contents 1. Theory of Operation... 3 2. Calculation... 4 3. Configuration Example... 4 4. Implementation... 4 4.1 Timer/Counter0 Overflow Interrupt Service Routine... 5 4.2 not_leap Subroutine... 6 5. Accuracy... 7 6. Revision History... 8 2

1. Theory of Operation The implementation of an RTC utilizes the asynchronous operation of the RTC module. In this mode, Timer/Counter0 runs independently from the CPU clock. Figure 1-1 shows the Atmel AVR Controller operating at 4MHz, from the divided internal calibrated RC-oscillator, when in active mode. When low power operation is desired, the AVR operates in power-down mode, with only the asynchronous timer running from an external 32.768kHz crystal. The software Real Time Clock (RTC) is implemented using an 8-bit Timer/Counter with overflow interrupts enabled. Each timer overflow interrupt triggers an update of the software variables second, minute, hour, date, month, and year at the correct intervals. Figure 1-1. Oscillator Connection for Real Time Clock Because the amount of time needed to make the Timer/Counter overflow is constant, each of these timer variables will be incremented by a fixed number, for each timer overflow. This happens in the timer overflow interrupt routine. To reduce power consumption, the AVR spends most of its time in Power-save mode, in which all on-chip modules are disabled, except for the RTC. As shown in Table 1-1, the MCU typically consumes less than 10µA in this mode. Each timer overflow interrupt will bring the device out of power-save mode, and back into active mode. When in active mode, the timer overflow interrupt routine is executed, before the device re-enters power-save mode. To calculate the total power consumption, the power consumption in power-save mode should be added to the power consumption in active mode. However, the code executed when the device is awake is expected to be executed in less than 100 clock cycles, keeping the device awake for no more than 25µs (@4MHz). The current consumed in this period can therefore be considered neglectable. According to the above and the current consumptions listed in Table 1-1, the current consumption can be estimated to be less than 10µA for this application, as opposed to 5mA if the device never was put in power-save mode. Table 1-1. Current Consumption for the Atmel ATmega128 AVR Controller Mode Typical Maximum Active 4MHz, 3V CC 5mA 5.5mA Idle 4MHz, 3V CC 2mA 2.5mA Power-down, 3V CC <5μA 10μA Power-save, 3V CC <10μA - 3

2. Calculation Given the frequency of the watch crystal, the user can determine the time for each Timer/Counter tick, by selecting the desired pre-scaling factor. As shown in Table 2-1, CS02, CS01, and CS00 in the TCCR0 (Timer/Counter0 Control Register) define the Timer/Counter pre-scaler source. In the table below clkt0s denotes the frequency of the Timer/Counter0 clock source. For example, if clkt0s equals 32.768kHz, as in our case, the Timer/Counter will tick at a frequency of 256Hz with a prescaler of 128. Table 2-1. Timer/Counter0 Prescaler Select CS02 CS01 CS00 Description (1) Overflow period 0 0 0 Timer/Counter0 is stopped 0 0 1 clkt0s 1/128s 0 1 0 clkt0s /8 1/16s 0 1 1 clkt0s /32 1/4s 1 0 0 clkt0s /64 1/2s 1 0 1 clkt0s /128 1s 1 1 0 clkt0s /256 2s 1 1 1 clkt0s /1024 8s Note: 1. clktos = 32.768kHz. 3. Configuration Example As shown in Figure 1-1, the crystal should be connected directly between the pins TOSC1 and TOSC2. Some devices might require additional external capacitors on these pins, as the internal oscillator characteristics can vary. Refer to the device datasheet for details on crystal connections. The oscillator is optimized for use with a 32.768kHz watch crystal, or an external clock signal in the interval of 0Hz - 256kHz. In this example, the eight LEDs in port B are used to display the value of the RTC. The LED on pin B0 will change state every second. The next six LEDs represent the minutes in binary, and the LED on pin B7 alternates every hour. Some considerations must be taken into account when clocking the Timer/Counter from an asynchronous clock source. A 32.768kHz crystal has a stabilization time of up to one second after power-up. The controller should therefore not enter power-save mode less than one second after power-up. Care should also be taken when configuring the timer/counter to operate in asynchronous mode. 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 verify that the written register is updated. 4. Implementation The software consists of two subroutines, a FUSES section and one Interrupt Service Routine (ISR). The init() routine configures the device to the needs of this application. 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 The desired prescaler is selected To synchronize with the external clock signal, the program will wait for the ASSR register to be updated 4

The TOIE0 bit in the TIMSK (Timer/Counter Interrupt Mask Register) is set to enable the Timer/Counter0 Overflow Interrupt The Global Interrupt Enable bit in SREG (Status Register) is set to enable interrupts. Setting this bit is done calling the sei() macro Calling the set_sleep_mode( ) macro with the desired sleep mode as argument sets the SM1 and SM0 bit in MCUCR (MCU Control Register) to select what sleep mode the device should enter The other sub routine, not_leap, corrects the date for leap years. The routine is described in detail in the flow diagram provided in Section 4.2. The FUSES section modifies the device fuses to make it run from the internal calibrated RC oscillator, with a prescaler of two. This results in a clocking speed of 4MHz. The main program controls the power down sequence. The sleep_mode() macro will place the controller in sleep mode. To make sure the interrupt is cleared upon wakeup, the main routine also makes sure the device never re-enters sleep mode within less than one TOSC cycle. 4.1 Timer/Counter0 Overflow Interrupt Service Routine The interrupt service routine is executed every time TCC0 overflows. The interrupt request wakes the MCU to update the timer variables. An interrupt routine 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 for every interrupt. Once it reaches 60, minute is incremented by 1 and second is set to 0. 5

Figure 4-1. Flow Chart, TCC0_Overflo ISR 4.2 not_leap Subroutine This routine checks whether or not a year is a leap year. It returns false if a year is leap, and true if not. A year is considered to be leap if both of the following conditions are met: 1. The year is divisible with four, and 2. If the year is divisible with 100, it also has to be divisible by 400. 6

Figure 4-2. Flow Chart, not_leap Subroutine 5. Accuracy The RTC on the AVR controller maintains high accuracy as long as the watch crystal is accurate. Asynchronous operation allows the timer to run without any delays, even when the CPU is under heavy load. However, a small neglectable discrepancy does occur because the timer variables are not updated in parallel. By the time they are finished updating, they deviate from the Timer/Counter very slightly. The largest discrepancy occurs when all the timer variables are overflowed. At this moment, second is 59, minute is 59, hour is 23, and so on. It takes 94 cycles for the MCU to complete the update. At a 4MHz CPU clock, the error between the RTC and the watch crystal will not exceed 23.5µs found by 94/(4 * 106). A typical error should be 6µs since 24 cycles are needed to update second. This error does not accumulate since the Timer is always synchronous with the watch crystal. 7

6. Revision History Doc. Rev. Date Comments 1259H 01/2014 New template and bugfixing 1259G 04/2009 Bugfixing 1259F 08/2006 Bugfixing 1259E Unknown 1259D Unknown 1259C Unknown 1259B Unknown 1259A Unknown Initial document release 8

Atmel Corporation 1600 Technology Drive San Jose, CA 95110 USA Tel: (+1)(408) 441-0311 Fax: (+1)(408) 487-2600 www.atmel.com Atmel Asia Limited Unit 01-5 & 16, 19F BEA Tower, Millennium City 5 418 Kwun Tong Road Kwun Tong, Kowloon HONG KONG Tel: (+852) 2245-6100 Fax: (+852) 2722-1369 Atmel Munich GmbH Business Campus Parkring 4 D-85748 Garching b. Munich GERMANY Tel: (+49) 89-31970-0 Fax: (+49) 89-3194621 Atmel Japan G.K. 16F Shin-Osaki Kangyo Building 1-6-4 Osaki, Shinagawa-ku Tokyo 141-0032 JAPAN Tel: (+81)(3) 6417-0300 Fax: (+81)(3) 6417-0370 2014 Atmel Corporation. All rights reserved. / Rev.: Atmel, Atmel logo and combinations thereof, Enabling Unlimited Possibilities, AVR, and others are registered trademarks or trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others. Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS AND PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and products descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life.