Issues with watchdog on Attiny45

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
void setup() {
pinMode(4,OUTPUT);
pwrDown();
}

void loop() {

digitalWrite(4,HIGH);
delay(100);
digitalWrite(4,LOW);
delay(100);


}

void radioOn(){             //this function enables 125khz decoding clock and carrier frequency at arduino pin 3
                            //******* SPECIAL NOTE: THE FOLLOWING SETTINGS WILL OUTPUT A 125KHZ SIGNAL ON PIN 3 WITH NO GLITCHES, DO NOT CHANGE ******************//
  TCCR0A = 0b00010010;      //SET OC2B TO TOGGLE //SET WGM2:0 TO CTC
  TCCR0B = 0b01000001;      //NO PRESCALER
  OCR0A = 64;               //COUNT TO 63 AND TOGGLE
  TCNT0 = 0;                //RESET COUNTER

  TIMSK = 0b00010000;      //enable compare match interrupt with timer 2.  calls ISR TIMER2_COMPA_vect
}

void pwrDown(){                   //this will execute an 8 second power down sleep
  MCUCR |= 0b00110000;            //set bits SE, SM1
  MCUCR &= 0b11110111;            //clear bit SM0
  WDTCR |= 0b00010000;            //set WDCE
  WDTCR = 0b01001000;             //SET WDIE (6) AND WDE (3)
  WDTCR = 0b00100001;             //CLEAR WDIE(6) AND WDE(3) AND SET PRESCALER TO 1001 (8S)
  WDTCR |= 0b01000000;            //SET WDIE(6) BIT ONLY
  __asm__ __volatile__("sleep");  //run sleep command
}

void halfDelay(){                 //this will execute an 1/2 second power down sleep
  MCUCR |= 0b00110000;            //set bits SE, SM1
  MCUCR &= 0b11110111;            //clear bit SM0
  WDTCR |= 0b00010000;            //set WDCE
  WDTCR = 0b01001000;             //SET WDIE (6) AND WDE (3)
  WDTCR = 0b00000101;             //CLEAR WDIE(6) AND WDE(3) AND SET PRESCALER TO 1001 (8S)
  WDTCR |= 0b01000000;            //SET WDIE(6) BIT ONLY
  __asm__ __volatile__("sleep");  //run sleep command
}

ISR(TIMER2_COMPA_vect){      //timer 2 interrupt necessary for decoding manchester bitstream
  //increment ticks count here
}

ISR(WDT_vect){
  MCUCR &= 0b11011111;            //clear SE until needed 
  WDTCR |= 0b00011000;            //set WDCE and WDE
  WDTCR &= 0b10100111;            //clear WDE AND WDIE
}

Hi, I'm trying to downsize some code from a 328p into an ATTINY45 because I only needed 3 io pins and it has 16mhz internal clock, plus is smaller and gives me more board real estate.  Only problem is, I'm having some issues with turning off the watchdog once it's been used, this will be a battery powered device, so using the WDT is necessary.  Here's the code I wrote to test the WDT delays I wrote.  What's happening is it starts up, waits about 8 seconds, flashed the LED rapidly for about 8 seconds, then turns off for another 8 seconds and repeats.  I referenced the datasheet for the procedure for changing the WDT registers, and I think I am compliant, but I can't seem to get the WDT to stop putting the ATTiny to sleep without actually sending the sleep command.  I think what's happening is the WDT is triggering a reset on it's own, that would explain the LED running, then apparently the whole program starting from 0.  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

GCC has it's own "libs" for the WDT and SLEEP, you must use, as your code above does not guarantee the timing needed for proper operation due to the way GCC can do strange things while optimizing code!

One of the GCC gurus will come along with details I'm sure.

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Fri. Nov 8, 2019 - 08:28 PM