ATmega168 watchdog too fast

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm

ATmega168 watchdog too fast

Post by uhe »

Hi,

for testing the watchdog I setup an ATmega168 (straight from the shop, no fuses changed) on a breadboard with an LED attached to pin PC5.
For the test I used the following sniplet to toggle the pin everytime the watchdog fires which I set to 8 seconds:

Code: Select all

#include <avr/interrupt.h>
#include <avr/io.h>

int main(void) {    
  DDRC |= _BV(PC5);                               // enable toggle LED
  sei();                                          // global IRQ enable	  
  WDTCSR = _BV(WDIE) | _BV(WDP0) | _BV(WDP3);     // enable WD 8 sec - Datasheet page 56
  while(1);	
  return 0;
}

ISR(WDT_vect) {
  PINC = _BV(PC5);                                // toggle PC5
}
The watchdog fires and the LED blinks, but way to fast!
It looks like *always on with flickering* and not on/off every 8 seconds.
Anyone any clue what I missed?

SiliconFarmer
 
Posts: 33
Joined: Sun May 31, 2009 2:07 pm

Re: ATmega168 watchdog too fast

Post by SiliconFarmer »

Stick some printf's in there, and I think you will quickly see your problems. I see 2 or 3 bugs.

Also, you should consider using setup().

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: ATmega168 watchdog too fast

Post by adafruit »

ISR(WDT_vect) {
PINC = _BV(PC5); // toggle PC5
}
is weird

SiliconFarmer
 
Posts: 33
Joined: Sun May 31, 2009 2:07 pm

Re: ATmega168 watchdog too fast

Post by SiliconFarmer »

You should read up on ALL of the WDTCSR bits. Bit 4 will be of special interest to you.

uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm

Re: ATmega168 watchdog too fast

Post by uhe »

@SiliconFarmer
printf() what and where should that go? Also who should call setup()?
I don't use an arduino, just an ATmega programmed with an usbtiny clone...
Which bug do you see?
I tryed using WDCE like this but with no success:

Code: Select all

  DDRC |= _BV(PC5);                               // enable toggle LED
  WDTCSR |= _BV(WDCE); 
  sei();
@Ladyada
The datasheet mentions this on page 72:
However, writing a logic one to a bit in the PINx Register, will result in a toggle in the corresponding bit in the Data Register.
Before i used one of these:

Code: Select all

(PINC & _BV(PINC5) ? (PORTC &= ~_BV(PC5)) : (PORTC |= _BV(PC5));
PORTC ^= _BV(PC5);


Since I've used the watchdog frequently within the tiny2313:

Code: Select all

int main(void) {    
  DDRD |= _BV(PD4);              // enable toggle LED
  sei();			                   // global IRQ enable	  
  WDTCSR = _BV(WDIE) | _BV(WDP0) | _BV(WDP1) | _BV(WDP2);     // enable WD 2 sec
  while(1) {};	
  return 0;
}

ISR(WDT_OVERFLOW_vect) {
  PIND = _BV(PD4);                // toggle PD4
}
just works, the pin used is different and the ISR name but apart from the datasheet shows no difference between the attiny and atmega watchdog..

uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm

Re: ATmega168 watchdog too fast

Post by uhe »

Okay, it works (with a little addition, which disables system reset i think):

Code: Select all

int main(void) {    
  DDRC |= _BV(PC5);                         // enable toggle LED  
  sei();			                           // global IRQ enable	    
  WDTCSR |= _BV(WDCE) | _BV(WDE);           // ??
  WDTCSR = _BV(WDIE) | _BV(WDP0) | _BV(WDP1) | _BV(WDP2);     // enable WD 2 sec  
  while(1) {};	
  return 0;
}

ISR(WDT_vect) {
  PINC = _BV(PC5);              // toggle PC5
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”