{"id":1520,"date":"2024-06-25T15:38:15","date_gmt":"2024-06-25T15:38:15","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1520"},"modified":"2024-08-11T15:59:37","modified_gmt":"2024-08-11T15:59:37","slug":"timer-counter-clear-timer-on-compare-match-ctc","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1520","title":{"rendered":"Timer\/Counter &#8211; Clear Timer on Compare Match (CTC)"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-09b11d0f72aaad156783b2dce6b19d49 wp-block-paragraph\" style=\"color:#5c5c5c\">This mode is similar to the normal port operation. The difference is that the compare value is store in the OCRx register instead of TCNTx. In order to use the OCRx register, the timer should operate in CTC mode, that<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nTCCR0|=(1&lt;&lt;WGM01);  \/\/CTC mode\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-a72a45f363392e16fbab5a4902836ae9 wp-block-paragraph\" style=\"color:#292929\">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;\nint main(void)\n  {\n    DDRC=0xff;\n    TCCR0|=(1&lt;&lt;WGM01);\t\t\t\t\t  \/\/CTC mode\n    TCCR0|=(1&lt;&lt;CS02)|(1&lt;&lt;CS00);  \t\t\t\/\/F_CPU\/1024 \n    TCNT0=0;\t\t\t\t\t\t\t\t\/\/clear Timer\n    while(1)\n      {\n         OCR0=97;\t\t\t\t\t\t\t\/\/100ms Timer\n         PORTC^=0xff;\n      }\n   return 0;\t\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\/CTC-normal-port-oprartion-1.rar\" style=\"padding-top:var(--wp--preset--spacing--20);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--20);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-b2e1dc82ae0140211a42bec4603dbca4 wp-block-paragraph\" style=\"color:#5c5c5c\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/CTC-normal-port-oprartion-1.rar\">Download the full program with proteus simulation<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-f5c5e5ff02ec0ce55a7e346f373b4302 wp-block-paragraph\" style=\"color:#5c5c5c\">As soon as OCRx register is equal is equal to TCNTx register an interrupt available and we can use the interrupt. In order to use the interrupt we need to set the interrupt enable bit i.e. Timer\/Counter-x, Output Compare Match Interrupt Enable bit. Let enable the Timer\/Counter0 Output Compare Match Interrupt and use it. The corresponding service routine is ISR(TIMER0_COMP_vect). Internal interrupt enable as the same as external interrupt. Three must be set<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"862\" height=\"137\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/basic-text-1.jpg\" alt=\"\" class=\"wp-image-1529\" style=\"width:510px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/basic-text-1.jpg 862w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/basic-text-1-300x48.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/basic-text-1-768x122.jpg 768w\" sizes=\"(max-width: 862px) 100vw, 862px\" \/><\/figure>\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\/interrupt.h&gt;\n  sei();\t\/\/Global Interrupt Enable\n  TIMSK|=(1&lt;&lt;OCIE0); \/\/Timer\/Counter0 Output Compare Match Interrupt Enable\n  ISR(TIMER0_COMP_vect)\n  {\n   \/******** do something at that interrupt *****************\/\n  }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-9f9c832ac8a18d9008ac6916bae096e3 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s make a time clock that display time in LCD display. The parameter is 8MHz external crystal and prescaler=256. Let make a delay of 8ms with the parameter. So the OCR0 value should be<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-fe5ec75cfb6cf40d8c3a7734cf3adecc wp-block-paragraph\" style=\"color:#292929\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OCR0=(8ms)*(8MHz\/256)-1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; =249<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-04661c3a26ec20c7a0e5acca40875d6a wp-block-paragraph\" style=\"color:#5c5c5c\">Since our OCR0 value is 249, so after 8ms an interrupt can available. We use this interrupt for our second, minute and hour calculation. In interrupt the second calculation is 1s=(1000\/8)=125. So we declare four variables millisecond, second, minute and hour. The logic is when millisecond=125 is one second, when 60 second 1 minute and 60 minute 1hour. The logic 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\/***********************************************************************************************\n************************************************************************************************\n*********************************  Digital Clock ************************************************\n************************************************************************************************\n***********************************************************************************************\/\n#include&lt;avr\/io.h&gt;\n#include&lt;stdio.h&gt;\n#include&lt;util\/delay.h&gt;\n#include&lt;avr\/interrupt.h&gt;\n#include&quot;lcd.h&quot;\nuint8_t millisecond=0,second=0,minute=0,hour=0;\nchar str&#x5B;20];\nint main(void)\n{\nLCD_INIT();\nLCD_write_string(1,1,&quot;**Digital Clock*&quot;);\nTCCR2|=(1&lt;&lt;WGM21)|(1&lt;&lt;CS22)|(1&lt;&lt;CS21);   \/\/normal CTC mode with 256 \nTCNT2=0;\t\t                         \/\/clear Timer\nTIMSK|=(1&lt;&lt;OCIE2);\t\t        \/\/Timer\/Counter0 Output Compare Match Interrupt Enable\nsei();\t\t\t\t\t\t\t\t\t \/\/Globel Interrupt Enable\nOCR2=249;\t\t\t\t\t\t\t\t \/\/F_CPU=8MHz,P=256,Delay=8ms\nwhile(1)\n{\n\tif(hour&lt;=12){   \n\t        sprintf(str,&quot;TIME %d:%d :%d am&quot;,hour,minute,second);\n\t\t\tLCD_write_string(1,2,str);\n\t\t\t}\n\telse{\n\t\t\tsprintf(str,&quot;TIME %d:%d :%d pm&quot;,hour,minute,second);\n\t\t\tLCD_write_string(1,2,str);\n\t\t\t}\n}\nreturn 0;\n}\nISR(TIMER2_COMP_vect)\n{\nmillisecond++;\nif(millisecond==125){\n\tsecond++;\n\tmillisecond=0;\n\tif(second==60){\n\t\tminute++;\n\t\tsecond=0;\n\t\t}\n\t\tif(minute==60){\n\t\t\thour++;\n\t\t\tminute=0;\n\t\t\tif(hour==24) hour=0;\n\t\t\t}\n\t}\n\n}\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\/Time.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-41a23cffcc19aca272093618710a4c9e wp-block-paragraph\" style=\"color:#323131\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Time.rar\">Download the full program with proteus simulation<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4ad22f9ec344192f0e85304ef4799c15 wp-block-paragraph\" style=\"color:#5c5c5c\">           In CTC mode we can also use the OC0 pin to generate a square wave. In that case we have to set the OC0 pin to output pin. See the COM01 and COM00 logic operation to make your desire program. The CTC mode of Timer1 and Timer2 is similar to Timer0. Let build a toggle CTC program with Timer1. For register description see the data sheet of ATmega32 Timer1. We use Timer\/Counter1, Output Compare A Match Interrupt. The full program 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;avr\/interrupt.h&gt;\n  uint8_t sample;\nint main(void)\n  {\n    DDRB=0xFF;\n    DDRD|=(1&lt;&lt;DDD5);\t\t\t\t\t\t\t\/\/OC1A output PIN\n    TCCR1B=(1&lt;&lt;WGM12)|(1&lt;&lt;CS12);\t\t\t\t\/\/CTC with prescaler=256\n    TCCR1A=(1&lt;&lt;COM1A0);\t\t\t\t\t\t    \/\/Toggle OC1A\/OC1B on compare match\n    TCNT1=0;\t\t\t\t\t\t\t\t\t\/\/Clear Timer\n    OCR1A=31249;\t\t\t\t\t\t\t\t\/\/F_CPU=8MHz,Prescaler=256,Delay=   1s\n    sei();\t\t\t\t\t\t\t\t\t\t\/\/Globel Interrupt Enable\n    TIMSK=(1&lt;&lt;OCIE1A); \/\/ Timer\/Counter1, Output Compare A Match Interrupt Enable\n   while(1);\nreturn 0;\n}\nISR(TIMER1_COMPA_vect)\n{\n    sample^=1;\n    sample ? (PORTB=0xFF):(PORTB=0x00);\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\/CTC-toggle-with-Interrupt-Timer1.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-9765989ae51a10acd90de104d954b7ad wp-block-paragraph\" style=\"color:#333232\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/CTC-toggle-with-Interrupt-Timer1.rar\">CTC toggle with Interrupt (Timer1).rar<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3dc4056092faed02d896eb12fe63bd0f wp-block-paragraph\" style=\"color:#5c5c5c\">We already know that as soon as OCRx register is equal to TCNTx register an interrupt available. We can use this interrupt without enabling the interrupt i.e. glag register. The all flags register are available at #include&lt;avr\/io.h&gt;, so no need of #include&lt;avr\/interrupt.h&gt; and ISR service routine.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4b3c5c763717627a312e12aea84ce37c wp-block-paragraph\" style=\"color:#5c5c5c\">The flag register of individual interrupt is active when an interrupt active that OCRx is equal to TCNTx. Let\u2019s modify the above program without interrupt handling<\/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;\nint main(void)\n  {\n   DDRB=0xFF;\n   DDRD|=(1&lt;&lt;DDD5);\t\t\t\t\t\t\t\/\/OC1A output PIN\n   TCCR1A=(1&lt;&lt;COM1A0);\t\t\t\t\t\t\/\/Toggle OC1A\/OC1B on compare match\n   TCCR1B=(1&lt;&lt;WGM12)|(1&lt;&lt;CS12);\t\t\t\t\/\/CTC with prescaler=256\n   TCNT1=0;\t\t\t\t\t\t\t\t\t\/\/Clear Timer\n   OCR1A=31249;\t\t\t\t\t\t\t\t\/\/F_CPU=8MHz,Prescaler=256,Delay=   1s\n  while(1)\n   {\n     if(TIFR&amp;(1&lt;&lt;OCF1A)){\n         PORTB^=0xFF;\n         TIFR|=(1&lt;&lt;OCF1A);\n         }\n    }\n return 0;\n }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-b5a10a0681bc16e04d8b2b396c6c54a3 wp-block-paragraph\" style=\"color:#5c5c5c\">Since we already know about our basic C to check a bit the condition is<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-336e10d485320357be258d5033c4453e wp-block-paragraph\" style=\"color:#373737\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C:\/&gt; (register name &amp;(1&lt;&lt; bit position))<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-9c2d2da7e3979a8a625a228c795666da wp-block-paragraph\" style=\"color:#5c5c5c\">Since the flag is set when OCRx value is equal to TCNTx value so in if() format we check the value and the flag is clear by writing 1 to it i.e. TIFR|=(1&lt;&lt;OCF1A);.<\/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\/CTC-toggle-without-interruptTimer1.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-1ebea72c103a0589dcf794f8ad3574df wp-block-paragraph\" style=\"color:#2e2e2e\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/CTC-toggle-without-interruptTimer1.rar\">CTC toggle without interrupt(Timer1).rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This mode is similar to the normal port operation. The difference is that the compare value is store in the OCRx register instead of TCNTx. In order to use the OCRx register, the timer should operate in CTC mode, that The main program is Download the full program with proteus simulation As soon as OCRx [&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-1520","post","type-post","status-publish","format-standard","hentry","category-timer-counter"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1520","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=1520"}],"version-history":[{"count":23,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1520\/revisions"}],"predecessor-version":[{"id":2590,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1520\/revisions\/2590"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}