# main.c File Reference

ATMega48PA power consumption main program file. More...

`#include <stdint.h>`
`#include <stdbool.h>`
`#include <avr/io.h>`
`#include <avr/sleep.h>`
`#include <avr/interrupt.h>`
`#include <avr/wdt.h>`
`#include <util/delay.h>`
Include dependency graph for main.c:

Go to the source code of this file.

## Defines
#define | F_CPU 1000000UL
## Functions
void | ASYNC_Init (void)
| Init0 init.
void | Disable_RTC (void)
| Disable RTC.
| ISR (INT0_vect)
| Init0 interrupt.
void | LED_Init (void)
| Blink LED.
void | LOWPOWER_Init (void)
| This is the low power init routine.
int | main (void)
| This is the main routine.
void | RTC_Init (void)
| Enable RTC.
## Variables
uint8_t | gState = 0
| Global variable to keep track of which state the program is in.
volatile bool | run = false

## Detailed Description

ATMega48PA power consumption main program file.

This file contains a simple program for measuring the current consumption of the ATmega48PA on the picoPower board in Active,Idle, Power save and Power down modes.

Application note:
  AVR32917: picoPower board

Documentation
  For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html

Author:
  Atmel Corporation: http://www.atmel.com
  Support email: avr@atmel.com

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file main.c.

## Define Documentation

#define F_CPU 1000000UL

Definition at line 62 of file main.c.

## Function Documentation

void ASYNC_Init | ( | void |  | ) |

Init0 init.

Definition at line 127 of file main.c.

Referenced by main().

    00128 {
    00129     EICRA = 0x0;
    00130     EIMSK |= (1 << INT0);
    00131 }

void Disable_RTC | ( | void |  | ) |

Disable RTC.

Definition at line 107 of file main.c.

Referenced by main().

    00108 {
    00109     ASSR &= ~(1<<AS2);
    00110     PRR |= (1 << PRTIM2 );
    00111 }

ISR | ( | INT0_vect |  | ) |

Init0 interrupt.

This ISR simpl updates run flag and wake up chip

Definition at line 209 of file main.c.

References run.

    00210 {
    00211     run = true;
    00212 }

void LED_Init | ( | void |  | ) |

Blink LED.

Definition at line 116 of file main.c.

Referenced by main().

    00117 {
    00118     DDRC  |= (1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4);
    00119     PORTC |= (1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4);
    00120     _delay_ms( 500 );
    00121     PORTC &= ~((1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4));
    00122 }

void LOWPOWER_Init | ( | void |  | ) |

This is the low power init routine.

Definition at line 75 of file main.c.

Referenced by main().

    00076 {
    00077     wdt_disable();
    00078     sleep_bod_disable();
    00079
    00080     PRR = (1 << PRTWI) | (1 << PRTIM2 ) | (1 << PRTIM1 ) | \
    00081           (1 << PRTIM0 ) | (1 << PRSPI ) | (1 << PRUSART0 ) | (1 << PRADC ) ;
    00082
    00083     /* set pin input and pull up */
    00084     DDRB = 0x00;
    00085     PORTB = 0xFF;
    00086     DDRC = 0x00;
    00087     PORTC = 0xFF;
    00088     DDRD = 0x00;
    00089     PORTD = 0xFF;
    00090     /* Disable digital input */
    00091     DIDR1 = (1 << AIN1D) | (1 << AIN0D);
    00092     DIDR0 = (1 << ADC5D) | (1 << ADC4D) | (1 << ADC3D) | (1 << ADC2D) | (1 << ADC1D) | (1 << ADC0D);
    00093 }

int main | ( | void |  | ) |

This is the main routine.

Definition at line 136 of file main.c.

References ASYNC_Init(), Disable_RTC(), gState, LED_Init(), LOWPOWER_Init(), RTC_Init(), and run.

    00137 {
    00138     LOWPOWER_Init();
    00139     ASYNC_Init();
    00140     LED_Init();
    00141
    00142     for(;;) {
    00143         switch(gState) {
    00144             case 0:
    00145                 DDRC  = (1 << PINC4);
    00146                 PORTC = (1 << PINC4);
    00147                 break;
    00148             /* Idle */
    00149             case 1:
    00150                 set_sleep_mode(SLEEP_MODE_IDLE);
    00151                 DDRC  = (1 << PINC3);
    00152                 PORTC = (1 << PINC3);
    00153                 break;
    00154             /* Power-save */
    00155             case 2:
    00156                 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
    00157                 DDRC  = (1 << PINC2);
    00158                 PORTC = (1 << PINC2);
    00159                 RTC_Init();
    00160                 break;
    00161             /* Power-down */
    00162             case 3:
    00163                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    00164                 DDRC  = (1 << PINC1);
    00165                 PORTC = (1 << PINC1);
    00166                 break;
    00167             /* Shouldn't go to here. Go to Idle.*/
    00168             default:
    00169                 gState = 1;
    00170                 set_sleep_mode(SLEEP_MODE_IDLE);
    00171                 DDRC  = (1 << PINC3);
    00172                 PORTC = (1 << PINC3);
    00173         }
    00174
    00175         sei();
    00176         _delay_ms( 250 );
    00177         // Check if key
    00178         while ( !(PIND & (1 << PIND2))){
    00179         }
    00180         run = false;
    00181
    00182         PORTC &= ~((1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4));  //LED OFF
    00183         DDRC = 0x0;//INPUT
    00184
    00185         if (gState == 0) {
    00186             while (!run ){
    00187             }
    00188         } else {
    00189             cli();
    00190             sleep_enable();
    00191             sleep_bod_disable();
    00192             sei();
    00193             sleep_cpu();
    00194             sleep_disable();
    00195             cli();
    00196             /* Wake up from Power-Save mode */
    00197             if (gState == 2) {
    00198                 Disable_RTC();
    00199             }
    00200         }
    00201         gState++;
    00202         if (gState > 3) gState =0;
    00203     }
    00204 }

Here is the call graph for this function:

void RTC_Init | ( | void |  | ) |

Enable RTC.

Definition at line 98 of file main.c.

Referenced by main().

    00099 {
    00100     PRR &= ~(1 << PRTIM2 );
    00101     ASSR |= (1 << AS2);
    00102 }

## Variable Documentation

uint8_t gState = 0

Global variable to keep track of which state the program is in.

Definition at line 67 of file main.c.

Referenced by main().

volatile bool run = false

Definition at line 68 of file main.c.

Referenced by ISR(), and main().