One of the features that was missing in my firmware was the sleep/power save mode. Since I already spent all my GPIOs on the LEDs and the sensor, I had to build a logic with the reset button.
The solution I found was to use the EEPROM.
When the AVR resets, it reads the first byte from the EEPROM, negates it, and writes it back. When the value read is 1/true, the program runs. When it is not, the MCU is put to sleep.
All of this logic was simply put at the top of the main function body.
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <avr/sleep.h>
int main(void) {
/* When reset, read whether program shall be run */
uint8_t run = eeprom_read_byte (EEPROM_ADDR);
/* invert value and write back to eeprom */
eeprom_write_byte (EEPROM_ADDR, run ? 0 : 1);
if(!run) {
/* disable all interrupts and go to sleep */
cli();
sleep_mode();
}
while(1) {
/* control loop code here */
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.