- Main Page
- Related Pages
- Files

- File List
- Globals

# XMEGA_power_consumption.c

Go to the documentation of this file.

    00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
    00052 /*============================ INCLUDES ======================================*/
    00053 #include "avr_compiler.h"
    00054 #include "lowpower.h"
    00055 #include "rtc_driver.h"
    00056 #include "clksys_driver.h"
    00057 #include <avr/sleep.h>
    00058
    00059 /*============================ MACROS ========================================*/
    00060 /* GPIO define*/
    00061 #define SWITCHPORT                PORTC
    00062 #define LEDPORT                   PORTD
    00063 #ifndef F_CPU
    00064 # define F_CPU 2000000UL
    00065 #endif
    00066
    00067 /*============================ GLOBAL VARIABLES ==============================*/
    00069 uint8_t gState = 0;
    00070 volatile bool run = false;
    00071
    00072 /*============================ IMPLEMENTATION ================================*/
    00076 void ASYNC_Init( void )
    00077 {
    00078     /* Pin 2 is used as source for interrupt */
    00079     SWITCHPORT.INT0MASK  = PIN2_bm;
    00080
    00081     /* Input configuaratin as low level */
    00082     SWITCHPORT.PIN2CTRL  =  PORT_ISC_LEVEL_gc | PORT_OPC_PULLUP_gc;
    00083
    00084     /* Enable low level interrupt */
    00085     SWITCHPORT.INTCTRL   = PORT_INT0LVL_LO_gc;
    00086
    00087     /* Enable low leverl interrutp excute */
    00088     PMIC.CTRL |= PMIC_LOLVLEX_bm;
    00089 }
    00093 void LED_Init( void )
    00094 {
    00095     LEDPORT.DIR = PIN3_bm | PIN4_bm | PIN5_bm | PIN6_bm;
    00096     LEDPORT.OUT |= PIN3_bm | PIN4_bm | PIN5_bm | PIN6_bm;//on
    00097     delay_us( 500000 );
    00098     LEDPORT.OUT &= ~(PIN3_bm | PIN4_bm | PIN5_bm | PIN6_bm);//off
    00099 }
    00100
    00104 void RTC_Init( void )
    00105 {
    00106     /* Enable external 32 kHz XTAL oscillator in low-power mode for RTC */
    00107     OSC.XOSCCTRL = OSC_XOSCSEL_32KHz_gc | OSC_X32KLPM_bm;
    00108     OSC.CTRL |= OSC_XOSCEN_bm;
    00109
    00110     /* Wait for oscillator to stabilize */
    00111     do { } while (!( OSC.STATUS & OSC_XOSCRDY_bm ));
    00112
    00113     /* Set the oscillator as clock source for RTC */
    00114     CLK.RTCCTRL = CLK_RTCSRC_TOSC_gc | CLK_RTCEN_bm;
    00115
    00116     /* Wait until RTC is ready */
    00117     do { } while ( RTC_Busy() );
    00118 }
    00122 void Enable_RTC ( void )
    00123 {
    00124     /* Clear bit for RTC in PRR (it is set by LOWPOWER_Init()).*/
    00125     PR.PRGEN &= ~PR_RTC_bm;
    00126 }
    00130 void Disable_RTC( void )
    00131 {
    00132     PR.PRGEN |= PR_RTC_bm;
    00133 }
    00134
    00139 int main(void)
    00140 {
    00141
    00142     RTC_Init();
    00143     LOWPOWER_Init();
    00144     ASYNC_Init();
    00145     LED_Init();
    00146
    00147     /* ATxmega32a4 A version bug, see Errata */
    00148     CCP = 0xd8;
    00149     NVM.CTRLB |= 0x06;
    00150
    00151     for(;;) {
    00152         switch(gState) {
    00153             case 0:
    00154                 LEDPORT.OUT = (PIN6_bm);
    00155                 break;
    00156             /* Idle */
    00157             case 1:
    00158                 set_sleep_mode(SLEEP_MODE_IDLE);
    00159                 LEDPORT.OUT = (PIN5_bm);
    00160                 break;
    00161             /* Power-save */
    00162             case 2:
    00163                 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
    00164                 LEDPORT.OUT = (PIN4_bm);
    00165                 Enable_RTC();
    00166                 break;
    00167             /* Power-down */
    00168             case 3:
    00169                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    00170                 LEDPORT.OUT = (PIN3_bm);
    00171                 break;
    00172             /* Shouldn't go to here. Go to Idle.*/
    00173             default:
    00174                 set_sleep_mode(SLEEP_MODE_IDLE);
    00175                 LEDPORT.OUT = (PIN5_bm);
    00176     }
    00177     sei();
    00178
    00179     delay_us( 250000 );
    00180
    00181     LEDPORT.OUT &= ~(PIN3_bm | PIN4_bm | PIN5_bm | PIN6_bm);//turn off
    00182     /* Set pull down to avode light LED */
    00183     LEDPORT.PIN3CTRL = 0X0;
    00184     LEDPORT.PIN4CTRL = 0X0;
    00185     LEDPORT.PIN5CTRL = 0X0;
    00186     LEDPORT.PIN6CTRL = 0X0;
    00187
    00188     LEDPORT.DIR = 0x0;
    00189
    00190     run = false;
    00191     if (gState != 0) {
    00192         sleep_enable();
    00193         sleep_cpu();
    00194         sleep_disable();
    00195         cli();
    00196         /* Wake up from RTC */
    00197         if (gState == 2) {
    00198             Disable_RTC();
    00199         }
    00200     } else {
    00201         /* Active mode */
    00202         while(!run){
    00203         };
    00204         cli();
    00205     }
    00206
    00207     LEDPORT.DIR = PIN3_bm | PIN4_bm | PIN5_bm | PIN6_bm;//Pin output
    00208
    00209     /* switch status */
    00210     gState++;
    00211     if (gState > 3) gState =0;
    00212     };
    00213 }
    00214
    00215
    00220 ISR(PORTC_INT0_vect)
    00221 {
    00222     run = true;
    00223 }