/* * lowpower2.c */ //#include <avr/io.h> #include <avr/io.h> #include "avr/interrupt.h" #include "avr/wdt.h" #include "avr/sleep.h" #include <avr/power.h> #include <avr/interrupt.h> //#include <util/delay.h> void optimize_power_consumption(void){ /* Disable digital input buffer on ADC pins */ DIDR0 = (1 << ADC5D) | (1 << ADC4D) | (1 << ADC3D) | (1 << ADC2D) | (1 << ADC1D) | (1 << ADC0D); DIDR0 |= 0xC0 ; /*ADC7D and ADC6D are undefined in header file so set bits this way*/ /* Disable digital input buffer on Analog comparator pins */ DIDR1 |= (1 << AIN1D) | (1 << AIN0D); /* Disable Analog Comparator */ ACSR |= (1 << ACD); /*Power shutdown to unused peripherals*/ PRR0 = 0xDF; PRR1 = 0x3F; /*Unused pins set as input pull up*/ DDRB &= 0xE0; DDRC &= 0xC9; DDRD &= 0x03; DDRE &=0xF3; PORTB |=~(0xE0); PORTC |=~(0xC9); PORTD |=~(0x03); PORTE |=~(0xf3); /*Watchdog Timer OFF*/ /* Disable interrupts */ cli(); /* Reset watchdog timer */ wdt_reset(); /* Clear WDRF in MCUSR */ MCUSR &= ~(1<<WDRF); /* Turn off WDT */ WDTCSR = 0x00; /* Set sleep mode */ set_sleep_mode(SLEEP_MODE_PWR_DOWN); } /******************************************************************************** TMR 0 Initialization ********************************************************************************/ void TMR0_init( void ){ // enable timer overflow interrupt for both Timer0 and Timer1 TIMSK0=(1<<TOIE0) ; // set timer0 counter initial value to 0 TCNT0=0x00; // start timer0 with /1024 prescaler TCCR0B = (1<<CS02) | (1<<CS00); // enable interrupts sei(); } uint8_t count; /* Timer 0 Interrupt */ ISR(TIMER0_OVF_vect){ count++; if(count==20){ count=0; // Toogle LED PORTB=PORTB ^ 0x20; } } ISR (PCINT0_vect) { if (!(PINB & (1<<PINB7))) // if PINB7 is low (Switch pressed) { PORTB |= (1<<PORTB5); // Turn ON LED } else { PORTB &= ~(1<<PORTB5); // Turn OFF LED } } int main(void) { TMR0_init(); DDRB |= 1<<DDRB5; // Direction of pin PB5 set as Output DDRB &= ~(1<<DDB7); //Set PORTB7 as input optimize_power_consumption(); PCMSK0 |= (1<<PCINT7); //Enable Pin Change Interrupt 7 PCICR |= (1<<PCIE0); //Enable the interrupt enable bit for PCINT sei(); //set_sleep_mode(SLEEP_MODE_PWR_DOWN); //sleep_enable(); //sleep_cpu(); /* Replace with your application code */ while (1) { sleep_mode(); } }