Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

8051 Interrupts

1
Learning Objective
 What is an Interrupt?
 Types of 8051 Interrupts
 Enabling and Disabling Interrupts
 Interrupt Priority
 Writing the ISR (Interrupt Service Routine)
 8051 Program using an Interrupt

2
Interrupts

3
Interrupts

mov a, #2
mov b, #16
Program Execution

mul ab
mov R0,a
mov R1,b interrupt
mov a, #12 ISR: orl P1MDIN, #40h
mov b, #20
mul ab orl P1MDOUT,#40h
add a, R0 setb P1.6
mov R0,a here: sjmp here
mov a, R1 cpl P1.6
addc a, b reti
mov R1, a return
end

4
Types of 8051 Interrupts

Interrupts

External Internal

Serial
INT 0 INT 1 Timer
Communication

Timer 0

Timer 1

5
Interrupt Vectors
Interrupt Vector Address
System Reset 0000H
External Interrupt 0 0003H
Timer 0 000BH
External Interrupt 1 0013H
Timer 1 001BH
Serial Port 0023H
Timer 2 002BH
6
IE (Interrupt Enable) Register

 EA : Global enable/disable.
 --- : Undefined.
 ET2 :Enable Timer 2 interrupt.
 ES :Enable Serial port interrupt.
 ET1 :Enable Timer 1 interrupt.
 EX1 :Enable External 1 interrupt.
 ET0 :Enable Timer 0 interrupt.
 EX0 :Enable External 0 interrupt.

7
Enabling and disabling an interrupt
 Using bit operation
 In the middle of program
SETB EA SETB IE.7 ;Enable All
SETB ET0 SETB IE.1 ;Enable Timer0 ovrf
SETB ET1 SETB IE.3 ;Enable Timer1 ovrf
SETB EX0 SETB IE.0 ;Enable INT0
SETB EX1 SETB IE.2 ;Enable INT1
SETB ES SETB IE.4 ;Enable Serial port

 Using MOV instruction


 In the beginning of program
MOV IE, #10010110B
 Use CLR instruction to disable interrupt

8
Example
 Show the instructions to
 (a) enable the serial interrupt, Timer 0 interrupt, and external
hardware interrupt 1 (EX1),
 (b) disable (mask) the Timer 0 interrupt,
 (c) show how to disable all the interrupts with a single
instruction.

 Solution
 (A) MOV IE, #10010110B
 (B) CLR IE.1
 (C) CLR IE.7

9
Timer Interrupt
 Roll-over timer flag and interrupt
 if the timer interrupt is enabled, whenever TF=1, the
microcontroller is interrupted in whatever it is doing, and
jumps to the interrupt vector table to service the ISR
 In this way, the microcontroller can do other things until
it is notified that the timer has rolled over

10
Timer Example
 Write a program that continuously gets 8-
bit data from P0 and sends it to P1 while
simultaneously creating a square wave of
200 us period on pin P2.1. Use Timer 0 to
create the square wave. Assume that
XTAL = 11.0592 MHz.
 Re write above example to create a
square wave that has a high portion of
1085us and a low portion of 15us.

11
Solution
ORG 0 ;Reset entry point
SJMP MAIN ;Jump to main program
ORG 000BH ;Timer 0 interrupt vector
T0ISR: CPL P2.1 ;Toggle P2.1 pin
RETI ;Return from ISR to Main program
ORG 0030H ;Main Program entry point
MAIN: MOV TMOD,#02H ;Timer 0, mode 2
MOV P0,#0FF ;Make P0 an input port
MOV TH0,#-92 ;For 100us delay
MOV IE,#82H ;Enable timer 0 interrupt
SETB TR0 ;Start timer

BACK: MOV A,P0 ;Get data from P0


MOV P1,A ;Send data to P1
SJMP BACK
END

12
Solution: As 1085us is 1000x1.085us we need to use mode 1 of
timer 1.
ORG 0 ;Reset entry point
SJMP MAIN ;Jump to main program
ORG 001BH ;Timer 1 interrupt vector
SJMP ISR_TI
ORG 0030H ;Main Program entry point
MAIN: MOV TMOD,#10H ;Timer 1, mode 1
MOV P0,#0FF ;Make P0 an input port
MOV TL1,#18H ;Low byte of -1000
MOV TH1,#0FCH ;High byte of -1000
MOV IE,#88H ;Enable timer 1 interrupt
SETB TR1 ;Start timer
BACK: MOV A,P0 ;Get data from P0
MOV P1,A ;Send data to P1
SJMP BACK
;Timer 1 ISR
ISR_T1: CLR TR1 ;stop timer 1
CLR P2.1 ;Start low portion of waveform
MOV R2,#04H ;2MC
HERE DJNZ R2,HERE ;4x2 machine cycle 8MC
MOV TL1,#18H ;Reload Low byte of T1 2MC
MOV TH1,#0FCH ;Reload High byte of T1 2MC
SETB TR1 ;start timer 1 1MC
SETB P2.1 ;High portion of waveform
RETI ;Return from ISR to Main program
END

13
Example: 10khz square wave with 50% duty cycle
using timer and Interrupt
ORG 0 ;Reset entry poit
SJMP MAIN ;Jump above interrupt

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
HERE: SJMP HERE ;Do nothing just wait
END

14
Example: Two timers
 Write a program using interrupts to
simultaneously create 7 kHz and 500 Hz
square waves on P1.7 and P1.6

8051 143s
P1.7 71s

2ms
P1.6
1ms

15
ORG 0
SJMP MAIN

ORG 000BH
SJMP T0ISR T0ISR: CPL P1.7
RETI
ORG 001BH
SJMP T1ISR T1ISR: CLR TR1
MOV TH1,#HIGH(-1000)
ORG 0030H MOV TL1,#LOW(-1000)
MAIN: MOV TMOD,#12H SETB TR1
MOV TH0,#-71 CPL P1.6
SETB TR0 RETI
SETB TF1 END
MOV IE,#8AH
HERE: SJMP HERE

Note:
There is no need for a “CLR TFx” instruction in timer ISR as 8051 clears
the TF internally upon jumping to ISR
We must reload timer in mode 1, there is no need on mode 2 (timer
auto reload)

16
External Interrupt
 External Interrupts INT0 & INT1 can each be
either level triggered or edge triggered.
 Set IE0 and IE1 flag respectively
 Can be configured as
 rising edge-triggered
 falling edge-triggered
 When they are edge triggered controller clears
IE0 / IE1 flag in the response
 When they are level triggered, then interrupt
enable flag is cleared at high level of interrupt
signal.

17
External interrupt type control
(MSB) Using TCON Register (LSB)
IE1 IT1 IE0 IT0
for Interrupt
 IE0 / IE1: External interrupt 0/1 edge flag.
 Set by CPU when external interrupt edge (H-to-L) is detected.
 Does not affected by H-to-L while ISR is executed (no int on int)
 Cleared by CPU when RETI executed.

 IT0 / IT1: External interrupt 0/1 type flag.


 Set/cleared by software
 IT=1 edge trigger
 IT=0 level trigger

18
External Interrupts

19
Sampling low level triggered interrupt

 To ensure the activation of the hardware


interrupt at the INTx pin, make sure that
the duration of the low-level signal is
around 4 machine cycles

20
Sampling edge triggered interrupt
 External source must be held high for at
least one machine cycle, and then held
low for at least one machine cycle to
ensure that the transition is seen by the
microcontroller

21
Example: External Interrupt
 Assume that INT1 pin is connected to a switch that is
normally high. Whenever it goes low, it should turn on an
LED. The LED is connected to P1.3 and is normally off.
When LED is turned on it should stay on for a fraction of a
second. As long as the switch is pressed low, the LED
should stay on.
Solution
ORG 0000H
SJMP MAIN
;ISR for h/w external interrupt INT1
ORG 0013H ;ISR for INT1
SETB P1.3 ;Turn on LED
MOV R0,200 ;Keep LED on for a while
WAIT: DJNZR0,WAIT
CLR P1.3 ;Turn off LED
RETI
;main program for initialization

ORG 30H
MAIN: SETBIT1 ;on negative edge of INT1
MOV IE,#10000100B
WAIT2: SJMP WAIT2
END

23
 Assuming that INT1 is connected to a
pulse generator. Write a program in which
the falling edge of the pulse will send a
high to P1.3, which is connected to an
LED.

24
Solution
ORG 0000H
SJMP MAIN
;ISR for h/w external interrupt INT1
ORG 0013H ;ISR for INT1
SETB P1.3 ;Turn on LED
MOV R0,255 ;Keep LED on for a while
WAIT: DJNZR0,WAIT
CLR P1.3 ;Turn off LED
RETI
;main program for initialization

ORG 30H
MAIN: SETB TCON.2 ;Make INT1 edge-triggered INT
MOV IE,#10000100B;Enable INT1
WAIT2: SJMP WAIT2
END

25
Example of external interrupt

26
Program for furnace
Org 0000h
Sjmp main
Org 0003h
x0isr: clr p1.7
Reti
Org 0013h
x1isr: setb p1.7
Reti
Org 0030h
Main: mov ie,#85h
Setb it0
Setb it1
Setb p1.7
Jb p3.2,skip
Clr p1.7
Skip: Sjmp SKIP
end

27
Interrupt Priority
 External Interrupt 0
 Timer 0
 External Interrupt 1
 Timer 1
 Serial Communication
 Priority can also be set to “high” or “low”
by IP (Interrupt Priority) reg.

28
Interrupt Priority (IP) Register
--- --- 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

29
Interrupt Priorities Example
 MOV IP,#00000100B or SETB IP.2 gives
priority order
1. Int1
2. Int0
3. Timer0
4. Timer1
5. Serial
 MOV IP,#00001100B gives priority order
1. Int1
2. Timer1
3. Int0
4. Timer0
5. Serial

30
Interrupt inside an interrupt
 A high-priority interrupt can interrupt a
low-priority interrupt
 All interrupt are latched internally
 Low-priority interrupt wait until 8051 has
finished servicing the high-priority
interrupt

31
Solve More examples
of
Interrupting…

32

You might also like