{"id":1800,"date":"2024-06-28T05:12:31","date_gmt":"2024-06-28T05:12:31","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1800"},"modified":"2024-08-11T17:09:20","modified_gmt":"2024-08-11T17:09:20","slug":"watchdog-timer","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1800","title":{"rendered":"Watchdog Timer"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-1 wp-block-paragraph\" style=\"color:#5c5c5c\">Atmel family has another type of Timer to watch the program flow. We can enable the timer and if error occurs it will restart the CPU. So much we are excited, for it is hard to succeed developing absolutely perfect and error free program. The watchdog cannot help us to better programs however it can ensure that our program, if it said good-by and the \u00b5C start again. Let\u2019s write a program to set PORTB of ATmega32 from bottom to top. Let\u2019s write the program-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n  #include&lt;avr\/io.h&gt;\n  #include&lt;util\/delay.h&gt;\nint main(void)\n{\n   DDRB=0xff;\n   uint8_t x=7;\n   while(1)\n   {\n    while(x&gt;=0)\n      {\n        PORTB=(1&lt;&lt;x); _delay_ms(80);\n        x--;\n      }\n   x=7;\n  }\nreturn 0;\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-2 wp-block-paragraph\" style=\"color:#5c5c5c\">It seems like in a continuous loop i.e. while(1) PORTB is set from bottom to top and again from bottom to top and the process continue. But if you run the program and simulate in proteus or in real life it is seen that PORTB will set from PINB7 to PINB0 and the loop terminate there. No repeat, so what happen? Completely simply, because as unsigned defined variable can never become smaller than zero. But the compiler does not give any warning or error? The program would hang itself up in here and would turn eternally in the loop. And here the watchdog comes exactly to the course. The watchdog cannot correct the program but start from beginning just like RESET. It simply gives some time to execute, if the execution hanged itself than it RESET the microcontroller and start from begin. We will discuss this in this article however the correction in the above program is replace the <strong>uint8_t x=7<\/strong> with <strong>int8_t x=7<\/strong>.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3 wp-block-paragraph\" style=\"color:#5c5c5c\">The Watchdog contains a separate timer\/Counter, which with an internally produced clock by 1MHz with 5V one clocks, so no need for external timer. It is already calculated for every microcontroller without the effect of internal or external oscillator. Let\u2019s first see the control register and there uses<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"658\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/watchdog-timer-1024x658.jpg\" alt=\"\" class=\"wp-image-1804\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/watchdog-timer-1024x658.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/watchdog-timer-300x193.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/watchdog-timer-768x494.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/watchdog-timer.jpg 1196w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4 wp-block-paragraph\" style=\"color:#5c5c5c\">You can use register from the table or the function available in the <strong>#include&lt;avr\/wdt.h&gt;<\/strong> library. Let use both. To enable the Watchdog \u2013<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5 wp-block-paragraph\" style=\"color:#252525\">       WDTCR|=(1&lt;&lt;WDE); or wdt_enable(WDTO_xS);<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6 wp-block-paragraph\" style=\"color:#5c5c5c\">Enable the watchdog timer, configuring it for expiry after <code>timeout<\/code> (which is a combination of the <code>WDP0<\/code> through <code>WDP2<\/code> bits to write into the <code>WDTCR<\/code> register; for those devices that have a <code>WDTCSR<\/code> register, it uses the combination of the <code>WDP0<\/code> through <code>WDP3<\/code> bits). Some Timeout values and pre-defined as constants and some are listed according to the bit setting.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"386\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/watchdog-time-out-1024x386.jpg\" alt=\"\" class=\"wp-image-1809\" style=\"width:767px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/watchdog-time-out-1024x386.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/watchdog-time-out-300x113.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/watchdog-time-out-768x289.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/watchdog-time-out.jpg 1174w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-7 wp-block-paragraph\" style=\"color:#5c5c5c\">For use the time out condition see the bits available as watchdog timer prescaler.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8 wp-block-paragraph\" style=\"color:#252525\">To disable watchdog<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-9 wp-block-paragraph\" style=\"color:#5c5c5c\">The bet can be only deleted as long as the bit WDTOE stands to 1. That means after you enable Watchdog timer the thing you need is <\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-10 wp-block-paragraph\" style=\"color:#252525\">WDTCR |= (1&lt;&lt;WDTOE) | (1&lt;&lt;WDE); or wdt_disable(); [use the #include&lt;avr\/wdt.h&gt;]<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-11 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s change the program which is discuss at the beginning by replacing the uint8_t with int8_t and set the watchdog timer to check our logic. If the logic is correct then the watchdog disable otherwise it reset the microcontroller. Let the process be seen by our self. In that case we set a PINB0 of ATmega8 and it is clear if the loop success. The full code as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n  #include&lt;avr\/io.h&gt;\n  #include&lt;util\/delay.h&gt;\n  #include&lt;avr\/wdt.h&gt;\nint main(void)\n{\n  DDRD=0xff;\n  DDRB|=(1&lt;&lt;DDB0);\n  PORTB|=(1&lt;&lt;PINB0);\n  _delay_ms(800);\n  \/************ Turn on WTD  ****************\/\n  wdt_enable(WDTO_1S);\n  \/\/WDTCR|=(1&lt;&lt;WDE);\n  \/\/WDTCR |=(1&lt;&lt;WDP2)|(1&lt;&lt;WDP1);\/\/time out condition 1s\n  \/******************************************\/\n  uint8_t x=7;\n  while(1)\n  {\n     while(x&gt;=0)\n\t    {\n\t\t  PORTD=(1&lt;&lt;x); _delay_ms(50);\n\t\t  x--;\n\t    }\n\t x=7;\n  \/************ Turn off WDT *****************\/\n  \/\/wdt_disable(); \n  WDTCR |= (1&lt;&lt;WDCE) | (1&lt;&lt;WDE);\n  WDTCR = 0x00;\n  \/*******************************************\/\n  PORTB&amp;=~(1&lt;&lt;PINB0);\n  }\n return 0;\n}\n\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-12 wp-block-paragraph\" style=\"color:#5c5c5c\">Here int8_t can have both positive and negative value. So the watchdog timer disable before 2s timeout. Now your task is to check the program by replacing int8_t with uint8_t and see what happen?<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Error-Checking.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-13 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Error-Checking.rar\">Check ececution.rar<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-14 wp-block-paragraph\" style=\"color:#252525\"><strong>This type of Timer is very useful in sensor application and time-out condition. Be careful in using this Timer. Let\u2019s check the sensor condition DHT11 temperature sensor.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"477\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sensor-checking-1024x477.jpg\" alt=\"\" class=\"wp-image-1825\" style=\"width:708px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sensor-checking-1024x477.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sensor-checking-300x140.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sensor-checking-768x358.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sensor-checking.jpg 1236w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-15 wp-block-paragraph\" style=\"color:#5c5c5c\"><strong>Connect PC0 with DHT11 sensor. If the sensor pin is disconnect then it will show sensor error and it will restart again and again unless sensor is properly connected. Download the hex file and try it.<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Sensor-Checking.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-16 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Sensor-Checking.rar\">Sensor Checking.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Atmel family has another type of Timer to watch the program flow. We can enable the timer and if error occurs it will restart the CPU. So much we are excited, for it is hard to succeed developing absolutely perfect and error free program. The watchdog cannot help us to better programs however it can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1800","post","type-post","status-publish","format-standard","hentry","category-watchdog-timer"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1800"}],"version-history":[{"count":15,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1800\/revisions"}],"predecessor-version":[{"id":2619,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1800\/revisions\/2619"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}