{"id":1026,"date":"2015-03-31T10:59:56","date_gmt":"2015-03-31T14:59:56","guid":{"rendered":"https:\/\/ve2cuy.wordpress.com\/?page_id=1026"},"modified":"2015-03-31T10:59:56","modified_gmt":"2015-03-31T14:59:56","slug":"watch-the-dog","status":"publish","type":"page","link":"https:\/\/ve2cuy.com\/blog\/index.php\/watch-the-dog\/","title":{"rendered":"Understanding the PIC 16F watchdog"},"content":{"rendered":"<h3>What is a micro-controller watchdog?<\/h3>\n<blockquote><p>\u00ab\u00a0<em>Suppose you have written a program that is continuously running on a PIC.\u00a0Now, you want to make sure that this program is always running, and that no matter what happens it will never stop.\u00a0The first thing you would have, of course, is a loop back at the end of the program that brings us back to the start of the program.\u00a0But consider this case.\u00a0Let us say that the PIC is monitoring an input.\u00a0When this input goes high, it jumps to another part of the program and waits for another pin to go high.\u00a0If the second pin doesn\u2019t go high, the PIC will just sit there and wait.\u00a0It will only exit if the second pin goes high. Let us consider another example.\u00a0Suppose you have written a program. You have compiled it successfully, and you have even simulated it over and over again using a simulator such as MPLAB.\u00a0Everything seems to work fine.\u00a0You program the PIC and place it into a circuit.\u00a0However after a long period of time, the program gets stuck somewhere and the PIC gets caught in a loop.\u00a0What\u2019s needed in both cases is some kind of\u00a0 reset if the program gets stuck. This is the purpose of a watchdog circuit.<\/em>\u00a0\u00bb<\/p>\n<p><strong>Source from<\/strong>\u00a0<a href=\"http:\/\/www.hobbyprojects.com\/pic_tutorials\/tutorial13.html\">hobbyprojects<\/a>.<\/p><\/blockquote>\n<h1>Example of a watchdog in action<\/h1>\n<p><a href=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/watch-dog-02.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1037\" src=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/watch-dog-02.gif\" alt=\"watch-dog-02\" width=\"660\" height=\"577\" \/><\/a><\/p>\n<p>Here, we have a classic example of a PIC being reset by its watchdog .<\/p>\n<p>The PIC used in the simulation &#8211; 16F866 &#8211; has a default watchdog countdown timer\u00a0of about 2.3 seconds.<\/p>\n<p>So, the count from 0 to 9 is interrupted before the end of the sequence and the program is restarted.<\/p>\n<p>Download the Proteus <a href=\"https:\/\/drive.google.com\/uc?export=download&amp;id=0BwVyrqVfxGkReVVJZkxvV2ZLU00\">Watchdog start project<\/a>.<\/p>\n<h1>Program listing<\/h1>\n<p>[sourcecode language=\u00a0\u00bbcpp\u00a0\u00bb]<br \/>\n\/* Main.c<br \/>\n *<br \/>\n * By: Alain Boudreault (VE2CUY)<br \/>\n * Created: mars 29 2015<br \/>\n * Processor: PIC16F886<br \/>\n * Compiler: MPLAB XC8<br \/>\n * &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n * Description: Program will try to display a count from 0 to 9<br \/>\n * but will be interrupted by the PIC watchdog.<br \/>\n *\/<\/p>\n<p>#include &amp;lt;xc.h&amp;gt;<br \/>\n#pragma config FOSC=INTRC_CLKOUT \/\/ http:\/\/gputils.sourceforge.net\/html-help\/PIC16F887-conf.html<br \/>\n\/\/ #pragma config WDTE = OFF \/\/ Watchdog Timer Enable bit (WDT enabled) &#8211; default value is ON<br \/>\n#pragma config LVP = OFF \/\/ Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit at off<\/p>\n<p>#define OUT 0<br \/>\n#define LED_PIN_DIRECTION TRISA0<br \/>\n#define LED_PIN RA0 \/\/ Use to monitor the watchdog<br \/>\n#define END_OF_TIME 1<br \/>\n#define ON 1<br \/>\n#define OFF 0<br \/>\n#define _XTAL_FREQ 1000000 \/\/ needed for __delay_ms()<br \/>\n#define SEVENSEG_PORT PORTC<\/p>\n<p>\/\/ Conversion array from digit to 7 segment LED Display<br \/>\n\/\/ Note: segments bit is inverted because LED display is common anode.<br \/>\nstatic unsigned char digit_to_7segment[] = { ~0B00111111, \/* 0 *\/<br \/>\n ~0B00000110, \/* 1 *\/<br \/>\n ~0B01011011, \/* 2 *\/<br \/>\n ~0B01001111, \/* 3 *\/<br \/>\n ~0B01100110, \/* 4 *\/<br \/>\n ~0B01101101, \/* 5 *\/<br \/>\n ~0B01111101, \/* 6 *\/<br \/>\n ~0B00000111, \/* 7 *\/<br \/>\n ~0B01111111, \/* 8 *\/<br \/>\n ~0B01101111 \/* 9 *\/<br \/>\n }; \/\/ .GFEDCBA<\/p>\n<p>\/\/ Program entry point<br \/>\nvoid main(void)<br \/>\n {<br \/>\n unsigned char number = 0;<br \/>\n TRISC = OUT;<br \/>\n LED_PIN_DIRECTION = OUT; \/\/ Monitor the watchdog<br \/>\n LED_PIN = ON; LED_PIN = OFF; \/\/ Blink when program start<\/p>\n<p> while (END_OF_TIME) {<br \/>\n SEVENSEG_PORT = digit_to_7segment[number];<br \/>\n __delay_ms(500);<br \/>\n if (++number &amp;gt; 9) { \/\/ Back to digit &lsquo;0&rsquo;?<br \/>\n number=0;<br \/>\n \/\/ Blink the dot.<br \/>\n SEVENSEG_PORT = ~0B10000000; __delay_ms(200);<br \/>\n SEVENSEG_PORT = 0xFF; __delay_ms(200);<br \/>\n } \/\/ if nb &amp;gt; 9<br \/>\n \/\/asm(&amp;quot;CLRWDT&amp;quot;); \/\/ Clear the watchdog timer<br \/>\n } \/\/ while END_OF_TIME<br \/>\n } \/\/ main()<\/p>\n<p>[\/sourcecode]<\/p>\n<h1>How to fix the problem<\/h1>\n<ol>\n<li>The watchdog is <span style=\"color:#ff0000;\">not needed<\/span>, disable it.<\/li>\n<li>The watchdog is <span style=\"color:#339966;\">needed<\/span>, configure\u00a0it\u00a0accordingly.<\/li>\n<\/ol>\n<h1><\/h1>\n<h1>1 &#8211; Disable the watchdog<\/h1>\n<p>Uncomment line 14 of the program and run the program again<\/p>\n<pre>#pragma config WDTE = OFF \/\/ set Watchdog Timer Enable bit OFF - default value is ON<\/pre>\n<p><strong>Result<\/strong><\/p>\n<p><a href=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/watchdog-03.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1048\" src=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/watchdog-03.gif\" alt=\"watchdog-03\" width=\"660\" height=\"444\" \/><\/a><\/p>\n<p>The program is no longer being reset by the watchdog.<\/p>\n<h1>2 &#8211;\u00a0The Watchdog Timer (WDT)<\/h1>\n<p><em>To be completed&#8230;<\/em><\/p>\n<p>The WDT has the following features:<\/p>\n<p>\u2022 Operates from the LFINTOSC (31 kHz)<br \/>\n\u2022 Contains a 16-bit prescaler<br \/>\n\u2022 Shares an 8-bit prescaler with Timer0 (default value)<br \/>\n\u2022 Time-out period is from <strong>1 ms<\/strong> to<strong> 268 seconds<\/strong><br \/>\n\u2022 Configuration bit and software controlled<\/p>\n<h1>Using the timer0 prescaler<\/h1>\n<p>By default (PSA = 1;\u00a0), the watchdog will be using the Timer0 prescaler.<\/p>\n<p><b class=\"small\">OPTION_REG:\u00a0Bit &lt;2:0&gt; &#8211; PS&lt;2:0&gt;: Timer0 &#8211; Prescaller rate selection <\/b><\/p>\n<table class=\"def_table\">\n<tbody>\n<tr>\n<th><b>PS2<\/b><\/th>\n<th><b>PS1<\/b><\/th>\n<th><b>PS0<\/b><\/th>\n<th><b>TMR0<\/b><\/th>\n<th><b>WDT<\/b><\/th>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>1:2<\/td>\n<td>1:1 &#8211; 18 ms<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>1:4<\/td>\n<td>1:2 &#8211; 36 ms<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>1:8<\/td>\n<td>1:4 &#8211; 72 ms<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>1:16<\/td>\n<td>1:8 &#8211; 144 ms<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>1:32<\/td>\n<td>1:16 &#8211; 288 ms<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>1:64<\/td>\n<td>1:32 &#8211; 576 ms<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>1:128<\/td>\n<td>1:64 &#8211; 1.1 sec<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>1:256<\/td>\n<td>1:128 &#8211; 2.3 sec<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"center\"><b class=\"small\">Table 1 &#8211; Prescaler Rate<\/b><\/p>\n<h1 class=\"center\">Using the watchdog prescaler<\/h1>\n<p class=\"center\">PSA = 0, will enable the internal\u00a0watchdog prescaler wich will give more flexibility than Timer0 prescaler.<\/p>\n<p class=\"center\">The WDT derives its time base from the 31 kHz LFINTOSC<\/p>\n<p class=\"center\"><strong>WDTCON<\/strong> &#8211;\u00a0bit 4-1 <strong>WDTPS&lt;3:0&gt;<\/strong>: Watchdog Timer Period Select bits<\/p>\n<table style=\"height:419px;\" width=\"524\">\n<tbody>\n<tr>\n<th>WDTPS&lt;3:0&gt;<\/th>\n<th>Ratio<\/th>\n<th>Period<\/th>\n<\/tr>\n<tr>\n<td>0000<\/td>\n<td>1:32<\/td>\n<td>1 ms<\/td>\n<\/tr>\n<tr>\n<td>0001<\/td>\n<td>1:64<\/td>\n<td>2 ms<\/td>\n<\/tr>\n<tr>\n<td>0010<\/td>\n<td>1:128<\/td>\n<td>4 ms<\/td>\n<\/tr>\n<tr>\n<td>0011<\/td>\n<td>1:256<\/td>\n<td>8 ms<\/td>\n<\/tr>\n<tr>\n<td>0100<\/td>\n<td>1:512<\/td>\n<td>16 ms<\/td>\n<\/tr>\n<tr>\n<td>0101<\/td>\n<td>1:1024<\/td>\n<td>32 ms<\/td>\n<\/tr>\n<tr>\n<td>0110<\/td>\n<td>1:2048<\/td>\n<td>64 ms<\/td>\n<\/tr>\n<tr>\n<td>0111<\/td>\n<td>1:4096<\/td>\n<td>128 ms<\/td>\n<\/tr>\n<tr>\n<td>1000<\/td>\n<td>1:8192<\/td>\n<td>256 ms<\/td>\n<\/tr>\n<tr>\n<td>1001<\/td>\n<td>1:16384<\/td>\n<td>512 ms<\/td>\n<\/tr>\n<tr>\n<td>1010<\/td>\n<td>1:32868<\/td>\n<td>1 sec (default on reset)<\/td>\n<\/tr>\n<tr>\n<td>1011<\/td>\n<td>1:65536<\/td>\n<td>2 sec<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>PERIOD\u00a0= \u00a01 \/ (LFINTOSC \/ RATIO).<\/p>\n<p>Example,<\/p>\n<p>1 \/ (31kHz\u00a0\/ 8192) = \u00a00,264 sec =<strong> 264 ms<\/strong><\/p>\n<p><span style=\"color:#ff0000;\">Note<\/span>: LFINTOSC as\u00a0+\/-15% accuracy.<\/p>\n<h1>Scope tests<\/h1>\n<p>Here are the results of a few variation of the\u00a0<strong>WDTCON<\/strong> register on a logic probe:<\/p>\n<p>WDTCON = 0b000<strong>0000<\/strong>0 (1ms)<\/p>\n<p><a href=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00000000.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1076\" src=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00000000.png\" alt=\"WDTCON.0b00000000\" width=\"660\" height=\"133\" srcset=\"https:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00000000.png 761w, https:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00000000-300x60.png 300w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/a><\/p>\n<p>WDTCON.0b000<strong>0001<\/strong>0 (2ms)<\/p>\n<p><a href=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00000010.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1077\" src=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00000010.png\" alt=\"WDTCON.0b00000010\" width=\"558\" height=\"130\" srcset=\"https:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00000010.png 558w, https:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00000010-300x70.png 300w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/a><\/p>\n<p>WDTCON.0b000<strong>0101<\/strong>0 (32ms)<\/p>\n<p><a href=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00001010.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1078\" src=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00001010.png\" alt=\"WDTCON.0b00001010\" width=\"555\" height=\"133\" srcset=\"https:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00001010.png 555w, https:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00001010-300x72.png 300w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/a><\/p>\n<p>WDTCON.0b000<strong>1001<\/strong>0 (512ms)<\/p>\n<p><a href=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00010010.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1079\" src=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00010010.png\" alt=\"WDTCON.0b00010010\" width=\"557\" height=\"131\" srcset=\"https:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00010010.png 557w, https:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00010010-300x71.png 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/a><\/p>\n<p>WDTCON.0b000<strong>1011<\/strong>0 (2sec)<\/p>\n<p><a href=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00010110.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1080\" src=\"http:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00010110.png\" alt=\"WDTCON.0b00010110\" width=\"558\" height=\"132\" srcset=\"https:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00010110.png 558w, https:\/\/ve2cuy.com\/blog\/wp-content\/uploads\/2015\/03\/wdtcon-0b00010110-300x71.png 300w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a micro-controller watchdog? \u00ab\u00a0Suppose you have written a program that is continuously running on a PIC.\u00a0Now, you want to make sure that this program is always running, and that no matter what happens it will never stop.\u00a0The first thing you would have, of course, is a loop back at the end of the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-1026","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/ve2cuy.com\/blog\/index.php\/wp-json\/wp\/v2\/pages\/1026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ve2cuy.com\/blog\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ve2cuy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ve2cuy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ve2cuy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1026"}],"version-history":[{"count":0,"href":"https:\/\/ve2cuy.com\/blog\/index.php\/wp-json\/wp\/v2\/pages\/1026\/revisions"}],"wp:attachment":[{"href":"https:\/\/ve2cuy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}