LAB – 7 Programming on Interrupts
Aim:
To write a program to switch on LEDs for a while, as a reaction to interrupts.
Task:
1. Configure two I/O pin for LOW to HIGH transition Interrupt source and HIGH to
LOW transition Interrupt source.
2. Write two Interrupt Service Routine (IRS) for LOW to HIGH transition
Interrupt(ISRL2H) and HIGH to LOW transition Interrupt(ISRH2L), which will
display( print)the occurrence of positive edge or negative edge Interrupt when it is
called.
3. Make ON of LED1 for a while, if a positive edge interrupt signal occurs and call
ISRL2H. Make ON of LED2 for a while, if the negative edge Interrupt signal occurs
and call ISRH2L. Don’t use inbuilt function GPIO.add_event_detect() to detect
interrupt.
Algorithm:
1. Configure I/O Pins for Interrupts:
o Set up two GPIO pins for input mode (e.g., pin1 and pin2).
o Manually poll the pin states in a loop.
o Compare the current state with the previous state to detect a change.
o When a transition (low-to-high or high-to-low) occurs, trigger the respective
ISR.
2. Write ISRs for Transitions:
o ISRL2H: Called when a low-to-high transition is detected. It prints a message
and turns on LED1.
o ISRH2L: Called when a high-to-low transition is detected. It prints a message
and turns on LED2.
3. Control LEDs Based on the Interrupts:
o LED1 is turned on for a while when a positive edge is detected.
o LED2 is turned on for a while when a negative edge is detected.
o Use a delay to keep the LEDs on for a short time.
Pin & Circuit Diagram:
Figure 1. Pin Configuration of Board
Note: Interrupt sources are not shown in the figure.
Program:
Pre-Lab Questions:
1. What is polling? Write the merit and demerit of polling?
2. Define interrupt? What do you understand by interrupt service routine?
3. Explain the function ‘[Link].event_detect( ).
Post Lab Questions:
1. In a room number of persons entering and leaving has to be recorded
automatically. A system is installed for this. The hardware contains two
Infra-Red (IR) source and detector like our TV remote and TV system.
Whenever IR light is broken a movement is detected. Two sensors are
spaced apart to find out whether the person is leaving or entering. Sensor A
is placed first and Sensor B is placed second. So, if Sensor B is triggered
then Sensor A is triggered then it is treated as leaving. If it is Sensor A and
B then it is treated as entering. For clarity please refer below figure.
Write a suitable pseudo code or algorithm for above scenario. Explain the use of
interrupts in this case.
2. Compare edge triggering and level triggering. Out of the two which one you will
prefer. Justify your answer.
Result: