Hola,
He estado jugando un poco con sleep del ATMega168. En concreto con Power save Mode, para sustituir esto en mis delays y poder ahorrar bateria.
En concreto pongo el micro en Sleep y se despierta cuando hay un Overflow del Timer2.
Viendo resultados, merece la pena....
Pongo el código de una simple aplicación que hace parpadear el led del pin 13 del Diecimila durante 2 segundos (para poder medir la corriente).
Los resultados han sido:
-Sleep Mode and turn off devices=> Led_on=12,98 mA & Led_off=9,91 mA
-No Sleep and turn off devices => Led_on=28,9 mA & Led_off=25,9 mA
-No Sleep => Led_on=32,2 mA & Led_off=29,2 mA
Turn off devices es que apago el ADC, Timer 0, Timer 1, USART, TWI y SPI.
SLEEP MODE TEST CODE
#include <avr/interrupt.h>
#include <avr/sleep.h>
volatile int cont;
void setup()
{
pinMode(13,OUTPUT);
TCCR2A=0;
TCCR2B=(0<<7)|(0<<6)|(0<<5)|(0<<4)|(0<<3)|(1<<2)|(1<<1)|(1<<0);
// Bit 0.- Overflow
TIMSK2=(0<<2)|(0<<1)|(1<<0);
//Power Save Mode
SMCR=(0<<3)|(1<<2)|(1<<1)|(0<<0);
}
ISR(TIMER2_OVF_vect)
{
cont++;
}
void loop()
{
sleep_enable();
//Turn off ADC
ADCSRA=0;
// TWI | TIMER2 | TIMER0 | VOID | TIMER1 | SPI | USART | ADC
PRR=(1<<7)|(0<<6)|(1<<5)|(1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0);
sei();
while (cont<122)
{
//Go to Sleep
sleep_mode();
}
//Disable Sleep
sleep_disable();
//Disable interrupts
cli();
cont=0;
digitalWrite(13,!digitalRead(13));
}
NO SLEEP AND TURN OFF DEVICES
#include <avr/interrupt.h>
#include <avr/sleep.h>
volatile int cont;
void setup()
{
pinMode(13,OUTPUT);
TCCR2A=0;
TCCR2B=(0<<7)|(0<<6)|(0<<5)|(0<<4)|(0<<3)|(1<<2)|(1<<1)|(1<<0);
// Bit 0.- Overflow
TIMSK2=(0<<2)|(0<<1)|(1<<0);
//Turn off ADC
ADCSRA=0;
// TWI | TIMER2 | TIMER0 | VOID | TIMER1 | SPI | USART | ADC
PRR=(1<<7)|(0<<6)|(1<<5)|(1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0);
sei();
}
ISR(TIMER2_OVF_vect)
{
cont++;
if (cont<122)
{
cont=0;
digitalWrite(13,!digitalRead(13));
}
}
void loop()
{
}
NO SLEEP
#include <avr/interrupt.h>
#include <avr/sleep.h>
volatile int cont;
void setup()
{
pinMode(13,OUTPUT);
TCCR2A=0;
TCCR2B=(0<<7)|(0<<6)|(0<<5)|(0<<4)|(0<<3)|(1<<2)|(1<<1)|(1<<0);
// Bit 0.- Overflow
TIMSK2=(0<<2)|(0<<1)|(1<<0);
//Turn off ADC
//ADCSRA=0;
// TWI | TIMER2 | TIMER0 | VOID | TIMER1 | SPI | USART | ADC
//PRR=(1<<7)|(0<<6)|(1<<5)|(1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0);
sei();
}
ISR(TIMER2_OVF_vect)
{
cont++;
if (cont==122)
{
cont=0;
digitalWrite(13,!digitalRead(13));
}
}
void loop()
{
}
Por si alguien le pueda venir bien...
Salu2,
Igor R.