Mega48 Watchdog not generating an interrupt

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can't get the watchdog to generate an interrupt in order to break out of a loop. I have read the datasheet and I can't work out what I've done wrong. The code just sits in the loop and never jumps to the ISR.

It might be my C coding, I should have stuck to assembler! :wink:

#include 
#include 
#include 
#include 
#include 
#include 

void intialisation (void)
{


	// set PORTB for all output
	DDRB = 0xFF;
	// set PORTC for 4 Inputs 
	DDRC = 0xB8;
	// set PORTD for 2 Inputs 
	DDRD = 0xF9;
	

	WDTCSR |= (1<<WDCE);   // enable WDT 4 cycle timer operation bit 

	WDTCSR = (1 << WDIE) | (1 << WDP1) | (1 << WDP0);


	
	sei();



}


ISR(WDT_vect)
{
	PORTB = 0xFF;
	
}


int main(void)
{

	intialisation();

	for(;;)
	{
	}

		
}

Any help is greatly appreciated, it's doing my head in.

BTW - I'm only running it in the simulator at the moment, just incase that's my problem.

TIA

Ian

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

Please see the data sheet, p 49ff. You must follow a specific procedure when configuring the WDT through writing to the WDTCSR register. (I'd quote some key paragraphs here, but Atmel lawyers have decided that we are not allowed to copy text out of the poop sheets).

Also, it looks as you are setting the WDT to time out after 256K cycles. This will take a looooong time to execute in the simulator, as it does not run in any way near MHz speeds. Rather think KHz for the simulator.

Lastly, you are lucky using the mega48 as mega48/88/168 are the only devices that have WDT support/capability in the simulator!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Many thanks, I've altered my code to the following


void intialisation (void)
{


	// set PORTB for all output
	DDRB = 0xFF;
	// set PORTC for 4 Inputs 
	DDRC = 0xB8;
	// set PORTD for 2 Inputs 
	DDRD = 0xF9;

	cli();

	wdt_reset();
	

	WDTCSR |= (1<<WDCE) | (1 << WDE);   // enable WDT 4 cycle timer operation bit 

	WDTCSR = (0 << WDIE) | (0 << WDP1) | (0 << WDP0) | (1 << WDE);// 

	sei();



}

But I cannot generate an interrupt unless the WDE (watchdog enable bit) is set. This only generates a system reset though, which I don't want. If I set the bits as per the datasheet to generate an interrupt (WDE = 0, WDIE = 1) nothing happens in the simulator (you'll notice I've shortened the timeout :wink: )

Is there any other way of determining if the watchdog is running, or is it just go/no-go in the simulator?

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

But in

WDTCSR = (0 << WDIE) | (0 << WDP1) | (0 << WDP0) | (1 << WDE);

you are clearing the WDIE bit!? If you disable the interrupt you will most certainly not get it.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Ahh yes, sorry I sent a copy of the last lot of code I simulated. :oops:

As I said, I've tried all combinations of WDE and WDIE as stated in the datasheet and I only see something happen when I set WDE, which is putting the device into a mode I don't want to use. Even setting both bits, which should give an interrupt and then a reset only appears to generate a reset.

Is it my ISR? Will you see a jump to an ISR in the simulator if the code is correct?

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

A couple of things based on the original code posted:

--Heed the datasheet about the first write to WDTCSR:

Quote:
1. In the same operation, write a logic one to the Watchdog change enable bit
(WDCE) and WDE. A logic one must be written to WDE regardless of the previous
value of the WDE bit.

So I've got a sequence like this (that I know works):

WDTCSR = BIT_WDTCSR_WDCE | BIT_WDTCSR_WDE | WDT_8_SEC;
WDTCSR = BIT_WDTCSR_WDIE | WDT_8_SEC;

But it may not work for you as-is (well, after you make the bit defines). Why? 'Cause your compiler may be optimizing the sequence, or otherwise manipulating it. With my compiler I'm turning off the optimizations around this code block. You will need to examine the output of your compiler and ensure that both operations are indeed carried out in separate instructions, and within 4 cycles.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Thanks for your help guys, works on the hardware now (still not on the simulator, but I'm not fussy)

:D

Ian

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

Great to hear that its working.

Quote:
still not on the simulator, but I'm not fussy

Maybe report that as a simulator bug/issue to Atmel then? Atmel keeps a list of simulator issues in the help files for our conveinence and reference. See sticky thread in the AVR Studio forum.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]