Professional Documents
Culture Documents
Interrupts
Lec note 8
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-1
Interrupts
Program Execution
mov a, #2
mov b, #16
mul ab
mov R0, a
mov R1, b
mov a, #12
mov b, #20
mul ab
add a, R0
mov R0, a
mov a, R1
addc a, b
mov R1, a
end
hsabaghianb @ kashanu.ac.ir
interrupt
ISR: inc r7
mov a,r7
jnz NEXT
cpl P1.6
NEXT: reti
return
Microprocessors 8-2
Interrupts Programming
An interrupt is an external or internal event that
interrupts the microcontroller to inform it that a
device needs its service.
Interrupts vs. Polling
A single microcontroller can serve several devices.
There are two ways to do that:
interrupts
polling.
Microprocessors 8-3
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-4
Interrupt Sources
Original 8051 has 6 sources of interrupts
Reset
Timer 0 overflow
Timer 1 overflow
External Interrupt 0
External Interrupt 1
Serial Port events (buffer full, buffer empty, etc)
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-5
Interrupt Vectors
Each interrupt has a specific place in code memory where
program execution (interrupt service routine) begins.
External Interrupt 0:
Timer 0 overflow:
External Interrupt 1:
Timer 1 overflow:
Serial :
Timer 2 overflow(8052+)
hsabaghianb @ kashanu.ac.ir
0003h
000Bh
0013h
001Bh
0023h
002bh
Microprocessors 8-6
main
03H
int0sr
0BH
t0sr
13H
int1sr
1BH
t1sr
23H
serialsr
30H
main:
END
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-7
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-8
EA
ET0
ET1
EX0
EX1
ES
SETB
SETB
SETB
SETB
SETB
IE.7
IE.1
IE.3
IE.0
IE.2
SETB
IE.4
;Enable
;Enable
;Enable
;Enable
;Enable
;Enable
All
Timer0 ovrf
Timer1 ovrf
INT0
INT1
Serial port
by mov instruction
Recommended in the first of program
MOV IE, #10010110B
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-9
Example
A 10khz square wave with 50% duty cycle
ORG
LJMP
0
MAIN
ORG 000BH
;Timer 0 interrupt vector
T0ISR:CPL
P1.0
;Toggle port bit
RETI
;Return from ISR to Main program
ORG 0030H
;Main Program entry point
MAIN: MOV
TMOD,#02H ;Timer 0, mode 2
MOV TH0,#-50 ;50 us delay
SETB
TR0
;Start timer
MOV IE,#82H
;Enable timer 0 interrupt
SJMP
$
;Do nothing just wait
END
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-10
Example
Write a program using interrupts to
simultaneously create 7 kHz and 500 Hz
square waves on P1.7 and P1.6.
8051
P1.7
143s
71s
2ms
P1.6
hsabaghianb @ kashanu.ac.ir
1ms
Microprocessors 8-11
ORG
LJMP
ORG
LJMP
ORG
LJMP
ORG
MAIN:
MOV
MOV
SETB
SETB
MOV
MOV
SJMP
T0ISR: CPL
RETI
T1ISR: CLR
MOV
MOV
SETB
CPL
RETI
END
0
MAIN
000BH
T0ISR
001BH
T1ISR
0030H
TMOD,#12H
TH0,#-71
TR0
TF1
IE,#8AH
IE,#8AH
$
P1.7
Solution
8051
P1.7
143s
71s
2ms
P1.6
1ms
TR1
TH1,#HIGH(-1000)
TL1,#LOW(-1000)
TR1
P1.6
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-12
Timer ISR
Notice that
There is no need for a CLR TFx instruction in
timer ISR
8051 clears the TF internally upon jumping to ISR
Notice that
We must reload timer in mode 1
There is no need on mode 2 (timer auto reload)
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-13
(MSB)
TF1 TR1
Timer 1
hsabaghianb @ kashanu.ac.ir
TF0 TR0
Timer0
IE1
IT1 IE0
for Interrupt
(LSB)
IT0
Microprocessors 8-14
External Interrupts
Level-triggered (default)
INT0
(Pin 3.2)
0003
IT0
1
2
IE0 (TCON.3)
Edge-triggered
Level-triggered (default)
INT0
(Pin 3.3)
0
1
2
0013
IT1
IE1 (TCON.3)
Edge-triggered
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-15
ORG 0000H
LJMP MAIN
WAIT:
;
ORG 0013H
SETB P1.1
MOV R0,200
DJNZ R0,WAIT
CLR P1.1
RETI
ORG 30H
MAIN: SETB IT1
;on negative edge of INT1
MOV IE,#10000100B
WAIT2: SJMP WAIT2
END
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-16
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-17
Skip:
end
Org 0030h
mov ie,#85h
Setb it0
Setb it1
Setb p1.7
Jb p3.2,skip
Clr p1.7
Sjmp $
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-18
Interrupt Priorities
What if two interrupt sources interrupt at the same
time?
The interrupt with the highest PRIORITY gets
serviced first.
All interrupts have a power on default priority order.
1. External interrupt 0 (INT0)
2. Timer interrupt0 (TF0)
3. External interrupt 1 (INT1)
4. Timer interrupt1 (TF1)
5. Serial communication (RI+TI)
Microprocessors 8-19
---
PT2
PS
PT1
PX1
PT0
PX0
IP.7: reserved
IP.6: reserved
IP.5: timer 2 interrupt priority bit(8052 only)
IP.4: serial port interrupt priority bit
IP.3: timer 1 interrupt priority bit
IP.2: external interrupt 1 priority bit
IP.1: timer 0 interrupt priority bit
IP.0: external interrupt 0 priority bit
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-20
---
MOV IP , #00000100B
1.
2.
3.
4.
5.
PT2
PS
PT1
PX1
PT0
PX0
Int1
Int0
Timer0
Timer1
Serial
Int1
Timer1
Int0
Timer0
Serial
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-21
---
PT2
PS
PT1
PX1
PT0
PX0
hsabaghianb @ kashanu.ac.ir
Microprocessors 8-22