You are on page 1of 13

General Overview of interrupts

Regardless of the Processor in Being Used: HC12, PIC, Raspberry Pi, Arduino , Photon

 Exceptions and Interrupt are used interchangeably.


 To be more precise, you could say interrupts are subset of exceptions.
 Exceptions: is an event, which occurs during the program execution.
 It is any event that can alter the normal CPU Instruction execution flow
 Internal event that handles abnormality during program executions
 Examples:
- Invalid instruction such divide by zero, single step execution (tracing),
- Illegal bus access such go to address. Such address may not be the
beginning of instruction address or data address.
- It is software trigger event
 Interrupt: is an event that can be triggered by external hardware which leads to stop
the program executing sequence and jump to and interrupt handler ( Interrupt
Service Routine - ISR). Such ISR can be system ISR or user ISR:
 Examples:
- System Hardware Interrupts Triggered by external signal: Reset, Printer is
out off papers, Print screen (Print Screen).

- Hardware Interrupts Triggered by GPIO: an action on an Input of GPIO Pin


will cause the CPU to jump to ISR is defined and written by the user (Read
ADC, Flash an output GPIO….)

 Errors are handled more smoothly.


 CPU utilization is improved.
Exceptions and Interrupt
Overall Process Sequence
- In both Types, once an expectation or an
interrupt occurs, the normal program
execution sequence is suspended,

- Identifying the cause of interrupt,

- the CPU status registers, the program


counters and other registers are saved
the stack area,

- Address of the ISR handler is loaded to


the program counter (Instruction
pointer),

- Execution of the ISR starts,

- At the end of the ISR, restoring the CPU


status registers and the program
counter from the stack,

- Resuming the interrupted program


normal execution from the next
instruction where it was interrupted,
inter the and jump to execute a user
defined ISR (IRS is a set of instructions
like read ADC, turn OFF an output,
Display Hello message.
Exceptions and Interrupt
Overall Process Sequence

Save Status & Working Regs., PC

Save Status & Working Regs., PC


Main Main Main
Program Program Program

Check Yes ISR


4 Int. Do Something
Once X time
expired Go
to next Inst.

Check Yes ISR


4 Int. Do Something
Board mode Board mode
Pin Numbering Declaration
For the GPIO Pins to be usable, pin mode has to be configured first.
There are two modes of operation-

• GPIO.BOARD mode:- Board numbering scheme. It refers to the pin


numbers on header and the number goes from 1-40. Pin 22 in
BOARD mode corresponds
Example “Pin 05” board mode
• GPIO.BCM mode :- Broadcom chip-specific pin numbers. These are
the numbers after "GPIO“

Example GPIO3 in BCM i.e.


“Pin 05” board mode = “GPIO3” in BCM

Warning - the BCM channels move around a little between previous


revisions of the Raspberry Pi board.
• The BOARD numbering system stays the same between board
revisions.
• It is recommended to use Board numbering to make the script more
meaningful and avoid revision checking.
Therefor, Our default mode is the BOARD mode in the class.
GPIO Polling
Previously, you have used if statements and while loops to check for the status of the
Rpi GPIO inputs. Then execute certain statement or make an action on an output in
case any of these inputs change its status.
This is called polling techniques. An example of such programs is shown below:

if GPIO.input(channel):
print('Input was HIGH')
else:
print('Input was LOW')
#OR
while (GPIO.input(channel)):
print('Input was HIGH')

The above two statements have disadvantage. The program will loop until the
condition is met. This will tied the CPU until the condition is met.
To avoid such infinite loop, you can use a wait_for_edge function. This function is
predefined in the GPIO library. The function polls the associated pin and keeps
waiting for it to change its status either with a rising or a falling edge or both.
Moreover using this function the user can specify a timeout parameter to exit the wait
loop if the edge didn’t occur with the specify timeout. This timeout feature gives an
advantage over a normal while loop.
The syntax for such function is as follow:
GPIO.wait_for_edge (18, GPIO.FALLING, timeout=1000)

#where 18 is the input pin number, edge on such pin can be RISING, FALLING or
BOTH and the timeout is the time to exit the wait if the edge did not occur in
milliseconds.
Using such functions you will be wasting you processor time and resources hence the
need for interrupts arise.
GPIO Interrupts
Each GPIO pin, when configured as a general-purpose input, can be configured as an
interrupt driven event. Interrupt can be activated as follows:
•Synchronous Rising/Falling edge:
The normal rising/falling edge detection has a small amount of synchronization
built into the detection. Every system clock cycle, the pin is sampled with the
criteria for generation of an interrupt being a stable transition within a 3-cycle
window, i.e. a record of "1 0 0" for falling or "0 1 1“ for rising.
•Asynchronous Rising/Falling edge:
Asynchronous detection bypasses this synchronization to enable the detection
of very narrow events.
Once an interrupt edge is detected, The CPU will start executing the related interrupt
service routine (function).

Asyn. Asyn.
Falling Edge Rising Edge

Syn. Falling Edge Syn. Rising Edge


GPIO.add_event_detect(channel, GPIO.Edge, callback=my_callback, bouncetime=x)
Where Channel is the GPIO input, Edge can be RISINF or FALLING, my_callback is
the name of ISR function and x is the time needed to overcome the denounce effect
in milliseconds
GPIO. add_event_detect (18, GPIO.FALLING, callback=action , bouncetime=1000)
Interrupt Driven Program
PIR Sensors Application
A PIR sensor is a digital sensor used to detect motion; the
sensor radiates IR and reflects the signals back to the senor
using a set of special lenses. Based on the deference between
the signals read back from each lens it can detect whether an
object is moving in its range. As a result simple to use the
sensor it should be powered up and then wait 1-2 seconds for
the sensor to get a snapshot of the still room. If anything
moves after that period, the ‘alarm’ pin will go low.
Connecting the pin to a digital I/O of a microcontroller one can
write programs to detect motion in the surrounding
environment. The alarm pin is an open collector meaning you
will need a pull up resistor on the alarm pin.

R = Power

W = Ground

B = Output
Phototransistor IR LED
Lab Work:

1. Use the polling method to write a program to flash an


LED connected to GPIO27 for 4 times only after an
event (falling edge) is detected on PIN 18 and print
“Hello”.

2. Write a program to continuously print “Hello” while


the program is running. If an event (falling edge) is
detected on PIN 18 an interrupt service routine will
flash an LED connected to GPIO27 for 4 times only.

3. Interface the PIR sensor SEN-13285 with one of the


GPIO pins and develop an interrupt based program to
alert the user if motion is detected. The alert should
be via turning ON an LED connected to GPIO 27 and
writing the statement “Motion Detected on the
terminal”. The LED should be OFF otherwise and the
terminal should display “No motion”.

You might also like