I'm using sleep mode to turn off my device after the routine has finished executing and want to use interrupts to wake it up every 33 milliseconds. Basically, the idea is that if the routine completes in less than 33 milliseconds the processor can shutdown and save power for whatever remaining time, which will hopefully translate to reduced power consumption. I've currenlty managed to turn off my device with the sleepNow()
function you see below, but the interrupt I have programmed in doesn't seem to work. The program simply goes to sleep and never wakes up. What am I doing wrong?
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
const uint16_t PERIOD = 2150;
void setup()
{
noInterrupts();
TCCR1A = 0; // undo the timer config done...
TCCR1B = 0; // ...by the Arduino core library
TCNT1 = 0; // reset the timer
OCR1A = PERIOD - 1; // set the period
TIMSK1 = _BV(OCIE1A); // enable TIMERx_COMPA interrupt
TCCR1B |= (1 << CS12); // ...and set the prescaler
interrupts();
Serial.begin(9600);
}
ISR(TIMER1_COMPA_vect)
{
Serial.println(millis());
}
void loop()
{
Serial.println("Program is on");
Serial.flush();
sleepNow();
}
void sleepNow()
{
// Choose our preferred sleep mode:
set_sleep_mode(SLEEP_MODE_PWR_SAVE);
// Set sleep enable (SE) bit:
sleep_enable();
// Put the device to sleep:
sleep_mode();
// Upon waking up, sketch continues from this point.
sleep_disable();
}
SLEEP_MODE_PWR_SAVE
.ISR
and re-enters the loop. It just infinently prints out the time. It didn't use to do that before when I used IDLE and Timer 1.ISR
function the program runs just fine.