{"id":7126,"date":"2021-12-23T14:11:33","date_gmt":"2021-12-23T14:11:33","guid":{"rendered":"https:\/\/www.qec.tw\/?p=7126"},"modified":"2024-05-20T05:12:46","modified_gmt":"2024-05-20T05:12:46","slug":"attachinterrupt","status":"publish","type":"post","link":"https:\/\/www.qec.tw\/zh\/86duino-reference\/attachinterrupt\/","title":{"rendered":"attachInterrupt()"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">[External Interrupts]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Description<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs. Replace any previous function that was attached to the interrupt. Most 86Duino boards have three external interrupts: numbers 0 (on digital pin 42), 1 (on digital pin 43), and 2 (on digital pin 44). The following table below shows the available interrupt pins on various 86Duino boards.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Pins for int.0~5 :<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><\/tr><tr><td>Board<\/td><td>int.0<\/td><td>int.1<\/td><td>int.2<\/td><td>int.3<\/td><td>int.4<\/td><td>int.5<\/td><\/tr><tr><td>Zero<\/td><td>42<\/td><td>43<\/td><td>44<\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td>One<\/td><td>42<\/td><td>43<\/td><td>44<\/td><td>18<\/td><td>19<\/td><td>20<\/td><\/tr><tr><td>EduCake<\/td><td>42<\/td><td>43<\/td><td>44<\/td><td>18<\/td><td>19<\/td><td>20<\/td><\/tr><tr><td>QEC 7 \/ QEC 9 \/ QEC 15<\/td><td>2<\/td><td>4<\/td><td>7<\/td><td><\/td><td><\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Pins for int.6~11 :<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><\/tr><tr><td>Board<\/td><td>int.6<\/td><td>int.7<\/td><td>int.8<\/td><td>int.9<\/td><td>int.10<\/td><td>int.11<\/td><\/tr><tr><td>Zero<\/td><td><\/td><td><\/td><td><\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td>One<\/td><td>33<\/td><td>34<\/td><td>35<\/td><td>36<\/td><td>37<\/td><td>38<\/td><\/tr><tr><td>EduCake<\/td><td><\/td><td><\/td><td><\/td><td><\/td><td><\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Note<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interrupts should be handled as soon as possible, and so it isn\u2019t a good programming style to use&nbsp;<code>delay()<\/code>&nbsp;in the attached function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Serial data received while in the attached function may be lost. You should declare as&nbsp;<em>volatile<\/em>&nbsp;any variables that you modify within the attached function. See the section on ISRs below for more information.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Interrupts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interrupts are useful for making things happen automatically in microcontroller programs, and can help solve timing problems. Good tasks for using an interrupt may include reading a rotary encoder, or monitoring user input.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you wanted to insure that a program always caught the pulses from a rotary encoder, so that it never misses a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly poll the sensor lines for the encoder, in order to catch pulses when they occurred. Other sensors have a similar interface dynamic too, such as trying to read a sound sensor that is trying to catch a click, or an infrared slot sensor (photo-interrupter) trying to catch a coin drop. In all of these situations, using an interrupt can free the microcontroller to get some other work done while not missing the input.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">About Interrupt Service Routines (ISRs)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ISRs are special kinds of functions that have some unique limitations most other functions do not have. An ISR cannot have any parameters, and they shouldn\u2019t return anything.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generally, an ISR should be as short and fast as possible. If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be ignored (turned off) until the current one is finished.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike Arduino, the&nbsp;<code>delay()<\/code>&nbsp;and&nbsp;<code>millis()<\/code>&nbsp;functions of 86Duino don\u2019t rely on interrupts and therefore will work in ISRs as expected. But since this isn\u2019t a good programming style, you should always avoid calling&nbsp;<code>delay()<\/code>&nbsp;in ISRs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Typically global variables are used to pass data between an ISR and the main program. To make sure variables used in an ISR are updated correctly, declare them as&nbsp;<code>volatile<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>attachInterrupt(interrupt, ISR, mode)<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Parameters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>interrupt<\/code>: the number of the interrupt (int)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ISR<\/code>: the ISR to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mode<\/code>: defines when the interrupt should be triggered. Three constants are predefined as valid values:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>CHANGE<\/code>\u00a0to trigger the interrupt whenever the pin changes value,<\/li>\n\n\n\n<li><code>RISING<\/code>\u00a0to trigger when the pin goes from low to high,<\/li>\n\n\n\n<li><code>FALLING<\/code>\u00a0for when the pin goes from high to low.<\/li>\n\n\n\n<li><code>HIGH<\/code>\u00a0to trigger the interrupt whenever the pin is high.<\/li>\n\n\n\n<li><code>LOW<\/code>\u00a0to trigger the interrupt whenever the pin is low.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Returns<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">none<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example Code<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>int pin = 13;\nvolatile int state = LOW;\n \nvoid setup()\n{\n  pinMode(pin, OUTPUT);\n  attachInterrupt(0, blink, CHANGE);\n}\n \nvoid loop()\n{\n  digitalWrite(pin, state);\n}\n \nvoid blink()\n{\n  state = !state;\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">See also<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>[Language] <a href=\"https:\/\/www.qec.tw\/ethercat\/detachinterrupt\/\">detachInterrupt<\/a>()<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.qec.tw\/86duino\/86duino-language-reference\/\">Language Reference Home<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>The text of the 86Duino reference is a modification of&nbsp;<a href=\"http:\/\/arduino.cc\/en\/Reference\/HomePage\" target=\"_blank\" rel=\"noopener\">the Arduino reference<\/a>&nbsp;and is licensed under a&nbsp;<a href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/\" target=\"_blank\" rel=\"noopener\">Creative Commons Attribution-ShareAlike 3.0 License<\/a>. Code samples in the reference are released into the public domain.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[External Interrupts] Description Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs. Replace any previous function that was attached to the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7127,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[195],"tags":[162],"class_list":["post-7126","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-86duino-reference","tag-86duino"],"_links":{"self":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/7126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/comments?post=7126"}],"version-history":[{"count":0,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/7126\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media\/7127"}],"wp:attachment":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media?parent=7126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/categories?post=7126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/tags?post=7126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}