{"id":1355,"date":"2024-06-25T13:22:26","date_gmt":"2024-06-25T13:22:26","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1355"},"modified":"2024-08-11T15:34:39","modified_gmt":"2024-08-11T15:34:39","slug":"external-interrupt","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1355","title":{"rendered":"External interrupt"},"content":{"rendered":"\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:66.66%\">\n<p class=\"has-text-color has-link-color wp-elements-45f9e9cf08a12696019af076d0fd4676 wp-block-paragraph\" style=\"color:#5c5c5c\">An interrupt is a hardware initiated procedure that interrupts whatever program is currently executing. In AVR an interrupt is a service routine that executed when it is called and back to the main program where it is halted after executing service routine. In AVR there are two types of interrupts.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-f4b7bfc7f0be153e24262672bd0cfdc2 wp-block-paragraph\" style=\"color:#292929\">i. External interrupts<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-60716a34108ee30258079410cd0f7617 wp-block-paragraph\" style=\"color:#5c5c5c\">In external interrupt external source causes interrupt. Some pins are defined as external pin for generating Interrupt, whenever any eternal pin is pressed if active, then the corresponding service routine will execute first halting the main program. After the executing the interrupt service routine the main program will continue with where it has been halting. <\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"403\" height=\"393\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/External-Interrupt.jpg\" alt=\"\" class=\"wp-image-1356\" style=\"width:190px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/External-Interrupt.jpg 403w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/External-Interrupt-300x293.jpg 300w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><figcaption class=\"wp-element-caption\">External Interrupt PIN<\/figcaption><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6043a1ada771ab49e0bcdbe0af5ceefd wp-block-paragraph\" style=\"color:#252525\">ii. Internal interrupts<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-1e2ba7e482da5bd5496266edaed8d237 wp-block-paragraph\" style=\"color:#5c5c5c\">Those interrupts can be occurred by using program. In interrupts there is an important factor which is purity. Each interrupt will execute according to there purity. Suppose both INT0 and INT1 external interrupt pin has been pressed at the same time, then only INT0 will be executed. Lets look at the purity according to the datasheet of ATmega32 provide-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"911\" height=\"783\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/External-interrupt-purity.jpg\" alt=\"\" class=\"wp-image-1364\" style=\"width:685px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/External-interrupt-purity.jpg 911w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/External-interrupt-purity-300x258.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/External-interrupt-purity-768x660.jpg 768w\" sizes=\"(max-width: 911px) 100vw, 911px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3f9f6388eff2ec0b8ba8ee4fdc208a09 wp-block-paragraph\" style=\"color:#5c5c5c\">In this article we begin our journey with external interrupt only. Internal interrupt will be discussed later but the fundamental remain the same. Before we discuss about the register available and our task lets discuss about the bits of a register and there operation.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8a42493959d5899eedae7d607e3f61f1 wp-block-paragraph\" style=\"color:#272727\">Bit Operation -&gt; SREG(Status Register)<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"146\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Sag-Register-1024x146.jpg\" alt=\"\" class=\"wp-image-1369\" style=\"width:666px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Sag-Register-1024x146.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Sag-Register-300x43.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Sag-Register-768x109.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Sag-Register.jpg 1195w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5ceb53f9500e40f390e9a0c3b54c9ba6 wp-block-paragraph\" style=\"color:#5c5c5c\">In the Status Register there is 8bit=1byte. To enable all interrupt we need to set the bit 7 i.e. I bit. Since according to C program to set a particular bit<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n      variable_name |=(1&lt;&lt;bit position); \/\/ To set a particular bit\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-e8e8ba0ae3d5789c9219117e96e1177b wp-block-paragraph\" style=\"color:#5c5c5c\">And to clear a particular bit<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n      variable_name &amp;=~(1&lt;&lt;bit position); \/\/ To clear a particular bit\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-51395bd8af88247e909ed8d1e84c7a85 wp-block-paragraph\" style=\"color:#5c5c5c\">We need to set the I bit of Status register without affecting the other bits. So we use C control logic to all of our register section. So that the condition to<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"82\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sreg-bit-1024x82.jpg\" alt=\"\" class=\"wp-image-1380\" style=\"width:664px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sreg-bit-1024x82.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sreg-bit-300x24.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sreg-bit-768x62.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/sreg-bit.jpg 1093w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-454900ca5a8a804785fb82ad88f46bae wp-block-paragraph\" style=\"color:#5c5c5c\">The R\/W indicates that those bits can be both read and write. Note that the initial value indicates that the default value is 0, means that initially the bit is clear. So no need to clears the bits first. Now looks at the registers that are available for external interrupts<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"829\" height=\"1024\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/image-2-829x1024.png\" alt=\"\" class=\"wp-image-1384\" style=\"width:762px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/image-2-829x1024.png 829w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/image-2-243x300.png 243w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/image-2-768x948.png 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/image-2-1244x1536.png 1244w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/image-2.png 1302w\" sizes=\"(max-width: 829px) 100vw, 829px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-c986edfca547951983b3f97ea85bd2fc wp-block-paragraph\" style=\"color:#5c5c5c\">To work with interrupts three steps must be setup<\/p>\n\n\n\n<ol style=\"color:#272727\" class=\"wp-block-list has-text-color has-link-color wp-elements-c1c9259cf9af54cc3237331d89111d78\">\n<li>Set the global Interrupt Enable bit in SREG Register.<\/li>\n\n\n\n<li>Initialize the interrupt by appropriate configuring the MCUCR, MCUCSR and GICR register.<\/li>\n\n\n\n<li>Define the appropriate Interrupt Service Routine (ISR) for interrupt.<\/li>\n<\/ol>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-fc55958e88a675cf212468e4a7560592 wp-block-paragraph\" style=\"color:#5c5c5c\"><strong>Step1:<\/strong>  To set the Global Interrupt Enable bit<\/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\/interrupt.h&gt;\n    sei(); or SREG|=(1&lt;&lt;I);\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-447aa30d47655ae92611fb11ad13d4ab wp-block-paragraph\" style=\"color:#5c5c5c\">To clear Global Interrupt Enable bit<\/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\/interrupt.h&gt;\n    cli(); or SREG&amp;=~(1&lt;&lt;I);\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-31d143d6aa5a47cbfd2e5287d382cdf9 wp-block-paragraph\" style=\"color:#272727\">Note that to use an interrupt we should need a header file- #include&lt;avr\/interrupt.h&gt;<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-770108a75b7a6d627d02090e45427149 wp-block-paragraph\" style=\"color:#5c5c5c\"><strong>Step2:<\/strong> Lets make a simple program that set PORTC of Atmega 32 from up to down and when an interrupt occur it set PORTC from down to up. In this case we use INT0 pin and the control logic is a rising edge of INT0 generates an interrupt. In first case we already enable the Global interrupt enable bit. So our target is to enable the INT0 (External Interrupt request 0) bit and then the control logic.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nsei();   \/\/SREG|=(1&lt;&lt;I);\t\t\t    \/\/Global interrupt enable\nGICR |=(1&lt;&lt;INT0);\t\t\t\t\t\t\/\/External Interrupt Request 0 Enable\nMCUCR|=(1&lt;&lt;ISC00)|(1&lt;&lt;ISC01);\t\t  \/\/The rising edge of INT0 generates an interrupt request\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-c9c3484f61663d2a9431b483026ae758 wp-block-paragraph\" style=\"color:#2a2a2a\">PORTC from up to down<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n   void uptodown(void)\n   {\n      for(int16_t i=0;i&lt;=7;i++){\n          PORTC |=(1&lt;&lt;i);  _delay_ms(50);\n          PORTC &amp;=(~(1&lt;&lt;i));_delay_ms(50);\n          }\n  }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-47a2c47c707bbc0588537e3604f63747 wp-block-paragraph\" style=\"color:#1c1c1c\">PORT from down to up <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n   void downtoup(void)\n   {\n      for(int16_t i=7;i&gt;=0;i--){\n          PORTC |=(1&amp;lt;&amp;lt;i); _delay_ms(50);\n          PORTC &amp;amp;=(~(1&amp;lt;&amp;lt;i)); _delay_ms(50);\n          }\n   }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-45307b9594f6c65cdcb6a83b8da8d3ec wp-block-paragraph\" style=\"color:#5c5c5c\"><strong>Step3:<\/strong> In this step we have to define appropriate Interrupt service routine (ISR). An interrupt routine is defined with ISR(). This micro register and () mark the routine as an interrupt handler for the specific peripheral. The following is an example definition of a handler for the INT0 interrupt.<\/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\/interrupt.h&gt;\n ISR(INT0_vect)\n {\n   ;\/\/user defined code here\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<figure class=\"wp-block-image aligncenter size-full is-resized\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/ISR.rar\"><img loading=\"lazy\" decoding=\"async\" width=\"174\" height=\"121\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/download.png\" alt=\"\" class=\"wp-image-1407\" style=\"width:60px;height:auto\"\/><\/a><figcaption class=\"wp-element-caption\">download<\/figcaption><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color wp-elements-08ff1a4fb04459a3f2ba6e90f4616d8b wp-block-paragraph\" style=\"color:#2c2c2c\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/ISR-Vector.pdf\">ISR Vector.pdf<\/a> OR <a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/ISR-Vector.docx\">ISR Vector.doc<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-f6f35304d6e10a939fc1c2341fddfc66 wp-block-paragraph\" style=\"color:#2a2a2a\">OR simply open any internet browser and go to: <\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6053f6556953984b14206eaf18d5874e wp-block-paragraph\" style=\"color:#2a2a2a\"><a href=\"file:\/\/\/C:\\WinAVR-20100110\\doc\\avr-libc\\avr-libc-user-manual\\index.html\">file:\/\/\/C:\/WinAVR-20100110\/doc\/avr-libc\/avr-libc-user-manual\/index.html<\/a><\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-7993b4a04ed12934dd958c5f376d7b87 wp-block-paragraph\" style=\"color:#5c5c5c\">OR open it in your computer -&gt; C:\/WinAVR-20100110\/doc\/avr-libc\/avr-libc-user-manual\/index.html<\/p>\n\n\n\n<p class=\"has-text-align-center has-text-color has-link-color wp-elements-1f0957b366299e032a69d59e1518a36a wp-block-paragraph\" style=\"color:#262525\">                        &#8211;&gt;Library Reference<\/p>\n\n\n\n<p class=\"has-text-align-center has-text-color has-link-color wp-elements-fa6e99149e2b6a5a0d04189753182914 wp-block-paragraph\" style=\"color:#252525\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8211;&gt;&lt;avr\/interrupt.h&gt;: Interrupts&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-332eb3e950d1502d5ff0f95e37eb777c wp-block-paragraph\" style=\"color:#1f1e1e\">Download the int0.c and int0.h files. The main program as follow &#8211;<\/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*************************************************************************************************\nThis program will set PORTC form up-to-down and down-to-up when external trigger to INT0 bit\n************************************************************************************************\n***********************************************************************************************\/\n#include&lt;avr\/io.h&gt;\n#include&lt;util\/delay.h&gt;\n#include&lt;avr\/interrupt.h&gt;\n#include&quot;int0.h&quot;\nuint16_t sample=0;\nint main(void)\n{\n  DDRC=0xFF;\t\t\t\/\/ port c for output\n  int_int0(); \t\t\/\/INT0 interrupt enable call\n  while(1)\n  {\n    sample ? uptodown() : downtoup();\n  }\n  return 0;\n}\nISR(INT0_vect)\n{\n   sample^=0x01;\t\t\/\/Toggle condition\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-43113950317110afaa5ddd1a60488999 wp-block-paragraph\" style=\"color:#5c5c5c\">Note that in header file we include #include&lt;avr\/interrupt.h&gt; , that is because in INT0 initialization we include sei(); function that is the function of #include&lt;avr\/interrupt.h&gt; header file<\/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 has-background wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/External-Interrupt.rar\" style=\"background-color:#1c3755;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 wp-elements-e2a06b65970fbf31b9d5f803c5602f81 wp-block-paragraph\" style=\"color:#272727\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/External-Interrupt.rar\">Download the full program with proteus simulation<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>An interrupt is a hardware initiated procedure that interrupts whatever program is currently executing. In AVR an interrupt is a service routine that executed when it is called and back to the main program where it is halted after executing service routine. In AVR there are two types of interrupts. i. External interrupts In external [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-1355","post","type-post","status-publish","format-standard","hentry","category-eternal-interrupts"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1355","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=1355"}],"version-history":[{"count":43,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1355\/revisions"}],"predecessor-version":[{"id":2569,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1355\/revisions\/2569"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}