Hot!watchdog timer coneptual questions

Page: 12 > Showing page 1 of 2 - Powered by APG vNext Trial
Author
serge313
Junior Member
  • Total Posts : 91
  • Reward points : 0
  • Joined: 2017/09/04 21:22:10
  • Location: 0
  • Status: offline
2018/04/14 23:16:03 (permalink)
0

watchdog timer coneptual questions

Hello everyone, I have some questions regarding the watchdog timer. I know that the watchdog timer is used to reset the microcontroller when the timer value rolls over, but I have a couple questions regarding the watchdog timer for PIC16F690 as I'm reading about it and looking at the datasheet. 

1. What is the resolution of the watchdog timer? 8 bit or 16 bit.

2. What is the register name of the watchdog timer? Like for TMR0 I can say TMR0 and set it to a specific value like TMR0 = 85, but can I do the same for watchdog timer?

3. The watchdog timer uses a clock oscillator speed of 31 kHz. How do I know what each WDTPS value map to for getting a reset? I'm looking through sources like http://microchipdeveloper.com/8bit:wdt , and it says that bit values "00110" (1:2048) would mean that the watchdog would reset after 64 ms if the timer is not cleared. How is this determined knowing the oscillator speed of 31 kHz.
Note: I know that the tutorial has a larger pre-scalar range so "00110" wouldn't be supported on PIC16F690 though "0110" should.

4. Why is the default reset time for a watchdog 18 ms and how is it derived?

5. How do we clear the watchdog timer? In assembly, it is the instruction clrwdt, but what if I'm writing my code in C, how to do clear the timer? The tutorial from question 3 specifies disabling the WDT which means WDTCON.SWDTEN = 0, so would my function look like this to clear the timer?

void clearWDT(void)
{
WDTCON.SWDTEN = 0;
WDTCON.SWDTEN = 1;
}
Moreover, is this the conventional way to do it (if it even is correct). If not, what's the conventional way to do it.

6. What is a good watchdog timer reset value in real world embedded systems? I feel like something as small as 64 ms is so small that I'd be having clearWDT a lot in my code and I'd run the risk of having a loop that runs longer than 64 ms so I'd have a resetting bug which will be difficult to find. What's a good value such that I don't include too many clearWDT function calls but also don't take too long to reset?

7. Is it ever applicable to have a setWDT(int val) function? Having a clear makes sense, because it stops the software from resetting meaning the code is functionally working, but is setting the watchdog ever necessary and if so, what value should it be set to for making something useful?
post edited by serge313 - 2018/04/14 23:37:04
#1

38 Replies Related Threads

    serge313
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2017/09/04 21:22:10
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/14 23:20:53 (permalink)
    0
    Thank you all for the help in advanced (:
    post edited by serge313 - 2018/04/14 23:32:42
    #2
    dan1138
    Super Member
    • Total Posts : 2774
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/14 23:49:28 (permalink)
    +3 (3)
    The answers to your questions come from real world experience. While it is possible that you will get replies that give you specific answers the only ones you are likely to benefit from are those you find when looking for solutions to problems you have in a real project.
     
    The reasons you need a watchdog timer vary. When and how to implement one tend to be very project specific. So general suggestions regarding WDTs are for the most part useless.
     
    It is enough to say that you will realize that you need a WDT for a project and to turn the feature off until you do.
     
    For my own curiosity why do you want to use the PIC16F690?
    #3
    Mysil
    Super Member
    • Total Posts : 2790
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/15 03:50:01 (permalink)
    +3 (3)
    Hi,
    That was a lot of questions, most of those detail questions are explained in datasheet for the device you are using.
     
    The background reason for using a watchdog timer,
    is the sad fact that it is wery difficult to write programs that do not have bugs,
    and impossible to prove that a complicated program do not have any bugs at all.
    If it is accepted that a program running in a microcontroller, might have a bug that could cause the program to hang, then a watchdog timer make more sense.
    The concept is that the program should confirm that: "Yes, I am still alive." at regular intervals. If that does not happen, the hardware watchdog timer will force the microcontroller to restart.
    It is not elegant, but it is better than a microcontroller program that hang forever.
     
    The resolution of the watchdog timer, is the period time of the oscillator driving it. For 31 kHz frequency, that is about 32 microseconds.
    The Range of the watchdog counter is 16 bits, that makes for timeout intervals between
    32 microseconds * 32 = 1024 microsecond, is about 1 millisecond,
    and: 32 microseconds * 65536 = 2 097 152 microsecond, that is about 2 seconds.
    Not every value may be selected, you may select one of the available taps with #pragma config  ... settings.
    Then the prescaler that is shared with Timer 0 may be used for watchdog timer, making for a maximum timeout of about 268 seconds, it is actually selected by reset default.
     
    5. No, not by disabling and enabling  the watchdog timer.
        In XC8 C Compiler Users Guide, page 367, there is this macro function: 
        CLRWDT();   /* Feed the dog, so it do not become hungry. */ 

        It will make the compiler insert a clrwdt instruction.
     
    7,  It may make sense to force the watchdog timer to take control, but not by setting any particular value.
         If a test in your code determine that some impossible situation have occurred, that cannot be resolved,
         then if you make a infinite loop deliberately, then if the watchdog timer is working, it will sooner or later take action.  A way to do this is:
         while (1);   /* Something wrong, hang here until watchdog timer reset the microcontroller.*/ 

        If you can run the program with debugger, or in simulator, watchdog timer cannot be used. In this case, when you stop the program in debugger, you will know what have happened.
     
       Mysil
    #4
    serge313
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2017/09/04 21:22:10
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/15 11:07:08 (permalink)
    0
    I thought the reason for needing a watchdog timer is pretty consistent across all projects for professional applications. Why else would you need a watchdog timer other than resetting the microcontroller if the software gets hung up?
     
    I'm using the PIC16F690 because I have one and I just want to understand the concepts concerning WDT.
    #5
    serge313
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2017/09/04 21:22:10
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/15 11:19:24 (permalink)
    0
    1. How do you know the WDT register is 16 bits? I couldn't find that in the datasheet.

    2. So you're saying that for a pre-scalar of 1:2048, the period of the WDT is 1/31,000 * 2048 = ~66 ms, so that is the period of 1 clock pulse. But, since the register is 16 bits, the max value before the micro resets will be 66 ms * 65536 = 4.329 seconds. Since you calculated it without a pre-scalar, it's just 32 us * 65536 = ~2 seconds. Therefore, the micro will reset after 2.1 seconds. If the prescalar was 1:65536, then the calculation for the reset time will be 32 us * 65536 * 65536 Is all this correct?

    3. Is there a way to say something like:asm("CLRWDT");because what if I'm not using that compiler? I would want something that works universally.
    post edited by serge313 - 2018/04/15 11:50:33
    #6
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2645
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/15 12:05:58 (permalink)
    +1 (1)
    Watchdog timer has other uses besides just resetting the mpu.
     
    Have you tried using with Sleep to wake the mpu up?

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #7
    serge313
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2017/09/04 21:22:10
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/15 12:42:09 (permalink)
    0
    I haven’t used the watchdog timer to wake up from sleep. When does the micro go to sleep if it’s executing code in an infinite loop?
    #8
    qɥb
    Monolothic Member
    • Total Posts : 2983
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: online
    Re: watchdog timer coneptual questions 2018/04/15 13:23:06 (permalink)
    +1 (1)
    serge313
    3. Is there a way to say something like:asm("CLRWDT");because what if I'm not using that compiler? I would want something that works universally.

    Silly question. There is no such thing as "Device specific instructions" that "work universally".
     
    serge313
    I haven’t used the watchdog timer to wake up from sleep. When does the micro go to sleep if it’s executing code in an infinite loop?

    Who said it was executing an infinite loop? That's just a bad assumption.
    Gort said it "has other uses". In that scenario, it would go to sleep after executing a sleep instruction.
    If setup correctly, the WDT would wake it back up from sleep when the watchdog timeout expired.
     
    post edited by qɥb - 2018/04/15 13:24:09

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #9
    Mysil
    Super Member
    • Total Posts : 2790
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/15 14:32:35 (permalink)
    +2 (2)
    Hi,
    In Datasheet for PIC16F690 and family,   DS41262E-page 81 
    Schematic diagram of Watchdog is hidden in plain sight, together with Timer 1 and the shared Prescaler in figure 5-1.    and FIGURE 14-9:  page 212
    There is no register accessible to Read or Write the watchdog counter value,
    only a selector that is used to pick a single bit from the counter chain,  WDTCON register set the tap to use.
    Same with the  prescaler shared with Timer 0, that one is 7 bits long when used together with WDT.
     
    2. There may be some misunderstanding: There is no: 32 us * 65536 * 65536
    WDT is nothing but the 16-bit prescaler that is controlled by WDTCON,
    and the prescaler shared with Timer 0 and controlled by OPTION_REG,
    so maximum is 32 us * 65536 * 128.
     
    3. Putting assembly instructions into a C language program is an extension anyway.
    Gcc have this description: https://gcc.gnu.org/onlin..s/gcc/Extended-Asm.html
    You may compare that with XC8 C Compiler User's Guide section 5.12.2  DS50002053H-page 222
    This document is installed in a /docs/ directory together with other compiler files.
     
       Mysil
    post edited by Mysil - 2018/04/15 14:38:08
    #10
    serge313
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2017/09/04 21:22:10
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/15 22:28:00 (permalink)
    0
    1. So if I wanted to clear the watchdog with a different compiler in C for the same microcontroller, I would have to look at their manual to see how they support clearing the watchdog and call the function accordingly?
     
    2. Yes, bad assumption for thinking all embedded projects should run in an infinite loop. Why would you put the micro to sleep, save power?
    post edited by serge313 - 2018/04/15 22:29:54
    #11
    qɥb
    Monolothic Member
    • Total Posts : 2983
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: online
    Re: watchdog timer coneptual questions 2018/04/15 22:37:37 (permalink)
    +2 (2)
    serge313
    1. So if I wanted to clear the watchdog with a different compiler in C for the same microcontroller, I would have to look at their manual to see how they support clearing the watchdog and call the function accordingly?

    Yes, although it will almost certainly be a macro, not a function, as it is a single assembly instruction to clear the watchdog.
     

    2. Yes, bad assumption for thinking all embedded projects should run in an infinite loop. Why would you put the micro to sleep, save power?

    Yes, to save power. Have you actually looked in the datasheet to see what the SLEEP instruction does?

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #12
    serge313
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2017/09/04 21:22:10
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/15 22:59:05 (permalink)
    0
    1. I looked at those diagrams and it didn't show how the watchdog timer register was a 16 bit register, but the pre-scalar register was 16-bits. My concern is how much does the timer need to count until it overflows i.e resets.

    2. Page 209 with the WDTPS can have a pre-scalar of 4 bits with a max of 1:65536 but the option register in page 83 can also be used to set the pre-scalar of the WDT by setting PSA = 0 and PS bits <2:0> and have a pre-scalar of 1:128. How do I select which one to use?

    3. I'm a bit confused. If the watchdog timer register is 16 bits (as per question 1), that means that it takes 65536 counts for it to count before it overflows and once it does overflow, the microcontroller resets. The pre-scalar seems to be different depending on whether I'm using WDTPS from WDTCON register or PS from OPTION_REG register. Assuming I do use WDTPS, then the max pre-scalar value is 65536, so if my period is 32 us, then with the pre-scalar it is 32 * 65536. However, that is the amount of time it takes for 1 tick/count. If the WDT register is 16 bits, then it would take another 65536 counts so 32 us * 65536 * 65536. Is this incorrect and if so where is my mistake?
    #13
    serge313
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2017/09/04 21:22:10
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/15 23:04:07 (permalink)
    0
    1. I understand, thank you.
    2. I actually have not looked much into the sleep mode and its instruction in the datasheet since I have to read up on concept first on the web before I go to the datasheet. I feel like sometimes the datasheet assumes you know what everything means so I try to look at it after I have a clear understanding of what's happening. I still have to read up on sleep mode since when learning WDT I didn't pay too much attention to it and once I do, I'll read the datasheet.
    #14
    qɥb
    Monolothic Member
    • Total Posts : 2983
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: online
    Re: watchdog timer coneptual questions 2018/04/15 23:23:07 (permalink)
    +1 (1)
    serge313
    1. I looked at those diagrams and it didn't show how the watchdog timer register was a 16 bit register, but the pre-scalar register was 16-bits. My concern is how much does the timer need to count until it overflows i.e resets.

    The box labelled "Watchdog Timer" is just a switch, there is no counter in there. It just connects the 31kHz clock to the prescaler. Nothing more.
    This is easier to see in Figure 14-9 on page 207.
     

    2. Page 209 with the WDTPS can have a pre-scalar of 4 bits with a max of 1:65536 but the option register in page 83 can also be used to set the pre-scalar of the WDT by setting PSA = 0 and PS bits <2:0> and have a pre-scalar of 1:128. How do I select which one to use?

    You can use both.
    The WDTCON prescaler is always active.
    The PSA bit in the OPTION_REG controls if the second prescaler is also active.
    You can clearly see this in Figure 14-9.


    3. I'm a bit confused. If the watchdog timer register is 16 bits (as per question 1), that means that it takes 65536 counts for it to count before it overflows and once it does overflow, the microcontroller resets. The pre-scalar seems to be different depending on whether I'm using WDTPS from WDTCON register or PS from OPTION_REG register. Assuming I do use WDTPS, then the max pre-scalar value is 65536, so if my period is 32 us, then with the pre-scalar it is 32 * 65536. However, that is the amount of time it takes for 1 tick/count. If the WDT register is 16 bits, then it would take another 65536 counts so 32 us * 65536 * 65536. Is this incorrect and if so where is my mistake?

    You keep adding in another 16 bit counter that does not exist.
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #15
    serge313
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2017/09/04 21:22:10
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/16 07:38:21 (permalink)
    0
    1. The watchdog timer is not a counter? I thought it worked just like TMR0, TMR1 and TMR2 by having a counter in them counting from 0 to 2^n - 1, where n is register size, that's why they're called timers.
     
    2. Based on that figure, it looks like it's being muxed so only one can be chosen based on PSA.
     
    3. I was under the assumption that the watchdog timer works like a counter like TMR0, TMR1 and TMR2. If I was to calculate how long it takes for TMR1 to get reset, then it'd be 32 us * pre-scalar * 2^16, since it's a 16 bit register. So I applied the same concept here for watchdog timer to calculate how long it takes before it reaches the last count value. If pre-scalar is 1:65536, then 32 us * 65536 * 2^16. But you're saying the watchdog timer is not counting up to 2^16 every clock cycle? Instead, it only has one pulse and once the time exceeds that pulse based on pre-scalar, the micro resets. Is that correct?
    #16
    Mysil
    Super Member
    • Total Posts : 2790
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/16 08:15:23 (permalink)
    +2 (2)
    Hi,
    PIC16F690 is an awkward device to use as a learning platform, as it cannot be debugged in hardware.
    Why don't you make a program as simple as possible:
    Make a LED flashing program with WDT enabled to maximum time.
    Then after 10 flashes, light another LED, and make a infinite loop without clearing the watchdog timer:    while(1);
    Try it out, and see how long it lasts?
     
    When it does not work as expected, show the whole program in this thread.
     
       Mysil
    #17
    qɥb
    Monolothic Member
    • Total Posts : 2983
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: online
    Re: watchdog timer coneptual questions 2018/04/16 13:15:31 (permalink)
    +2 (2)
    serge313
    1. The watchdog timer is not a counter? I thought it worked just like TMR0, TMR1 and TMR2 by having a counter in them counting from 0 to 2^n - 1, where n is register size, that's why they're called timers.

    There is no extra counter.
    Whatever combination of prescalers you select does all the counting required.
     
    [qyote]
    2. Based on that figure, it looks like it's being muxed so only one can be chosen based on PSA.
    The first prescaler is dedicated to the WDT.
    The second one can be used be either TMR0 or the WDT, selected by the PSA bit.
     

    3. I was under the assumption that the watchdog timer works like a counter like TMR0, TMR1 and TMR2. If I was to calculate how long it takes for TMR1 to get reset, then it'd be 32 us * pre-scalar * 2^16, since it's a 16 bit register. So I applied the same concept here for watchdog timer to calculate how long it takes before it reaches the last count value. If pre-scalar is 1:65536, then 32 us * 65536 * 2^16. But you're saying the watchdog timer is not counting up to 2^16 every clock cycle? Instead, it only has one pulse and once the time exceeds that pulse based on pre-scalar, the micro resets. Is that correct?

    Yes.

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #18
    dan1138
    Super Member
    • Total Posts : 2774
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/16 15:02:47 (permalink)
    +1 (1)
    Observation on the WDT implementation of the PIC16F690.

    See TABLE 17-2: OSCILLATOR PARAMETERS

    Parameter: OS09, LFOSC, Internal Uncalibrated LFINTOSC Frequency

    Maximum count of WDT = 8388608 @ 15,000Hz timeout in 559.240 seconds
    Maximum count of WDT = 8388608 @ 31,000Hz timeout in 270.600 seconds
    Maximum count of WDT = 8388608 @ 45,000Hz timeout in 186.413 seconds

    The maximum count for the WDT uses the 16-bit WDT prescaler plus the TIMER0 8-bit prescaler.

    Note that only 7 of the 8-bits of the TIMER0 prescaler are available to the WDT count chain.

    This yields a ripple counter of 23-bit for use as a WDT timeout duration counter.

    No matter how the WDT counter is configured the execution of the CLRWDT, or SLEEP opcode will clear this ripple counter.

    Changes to the configuration of the WDT prescalers while the WDT is enabled can result in an WDT reset.

    You need to take note that the LFOSC frequency varies over a range of ±50% depending on VDD voltage and die temperature.

    This means that the WDT timeout interval is not usually a suitable method to measure long term events related to the way people think of time.

    For example if you need your controller to wake up every 24 hours to do something but you use only the WDT timeout interval the time of day the controller will wake up can vary by as much as 12 hours and may gain or lose up to 12 hours each time.

    In projects where lowest average power usage is essential this kind of temporal drifting can be acceptable.
    post edited by dan1138 - 2018/04/17 17:37:23
    #19
    serge313
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2017/09/04 21:22:10
    • Location: 0
    • Status: offline
    Re: watchdog timer coneptual questions 2018/04/17 06:54:44 (permalink)
    0
    Mysil
    Hi,
    PIC16F690 is an awkward device to use as a learning platform, as it cannot be debugged in hardware.
    Why don't you make a program as simple as possible:
    Make a LED flashing program with WDT enabled to maximum time.
    Then after 10 flashes, light another LED, and make a infinite loop without clearing the watchdog timer:    while(1);
    Try it out, and see how long it lasts?
     
    When it does not work as expected, show the whole program in this thread.
     
       Mysil


    Yes, the PIC16F690 is a sort of outdated device to learn. I actually used PIC16F1618 that does support the debugger from the programmer.
    I will attempt that program next, but it should last about 32 us * 65536 * 128 seconds before the program resets.
    #20
    Page: 12 > Showing page 1 of 2 - Powered by APG vNext Trial
    Jump to:
    © 2018 APG vNext Trial Version 4.5