The 8051 Microcontroller - Chapter 6: Interrupts - Lê Chí Thông
When an Interrupt occurs?
Ref. I. Scott Mackenzie Lê Chí Thông 6
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
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.
Bạn đang xem 20 trang mẫu của tài liệu "The 8051 Microcontroller - 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:
- the_8051_microcontroller_chapter_6_interrupts_le_chi_thong.pdf
Nội dung text: The 8051 Microcontroller - 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 chithong@hcmut.edu.vn sites.google.com/site/chithong Ref. I. Scott Mackenzie, The 8051 Microcontroller What Is an Interrupt? (Source: ) • 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 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 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 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
- Đ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 ;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 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 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 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 ;Reset LJMP MAIN 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 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 ;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 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 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 CTNGATTIMER0: LJMP CTCHINH CPL P1.1 ORG 000BH CLR TR0 LJMP CTNGATTIMER0 MOV TH0,#HIGH(-500) ORG 0030H MOV TL0,#LOW(-500) CTCHINH: SETB TR0 MOV TMOD,#01H RETI MOV TH0,#HIGH(-500) END MOV TL0,#LOW(-500) SETB TR0 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 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 $ 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 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 ORG 0000H T0ISR: CPL P1.7 LJMP MAIN RETI ORG 000BH T1ISR: CPL P1.6 LJMP T0ISR CLR TR1 ORG 001BH MOV TH1,#HIGH(-1000) LJMP T1ISR MOV TL1,#LOW(-1000) ORG 0030H SETB TR1 MAIN: MOV TMOD,#12H RETI MOV IE,#8AH END MOV TH0,#-71 SETB TR0 MOV TH1,#HIGH(-1000) MOV TL1,#LOW(-1000) SETB TR1 SJMP $ Ref. I. Scott Mackenzie Lê Chí Thông 41 Character Output Using Interrupts Write a program using interrupts to continually transmit the ASCII code set (excluding control codes) to a terminal attached to the 8051’s serial port (1200 baud, 12 MHz crystal). The ASCII codes consist of 95 graphic codes (20H to 7EH) and 33 control codes (00H to 1FH, and 7FH). ORG 0000H SPISR: CJNE A,#7FH,SKIP LJMP MAIN MOV A,#20H ORG 0023H SKIP: MOV SBUF,A LJMP SPISR INC A ORG 0030H CLR TI MAIN: MOV TMOD,#20H RETI MOV TH1,#-26 END SETB TR1 MOV SCON,#42H MOV IE,#90H MOV A,#20H SETB TI SJMP $ Ref. I. Scott Mackenzie Lê Chí Thông 42 sites.google.com/site/chithong 21
- ĐH Bách Khoa TP.HCM Lê Chí Thông Furnace Controller Using interrupts, design an 8051 furnace controller that keeps a building at 20 oC ± 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. /HOT = 0 if T > 21 oC /COLD = 0 if T 21 degrees, CLR P1.7 ; turn furnace off SKIP: SJMP $ ;do nothing END Ref. I. Scott Mackenzie Lê Chí Thông 46 sites.google.com/site/chithong 23
- ĐH Bách Khoa TP.HCM Lê Chí Thông Intrusion Warning System (2) Design an intrusion warning system using interrupts that sounds a 400 Hz tone for 50 ms (using loudspeaker connected to P1.7 ) whenever a door sensor connected /INT0 makes a high-to-low transition . P3.2 50 ms Ref. I. Scott Mackenzie Lê Chí Thông 49 Intrusion Warning System (2) Design an intrusion warning system using interrupts that sounds a 400 Hz tone for 50 ms (using loudspeaker connected to P1.7 ) whenever a door sensor connected /INT0 makes a high-to-low transition . ORG 0000H SETB ET0 LJMP MAIN SETB ET1 ORG 0003H RETI LJMP E0ISR T0ISR: CLR TR0 ORG 000BH CLR ET0 LJMP T0ISR CLR ET1 ORG 001BH RETI LJMP T1ISR T1ISR: CLR TR1 ORG 0030H MOV TH1,#HIGH(-1250) MAIN: SETB IT0 MOV TL1,#LOW(-1250) MOV TMOD,#11H CPL P1.7 MOV IE,#81H SETB TR1 SJMP $ RETI E0ISR: MOV TH0,#HIGH(-50000) END MOV TL0,#LOW(-50000) SETB TR0 Ref. I. ScottSETB Mackenzie TF1 Lê Chí Thông 50 sites.google.com/site/chithong 25
- ĐH Bách Khoa TP.HCM Lê Chí Thông References • I. Scott Mackenzie, The 8051 Microcontroller • Các tài li ệu trên Internet không trích d ẫn ho ặc không ghi tác gi ả Lê Chí Thông 53 sites.google.com/site/chithong 27