Xmega Application Note


rtc_example.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00050 #include "avr_compiler.h"
00051 #include "rtc_driver.h"
00052 
00053 
00054 #define LED_PORT          PORTD
00055 #define RTC_CYCLES_1S     1024
00056 
00057 
00059 typedef struct RTC_BCD_struct{
00060         uint8_t sec_ones;       
00061         uint8_t sec_tens;       
00062 } RTC_BCD_t;
00063 
00064 
00078 int main(void)
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 }
00113 
00114 
00120 ISR(RTC_OVF_vect)
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 }
@DOC_TITLE@
Generated on Wed Apr 23 08:25:23 2008 for AVR1314 Using the Xmega Real Time Counter by doxygen 1.5.5