Download ST7 USER GUIDE
Transcript
5 - Peripherals This bit is set whenever the last reset was triggered by the Watchdog. This allows the program to check whether the current start is a fresh one, or results from the recovering from an error condition. 5.4.3 Using the Watchdog to protect an application The right value for the watchdog time-out is always difficult to find, except when the rewinding is performed in the service routine of a timer interrupt, that occurs at a fixed frequency. This is not the best way to use the watchdog, considering what has been said above, since it is likely that a timer interrupt will keep on being serviced even when the main program has crashed. If the rewinding is done in the main program, it is often difficult to estimate the greatest possible interval between rewindings, since this time may vary depending on the various events that may occur. Here are two pieces of advice for anyone wanting use the watchdog timer: ■ Do not activate the watchdog timer while debugging the program. Otherwise you may get some unexpected resets that may fool the in-circuit emulator. ■ When the program is fully debugged, try several values by dichotomy. First set the value to half the maximum. If the reset occurs (which is detected by putting a breakpoint at the entry point of the main program), double this value and try again. If no reset occurs, take the value at midway and try again. Reduce the value this way as much as possible, each time using the program with all its features if possible. When you think you have found the smallest value that never produces a reset, multiply this value by a safety factor and keep it. The safety factor depends on how much opportunity you had to actually test the program through all its paths, nooks and crannies. If this were actually possible, a factor of 1.5 may be sufficient. Otherwise, a factor of 2 or more is advisable. The terms of the trade-off are, on one hand, getting unwanted resets when nothing goes wrong, and in the other hand, reducing the efficiency of the safety device. On models that have a WDOGF bit in the watchdog status register, the program may be written so that it behaves differently if the restart has been caused by a previous malfunction. 5.5 16-BIT TIMER This peripheral is a powerful piece of hardware that illustrates the ideas put forth in the introduction of this chapter. Its purpose is to handle time-related events, such as pulse counting, frequency measurement, interval measurement, and pulse generation, either single or periodic. Good quality processing of such events usually implies a time accuracy in the microsecond range. This is out of reach for a low-end, 8-bit microcontroller. This is where the timer comes in to play: it does the time-related processing, and frees the core from stringent timing constraints. 103/315