Arduino Interrupts Paul MacDougal September 8, 2014.

Slides:



Advertisements
Similar presentations
Interrupts, Low Power Modes and Timer A (Chapters 6 & 8)
Advertisements

Lab7: Introduction to Arduino
4-1 Timers Timers can be used for  timing  event counting  pulse width measurement  pulse generation  frequency multiplication There are 8 Timers.
Anurag Dwivedi & Rudra Pratap Suman.  Open Source electronic prototyping platform based on flexible easy to use hardware and software.
What is Arduino?  Arduino is a ATMEL 168 micro-controller kit designed specially for small projects  User friendly IDE(Integrated Development Environment)
Panasonic EVE-KC2F2024B 24 pulses per revolution 6mm diameter flattened output shaft output type: quadrature (incremental) minimum life: 15,000 rotations.
68HC11 Polling and Interrupts
Arduino Microcontrollers SREEJAA SUNDARARAJU AND R. SCOTT CARSON BME 462.
Mark Neil - Microprocessor Course 1 Timers and Interrupts.
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
Butterfly I/O Ports CS-212 Dick Steflik. I/O for our labs To get data into and out of our Butterfly its a little trickier than using printf and scanf.
AVR Programming CS-212 Dick Steflik. ATmega328P I/O for our labs To get data into and out of our Arduino its a little trickier than using printf and.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
External & internal Interrupts. Interrupt Sources There are 21 different interrupts and each one has its own vector located in a predefined location at.
The 8051 Microcontroller and Embedded Systems
Basic Circuits – Lab 2 Arduino and Sensors Xmedia Spring 2011.
Arduino John Marcoux Christopher Lesch Thomas Dodge Unless otherwise noted, all information and pictures came from:
1 Timing System Timing System Applications. 2 Timing System components Counting mechanisms Input capture mechanisms Output capture mechanisms.
COMP201 Computer Systems Exceptions and Interrupts.
UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)
A Few Words From Dilbert
Clock Options and Sleep Modes. Clock Sources Flash Fuse bits can be programmed to choose one of the following Clock sources: 1. External RC Osc. f = 1/(3RC).
Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm.
Khaled A. Al-Utaibi  The Push Button  Interfacing Push Buttons to Arduino  Programming Digital Inputs  Working with “Bouncy”
Lecture 11: TI MSP430 Timers Compare Modes
AVR Programming: Interrupts September 17, What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.
Timer Timer is a device, which counts the input at regular interval (δT) using clock pulses at its input. The counts increment on each pulse and store.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
1 Interrupts, Resets Today: First Hour: Interrupts –Section 5.2 of Huang’s Textbook –In-class Activity #1 Second Hour: More Interrupts Section 5.2 of Huang’s.
Timers and Scheduled Interrupts
Microprocessor and Interfacing Example: Writing a Game Need to Check Keyboard Input.
Interrupt On a very basic level, an interrupt is a signal that interrupts the current processor activity. It may be triggered by an external event (change.
CSCI1600: Embedded and Real Time Software Lecture 16: Advanced Programming with I/O Steven Reiss, Fall 2015.
Microcontrollers, Microcomputers, and Microprocessors
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
WCL 조윤정 Arduino Bootloader 코드 분석. BAUD_RATE 설정 /* set the UART baud rate defaults */ #ifndef BAUD_RATE #if F_CPU >= L #define BAUD_RATE.
CS-280 Dr. Mark L. Hornick 1 Sequential Execution Normally, CPU sequentially executes instructions in a program Subroutine calls are synchronous to the.
Embedded Programming and Robotics Lesson 11 Arduino Interrupts 1.
Embedded systems and sensors 1 Part 2 Interaction technology Lennart Herlaar.
Microcontroller basics Embedded systems for mortals.
LAB1 TYWU. Devices Dip Switch (4 Switches) Triple Output LED (RGB) –Common Cathode.
Atmega328p Introduction for Digital and PWM Output Brion L Fuller II Robotics Club.
Arduino “Getting Started” Instructor : Dr Matthew Miss Khin Yi Kyaw
DEPARTMENT OF ELECTRONICS ENGINEERING V-SEMESTER MICROPROCESSOR & MICROCONTROLLER 1 CHAPTER NO microcontroller & programming.
Microprocessors A practical approach..
Pulse Width Modulation Instructor Dr Matthew Khi Yi Kyaw.
Timers and Scheduled Interrupts
Outline Introduction to Arduino UNO Programming environment setup GPIO
Lab 7 Basic 1: Game of Memory
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
80C51 Block Diagram ECE Overview.
Embedded Systems Programming Examples and Comparison
Microcontroller basics
68HC11 Interrupts & Resets.
Microcontroller basics
UNIT – Microcontroller.
Callback TYWu.
Arduino Part 1 Topics: Microcontrollers Programming Basics
Arduino.
BVM Engineering College Electrical Engineering Department : Microprocessor and Microcontroller Interfacing Interrupts of 8051 Prepared by:
UNIT 5 TIMRERS/COUNTERS
The Arduino Microcontroller: Atmel AVR Atmega 328
Arduino Interrupts I am amazed by how much I learned in preparing for this talk. I would encourage each of you to consider making a presentation, however.
CENG2400 Revision Q1a A system has an ARM processor with a 32-bit General Purpose Input Output (GPIO) module. Two on/off switches are connected to bit-3.
Arduino : Introduction & Programming
Programming 2: The Arduino IDE & First Sketches
Arduino程式範例.
Introduction to Arduino IDE and Software
Presentation transcript:

Arduino Interrupts Paul MacDougal September 8, 2014

What are they? Interrupts are a way for a microcontroller to temporarily stop what it is doing to handle another task. The currently executing program is paused, an ISR (interrupt service routine) is executed, and then your program continues, none the wiser.

Kinds of interrupts There are 26 different interrupts on an Arduino Uno –1 Reset –2 External Interrupt Request 0 (pin D2) –3 External Interrupt Request 1 (pin D3) –4 Pin Change Interrupt Request 0 (pins D8 to D13) –5 Pin Change Interrupt Request 1 (pins A0 to A5) –6 Pin Change Interrupt Request 2 (pins D0 to D7) –7 Watchdog Time-out Interrupt –8 Timer/Counter2 Compare Match A –… –18 SPI Serial Transfer Complete –19 USART Rx Complete –… –25 2-wire Serial Interface (I2C) –…

When would you use one? Interrupts can detect brief pulses on input pins. Polling may miss the pulse while you are doing other calculations. Interrupts are useful for waking a sleeping processor. Interrupts can be generated at a fixed interval for repetitive processing. And more …

Example 1 (no interrupts) const byte LED = 13, SW = 2; void setup() { pinMode(LED, OUTPUT); pinMode(SW, INPUT_PULLUP); } void handleSW() { digitalWrite(LED, digitalRead(SW)); } void loop() { handleSW(); }

Example 2 (no interrupts) const byte LED = 13, SW = 2; void handleSW() { digitalWrite(LED, digitalRead(SW)); } void handleOtherStuff() { delay(250); } void setup() { pinMode(LED, OUTPUT); pinMode(SW, INPUT_PULLUP); } void loop() { handleSW(); handleOtherStuff(); }

Example 3 (interrupt) const byte LED = 13, SW = 2; void handleSW() { // ISR digitalWrite(LED, digitalRead(SW)); } void handleOtherStuff() { delay(250); } void setup() { pinMode(LED, OUTPUT); pinMode(SW, INPUT_PULLUP); attachInterrupt(INT0, handleSW, CHANGE); } void loop() { // handleSW();  commented out handleOtherStuff(); }

ISR Interrupt Service Routines should be kept short. Interrupts are disabled when the ISR is called, so other interrupts are postponed. Do not call millis() or delay() or Serial or … This one is good: void myISR () { count++; }

What we have learned The hardware can call a routine for us based on activity on pin 2 (INT0) Our loop() code does not need to know what is happening But, we often want to know what is going on. How do we share that information?

Example 4 const byte LED = 13, SW = 2; volatile unsigned char count = 0; void handleSW () { digitalWrite(LED, digitalRead(SW)); count++; } unsigned char lastCount = -1; void handleOtherStuff() { if (count != lastCount) { Serial.print("Count "); Serial.println(count); lastCount = count; } void loop () { handleOtherStuff(); } void setup () { //Start up the serial port Serial.begin(9600); Serial.println(F(“Example4")); pinMode (LED, OUTPUT); pinMode (SW, INPUT_PULLUP); attachInterrupt(INT0, handleSW, CHANGE); }

A little more on sharing data An interrupt can happen at any time. If you share a multi-byte value (e.g. short int) between an ISR and your code, you have to take additional precautions. volatile short count; if (count == 256) … 1fa: lds r24, 0x0110 ; count lower 1fe: lds r25, 0x0111 ; count upper 202: subi r24, 0x00 204: sbci r25, 0x01 206: 69 f5 brne.+90

Sharing continued // Disable interrupts and copy noInterrupts(); short int myCount = count; interrupts(); if (myCount == 256) … 1fa: f8 94 cli 1fc: lds r24, 0x : lds r25, 0x : sei 206: subi r24, 0x00 208: sbci r25, 0x01 20a: 69 f5 brne.+90

What we have learned Switches bounce and we may be interrupted more often than expected We must take precautions when sharing data between an ISR and the main code

Pin Change Interrupt Pin 2 is INT0 Pin 3 is INT1 But, what about pins 0,1,4,5,6,… Pin Change Interrupts can monitor all pins

Example 5 #include const byte LED = 13, SW = 5; volatile unsigned char count = 0; void handleSW () { digitalWrite(LED, digitalRead(SW)); count++; } unsigned char lastCount = -1; void handleOtherStuff() { if (count != lastCount) { Serial.print("Count "); Serial.println(count); lastCount = count; } void loop () { handleOtherStuff(); } void setup () { //Start up the serial port Serial.begin(9600); Serial.println(F(“Example4")); pinMode (LED, OUTPUT); pinMode (SW, INPUT_PULLUP); PCintPort::attachInterrupt(SW, handleSW, CHANGE); }

What we have learned We can monitor any pin and have it generate an interrupt Different pins can have different ISRs

Example 6 #include void wake() { // ISR sleep_disable(); // first thing after waking from sleep: PCintPort::detachInterrupt(SW); // stop LOW interrupt } void sleepNow() { set_sleep_mode(SLEEP_MODE_PWR_DOWN); noInterrupts(); // stop interrupts sleep_enable(); // enables sleep bit in MCUCR PCintPort::attachInterrupt(SW, wake, LOW); interrupts(); // allow interrupts sleep_cpu(); // here the device is put to sleep }

Timer Interrupts There are three timers on an Uno. Two are 8 bit and one is 16 bit. They can generate an interrupt when they overflow or when they match a set value. The frequency at which the timers increment is programmable Arduino uses the timers for PWM and for timing (delay(), millis(), micros())

Timers Timer0 – 8 bit – controls PWM on pins 5 and 6. Also controls millis() Timer1 – 16 bit – controls PWM on pins 9 and 10. Timer2 – 8 bit – controls PWM on pins 11 and 3.

Example 7 #include const byte LED = 13; void handleOtherStuff() { delay(250); } unsigned int led = LOW; void timerISR() { digitalWrite(LED, led); led ^= (HIGH^LOW); } void setup () { pinMode (LED, OUTPUT); Timer1.initialize(); // breaks analogWrite() for digital pins 9 and 10 Timer1.attachInterrupt(timerISR, ); // attaches timerISR() as a timer overflow interrupt -- blinks at 1 Hz } void loop () { handleOtherStuff(); }

What have we learned The fundamental Arduino code uses each of the timers. We can sacrifice some functionality and use them for our own purposes. The timers are very complex (pages in the datasheet). They can be used for lots of cool things.

Watchdog Timer The watchdog timer is a separate timer. A selectable timeout is programmable (15ms, 30ms, 60ms, 120ms, 250ms, 500ms, 1s, 2s, 4s, 8s) Times are approx. If the SW does not reset the WDT (kick the dog) within the timeout period, an interrupt or a reset (or both) occur.

Example 8 #include const byte LED = 13; uint8_t led = LOW; ISR (WDT_vect) { wdt_setup(WDTO_500MS); digitalWrite(LED, led); led ^= (HIGH^LOW); } void setup () { // configure the pins pinMode (LED, OUTPUT); noInterrupts(); wdt_setup(WDTO_500MS); interrupts(); } void loop () { delay(250); } void wdt_setup(uint8_t duration) { // interrupts should be disabled wdt_reset(); // kick the dog WDTCSR = (1<<WDCE) |(1<<WDE) |(1<<WDIF); WDTCSR = (0<< WDE)|(1<<WDIE) |(duration&0x7) |((duration&0x8)<<2); }

Resources Interrupts 488http:// 488 Timers PNphpBB2&file=viewtopic&t=50106http:// PNphpBB2&file=viewtopic&t=50106

Q/A Questions?

Notes All examples were compiled using arduino and run on an Arduino Uno R3

#if defined(__AVR_ATtiny45__) #error "__AVR_ATtiny45" #elif defined(__AVR_ATtiny84__) #error "__AVR_ATtiny84" #elif defined(__AVR_ATtiny85__) #error "__AVR_ATtiny85" #elif defined (__AVR_ATtiny2313__) #error "__AVR_ATtiny2313" #elif defined (__AVR_ATtiny2313A__) #error "__AVR_ATtiny2313A" #elif defined (__AVR_ATmega48__) #error "__AVR_ATmega48" #elif defined (__AVR_ATmega48A__) #error "__AVR_ATmega48A" #elif defined (__AVR_ATmega48P__) #error "__AVR_ATmega48P" #elif defined (__AVR_ATmega8__) #error "__AVR_ATmega8" #elif defined (__AVR_ATmega8U2__) #error "__AVR_ATmega8U2" #elif defined (__AVR_ATmega88__) #error "__AVR_ATmega88" #elif defined (__AVR_ATmega88A__) #error "__AVR_ATmega88A" #elif defined (__AVR_ATmega88P__) #error "__AVR_ATmega88P" #elif defined (__AVR_ATmega88PA__) #error "__AVR_ATmega88PA" #elif defined (__AVR_ATmega16__) #error "__AVR_ATmega16" #elif defined (__AVR_ATmega168__) #error "__AVR_ATmega168" #elif defined (__AVR_ATmega168A__) #error "__AVR_ATmega168A" #elif defined (__AVR_ATmega168P__) #error "__AVR_ATmega168P" #elif defined (__AVR_ATmega32__) #error "__AVR_ATmega32" #elif defined (__AVR_ATmega328__) #error "__AVR_ATmega328" #elif defined (__AVR_ATmega328P__) #error "__AVR_ATmega328P" #elif defined (__AVR_ATmega32U2__) #error "__AVR_ATmega32U2" #elif defined (__AVR_ATmega32U4__) #error "__AVR_ATmega32U4" #elif defined (__AVR_ATmega32U6__) #error "__AVR_ATmega32U6" #elif defined (__AVR_ATmega128__) #error "__AVR_ATmega128" #elif defined (__AVR_ATmega1280__) #error "__AVR_ATmega1280" #elif defined (__AVR_ATmega2560__) #error "__AVR_ATmega2560" #else #error "Unknown processor" #endif

Arduino main() #include int main(void) { init(); #if defined(USBCON) USBDevice.attach(); #endif setup(); for (;;) { loop(); if (serialEventRun) serialEventRun(); } return 0; }

ISR(INT0_vect) { 2e8: 1f 92 push r1 2ea: 0f 92 push r0 2ec: 0f b6 in r0, 0x3f ; 63 2ee: 0f 92 push r0 2f0: eor r1, r1 2f2: 2f 93 push r18 2f4: 3f 93 push r19 2f6: 4f 93 push r20 2f8: 5f 93 push r21 2fa: 6f 93 push r22 2fc: 7f 93 push r23 2fe: 8f 93 push r24 300: 9f 93 push r25 302: af 93 push r26 304: bf 93 push r27 306: ef 93 push r30 308: ff 93 push r31 30a: lds r24, 0x e: lds r25, 0x : 89 2b or r24, r25 314: 29 f0 breq.+10 ; 0x : e lds r30, 0x a: f lds r31, 0x e: icall 320: ff 91 pop r31 322: ef 91 pop r30 324: bf 91 pop r27 326: af 91 pop r26 328: 9f 91 pop r25 32a: 8f 91 pop r24 32c: 7f 91 pop r23 32e: 6f 91 pop r22 330: 5f 91 pop r21 332: 4f 91 pop r20 334: 3f 91 pop r19 336: 2f 91 pop r18 338: 0f 90 pop r0 33a: 0f be out 0x3f, r0 ; 63 33c: 0f 90 pop r0 33e: 1f 90 pop r1 340: reti