1 / 8

Embedded Programming and Robotics

Learn about Arduino interrupts, a hardware event that can make your program far more responsive. Understand the overview, available interrupts, interrupt service routines, and how to write a sample program using interrupts.

duque
Download Presentation

Embedded Programming and Robotics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Embedded Programming and Robotics Lesson 11 Arduino Interrupts Arduino Interrupts

  2. Interrupts Overview • An interrupt is a hardware event • The processor stops what it was doing and calls the code at a specific location, determined by the type of interrupt • Further interrupts are disabled • The Interrupt Service Routine (ISR) must save state, then restore it upon return (Arduino does this for you) • Timing is critical; ISRs must execute very quickly Arduino Interrupts

  3. Interrupts Overview • Most Arduino programs poll various sensors, and the Arduino can do this quickly • This is costly because the program must read the device whether data is present or not • Interrupts can make your program far more responsive Arduino Interrupts

  4. Available Interrupts • On the Arduino Uno we’re using, only pins 2 and 3 can cause interrupts • Thus you can have two different service routines for two different sensors • The Mega has six available interrupts Arduino Interrupts

  5. Interrupt Service Routines • Use the following function to link a service routine to an interrupt: attachInterrupt(interrupt, ISR, mode) • Interrupt is either 0 or 1, which is pin 2 or 3 on the Uno • ISR is the name of the interrupt service routine • Mode is one of the following: • LOW to trigger the interrupt whenever the pin is low, • CHANGE to trigger the interrupt whenever the pin changes value • RISING to trigger when the pin goes from low to high, • FALLING for when the pin goes from high to low. Arduino Interrupts

  6. Interrupt Service Routines • ISRs take no parameters and return no values • Variables modified by ISRs should be declared as volatile volatile intmscount; attachInterrupt(0, increment, RISING); Arduino Interrupts

  7. Interrupt Service Routines void increment() { mscount++; } Arduino Interrupts

  8. Sample Program • People can perceive events that take longer than a specific duration • Write a program that turns on an LED for a variable number of milliseconds, then off for 2 seconds. Start at 1 millisecond • Write an interrupt service routine that, when a switch is pressed, increments the number of milliseconds by 1 • Press the switch until you notice that the LED blinks • If you have time, connect the LCD panel to show the “on” time so you don’t have to count button presses Arduino Interrupts

More Related