watchdog timer

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

Hi, i want to use watchdog timer feature of attiny404, i couldn't get how to reset watchdog timer counter using c code, i went through few discussion made earlier in this forum but my doubt is, to use the WDT_RESET() function do i need any special header files ??

vignesh

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

can i refresh the WDT by writing the period value again into WDT.CTRLA register ??

 

vignesh

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

The answer can be easily obtained by Google's and search for "AVR" and "WDT_RESET".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define F_CPU 5000000
#include <avr/io.h>
#include <avr/wdt.h>

void main(void) {

  _PROTECTED_WRITE(WDT.CTRLA, WDT_PERIOD_8KCLK_gc); /* 8 sec watchdog */

  sei();
  while (1) {
    wdt_reset();
    ...
  }
}

 

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

Did you read the page in the manual?...

 

https://www.nongnu.org/avr-libc/...

 

But basically you start the WDT then keep kicking the dog with wdt_reset().

 

As always.... WDT is something you must design in from the start. Don't try to add it at the end as an afterthought. 

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

I have been looking at the datasheet and <avr/wdt.h> and i have some question about how to set it up:

 

1. I have been looking at the implementation for <avr/wdt.h> and it dosent say that it can be used for ATtiny404, so the question is can it?

 

2. If you cant use the libary, how you you start and reset the wdt?
 

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

This is an example of setting with a fuse.
Watchdog is automatically enabled at boot.
If you remove wdt_reset(), the device will reset every second.

 

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

FUSES = {
    .WDTCFG     = WDT_PERIOD_OFF_gc | WDT_PERIOD_1KCLK_gc,
//  .BODCFG     = FUSE_BODCFG_DEFAULT,
//  .OSCCFG     = FUSE_OSCCFG_DEFAULT,
//  .SYSCFG0    = FUSE_SYSCFG0_DEFAULT,
//  .SYSCFG1    = FUSE_SYSCFG1_DEFAULT,
//  .CODESIZE   = 0,
//  .BOOTSIZE   = 0,
};

int main(void){
    wdt_reset();

    while (1){
        wdt_reset();
    }
}

 

Fix mistakes.

    .WDTCFG     = WDT_WINDOW_OFF_gc | WDT_PERIOD_1KCLK_gc,

 

Last Edited: Tue. Sep 20, 2022 - 04:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok, that makes sense. I look abit into the fuses and i dont understand them fully yet. 

 

I could not locate fuses so I found FUSE_WDTCFG, is that the same? 

I like to have all my functions in seperate .c / .h files so you you think that the code under will work and do i need to add #include <avr/wdt.h> in the .c file or just main as i'm only using wdt_reset() from there?

 

thanks for the help :)

 

#include "WDT_ATTINY404.h" 
#include <avr/io.h>

void init_WDT(){
FUSE_WDTCFG = ( window_WDT(0) | Period_WDT(11));
}

void window_WDT(unsigned int Selector){

    if (Selector == '0'){
        WDT_WINDOW_OFF_gc = (0x00<<4);  /* Window mode off */
    }
    else if(Selector == '1'){
        WDT_WINDOW_8CLK_gc = (0x01<<4);  /* 8 cycles (8ms) */
    }
    else if(Selector == '2'){
        WDT_WINDOW_16CLK_gc = (0x02<<4);  /* 16 cycles (16ms) */
    }
    else if(Selector == '3'){
        WDT_WINDOW_32CLK_gc = (0x03<<0);  /* 32 cycles (32ms) */
    }
    else if(Selector == '4'){
        WDT_WINDOW_64CLK_gc = (0x04<<0);  /* 64 cycles (64ms) */
    }
    else if(Selector == '5'){
        WDT_WINDOW_128CLK_gc = (0x05<<0);  /* 128 cycles (0.128s) */
    }
    else if(Selector == '6'){
        WDT_WINDOW_256CLK_gc = (0x06<<0);  /* 256 cycles (0.256s) */
    }
    else if(Selector == '7'){
        WDT_WINDOW_512CLK_gc = (0x07<<0);  /* 512 cycles (0.512s) */
    }
    else if(Selector == '8'){
        WDT_WINDOW_1KCLK_gc = (0x08<<0);  /* 1K cycles (1.0s) */
    }
    else if(Selector == '9'){
        WDT_WINDOW_2KCLK_gc = (0x09<<0);  /* 2K cycles (2.0s) */
    }
    else if(Selector == '10'){
        WDT_WINDOW_4KCLK_gc = (0x0A<<0);  /* 4K cycles (4.1s) */
    }
    else if(Selector == '11'){
        WDT_WINDOW_8KCLK_gc = (0x0B<<0);  /* 8K cycles (8.2s) */
    }
}
void Period_WDT(unsigned int Selector){

    if (Selector == '0'){
        WDT_PERIOD_OFF_gc = (0x00<<0);  /* Watch-Dog timer Off */
    }
    else if(Selector == '1'){
        WDT_PERIOD_8CLK_gc = (0x01<<0);  /* 8 cycles (8ms) */
    }
    else if(Selector == '2'){
        WDT_PERIOD_16CLK_gc = (0x02<<0);  /* 16 cycles (16ms) */
    }
    else if(Selector == '3'){
        WDT_PERIOD_32CLK_gc = (0x03<<0);  /* 32 cycles (32ms) */
    }
    else if(Selector == '4'){
        WDT_PERIOD_64CLK_gc = (0x04<<0);  /* 64 cycles (64ms) */
    }
    else if(Selector == '5'){
        WDT_PERIOD_128CLK_gc = (0x05<<0);  /* 128 cycles (0.128s) */
    }
    else if(Selector == '6'){
        WDT_PERIOD_256CLK_gc = (0x06<<0);  /* 256 cycles (0.256s) */
    }
    else if(Selector == '7'){
        WDT_PERIOD_512CLK_gc = (0x07<<0);  /* 512 cycles (0.512s) */
    }
    else if(Selector == '8'){
        WDT_PERIOD_1KCLK_gc = (0x08<<0);  /* 1K cycles (1.0s) */
    }
    else if(Selector == '9'){
        WDT_PERIOD_2KCLK_gc = (0x09<<0);  /* 2K cycles (2.0s) */
    }
    else if(Selector == '10'){
        WDT_PERIOD_4KCLK_gc = (0x0A<<0);  /* 4K cycles (4.1s) */
    }
    else if(Selector == '11'){
        WDT_PERIOD_8KCLK_gc = (0x0B<<0);  /* 8K cycles (8.2s) */
    }
}
 

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


already defined.

You don't have to bother to create it, just copy it.

------------------------------------------------------------------

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

Yeah, found out about that.

 

thanks for the help.