XMEGA Application Note


xplain_sleep_example.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00064 #include "avr_compiler.h"
00065 #include "ccpwrite.h"
00066 #include "rtc_driver.h"
00067 #include "sleepmgr.h"
00068 #include "lowpower.h"
00069 
00070 
00085 #define SLEEP_MODE SLEEPMGR_SAVE
00086 
00087 
00094 #define USE_RTC
00096 #define RTC_PERIOD      5
00097 
00098 
00100 #ifndef F_CPU
00101 #define F_CPU 2000000
00102 #endif // F_CPU
00103 
00104 
00112 int main(void)
00113 {
00114     // Initialize to the least power consuming state.
00115     LOWPOWER_Init();
00116     
00117     // Enable EEPROM and Flash power reduction mode.
00118     CCPWrite(&NVM.CTRLB, NVM_EPRM_bm | NVM_FPRM_bm);
00119 
00120     // Initialize the sleep manager, lock a sleep mode if configured.
00121     SLEEPMGR_Init();
00122 #ifdef SLEEP_MODE
00123     SLEEPMGR_Lock( SLEEP_MODE );
00124 #endif // SLEEP_MODE    
00125         
00126         
00127     // If use of the RTC as interval timer is configured, set it up.
00128 #ifdef USE_RTC
00129     // Clear bit for RTC in PRR (set by LOWPOWER_Init()).
00130     PR.PRGEN &= ~PR_RTC_bm;
00131 
00132     // Enable external 32 kHz XTAL oscillator in low-power mode for RTC.
00133     OSC.XOSCCTRL = OSC_XOSCSEL_32KHz_gc | OSC_X32KLPM_bm;
00134     OSC.CTRL |= OSC_XOSCEN_bm;
00135 
00136     // Wait for oscillator to stabilize.
00137     do { } while (!( OSC.STATUS & OSC_XOSCRDY_bm ));
00138     
00139     // Set the oscillator as clock source for RTC.
00140     CLK.RTCCTRL = CLK_RTCSRC_TOSC_gc | CLK_RTCEN_bm;
00141 
00142     // Wait until RTC is ready.
00143     do { } while ( RTC_Busy() );
00144 
00145     // Configure RTC wakeup period.
00146     RTC_Initialize( RTC_PERIOD, 0, 0, RTC_PRESCALER_DIV1024_gc );
00147     
00148     // Enable the RTC compare interrupts so that the device can wake up.
00149     RTC_SetCompareIntLevel( RTC_COMPINTLVL_LO_gc );
00150     PMIC.CTRL |= PMIC_LOLVLEN_bm;
00151     sei();
00152 #endif // USE_RTC
00153 
00154     // Main loop.
00155     do {
00156         /* On wake-up, stay in ACTIVE mode for 0.5 s and then go back to sleep.
00157          *
00158          * In an actual application, you would process events/data here.
00159          */
00160         delay_us(500000);
00161         
00162         // Due to errata, Flash power reduction mode should be disabled.
00163         CCPWrite(&NVM.CTRLB, NVM_EPRM_bm);
00164 
00165         SLEEPMGR_Sleep();
00166         
00167         // Re-enable EEPROM and Flash power reduction mode after sleep.
00168         CCPWrite(&NVM.CTRLB, NVM_EPRM_bm | NVM_FPRM_bm);
00169     } while (1);
00170 }
00171 
00172 
00173 /* RTC compare ISR
00174  *
00175  * The RTC is only used to wake the device up at intervals, so the ISR
00176  * does not need to do anything.
00177  */
00178 ISR(RTC_COMP_vect)
00179 {
00180 }
@DOC_TITLE@
Generated on Mon Nov 9 13:44:26 2009 for XMEGA power consumption evaluation code by doxygen 1.5.9