Saturday 9 May 2020

Attiny25/45/85 Low Power Led Flasher

Connect Led Pin to PB0 of Attiny25/45/85 (Pin 5)

CODE:

//ATTINY25/45/85
#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 boolean f_wdt = 1;
void setup() {
  pinMode(pinLed, OUTPUT);

  // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
  // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec

  setup_watchdog(9);    // Set Sleep Time (approximately 8 seconds sleep)
}

void loop() {
  if (f_wdt == 1) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs
    f_wdt = 0;     // reset flag

    digitalWrite(pinLed, HIGH); // let led blink
    delay(70);                               //Set Led On Time
    digitalWrite(pinLed, LOW);

    pinMode(pinLed, INPUT); // set all used port to intput to save power
    system_sleep();
    pinMode(pinLed, OUTPUT); // set all ports into state before sleep
  }
}

// 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
}

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 = 1;        // set global flag
}

//-------------------------------------------------------------------------------------------//

Attiny13 Low Power Led Flasher

Connect Led to Pin PB0 of Attiny13 (Pin No 5)


CODE:

//ATTINY13A

#define F_CPU 1200000UL       // MCU frequency in hertz

#include <avr/io.h>
#include <avr/wdt.h>         
#include <avr/sleep.h>       
#include <avr/power.h>
#include <avr/interrupt.h>   
#include <util/delay.h>

#define led 0                 // PB0

int main( void )              // similar to void setup ()
{
  wdt_reset();                // first you need to reset the Wachdog
  // otherwise there may be a reboot
  pinMode(led, OUTPUT);
 
while (1) {                 // perpetual loop, analog of void loop ()
    digitalWrite(led, HIGH);
    _delay_ms(50);            // On Time
    digitalWrite(led, LOW);
    sleepFewSeconds();        // sleep for 8 sec.
  }
}

void sleepFewSeconds() {
  wdt_reset();                // reset the watchdog
  PORTB = 0x00;               // submit the log. 0 to all ports
  DDRB = 0x00;
  ADCSRA &= ~(1 << ADEN);     // disable ADC
 
// otherwise there will be excess current consumption in sleep mode
  MCUSR &= ~(1 << WDRF);

 /* Start the WDT Config change sequence. */
  WDTCR |= (1 << WDCE) | (1 << WDE);
  /* Configure

the prescaler and the WDT for interrupt mode only*/
  // Uncomment the interval you need
  WDTCR = (1 << WDP0) | (1 << WDP3) | (1 << WDTIE); // 8sec
  //WDTCR = (1<<WDP3) | (1<<WDTIE); // 4sec
  //WDTCR = (1<<WDP2) | (1<<WDP1) | (1<<WDP0) | (1<<WDTIE); // 2sec
  //WDTCR = (1<<WDP2) | (1<<WDP1) | (1<<WDTIE); // 1sec
  //WDTCR = (1<<WDP2) | (1<<WDP0) | (1<<WDTIE); // 0.5sec
 
WDTCR |= (1 << WDTIE);
  sei();      // Enable global interrupts
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  while (1) {
    sleep_enable();   // allow sleep
    sleep_cpu();      // sleep!
    sleep_disable();
  }
}