150 likes | 307 Views
Interrupts. Surjo Dutta and David Friedman. What are interrupts?. A signal which indicates to the microprocessor to pause one task, and perform another task Generally, there are two sources of interrupts Hardware/other devices in communication with the microprocessor Software triggers.
E N D
Interrupts Surjo Dutta and David Friedman
What are interrupts? • A signal which indicates to the microprocessor to pause one task, and perform another task • Generally, there are two sources of interrupts • Hardware/other devices in communication with the microprocessor • Software triggers
Interrupts on the Arduino Mega • The Arduino Mega has specific ‘interrupt pins’ which are directly read by hardware. • Interrupt pins (Digital): 2,3,18,19,20,21 • There are several ‘modes’ specifying interrupt conditions • LOW: triggers when pin is low • HIGH: triggers when pin is high • CHANGE: trigger when pin changes at all • RISING: trigger when pin goes from low -> high • FALLING: trigger when pin goes from high -> low • Can run a special function called an interrupt service routine (ISR) • Can only run one ISR at a time, and queues incoming interrupts in a First Come First Serve queue, ie, no way to prioritize interrupts • But reset gets priority over other interrupts
Interrupt service routines (ISR) • A function that executes when an interrupt is triggered • An ISR cannot have any parameters and cannot return anything • Associate an ISR with an interrupt pin with the ‘attachInterrupt’ function Triggered Main code Interrupt condition ISR Not triggered
How do you use them? • attachInterrupt (digitalPinToInterrupt(intPin), ISR, INT_MODE); • intPin: Digital pin to use as interrupt pin • ISR: The interrupt handler, takes no arguments and returns nothing • INT_MODE: LOW, HIGH, CHANGE, RISING, FALLING • Delay() depends on interrupts and hence can’t be used inside an ISR
Software Interrupts • Arduinos do not natively support software interrupts • External libraries like TimerThree.h can be used • They use one of the three built in hardware timers which were initially meant to set PWM periods • Now also used for timer overflow interrupt handling, and other features • Enables the microcontroller to be interrupted at even time intervals • Useful for two to four actions to be performed at different, indivisible frequencies
Sources • https://www.teachmemicro.com/arduino-interrupt-tutorial/ • http://www.engblaze.com/we-interrupt-this-program-to-bring-you-a-tutorial-on-arduino-interrupts/ • https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ • https://learn.sparkfun.com/tutorials/processor-interrupts-with-arduino/all