Bài giảng Microprocessing Systems - Chapter 6: Interrupts - Lê Chí Thông
ĐH Bách Khoa TP.HCM
Lê Chí Thông
The 8051 Microcontroller
Chapter 6
Interrupts
Lê Chí Thông
sites.google.com/site/chithong
Ref. I. Scott Mackenzie, The 8051 Microcontroller
What Is an Interrupt?
• An interrupt = Occurrence of a condition (an event)
• Deal with the event while another program is executing
• Do many things “simultaneously”
Ref. I. Scott Mackenzie
Lê Chí Thông
2
sites.google.com/site/chithong
1
Đ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 vs. Subroutine
• Similarity: CPU executes another program and then returns to
the original program.
• Difference: It is NOT known when the main program suspends
execution.
ISR_NAME:
SUBROUTINE_NAME:
…
…
…
…
RETI
RET
Ref. I. Scott Mackenzie
Lê Chí Thông
4
sites.google.com/site/chithong
2
Đ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)
Ref. I. Scott Mackenzie
Lê Chí Thông
5
When an Interrupt occurs?
An interrupt = Occurrence of a condition (an event)
• When a falling edge occur at /INT0 pin, External 0 Interrupt occurs.
• When a falling edge occur at /INT1 pin, External 1 Interrupt occurs.
• When Timer 0 is overflow, Timer 0 Interrupt occurs.
• When Timer 1 is overflow, Timer 1 Interrupt occurs.
• When the transmission is done (transmit buffer is empty) or the
reception is done (receiver buffer is full), Serial Port Interrupt
occurs.
Ref. I. Scott Mackenzie
Lê Chí Thông
6
sites.google.com/site/chithong
3
Đ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
Enabling and Disabling Interrupts
EA - ET2 ES ET1 EX1 ET0 EX0
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
1 = Enable; 0 = Disable
Ref. I. Scott Mackenzie
Lê Chí Thông
8
sites.google.com/site/chithong
4
ĐH Bách Khoa TP.HCM
Lê Chí Thông
Enabling and Disabling Interrupts
EA - ET2 ES ET1 EX1 ET0 EX0
IE (Interrupt Enable) Register
Eg. Timer 1 interrupt is enabled as follow:
SETB ET1
SETB EA
or
MOV IE,#10001000B
Eg. External 0 and serial interrupts are enabled as follow:
SETB EX0
SETB ES
SETB EA
or
MOV IE,#10010001B
Ref. I. Scott Mackenzie
Lê Chí Thông
9
Interrupt Priority
-
- PT2 PS PT1 PX1 PT0 PX0
IP (Interrupt Priority) Register
• PT2 : Priority for Timer 2 interrupt
• PS: Priority for Serial port interrupt
• PT1: Priority for Timer 1 interrupt
• PX1: Priority for External 1 interrupt
• PT0: Priority for Timer 0 interrupt
• PX0: Priority for External 0 interrupt
1 = Higher Level; 0 = Lower Level
Ref. I. Scott Mackenzie
Lê Chí Thông
10
sites.google.com/site/chithong
5
ĐH Bách Khoa TP.HCM
Lê Chí Thông
Interrupt Priority
-
- PT2 PS PT1 PX1 PT0 PX0
IP (Interrupt Priority) Register
• If 2 interrupts occur simultaneously
ꢀ a high-priority ISR executes
• If a low-priority ISR is executing when a high-priority interrupts
ꢀ the low-priority ISR is interrupted
ꢁ A high-priority interrupt can interrupt a low-priority ISR.
ꢁ A high-priority ISR cannot be interrupted.
• If 2 interrupts of the same priority occur simultaneously
ꢀ a fixed polling sequence determines which is serviced first
ꢁ The polling sequence is external 0, Timer 0, external 1, Timer
1, serial port, Timer 2.
Ref. I. Scott Mackenzie
Lê Chí Thông
11
Interrupt Flags
When an event occurs ꢀ the corresponding interrupt flag is set
ꢀ Interrupt occurs
• 5 Interrupt Sources: 2 external interrupt, 2 timer interrupt
and 1 serial port interrupt
• 6 Interrupt Flags: IE0, IE1, TF0, TF1, TI, and RI
Ref. I. Scott Mackenzie
Lê Chí Thông
12
sites.google.com/site/chithong
6
Đ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.
13
Ref. I. Scott Mackenzie
Lê Chí Thông
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
System reset
External 0
Timer 0
FLAG
RST
VECTOR ADDRESS
0000H
IE0
0003H
TF0
000BH
External 1
Timer 1
IE1
0013H
TF1
001BH
Serial port
Timer 2
RI or TI
TF 2 or EXF2
0023H
002BH
Ref. I. Scott Mackenzie
Lê Chí Thông
14
sites.google.com/site/chithong
7
Đ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)?
Ref. I. Scott Mackenzie
Lê Chí Thông
16
sites.google.com/site/chithong
8
Đ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.
Ref. I. Scott Mackenzie
Lê Chí Thông
17
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
SETB P1.7
RETI
;ISR External Int 0
ORG 0030H ;Main program
MAIN: SETB EA
SETB EX0 ;Enable Ext Int 0
SETB IT0 ;Falling edge
SJMP $
END
Ref. I. Scott Mackenzie
Lê Chí Thông
18
sites.google.com/site/chithong
9
Đ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.
Ref. I. Scott Mackenzie
Lê Chí Thông
19
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.
SETB IT0
SJMP $
DELAY10MS:
ORG 0000H
LJMP MAIN
ORG 0003H
MOV TH0,#(-10000)
SETB P1.7
MOV TL0,#(-10000)
ACALL DELAY10MS
SETB TR0
CLR P1.7
JNB TF0,$
RETI
CLR TF0
CLR TR0
END
ORG 0030H
MAIN:MOV TMOD,#01H
SETB EA
SETB EX0
Ref. I. Scott Mackenzie
Lê Chí Thông
20
sites.google.com/site/chithong
10
Đ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.
Ref. I. Scott Mackenzie
Lê Chí Thông
21
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
ĐH Bách Khoa TP.HCM
Lê Chí Thông
An Example of a Program Using Small ISR
ORG 0000H ;Reset
LJMP MAIN
ORG 000BH ;Interrupt vector of Timer 0
T0ISR:…
…
RETI
;Return to main program
MAIN: …
…
END
Ref. I. Scott Mackenzie
Lê Chí Thông
23
An Example of a Program Using Large ISR
ORG 0000H
LJMP MAIN
ORG 0003H
LJMP E0ISR
ORG 000BH
LJMP T0ISR
ORG 0013H
LJMP E1ISR
ORG 001BH
LJMP T1ISR
ORG 0023H
LJMP SPISR
ORG 0030H
…
;Reset
;Interrupt vector of External 0
;Interrupt vector of Timer 0
;Interrupt vector of External 1
;Interrupt vector of Timer 1
;Interrupt vector of serial port
MAIN:
SJMP $
E0ISR: …
RETI
T0ISR: …
RETI
…
Ref. I. Scott Mackenzie
Lê Chí Thông
24
END
sites.google.com/site/chithong
12
ĐH Bách Khoa TP.HCM
Lê Chí Thông
Memory Organization
ISR Vector table
3-byte instruction
Ref. I. Scott Mackenzie
Lê Chí Thông
25
Timer (8 bit) Interrupt
Write a program using Timer 0 and interrupt to create a 10 kHz square
wave on P1.0. (Crystal 12 MHz)
ORG 0000H
LJMP MAIN
;Reset
ORG 000BH
;Interrupt vector of Timer 0
T0ISR: CPL P1.0
RETI
ORG 0030H
MAIN: MOV TMOD,#02H
MOV TH0,#-50
SETB TR0
MOV IE,#82H
SJMP $
END
Ref. I. Scott Mackenzie
Lê Chí Thông
26
sites.google.com/site/chithong
13
Đ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)
Ref. I. Scott Mackenzie
Lê Chí Thông
27
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
LJMP MAIN
ORG 001BH
T0ISR: CPL P1.7
RETI
;Reset
;Interrupt vector of Timer 1
ORG 0030H
MAIN: MOV TMOD,#20H
MOV TH1,#-50
SETB TR1
SETB EA
SETB ET1
SJMP $
END
Ref. I. Scott Mackenzie
Lê Chí Thông
28
sites.google.com/site/chithong
14
Đ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.
Ref. I. Scott Mackenzie
Lê Chí Thông
29
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
MOV P1,A
;Interrupt vector of Timer 1
RETI
ORG 0030H
MAIN: MOV TMOD,#20H
MOV TH1,#-100
SETB TR1
SETB EA
SETB ET1
SJMP $
END
Ref. I. Scott Mackenzie
Lê Chí Thông
30
sites.google.com/site/chithong
15
Đ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.
Ref. I. Scott Mackenzie
Lê Chí Thông
31
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.
ORG 0000H
LJMP MAIN
PHAT: JNB TI,$
CLR TI
ORG 000BH
ACALL PHAT
RETI
MOV SBUF,A
RET
END
ORG 0030H
MAIN: MOV TMOD,#22H
MOV SCON,#52H
MOV TH1,#-26
SETB TR1
MOV TH0,#-250
SETB TR0
SETB EA
SETB ET0
SJMP $
Ref. I. Scott Mackenzie
Lê Chí Thông
32
sites.google.com/site/chithong
16
ĐH Bách Khoa TP.HCM
Lê Chí Thông
Timer (16 bit) Interrupt
Write a program using Timer 0 and interrupt to create a 1 kHz square
wave on P1.1. (Crystal 12 MHz)
Ref. I. Scott Mackenzie
Lê Chí Thông
33
Timer (16 bit) Interrupt
Write a program using Timer 0 and interrupt to create a 1 kHz square
wave on P1.1. (Crystal 12 MHz)
ORG 0000H
LJMP CTCHINH
ORG 000BH
CTNGATTIMER0:
CPL P1.1
CLR TR0
LJMP CTNGATTIMER0
ORG 0030H
CTCHINH:
MOV TH0,#HIGH(-500)
MOV TL0,#LOW(-500)
SETB TR0
MOV TMOD,#01H
MOV TH0,#HIGH(-500)
MOV TL0,#LOW(-500)
SETB TR0
RETI
END
SETB EA
SETB ET0
SJMP $
Ref. I. Scott Mackenzie
Lê Chí Thông
34
sites.google.com/site/chithong
17
ĐH Bách Khoa TP.HCM
Lê Chí Thông
Timer (16 bit) Interrupt
Write a program using Timer 0 and interrupt to create a 1 kHz square
wave on P1.1. (Crystal 12 MHz)
ORG 0000H
LJMP CTCHINH
ORG 000BH
CTNGATTIMER0:
CPL P1.1
CLR TR0
LJMP CTNGATTIMER0
ORG 0030H
CTCHINH:
MOV TH0,#HIGH(-500)
MOV TL0,#LOW(-500)
SETB TR0
MOV TMOD,#01H
SETB EA
RETI
END
SETB ET0
SETB TF0 ; Ép ngắt
SJMP $
Ref. I. Scott Mackenzie
Lê Chí Thông
35
Practice Problem 1
Write a program using Timer 1 and interrupt to create a 100 Hz
square wave on P1.2. (Crystal 24MHz)
Ref. I. Scott Mackenzie
Lê Chí Thông
36
sites.google.com/site/chithong
18
Đ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
LJMP CTCHINH
ORG 001BH
CTNGATTIMER1:
CPL P1.2
CLR TR1
LJMP CTNGATTIMER1
ORG 0030H
MOV TH1,#HIGH(-10000)
MOV TL1,#LOW(-10000)
CTCHINH:
SETB TR1
RETI
END
MOV TMOD,#10H
SETB EA
SETB ET1
SETB TF1 ; Ép ngắt
SJMP $
Ref. I. Scott Mackenzie
Lê Chí Thông
37
Practice Problem 2
Write a program using Timer 1 and interrupt to complement P1.2
every 1 sec. (Crystal 12 MHz)
Ref. I. Scott Mackenzie
Lê Chí Thông
38
sites.google.com/site/chithong
19
Đ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
LJMP CTCHINH
ORG 001BH
CTNGATTIMER1:
CPL P1.2
CLR TR1
LJMP CTNGATTIMER1
ORG 0030H
MOV TH1,#HIGH(-10000)
MOV TL1,#LOW(-10000)
CTCHINH:
SETB TR1
RETI
END
MOV TMOD,#10H
SETB EA
SETB ET1
SETB TF1 ; Ép ngắt
SJMP $
Ref. I. Scott Mackenzie
Lê Chí Thông
39
Two Square Waves Using Timer Interrupts
Write a program using interrupts to create 7 kHz and 500 Hz square
waves on P1.7 and P1.6. (Crystal 12 MHz)
Ref. I. Scott Mackenzie
Lê Chí Thông
40
sites.google.com/site/chithong
20
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Microprocessing Systems - Chapter 6: Interrupts - Lê Chí Thông", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_microprocessing_systems_chapter_6_interrupts_le_ch.pdf

