Showing posts with label Watchdog Library. Show all posts
Showing posts with label Watchdog Library. Show all posts

Wednesday, May 23, 2018

Watchdog library | AVR microcontrollers | ATmega328P

Even if you are a dog or a cat person, you should still be using the watchdog.

For those who don't know, a watchdog is a timer inside the microcontroller generated by an RC oscillator - with 128kHz frequency on an AVR device. When it times out, the microcontroller is reset. To prevent it from resetting the MCU, the watchdog timer must be reset by the code inside the while loop. The idea is that if you have a loop and gets stuck, the watchdog timer will not be reset and so the microcontroller will be reset after the timer reaches the timeout period. Say you read a sensor and in a loop the code waits for the sensor response but the sensor is malfunctioning. If not for the watchdog, the MCU will get stuck and your drone will crash.



Watchdog extension library for AVR microcontrollers

On ATmega328 the available timeouts are 16ms, 32ms, 64ms, 0.125s, 0.25s, 0.5s, 1s, 2s, 4s, 8s. Choosing the right timeout depends on the specific application. The while loop must be able to finish executing the code and reset the watchdog timer before the timer runs out. For critical applications where if the CPU being stuck for more than 1 second is unacceptable, you can choose timeouts of a few milliseconds. Those cases can be a drone where reaction time needs to be fast, or a 3D printer reaches the end and the motor needs to be stopped in time. But in most cases the timeout can even be 8s, like when taking room temperature readings.

Other uses for the Watchdog timer