Question

I am working with ATMEGA128 16AC. so i want to use watchdog timer to reset the AVR atleast after 10 secs. For ex keypad key is not pressed within 10 secs the controller should get restarted.

I tried with library functions like wdt_enable(value);and by configuring WDTCR reg too, but failed to get 10 secs delay.

Please help me out in this issue.

Thanks in Advance.

Was it helpful?

Solution 2

In ATmega 128 use 16 bit timer configure it as shown below.

// Enable overflow interrupt.
TIMSK |= (1 << TOIE1);      

// Enable global interrupts.            
sei();

// Start timer at Fcpu/1024. Approx 3.33sec for each cycle.(F_CPU = 16MHz External)                                     
TCCR1B |= (1 << CS12) | (1 << CS10); 

So when you configure the timer like this, timer will overflow every 3.33secs and then you can define multiples of this overflow like

#define INPUT_KEY_TIMEOUT 10  

just you need to check the status in the timer ISR as

  if(system_timer_count == INPUT_KEY_TIMEOUT) {
      //Do something if the following condition met.
  }

By this you can create any time delay without disturbing controller's normal work. Only when the above condition met the controller is ready to do whatever in the ISR.

Hope this helps others.

OTHER TIPS

The Watchdog timer is dependent on two things (from the datasheet):

  • The Watchdog RC oscillator operating mode (Table 13, page 41). You want to set this to the lowest value possible, which is 1MHz (the default value).
  • The Watchdog prescaler (Table 22, page 56). You want to set this to the highest possible value, which is 2,048K and gives you a maximum timeout of 1.8 seconds (if your Vcc is 5V).

Therefore you cannot use the WDT for anything longer than 1.8 seconds.

What you can do is to use one of the 16-bit timers (which also have prescalers, Table 62, page 136) to generate an interrupt, which calls an interrupt routine that you have to write. What the interrupt routine should do is decrease a global variable which is set at the startup in its declaration, and if this variable reaches 0, jump to address 0 (the same address at which the AVR begins execution after a reset). You can easily calculate what those values should be.

If a key is pressed, then reset the timer and the global variable. With all these interrupt routines modifying the variable, it will need to be declared volatile.

I have more experience with the PIC16 series, but I'm pretty sure that this would work an an AVR.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top