XMEGA Application Note


xmega_rtc32_sleep_example.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00062 #include "avr_compiler.h"
00063 #include "ccpwrite.h"
00064 #include "rtc32_driver.h"
00065 #include "sleepmgr.h"
00066 #include "lowpower.h"
00067 
00068 
00083 #define SLEEP_MODE SLEEPMGR_DOWN
00084 
00085 
00092 #define USE_RTC
00094 #define RTC_PERIOD      5
00095 
00096 
00098 #ifndef F_CPU
00099 #define F_CPU 2000000
00100 #endif // F_CPU
00101 
00102 
00110 int main(void)
00111 {
00112     // Initialize to the least power consuming state.
00113     LOWPOWER_Init();
00114         
00115     // Enable EEPROM and Flash power reduction mode.
00116     CCPWrite(&NVM.CTRLB, NVM_EPRM_bm | NVM_FPRM_bm);
00117 
00118     // Initialize the sleep manager, lock a sleep mode if configured.
00119     SLEEPMGR_Init();
00120 #ifdef SLEEP_MODE
00121     SLEEPMGR_Lock( SLEEP_MODE );
00122 #endif // SLEEP_MODE    
00123 
00124     // If use of the RTC as interval timer is configured, set it up.
00125 #ifdef USE_RTC
00126     // Clear bit for RTC in PRR (it is set by LOWPOWER_Init()).
00127     PR.PRGEN &= ~PR_RTC_bm;
00128     
00129     // Reset the battery backup module.
00130     RTC32_Reset();
00131 
00132     // Configure and enable TOSC, then set up and enable the RTC32 module.
00133     RTC32_ToscEnable( false );
00134     RTC32_Initialize( RTC_PERIOD, 0, 0 );
00135 
00136     // Enable RTC compare interrupts.
00137     RTC32_SetCompareIntLevel( RTC32_COMPINTLVL_LO_gc );
00138     PMIC.CTRL |= PMIC_LOLVLEN_bm;
00139     sei();
00140 #endif // USE_RTC
00141 
00142     // Main loop.
00143     do {
00144         /* On wake-up, stay in ACTIVE mode for 0.5 s and then go back to sleep.
00145          *
00146          * In an actual application, you would process events/data here.
00147          */
00148         delay_us(500000);
00149         
00150         // Due to errata, disable Flash power reduction before sleep.
00151         CCPWrite(&NVM.CTRLB, NVM_EPRM_bm);
00152 
00153         SLEEPMGR_Sleep();
00154         
00155         // Re-enable Flash power reduction mode after sleep.
00156         CCPWrite(&NVM.CTRLB, NVM_EPRM_bm | NVM_FPRM_bm);
00157     } while (1);
00158 }
00159 
00160 
00161 /* RTC32 compare ISR
00162  *
00163  * The RTC is only used to wake the device up at intervals, so the ISR
00164  * does not need to do anything.
00165  */
00166 ISR(RTC32_COMP_vect)
00167 {
00168 }
@DOC_TITLE@
Generated on Mon Nov 9 13:44:26 2009 for XMEGA power consumption evaluation code by doxygen 1.5.9