{"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-1 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-2 wp-block-paragraph\" style=\"color:#292929\">i. External interrupts<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3 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-4 wp-block-paragraph\" style=\"color:#252525\">ii. Internal interrupts<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5 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-6 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-7 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-8 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-9 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-10 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-11 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-12 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-13\">\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-14 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-15 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-16 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-17 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-18 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-19 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-20 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-21 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-22 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-23 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-24 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-25 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-26 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-27 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-28 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-29 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}]}}