Professional Documents
Culture Documents
Vi Xu Ly Le Chi Thong 8051 Chap6 Interrupts (Cuuduongthancong - Com)
Vi Xu Ly Le Chi Thong 8051 Chap6 Interrupts (Cuuduongthancong - Com)
Lê Chí Thông
chithong@hcmut.edu.vn
sites.google.com/site/chithong
Ref. I. Scott Mackenzie, The 8051 Microcontroller
What Is an Interrupt?
(Source: http://www.6502.org/tutorials/interrupts.html)
sites.google.com/site/chithong 1
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Interrupts
Hardware
Event
When an interrupt occurs, the main program temporarily suspends
execution and branches to the interrupt service routine (ISR),
perform the operation, and terminates with a “return from
interrupt” instruction (RETI).
Ref. I. Scott Mackenzie Lê Chí Thông 3
ISR_NAME: SUBROUTINE_NAME:
… …
… …
RETI RET
sites.google.com/site/chithong 2
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Interrupt Sources
• 2 External Interrupts
• 2 Timer Interrupts
• 1 Serial Port Interrupt
• Timer 2 Interrupt (8052 only)
sites.google.com/site/chithong 3
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Interrupt Sources
2 external interrupts (/INT0 and /INT1), 2 timer interrupts (TF0 and TF1), a
serial port interrupt (RI or TI), and Timer 2 interrupt (8052 only)
Ref. I. Scott Mackenzie Lê Chí Thông 7
• 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
1 = Enable; 0 = Disable
Ref. I. Scott Mackenzie Lê Chí Thông 8
sites.google.com/site/chithong 4
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Interrupt Priority
- - PT2 PS PT1 PX1 PT0 PX0
sites.google.com/site/chithong 5
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Interrupt Priority
- - PT2 PS PT1 PX1 PT0 PX0
Interrupt Flags
When an event occurs the corresponding interrupt flag is set
Interrupt occurs
sites.google.com/site/chithong 6
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Interrupt Flags
When an event occurs the corresponding interrupt flag is set
Interrupt occurs
• When a falling edge occur at /INT0 pin the IE0 flag is set
External 0 Interrupt occurs
• When a falling edge occur at /INT1 pin the IE1 flag is set
External 1 Interrupt occurs
• When Timer 0 is overflow the IE0 flag is set Timer 0
Interrupt occurs
• When Timer 1 is overflow the IE1 flag is set Timer 1
Interrupt occurs
• When the transmission is done (transmit buffer is empty) or the
reception is done (receiver buffer is full) the TI or RI flag is set
Serial Port Interrupt occurs.
Ref. I. Scott Mackenzie Lê Chí Thông 13
Interrupt Vectors
• When an interrupt is accepted, the value loaded into PC is
called interrupt vector. It is the address of the start of the
ISR.
INTERRUPT FLAG VECTOR ADDRESS
System reset RST 0000H
External 0 IE0 0003H
Timer 0 TF0 000BH
External 1 IE1 0013H
Timer 1 TF1 001BH
Serial port RI or TI 0023H
Timer 2 TF 2 or EXF2 002BH
Ref. I. Scott Mackenzie Lê Chí Thông 14
sites.google.com/site/chithong 7
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Processing Interrupts
• When an interrupt (an event) occurs:
The corresponding interrupt flag is set
The current instruction completes execution.
The PC is saved on the stack.
The PC is loaded with the interrupt vector, which is the
address of the start of the ISR.
The interrupt flag is automatically cleared, except RI &TI (and
TF2 & EXF2 for 8052)
The ISR executes and takes action in response to the interrupt.
• The ISR finishes with a RETI (return from interrupt) instruction.
This retrieves the old value of the PC from the stack and
execution of the main program continues.
Ref. I. Scott Mackenzie Lê Chí Thông 15
An Example
• Assume that External 0 interrupt was enabled.
• When a falling edge (or level 0) is applied to pin P3.2 (/INT0) IE0 is set
(automatically by hardware)
• The current PC is saved on the stack
• PC 0003H (and the main program is interrupted)
• The instruction at address 0003H (i.e. the first instruction of the ISR for
External 0) executes.
• When the ISR is done, the RETI instruction retrieves the old value of the PC
from the stack and the main program continues.
• Question: What will happen if there is NO falling edge (or level 0) applied to
pin P3.2 (/INT0) (i.e. NO interrupt signal occur) but bit IE0 is set by software
(i.e. by using SETB IE0)?
sites.google.com/site/chithong 8
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
External Interrupt
A push-button is connected to pin P3.2 (/INT0). A falling edge is created
when pushing this button. Write a program that sets pin P1.7 when pushing
the button down.
External Interrupt
A push-button is connected to pin P3.2 (/INT0). A falling edge is created
when pushing this button. Write a program that sets pin P1.7 when pushing
the button down.
ORG 0000H
LJMP MAIN
ORG 0003H ;ISR External Int 0
SETB P1.7
RETI
ORG 0030H ;Main program
MAIN: SETB EA
SETB EX0 ;Enable Ext Int 0
SETB IT0 ;Falling edge
SJMP $
END
sites.google.com/site/chithong 9
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Practice Problem 1
A push-button is connected to pin P3.2 (/INT0). A falling edge is created
when pushing this button. Write a program that creates one 10-ms pulse at
pin P1.7 when pushing the button down.
Practice Problem 1
A push-button is connected to pin P3.2 (/INT0). A falling edge is created
when pushing this button. Write a program that creates one 10-ms pulse at
pin P1.7 when pushing the button down.
sites.google.com/site/chithong 10
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Practice Problem 2
A push-button is connected to pin P3.3 (/INT1). A falling edge is created
when pushing this button. Write a program that increases the content of
location 40H of internal RAM when pushing the button down.
Practice Problem 2
An push-button is connected to pin P3.3 (/INT1). A falling edge is created
when pushing this button. Write a program that increases the content of
location 40H of internal RAM.
ORG 0000H
LJMP MAIN
ORG 0013H ;Int vector of external int 1
INC 40H
RETI
ORG 0030H
MAIN:SETB EA
SETB EX1 ;Enable ext int 1
SETB IT1 ;Falling edge
SJMP $
END
Ref. I. Scott Mackenzie Lê Chí Thông 22
sites.google.com/site/chithong 11
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 12
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Memory Organization
3-byte instruction
sites.google.com/site/chithong 13
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Practice Problem 1
Write a program using Timer 1 and interrupt to create a 20 kHz square
wave on P1.7. (Crystal 24 MHz)
Practice Problem 1
Write a program using Timer 1 and interrupt to create a 20 kHz square
wave on P1.7. (Crystal 24 MHz)
ORG 0000H ;Reset
LJMP MAIN
ORG 001BH ;Interrupt vector of Timer 1
T0ISR: CPL P1.7
RETI
ORG 0030H
MAIN: MOV TMOD,#20H
MOV TH1,#-50
SETB TR1
SETB EA
SETB ET1
SJMP $
END
sites.google.com/site/chithong 14
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Practice Problem 2
Write a program using Timer 1 and interrupt to output data from A to
Port 1 every 100 us. Use crystal 12 MHz.
Practice Problem 2
Write a program using Timer 1 and interrupt to output data from A to
Port 1 every 100 us. Use crystal 12 MHz.
ORG 0000H
LJMP MAIN
ORG 001BH ;Interrupt vector of Timer 1
MOV P1,A
RETI
ORG 0030H
MAIN: MOV TMOD,#20H
MOV TH1,#-100
SETB TR1
SETB EA
SETB ET1
SJMP $
END
sites.google.com/site/chithong 15
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Practice Problem 3
Write a program using Timer and interrupt to send data from A to
serial port (UART 8 bit, 1200 baud) every 250 us. Use crystal 12 MHz.
Practice Problem 3
Write a program using Timer and interrupt to send data from A to
serial port (UART 8 bit, 1200 baud) every 250 us. Use crystal 12 MHz.
sites.google.com/site/chithong 16
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 17
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Practice Problem 1
Write a program using Timer 1 and interrupt to create a 100 Hz
square wave on P1.2. (Crystal 24MHz)
sites.google.com/site/chithong 18
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Practice Problem 1
Write a program using Timer 1 and interrupt to create a 100 Hz
square wave on P1.2. (Crystal 24MHz)
ORG 0000H CTNGATTIMER1:
LJMP CTCHINH CPL P1.2
ORG 001BH CLR TR1
LJMP CTNGATTIMER1 MOV TH1,#HIGH(-10000)
ORG 0030H MOV TL1,#LOW(-10000)
CTCHINH: SETB TR1
MOV TMOD,#10H RETI
SETB EA END
SETB ET1
SETB TF1 ; Ép ngắt
SJMP $
Practice Problem 2
Write a program using Timer 1 and interrupt to complement P1.2
every 1 sec. (Crystal 12 MHz)
sites.google.com/site/chithong 19
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Practice Problem 2
Write a program using Timer 1 and interrupt to complement P1.2
every 1 sec. (Crystal 12 MHz)
ORG 0000H CTNGATTIMER1:
LJMP CTCHINH CPL P1.2
ORG 001BH CLR TR1
LJMP CTNGATTIMER1 MOV TH1,#HIGH(-10000)
ORG 0030H MOV TL1,#LOW(-10000)
CTCHINH: SETB TR1
MOV TMOD,#10H RETI
SETB EA END
SETB ET1
SETB TF1 ; Ép ngắt
SJMP $
sites.google.com/site/chithong 20
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 21
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 22
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
Furnace Controller
Using interrupts, design an 8051 furnace controller that keeps a building at 20oC ± 1oC.
Temperature sensors are connected to /INT0 and /INT1 and provide /HOT and /COLD
signals. The furnace ON/OFF solenoid is connected to P1.7.
Furnace Controller
Using interrupts, design an 8051 furnace controller that keeps a building at 20oC ± 1oC.
ORG 0000H
LJMP MAIN
ORG 0003H
E0ISR: CLR P1.7 ;turn furnace off
RETI
ORG 0013H
E1ISR: SETB P1.7 ;turn furnace on
RETI
ORG 0030H
MAIN: MOV IE,#85H ;enable external 0 & 1 interrupts
SETB IT0 ;negative edge triggered for external 0
SETB IT1 ;negative edge triggered for external 1
SETB P1.7 ;turn furnace on
JB P3.2,SKIP ;if T > 21 degrees,
CLR P1.7 ; turn furnace off
SKIP: SJMP $ ;do nothing
END
sites.google.com/site/chithong 23
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
P3.2
sites.google.com/site/chithong 24
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
P3.2
50 ms
sites.google.com/site/chithong 25
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
P3.2
sites.google.com/site/chithong 26
CuuDuongThanCong.com https://fb.com/tailieudientucntt
ĐH Bách Khoa TP.HCM Lê Chí Thông
References
Lê Chí Thông 53
sites.google.com/site/chithong 27
CuuDuongThanCong.com https://fb.com/tailieudientucntt