TOPIC4 - Part 1 - External HW Interrupt
TOPIC4 - Part 1 - External HW Interrupt
TOPIC 4
INTERRUPT
Part 1: External Hardware Interrupt
2
LEARNING OUTCOMES
At the end of this session, student should be able
to:
• The program associated with the interrupt is called the interrupt service
routine (ISR) or interrupt handler.
An Interrupt requires immediate attention, only once the microcontroller
will finish executing the interrupt code, then it can go back to continue
with the main program.
5
POLLING
The processor can also serve these events by polling method. But polling
is an inefficient technique as compared to interrupts.
In the polling method, the processor has to continuously wait for the
event signal. Thus it always remains busy using extra resources.
Although polling can monitor the status of several devices and serve
each of them as certain condition are met, this method wastes much of
the microcontroller’s time by polling devices that do not need service.
void main(void)
{
TRISB=0b00000111;
TRISD=0X00;
ADCON1=0XFF;
while (1)
{
if (SW1==0) // check if SW1 pressed
count++; POLLING
else if (SW2==0) //check if SW2 pressed
count--;
}
}
6
INTERRUPT vs POLLING
Interrupt Polling
Services based on request Monitors status of device and
from devices serves
Service based on priority. CPU No priority, round robin.
drops whatever it was doing When it sees a request, it
and serves the device and serves the device and then
then return back to its keeps polling.
original task. CPU is always ‘busy’ with
CPU is always ‘free’, when not polling doing the “while any
serving any interrupt request” loop
7
SOURCES OF INTERRUPT IN PIC18F4550
INTCON
INTCON2
INTCON3
PIR1, PIR2
PIE1, PIE2
IPR1, IPR2
RCON
INTCON REGISTER 10
External interrupts on the RB0, RB1 and RB2 pins are positive (rising) edge-
triggered.
If the corresponding INTEDGx bit in the INTCON2 register is set (= 1), the interrupt
is triggered by a rising edge;
if the bit is clear, the trigger is on the falling edge (negative edge-triggered).
Interrupt priority for INT1 and INT2 is determined by the value contained in the
interrupt priority bits, INT1IP (INTCON3) and INT2IP (INTCON3).
There is no priority bit associated with INT0. It is always a high priority interrupt
source.
ENABLING AND DISABLING 13
INTERRUPT
PROGRAMMING EXTERNAL HARDWARE 1
4
INTERRUPT
INTERRUPT
All three external hardware interrupts must be enabled
before they can take effect.
The following logic diagram shows the bits used to control its
operation.
STEP TO ENABLE EXTERNAL HARDWARE 16
INTERRUPT
3. Clear INTxIF
Solution:
while(1); //Wait
while(1); //Wait
}
Figure shows a push button switch is connected to the RB0/INT0 of PIC18F4550. This
switch produces the interrupt when pressed. Four red LEDs are connected to RC0
through RC3 port and an yellow LED is connected to port RA0. The main program is a
4-bit binary up counter that increments at a rate of approximately one second. The
value of the counter is sent to PORT-C and displayed on the four red LEDs. The
yellow LED is toggled on and off when the pushbutton switch is pressed. The switch
is active low and therefore, the interrupt is programmed on the falling edge of the
signal (INTEDGx = 0). The PIC runs at 4.0 MHz clock derived from the internal source.
Write C program.
REFERENCES 25