Last active
November 3, 2015 11:56
-
-
Save dinolupo/3f3ec26d5ff02961625b to your computer and use it in GitHub Desktop.
Low Power Blinking Led with Arduino and AT Tiny 85 processor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Ultra Low Power Led Flasher | |
with AtTiny85 @ 1Mhz | |
by Luca Soltoggio | |
06/01/2014 | |
http://www.arduinoelettronica.com | |
modified by Dino Lupo | |
03/11/2015 | |
*/ | |
#include <avr/sleep.h> | |
#include <avr/wdt.h> | |
#ifndef cbi | |
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) | |
#endif | |
#ifndef sbi | |
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) | |
#endif | |
int pinLed = 0; | |
volatile int f_wdt = 0; | |
// ---------------------------------------------------------------------------------- | |
// NON CAMBIARE NULLA PRIMA DI QUESTA LINEA ^^^^^^^^^^^ | |
// --------------------------------------------------|||||||||||--------------------- | |
/* | |
Tempo di sleep del watchdog (il timer a risparmio energetico) | |
impostare un valore da 0 a 9 corrispondente al tempo indicato in tabella | |
ad esempio per impostare intervalli di 1 secondo scrivere: | |
int tempo_di_sleep_del_watchdog = 6 | |
0 = 16ms 5 = 500ms | |
1 = 32ms 6 = 1 sec | |
2 = 64ms 7 = 2 sec | |
3 = 128ms 8 = 4 sec | |
4 = 250ms 9 = 8 sec | |
*/ | |
int tempo_di_sleep_del_watchdog = 7; | |
// numero di iterazioni del watchdog da eseguire prima di chiamare la routine | |
// ad esempio se hai scelto sleep=7 (cioè 2 secondi) e iterazioni 10 allora il tempo totale prima di eseguire | |
// la tua routine sarà di 20 secondi | |
int numero_di_iterazioni = 5; | |
// questa funzione viene eseguita quando il timer impostato dalle variabili in alto termina | |
void codice_che_viene_eseguito_al_risveglio() { | |
//esecuzione dell'operazione al risveglio | |
digitalWrite(pinLed,HIGH); // let led blink | |
delay(30); | |
digitalWrite(pinLed,LOW); | |
} | |
// --------------------------------------------||||||||||||--------------------------- | |
// NON CAMBIARE NULLA SOTTO QUESTA LINEA vvvvvvvvvvvv | |
// ---------------------------------------------------------------------------------- | |
void setup(){ | |
pinMode(pinLed,OUTPUT); | |
setup_watchdog(tempo_di_sleep_del_watchdog); // approximately 4 seconds sleep | |
} | |
void loop(){ | |
pinMode(pinLed,INPUT); // set all used port to intput to save power | |
system_sleep(); | |
pinMode(pinLed,OUTPUT); // set all ports into state before sleep | |
if (f_wdt >= numero_di_iterazioni) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs | |
f_wdt=0; // reset flag | |
codice_che_viene_eseguito_al_risveglio(); | |
} | |
} | |
// set system into the sleep state | |
// system wakes up when wtchdog is timed out | |
void system_sleep() { | |
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here | |
sleep_enable(); | |
sleep_mode(); // System sleeps here | |
sleep_disable(); // System continues execution here when watchdog timed out | |
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON | |
} | |
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms | |
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec | |
void setup_watchdog(int ii) { | |
byte bb; | |
int ww; | |
if (ii > 9 ) ii=9; | |
bb=ii & 7; | |
if (ii > 7) bb|= (1<<5); | |
bb|= (1<<WDCE); | |
ww=bb; | |
MCUSR &= ~(1<<WDRF); | |
// start timed sequence | |
WDTCR |= (1<<WDCE) | (1<<WDE); | |
// set new watchdog timeout value | |
WDTCR = bb; | |
WDTCR |= _BV(WDIE); | |
} | |
// Watchdog Interrupt Service / is executed when watchdog timed out | |
ISR(WDT_vect) { | |
f_wdt++; // set global flag | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment