{"id":1556,"date":"2024-06-25T16:52:24","date_gmt":"2024-06-25T16:52:24","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1556"},"modified":"2024-08-11T16:04:45","modified_gmt":"2024-08-11T16:04:45","slug":"timer-counter-overflow","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1556","title":{"rendered":"Timer\/Counter &#8211; Overflow"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-c975a6851727d70dee525965d1978546 wp-block-paragraph\" style=\"color:#5c5c5c\">Previously we learn about timer CTC. Now what happen if we do not compare the value? In that case the timer starts from BOTTOM to the MAXimum value i.e. 0xFF (for Timer0\/2) or 0xFFFF (Timer1) and that is timer overflow. In this case when the timer overflows an interrupt available i.e. Timer Overflow Interrupt vector.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-369bdfb9d2db4d1673ed559b22b4c484 wp-block-paragraph\" style=\"color:#5c5c5c\">Lets make 1s delay using Timer1, with F_CPU=8MHz and Prescaler=8<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-f79b32d1051b0e03da8768bd91400ca9 wp-block-paragraph\" style=\"color:#222222\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Maximum count Time=(Prescaler\/F_CPU)*(2^n) &nbsp; =(8\/8MHz)*(2^16) =65.536ms<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-d6a6b51f48ab0cb4408dea3753080307 wp-block-paragraph\" style=\"color:#5c5c5c\">So we need to overflow the Timer(1000\/65.536)=15.2587\u224815 times. The main program is<\/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;avr\/interrupt.h&gt;\n  uint8_t sample=0;\nint main(void)\n  {\n   DDRD=0xFF;\n   TCCR1A=0;\t\t\t\t\t\t\t\/\/since no source require\n   TCCR1B=(1&lt;&lt;CS11);\t\t\t\t\t\/\/normal operation with 8 presaling\n   TCNT1=0;\t\t\t\t\t\t\t    \/\/initialize timer1\n   sei();\t\t\t\t\t\t\t\t\/\/set global interrupt\n   TIMSK=(1&lt;&lt;TOIE1);\t\t\t\t\t\/\/set overflow interrupt\n  while(1);\nreturn 0;\n}\nISR(TIMER1_OVF_vect)\n{\n sample++;\n if(sample&gt;=15){\n\t\tPORTD^=0x0F;\n\t\tsample=0;\n\t}\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/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\/Timer1-Overflow.rar\" style=\"border-radius:10px;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-3e941b1b6a17a56de9926bdfbed508bd wp-block-paragraph\" style=\"color:#393939\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Timer1-Overflow.rar\">Timer1 Overflow with ISR.rar<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-c276bf575b65a5d62bd74cee742e8543 wp-block-paragraph\" style=\"color:#5c5c5c\">The flag register of individual interrupt is active when an interrupt active that TCNTx is over than 0xFF\/0xFFFF. Let\u2019s modify the above program without interrupt handling. Since the flag is set when TCNTx value over than 8bit or 16bit value, so in if() statement we check the overflow flag bit and clear the flag by writing 1 to it i.e. TIFR|=(1&lt;&lt;TOV1);<\/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  uint8_t sample=0;\nint main(void)\n  {\n   DDRD=0xFF;\n   TCCR1A=0;\t\t\t\t\t\t\/\/since no source require\n   TCCR1B=(1&lt;&lt;CS11);\t\t\t\t  \/\/normal operation with 8 presaling\n   TCNT1=0;\t\t\t\t\t\t\t\/\/initialize timer1\n  while(1)\n    {\n     if(TIFR&amp;(1&lt;&lt;TOV1)){\n        sample++;\n        if(sample&gt;=15){\n\t\t  PORTD^=0x0F;\n\t\t  sample=0;\n\t     }\n    TIFR|=(1&lt;&lt;TOV1);\n     }\n    }\n return 0;\n }\n<\/pre><\/div>\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\/Timer1-Overflow-without-Interrupt.rar\" style=\"border-radius:10px;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-84ba0c26b08df803c8d92b10e2df2a50 wp-block-paragraph\" style=\"color:#333333\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Timer1-Overflow-without-Interrupt.rar\">Timer1 Overflow without ISR.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Previously we learn about timer CTC. Now what happen if we do not compare the value? In that case the timer starts from BOTTOM to the MAXimum value i.e. 0xFF (for Timer0\/2) or 0xFFFF (Timer1) and that is timer overflow. In this case when the timer overflows an interrupt available i.e. Timer Overflow Interrupt vector. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-1556","post","type-post","status-publish","format-standard","hentry","category-timer-counter"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1556","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=1556"}],"version-history":[{"count":11,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1556\/revisions"}],"predecessor-version":[{"id":2595,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1556\/revisions\/2595"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}