Attiny44 Watchdog timer

Hello,

I would like to make an Attiny44 sleep in power down for 10 seconds and wake for 2 seconds using the wdt. Please can some advise me on some code for this.

Any help would be greatly appreciated.

Thanks,
J

If codes are provided, then how are you going to test them? What kind of hardware setup you have?

#include <avr/sleep.h>
#include <avr/wdt.h>

#define adc_disable() (ADCSRA &= ~(1 << ADEN)) // Disable ADC to save a lot of current consumption.

int ledState = 0;                       // Allows easy tracking of whether LED is on or off.
int ledPin = 4; 

// Use these values to set the on and off time period. The sketch uses 6 for 1 second on, 1 second off..
// 0 = 16ms, 1 = 32ms, 2 = 64ms, 3 = 128ms, 4 = 250ms, 5 = 500ms, 6 = 1 sec, 7 = 2 sec, 8 = 4 sec, 9 = 8 sec
int period = 9;                        // This = 4 second timeout. Refer to table above

volatile boolean f_wdt = true;

//****************************
 
void setup()
{

  adc_disable();                       // ADC uses ~320uA.
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // This is the lowest power consumption mode.

  pinMode(ledPin, OUTPUT);
  
  setup_watchdog(period);              // Set watchdog to wake system each 8 seconds.

} // End of setup()

//****************************
 
void loop()
{
  
  if (f_wdt == 1)                       // If this flag is set then watchdog timed out. This happens each second.
    {  
    f_wdt = false;                      // Reset the 'watchdog has timed out' flag.

    if (ledState == 0)
      {
      ledState = 1;
      digitalWrite(ledPin, HIGH);  
         
      }
    else
      {
      ledState = 0;
      digitalWrite(ledPin, LOW);
      
      }
    systemSleep();
    }
    
} // End of loop()

//****************************
 
// Put system to sleep. It will be woken when the watchdog times out.

void systemSleep() 
{
  
  sleep_enable();
   
  sleep_cpu();
   
  sleep_disable();                     // System continues execution here when watchdog times out.

} // End of systemSleep()

//****************************

// This ugly code manipulates some internal registers of the ATtiny.

void setup_watchdog(int interval) 
{
  byte bb;
 
  if (interval > 9 ) interval = 9;
  bb = interval & 7;
  if (interval > 7) bb |= (1 << 5);
  bb|= (1 << WDCE);
   
  MCUCR &= ~(1 << WDRF);

  WDTCSR |= (1 << WDCE) | (1 << WDE);

  WDTCSR = bb;                          // Set new watchdog timeout value.
  WDTCSR |= _BV(WDIE);
  
} // End of setup_watchdog()

//****************************
 
// This is the watchdog Interrupt Service Routine (ISR). It is executed when the watchdog times out but does nothing. The real work is done in loop().



ISR(WDT_vect) 

{
f_wdt=1;
  ;
  
} // End of ISR()

Hi There,

I have just added my code above.

I am using an attiny44.

How are you going to program your ATtiny44 -- Arduino as ISP Programmer or AVR Programmer or Parallel Programmer or ATtiny44 Dev Board?

You want your MCU to sleep for 10 sec and then to be wake-up by WDT. Do you know that the maximum time delay the WDT can generate is 8 sec (Fig-1)?


Figure-1:

Thank you for this :+1:, I thought there may be a way around it. I would still like to wake and sleep at different intervals to reduce power as much as possible.

I am using an USBASP-Programmer.

Have you tried the sketch of your post #3? Is it working?

Can you post pictures of your Programmer and the ATtiny Chip?