Menu

Watchdog Timer in avr's

Help
EA7-JFV
2017-10-01
2017-10-01
  • EA7-JFV

    EA7-JFV - 2017-10-01

    Hi, I'm trying to implement a reset routine in an AtMega328p,

    The goal of routine is from the serial port, could command an reset of the atmega when I want to load a new software, through a bootloader

    For that, I've think in willfully overflows the WDT, but I don't find any help in gcb about how to use wdt, only the #config WDT = ON, and in the posts of this forum, i found clrwdt (the compiler says error, and I substitute it by "asm wdr", but nothing more, and don't runs

    Somebody has a example of use WDT in an Atmega?

    Thanks to everybody

     
  • Theo

    Theo - 2017-10-04

    Manuel,

    try the following code as basis for your program.
    The code is based on the info in the ATmega328/P datasheet.
    It's mandatory to keep the right sequence of code.

    To check the code you are able to switch on/off the led by sending
    led on or led off via the serial connection
    By sending reset via the serial connection the microcontroller will reset.

    Success, Theo.

    '' Watchdog timer in reset mode
    
      #Chip mega328p,16
    
      #option explicit
    
     ' Setup Hardware Serial Port
      #Define USART_BAUD_RATE 9600
      Dir PORTD.0 In
      Dir PORTD.1 Out
      #Define USART_TX_BLOCKING
    
      ' This is mandatory to prevent unintentional time-out!
      ' Turn off global interrupt
      CLI
      ' Reset Watchdog Timer
      WDR
      ' Clear WDRF in MCUSR
      MCUSR = MCUSR AND WDRF
      ' Write logical one to WDCE and WDE
      ' Keep old prescaler setting
      WDTCSR = WDTCSR OR 0b00011000 ' WDCE = 1 , WDE = 1
      ' Turn off WDT
      WDTCSR = 0
      ' Turn on global interrupt
      SEI
    
      Dim MyString as String
    
      ; Setup Alive Led
      #Define LED PORTB.5
      Dir LED OUT
    
      repeat 3 ' Flash LED 3 times; meaning first part OK
        pulseout LED, 100 ms
        wait 100 ms
      end repeat
    
      Hserprint "Program started":HSerPrintCRLF
    
      Do Forever
            HSerGetString MyString  ' recieve string
            HSerPrint MyString  'echo received string
            HSerSend(13)
             If MyString = "led on" Then
                 Set Led On
             End If
             If MyString = "led off" Then
                 Set Led Off
             End If
             If MyString = "reset" Then
             ForceReset
             End If
        Loop
    
    
      Sub ForceReset
        CLI ' disable interrupts
        WDR ' reset watchdog timer
        WDTCSR = WDTCSR OR 0b00011000 ' WDCE=1, WDE=1
        WDTCSR = WDTCSR OR 0b01001000 ' WDIE=1, WDE=1, WDP3=0, WDP2=0, WDP1=0, WDP0=0
        SEI ' Enable global interrupts
      End Sub
    
     

    Last edit: Theo 2017-10-04
  • EA7-JFV

    EA7-JFV - 2017-10-05

    Theo, I have seen your answer, I can weigh it in a few days because unfortunately these days I am very busy. In a few days, I answer you about the code you have kindly shared

    Thank you very much, Theo!

     
  • EA7-JFV

    EA7-JFV - 2017-10-10

    Theo,
    I just tried your code with a minimal adaptation to my app, and it runs great, fine!

    Thank you very very much!!

     
  • Theo

    Theo - 2017-10-11

    Cheers, Manuel.
    Enjoy Great Cow Basic!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.