Xmega Application Note | |||||
This file contains an example application that demonstrates the RTC driver.
Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file rtc_example.c.
#include "avr_compiler.h"
#include "rtc_driver.h"
Go to the source code of this file.
Data Structures | |
struct | RTC_BCD_struct |
Struct to hold the BCD second values. More... | |
Defines | |
#define | LED_PORT PORTD |
#define | RTC_CYCLES_1S 1024 |
Typedefs | |
typedef struct RTC_BCD_struct | RTC_BCD_t |
Functions | |
ISR (RTC_OVF_vect) | |
RTC overflow interrupt service routine. | |
int | main (void) |
The main RTC example. |
#define LED_PORT PORTD |
#define RTC_CYCLES_1S 1024 |
typedef struct RTC_BCD_struct RTC_BCD_t |
ISR | ( | RTC_OVF_vect | ) |
RTC overflow interrupt service routine.
This ISR keeps track of the seconds and displays the current second count on LEDs connected to PORTD.
Definition at line 120 of file rtc_example.c.
References LED_PORT, RTC_BCD_struct::sec_ones, and RTC_BCD_struct::sec_tens.
00121 { 00122 static RTC_BCD_t rtcTime; 00123 00124 /* Increase and check if 1's reach top value. Reset 1's and increase 00125 * 10's if top is reached. 00126 */ 00127 if ( ++rtcTime.sec_ones > 9 ) { 00128 rtcTime.sec_ones = 0; 00129 rtcTime.sec_tens++; 00130 } 00131 00132 /* Check if 10's reach top value and reset to zero if it does. */ 00133 if ( rtcTime.sec_tens > 5 ) { 00134 rtcTime.sec_tens = 0; 00135 } 00136 00137 /* Display the inverted counters on active low LEDs. */ 00138 LED_PORT.OUT = ~( ( rtcTime.sec_tens << 4 ) | rtcTime.sec_ones ); 00139 }
int main | ( | void | ) |
The main RTC example.
This function demonstrates how the RTC can be used to implement a simple second counter. The RTC is configured to produce an interrupt once every second. The RTC overflow interrupt keeps track of time by increasing a counter each time it is run. The time is shown in binary coded decimal, using 7 LEDs. Four LEDs connected to pins 0-3 shows single seconds, while three LEDs connected to pins 4-6 shows tens of seconds. The second counter wraps at 60, so it starts at 0 once every minute.
Hardware setup:
Definition at line 78 of file rtc_example.c.
References LED_PORT, RTC_Busy, RTC_CYCLES_1S, RTC_Initialize(), and RTC_SetIntLevels().
00079 { 00080 /* Turn on internal 32kHz. */ 00081 OSC.CTRL |= OSC_RC32KEN_bm; 00082 00083 do { 00084 /* Wait for the 32kHz oscillator to stabilize. */ 00085 } while ( ( OSC.STATUS & OSC_RC32KRDY_bm ) == 0); 00086 00087 00088 /* Set internal 32kHz oscillator as clock source for RTC. */ 00089 CLK.RTCCTRL = CLK_RTCSRC_RCOSC_gc | CLK_RTCEN_bm; 00090 00091 /* Configure LED port as output. */ 00092 LED_PORT.DIR = 0xFF; 00093 00094 do { 00095 /* Wait until RTC is not busy. */ 00096 } while ( RTC_Busy() ); 00097 00098 /* Configure RTC period to 1 second. */ 00099 RTC_Initialize( RTC_CYCLES_1S, 0, 0, RTC_PRESCALER_DIV1_gc ); 00100 00101 /* Enable overflow interrupt. */ 00102 RTC_SetIntLevels( RTC_OVFINTLVL_LO_gc, RTC_COMPINTLVL_OFF_gc ); 00103 00104 /* Enable interrupts. */ 00105 PMIC.CTRL |= PMIC_LOLVLEN_bm; 00106 sei(); 00107 00108 do { 00109 /* Wait while the interrupt executes. */ 00110 nop(); 00111 } while (1); 00112 }
Generated on Wed Apr 23 08:25:24 2008 for AVR1314 Using the Xmega Real Time Counter by ![]() |