You could very well write a perfect application except for the watchdog framework and your project may reset a lot, or all of your code might be bad and just continually pet the watchdog, causing it to never reset. This can be done with static variables in the functions or by using function pointers rather than functions and change the actual function that is executed to reflect the current state of that subsystem.
Each subsystem becomes a state machine. Another way to go about working around long intentional waits with a quick biting watchdog is to break up the long running function into shorter pieces. Rather than:. Even still it can get more and more complicated.
Often you end up having to create a watchdog delegate which just checks for conditions to be met and does or does not pet the watchdog based on these conditions. This begins to get very complicated. The health methods could be very complex and could even change as the state of that subsystem changes, though it should be as simple as possible so that it is as easy as possible to verify that the code is correct, and also because changes to how the subsystem works will require changes to how you measure health.
If you can ensure that some code runs at regular intervals then you could just have an integer for each subsystem that acts as the subsystem's local watchdog. Some code maybe in a timer interrupt handler, but not necessarily will decrement and test each subsystem's variable.
If it reaches 0 for any subsystem then the watchdog is not tickled. You should also notice that this is really just a software watchdog, even though it may make use of the hardware watchdog to do the actual reset. You should also note that the more complicated the watchdog framework the more oppurtunity there is for errors in it as well as oppurtunity for errors in other code to cause it to work improperly.
For instance a pointer error such as:. Typically the WD timer needs to be service during idle time idle loop or idle task and in the lowest level drivers e. If this is not sufficient, your firmware may also be doing nothing but burning up CPU cycles in a delay routine. Its fine to add a WD timer service here but you may have to adjust your delay timer to account for the WD service time. If your application simply has some long, CPU intensive tasks that take more time to execute that the WD timer period allows, you might consider making the WD timer interval a bit longer.
This may not always be possible but I like to keep WD timer references out of the upper layers of the firmware to keep the application layer as portable as possible.
WD timers are typically hardware dependent, therefore any WD timer references in your code are rarely portable. The low-level drivers are rarely portable anyway so this typically is a better place to service a WD timer. How are we doing?
Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Collectives on Stack Overflow. Learn more. Programming in presence of watchdog timer Ask Question. Asked 11 years, 6 months ago. Active 11 years, 6 months ago. Viewed 10k times. I wouldn't like to call watchdog timer from the initialization function, I don't find this good.
Improve this question. Bogi Bogi 1, 3 3 gold badges 20 20 silver badges 32 32 bronze badges. You need to feed that dog on a regular interval. Like this watchdog timer works. We have a main part in program which runs over and over loop.
We are enabling watchdog timer is loaded with an initial value greater than the total delay in main program. Each time main program reset this timer. If any case the main program does not get back to reset the timer before it counts down to zero, an interrupt is generated to reset the processor.
Likewise watchdog timer protect micro-controller from hang case. Watchdog timer library. This function is used for resetting the watchdog timer. Reset function uses inside loop.
If your program uses a larger delay which greater than threshold delay of watchdog timer, add reset function before that delay too. Else, It will reset Micro-controller before completing that task.
Please log in or sign up to comment. A one button interface for a "Hour" countdown timer that uses a 3 digit binary number to select times other than an hour. Project tutorial by Ian Cumming. Below is a table showing the bit assignments with the division rates and the time for the WDT to time out:.
Think of these times as real time, rather than clock times. The nearest we have is mS, or 0. Simple, really. Now, there is a catch. By default the prescaler is assigned to the other internal timer. This means that we have to change the prescaler over to the WDT.
First, we have to reset the other counter to 0 first. We then have to change to Bank 1 to assign the prescaler to the WDT and to set up the time, and then come back to Bank 0.
The code is below, where xx is the prescaler time:. So now we come to the point where we need to work out how long our code takes in real time.
The principle is very simple, but could cause you to pull your hair out! As you are probably already aware, the PIC takes the external clock timing and divides it by 4. This internal time is called an instruction cycle. Now, some instructions take just one instruction cycle to complete, i. The data sheet tells us how many cycles each instruction takes. The easiest way to remember this is quite simple. Assume ALL instructions take 1 cycle.
But, if an instruction causes the program to go somewhere else, then it will take 2 cycles. Let me give you a couple of examples. The movwf command takes only one cycle, because it is only moving data from one place to another.
The goto command takes 2 cycles, because it is causing the Program Counter PC to go elsewhere in the program. We think you can see the pattern here. However, there are four commands which can take 1 or 2 cycles.
These commands have one thing in common. They will skip the next instruction is a certain condition is met.
If that condition is not met, then the next instruction will be carried out. If the result is not 0, then the next instruction will be executed. This instruction therefore takes 1 cycle.
If the result is 0, then the next instruction will be skipped, and the one following that will be executed. In this instance the instruction takes 2 cycles. The reason is that the instruction alters the value of the PC.
0コメント