{"id":4609,"date":"2015-02-06T08:49:26","date_gmt":"2015-02-06T16:49:26","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=4609"},"modified":"2015-08-03T17:30:15","modified_gmt":"2015-08-04T00:30:15","slug":"interrupts-vs-polling-whats-the-dif-interrupt-ference","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/interrupts-vs-polling-whats-the-dif-interrupt-ference\/","title":{"rendered":"Interrupts vs. Polling: What&#8217;s the Dif-(Interrupt)-ference?"},"content":{"rendered":"<p>Welcome back to the Digilent Blog!<\/p>\n<p>&nbsp;<\/p>\n<p>For those of you that have been coding for awhile, you likely have heard about both interrupts and polling. These are both techniques that can be used to alert your system board, such as a chipKIT microcontroller, when an input has occurred. But what is the difference between these two methods? Is one better than the other? Let&#8217;s find out.<\/p>\n<p>&nbsp;<\/p>\n<p>When working with a chipKIT board, one of the earliest projects that you might do is to set up a circuit so that when you press a button an LED turns on. The way the program knows whether or not you pressed a button is by digitally reading the current voltage state of the pin attached to the button and then appropriately turning the LED on or off. However, by digitally reading, the microcontroller has to constantly check the voltage state on that pin to see what the next action should be (turning the LED on or off). This method of continually checking is known as polling. It can get processor-intensive, though, since the microcontroller is always checking for that change, but it is also an easy way to make sure that as soon as a change happens your desired effect take place immediately after the change.<\/p>\n<figure id=\"attachment_4633\" aria-describedby=\"caption-attachment-4633\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/is-the-button-pressed.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-4633\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/is-the-button-pressed.jpg\" alt=\"Is the button pressed? Is the button pressed? Is the button pressed?\" width=\"600\" height=\"450\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/is-the-button-pressed.jpg 2666w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/is-the-button-pressed-600x450.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/is-the-button-pressed-1024x769.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/is-the-button-pressed-225x169.jpg 225w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/is-the-button-pressed-800x600.jpg 800w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/is-the-button-pressed-1200x900-cropped.jpg 1200w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4633\" class=\"wp-caption-text\">Is the button pressed? Is the button pressed? Is the button pressed?<\/figcaption><\/figure>\n<p>So what about interrupts? An interrupt is also a way to detect if a change occurred within your system, but it does not need to continually check if something as occurred. Rather, an interrupt changes a bit in hardware and causes an interrupt service routine (ISR) to occur immediately, even if the chipKIT board is in the middle doing something else, like printing something to a serial monitor (hence the name of &#8220;interrupt&#8221;). Typically, the interrupt service routine would set some sort of flag (<a title=\"True or False: Boolean Is a Data Type?\" href=\"https:\/\/digilent.com\/blog\/index.php\/true-or-false-boolean-is-a-data-type\/\" target=\"_blank\">a boolean<\/a>) to indicate to the system board that an interrupt has occurred, which the system board would then address more fully once it specifically checks on the status of that flag.<\/p>\n<figure id=\"attachment_4613\" aria-describedby=\"caption-attachment-4613\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/interrupt-in-the-middle.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4613\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/interrupt-in-the-middle.jpg\" alt=\"An interrupt happening in the middle of a Serial.print command\" width=\"600\" height=\"450\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/interrupt-in-the-middle.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/interrupt-in-the-middle-225x169.jpg 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4613\" class=\"wp-caption-text\">An interrupt happening in the middle of a Serial.print command.<\/figcaption><\/figure>\n<p>But if an interrupt occurs immediately, why not have everything associated with that interrupt be executed as soon as the interrupt happens? In all reality, the answer is you could have everything happen in the ISR. Realistically though, most applications are time sensitive to some degree, so it would be counterproductive to run large amounts of code in an interrupt that is designed to just briefly occur. Additionally, time dependent functions such as delay() will not work inside an ISR, nor would the counter for millis() continue to tick inside the interrupt. Because of this, it tends to be much easier to run an occasional poll within the main to check to see if the interrupt flag has changed in order to keep the system board running smoothly as designed.<\/p>\n<figure id=\"attachment_4617\" aria-describedby=\"caption-attachment-4617\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/check-interrupt-flag.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4617\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/check-interrupt-flag.png\" alt=\"The interrupt can be triggered at any time, but only checked at certain time.\" width=\"600\" height=\"450\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/check-interrupt-flag.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/check-interrupt-flag-225x169.png 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4617\" class=\"wp-caption-text\">The interrupt can be triggered at any time, but checked at a certain time.<\/figcaption><\/figure>\n<p>So what&#8217;s the major difference between interrupts and polling? One analogy\u00a0would be receiving a text on your cell phone; if you keeping pulling out your cell phone to check to see if anybody has texted you, that is polling. If you instead\u00a0are going about your day and then suddenly hear your phone buzz (an interrupt), you now know that you received a text (set the interrupt flag) and can then choose to check your phone right then or later when it is more convenient for you.<\/p>\n<p>&nbsp;<\/p>\n<p>Which method do you use most frequently?<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-4609 jlk' data-task='like' data-post_id='4609' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-4609 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-4609 jlk' data-task='unlike' data-post_id='4609' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-4609 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-4609 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>For those of you that have been coding for awhile, you likely have heard about both interrupts and polling. These are both techniques that can be used to alert your system board, such as a chipKIT microcontroller, when an input has occurred. But what is the difference between these two methods? Is one better than the other? Let&#8217;s find out.<\/p>\n","protected":false},"author":17,"featured_media":4633,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1563],"tags":[],"ppma_author":[4469],"class_list":["post-4609","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guide"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/is-the-button-pressed.jpg","authors":[{"term_id":4469,"user_id":17,"is_guest":0,"slug":"jamescolvin","display_name":"James Colvin","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/509febded809c5de0423909f6ae6587f?s=96&d=mm&r=g","author_category":"","user_url":"","last_name":"Colvin","last_name_2":"","first_name":"James","first_name_2":"","job_title":"Applications Engineer \/ Technical Support Engineer \/ Product Support Engineer \/ Technical Writer \/ the person to bother about T&M and JTAG when the senior design engineer is busy","description":"A local Digilent employee who is sometimes tricked into making other content besides documentation and supporting customers on the Digilent Forum, but then I get to write a little more informally so that's a plus. \r\n\r\nA sassy engineer, lover of puns and dad jokes, father and husband. \r\n\r\nI know both way too much and simultaneously almost nothing about a number of nerdy topics. If you want to hear me rant, ask me what data rate USB C operates at."}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/4609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=4609"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/4609\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/4633"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=4609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=4609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=4609"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=4609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}