Chapter 6 Interrupt Programming
Chapter 6 Interrupt Programming
Interrupt Programming
Why Interrupt?
A computer is much more than the CPU
Keyboard, mouse, screen, disk drives,
scanner, printer, sound card, camera, etc.
Disadvantage of Polling
Polling is like picking up your phone
every few seconds to see if you have a
call.
You may get a few phone calls a day
99% of your effort will be wasted
checking the phone.
Takes CPU time even when no request
pending
3
TRISB, 5
PORTB, 5
B 00001000
T0CON
0xFF
TMR0H
0xF2
TMR0L
INTCON, TMR0IF
T0CON, TMR0ON
INTCON, TMR0IF
Again
PORTB, 5
T0CON, TMR0ON
Here
Interrupt
Whenever a device needs the CPUs
service, the device notifies it by sending
an interrupt signal.
CPU then stops and serves the device.
Polling is like picking up your phone
every few seconds to see if you have a
call. Interrupts are like waiting for the
phone to ring.
5
PIC18 Interrupt
PIC18 Microcontroller family
Has multiple sources that can send
interrupt requests that can be classified
into:
Internal Events
External Events
External sources
Three pins of PORTB -RB0/INT0, RB1/INT1,and
RB2/INT2
These pins can be used to connect external
interrupting sources such as keyboards or
switches
Can select whether a rising or falling edge triggers
an interrupt.
10
Interrupt Priorities
The interrupt priority feature is enabled
by Bit7 (IPEN) in RCON register.
2 Cases:
IPEN = 0: Interrupt priority is disabled.
IPEN = 1: Interrupt priority is enabled.
Interrupts are associated with high and low
priorities.
A high-priority interrupt can interrupt a lowpriority interrupt in progress.
11
retfie
16
17
18
19
Main:
ORG 0x0000
goto Main
ORG 0x0008
goto T0_ISR
ORG 0x0100
bcf TRISB, 5
bcf PORTB, 5
movlw 0x08; Timer0, 16-bit, no prescale, internal ck
movwf T0CON
movlw 0xFF
movwf TMR0H
movlw 0xF2
movwf TMR0L; load FFF2 to TMR0
bcf
RCON, IPEN; Disable Interrupt Priorities
bcf
INTCON, TMR0IF; Clear interrupt flag bit
bsf
INTCON, TMR0IE; enable Timer0 interrupt
bsf
INTCON, GIE; enable global interrupt
bsf
T0CON, TMR0ON; start Timer0
20
bra
$
EXIT:
ORG 0X0200
btfss INTCON, TMR0IF
bra
EXIT
bcf
INTCON, TMR0IF
btg
PORTB, 5; toggle PortB.5 to create sq. wave
movlw 0xFF
movwf TMR0H
movlw 0xF2
movwf TMR0L; Reinitialize TMR0 to FFF2
retfie
END
21
Example: Observations
We avoided using the memory space allocated
to the interrupt vector table.
When awakened at address 0x0000, PIC18
executed the goto instruction, which redirects the
controller away from the interrupt vector table.
In Main, we enabled:
Timer0 interrupt: bsf INTCON, TMR0IE
Global interrupt: bsf INTCON, GIE
Example: Observations
When TMR0IF flag is raised, the MCU gets out
of the infinite loop in Main and goes to 0x0008
to execute the ISR associated with Timer0.
At this time, GIE bit is cleared indicating that the
MCU is currently serving an interrupt and
cannot be interrupted again.
The ISR for Timer0 is located starting at
memory location 0x0200 because it is too large
to fit into address space 0x08-17, the address
allocated to high-priority interrupts.
Upon execution of retfie, the program gets back
to Main and GIE is enabled to indicate that it
23
can now accept new interrupts.
24
25
T0_ISR:
EXIT:
RETFIE
END
26
Example: Observation
To activate a low-priority interrupt, we
need to enable three bits:
Global High-Priority Interrupt Enable (GIEH)
bit (bsf
INTCON, GIEH)
Global Low-Priority Interrupt Enable (GIEL)
bit (bsf
INTCON, GIEL)
The enable bit of the interrupt source (bsf
INTCON, TMR1IE)
Use of Interrupt: