Hardware Reference
In-Depth Information
Imagine you are at home, and you are waiting for an important parcel. This
parcel will be delivered to your letter box without requiring a signature. The
chances are that the postman will not knock on your door. You want to get
your hands on it as soon as possible, so you go outside to look at the letter box
frequently. It isn't there, so you wait for 10 minutes or so before having another
look. You have to decide when to stop working (if you can actually work at all)
before looking again, choosing a time that suits you. In computer terms, this
continual checking for an event is known as polling .
Interrupts are different. A few days later, you wait for another parcel; only this
time the parcel requires a signature, so the delivery man knocks on your door.
This gives you a little more freedom. Because you don't have to waste time by
looking inside the letter box every few minutes, you can get some work done.
The delivery man will knock on your door to let you know that he has arrived,
and at that time you can stop working for a few minutes to get your parcel. The
downside to this is that you have to react quickly; if the delivery man does not
get an answer quickly, he will go away. This situation is analogous to an interrupt .
Interrupts are a technique to let the processor continue working while waiting
for an external event. It might not occur at all, in which case the main program
continues, but if an external signal is received, the computer interrupts the main
program and executes another routine, known as an Interrupt Service Routine , or
ISR . ISRs are designed to be fast, and you should spend as little time as possible
inside an ISR. When servicing an interrupt, some functions will not continue to
work; delay() and millis() will not increment in interrupt context.
All Arduinos have interrupts; most use interrupts internally for serial commu-
nication or for timing counters. Some Arduinos have more user-programmable
interrupts. Table 4-1 shows which interrupts are available on which pins for
different models.
Table 4-1: Interrupt Pins on Arduinos
BOARD
INT.0
INT.1
INT.2
INT.3
INT.4
INT.5
Uno
2
3
Ethernet
2
3
Leonardo
3
2
0
1
7
Mega2560
2
3
21
20
19
18
The Arduino Due is different. It has highly advanced interrupt handling and
can effectively be programmed to interrupt on every digital pin.
attachInterrupt()
This function specii es which routine to call when a specii ed interrupt is received.
 
Search WWH ::




Custom Search