Xmega Application Note


rtc32_example1.c

Go to the documentation of this file.
00001 /*   This file is prepared for Doxygen automatic documentation generation   */
00059 /*============================ INCLUDES ======================================*/
00060 #include "avr_compiler.h"
00061 #include "rtc32_driver.h"
00062 #include "vbat.h"
00063 
00064 /*============================ IMPLEMENTATION ================================*/
00065 void chip_init(void)
00066 {
00067         /* 
00068         * Disable all modules except the 32-bit RTC to minimise power
00069         * consumption 
00070         */
00071         PR.PRGEN = PR_AES_bm | PR_EBI_bm | PR_EVSYS_bm | PR_DMA_bm;
00072         PR.PRPA = PR_ADC_bm | PR_AC_bm;
00073         PR.PRPB = PR_DAC_bm | PR_ADC_bm | PR_AC_bm;
00074         PR.PRPC = PR_TWI_bm | PR_USART0_bm | PR_USART1_bm | PR_SPI_bm
00075                 | PR_HIRES_bm | PR_TC0_bm | PR_TC1_bm;
00076         PR.PRPD = PR_USART0_bm | PR_USART1_bm | PR_SPI_bm | PR_HIRES_bm
00077                 | PR_TC0_bm | PR_TC1_bm;
00078         PR.PRPE = PR_TWI_bm | PR_USART0_bm | PR_HIRES_bm | PR_TC0_bm | PR_TC1_bm;
00079         PR.PRPF = PR_USART0_bm | PR_HIRES_bm | PR_TC0_bm;
00080 
00081         PORTA.DIR = 0x02;
00082         PORTA.OUTCLR = 0x02;
00083 
00084         /* Configure system clock to use 32 MHz internal RC oscillator */
00085         OSC.CTRL |= OSC_RC32MEN_bm;
00086         ENTER_CRITICAL_REGION( );
00087         CCP = 0xD8;
00088         CLK.PSCTRL = (uint8_t)CLK_PSADIV_1_gc | (uint8_t)CLK_PSBCDIV_1_1_gc;
00089         while ( (OSC.STATUS & OSC_RC32MRDY_bm) == 0 );
00090         CCP = 0xD8;
00091         CLK.CTRL = CLK_SCLKSEL_RC32M_gc;
00092         LEAVE_CRITICAL_REGION();
00093         
00094         /* Configure the interrupt system */
00095         PMIC.CTRL |= PMIC_LOLVLEN_bm;
00096 
00097 }
00098 
00099 void vbat_init(void)
00100 {
00101         vbat_reset();
00102         vbat_enable_xosc(0);
00103         RTC32_Initialize(0xffffffff, 0, 2 );
00104         RTC32_SetCompareIntLevel(RTC32_COMPINTLVL_LO_gc);
00105 }
00106 
00107 int main(void)
00108 {
00109         uint8_t vbat_status;
00110 
00111         chip_init();
00112         vbat_status = vbat_system_check(true);
00113         
00114         /* 
00115         * Depending on the VBAT system check appropriate actions need to 
00116         * be taken.
00117         * In this example we re-initialize the VBAT system in all
00118         * error cases.
00119         */
00120         switch (vbat_status)
00121         {
00122         case VBAT_STATUS_OK:
00123                 // Interrupts must be re-enabled
00124                 RTC32_SetCompareIntLevel(RTC32_COMPINTLVL_LO_gc);
00125                 break;
00126         case VBAT_STATUS_NO_POWER: // fall through
00127         case VBAT_STATUS_BBPOR: // fall through
00128         case VBAT_STATUS_BBBOD: // fall through
00129         case VBAT_STATUS_XOSCFAIL: // fall through
00130         default:
00131                 vbat_init();
00132                 break;
00133         }
00134 
00135         sei();
00136         
00137         while (true) {
00138                 RTC32_SetAlarm(2);
00139                 PORTA.OUTTGL = 0x02;
00140                 SLEEP.CTRL = SLEEP_SMODE_PSAVE_gc | SLEEP_SEN_bm;
00141                 cpu_sleep();
00142         }
00143 }
00144 
00145 #pragma vector= RTC32_COMP_vect
00146 __interrupt void rtc32_comp(void)
00147 {
00148 }
@DOC_TITLE@
Generated on Thu Jun 24 16:35:07 2010 for AVR1321: Using the XMEGA 32bit RTC by doxygen 1.6.1