1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

8051 chap6 interrupts VI XỬ LÝ

27 1 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

ĐH Bách Khoa TP.HCM Lê Chí Thơng The 8051 Microcontroller Chapter 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: http://www.6502.org/tutorials/interrupts.html) • 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 sites.google.com/site/chithong Lê Chí Thơng Đ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 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: … … RETI Ref I Scott Mackenzie sites.google.com/site/chithong SUBROUTINE_NAME: … … RET Lê Chí Thơng ĐH Bách Khoa TP.HCM Lê Chí Thơng Interrupt Sources • External Interrupts • Timer Interrupts • Serial Port Interrupt • Timer Interrupt (8052 only) Ref I Scott Mackenzie Lê Chí Thơng When an Interrupt occurs? An interrupt = Occurrence of a condition (an event) • • • • • When a falling edge occur at /INT0 pin, External Interrupt occurs When a falling edge occur at /INT1 pin, External Interrupt occurs When Timer is overflow, Timer Interrupt occurs When Timer is overflow, Timer 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 sites.google.com/site/chithong Lê Chí Thơng ĐH Bách Khoa TP.HCM Lê Chí Thơng Interrupt Sources external interrupts (/INT0 and /INT1), timer interrupts (TF0 and TF1), a serial port interrupt (RI or TI), and Timer interrupt (8052 only) Ref I Scott Mackenzie Lê Chí Thơng Enabling and Disabling Interrupts EA - ET2 ES ET1 EX1 ET0 EX0 IE (Interrupt Enable) Register • • • • • • • • EA : Global enable/disable - : Undefined ET2: Enable Timer interrupt ES: Enable Serial port interrupt ET1: Enable Timer interrupt EX1: Enable External interrupt ET0: Enable Timer interrupt EX0: Enable External interrupt = Enable; = Disable Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng Đ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 interrupt is enabled as follow: SETB ET1 SETB EA or MOV IE,#10001000B Eg External 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 Interrupt Priority - - PT2 PS PT1 PX1 PT0 PX0 IP (Interrupt Priority) Register • • • • • • PT2 : Priority for Timer interrupt PS: Priority for Serial port interrupt PT1: Priority for Timer interrupt PX1: Priority for External interrupt PT0: Priority for Timer interrupt PX0: Priority for External interrupt = Higher Level; = Lower Level Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 10 ĐH Bách Khoa TP.HCM Lê Chí Thơng Interrupt Priority - - PT2 PS PT1 PX1 PT0 PX0 IP (Interrupt Priority) Register • If 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 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 Ref I Scott Mackenzie Lê Chí Thơng 11 Interrupt Flags When an event occurs  the corresponding interrupt flag is set  Interrupt occurs • Interrupt Sources: external interrupt, timer interrupt and serial port interrupt • Interrupt Flags: IE0, IE1, TF0, TF1, TI, and RI Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 12 Đ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 Interrupt occurs • When a falling edge occur at /INT1 pin  the IE1 flag is set  External Interrupt occurs • When Timer is overflow  the IE0 flag is set  Timer Interrupt occurs • When Timer is overflow  the IE1 flag is set  Timer 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 System reset External FLAG RST IE0 Timer External Timer TF0 IE1 TF1 000BH 0013H 001BH Serial port Timer RI or TI TF or EXF2 0023H 002BH Ref I Scott Mackenzie sites.google.com/site/chithong VECTOR ADDRESS 0000H 0003H Lê Chí Thơng 14 Đ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 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 sites.google.com/site/chithong Lê Chí Thơng 16 Đ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 SETB P1.7 RETI ORG 0030H ;Main program MAIN: SETB EA SETB EX0 ;Enable Ext Int SETB IT0 ;Falling edge SJMP $ END Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 18 ĐH Bách Khoa TP.HCM Lê Chí Thơng Practice Problem 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 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 ORG 0000H LJMP MAIN ORG 0003H SETB P1.7 ACALL DELAY10MS CLR P1.7 RETI ORG 0030H MAIN:MOV TMOD,#01H SETB EA SETB EX0 Ref I Scott Mackenzie sites.google.com/site/chithong SETB IT0 SJMP $ DELAY10MS: MOV TH0,#(-10000) MOV TL0,#(-10000) SETB TR0 JNB TF0,$ CLR TF0 CLR TR0 END Lê Chí Thơng 20 10 Đ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 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 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 sites.google.com/site/chithong Lê Chí Thơng 26 13 ĐH Bách Khoa TP.HCM Lê Chí Thông Practice Problem Write a program using Timer 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 Write a program using Timer 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 T0ISR: CPL P1.7 RETI ORG 0030H MAIN: MOV TMOD,#20H MOV TH1,#-50 SETB TR1 SETB EA SETB ET1 SJMP $ END Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 28 14 ĐH Bách Khoa TP.HCM Lê Chí Thơng Practice Problem Write a program using Timer and interrupt to output data from A to Port every 100 us Use crystal 12 MHz Ref I Scott Mackenzie Lê Chí Thơng 29 Practice Problem Write a program using Timer and interrupt to output data from A to Port every 100 us Use crystal 12 MHz ORG 0000H LJMP MAIN ORG 001BH ;Interrupt vector of Timer 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 sites.google.com/site/chithong Lê Chí Thơng 30 15 ĐH Bách Khoa TP.HCM Lê Chí Thông Practice Problem Write a program using Timer and interrupt to send data from A to serial port (UART bit, 1200 baud) every 250 us Use crystal 12 MHz Ref I Scott Mackenzie Lê Chí Thơng 31 Practice Problem Write a program using Timer and interrupt to send data from A to serial port (UART bit, 1200 baud) every 250 us Use crystal 12 MHz ORG 0000H LJMP MAIN ORG 000BH ACALL PHAT RETI 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 sites.google.com/site/chithong PHAT: Lê Chí Thơng JNB TI,$ CLR TI MOV SBUF,A RET END 32 16 ĐH Bách Khoa TP.HCM Lê Chí Thơng Timer (16 bit) Interrupt Write a program using Timer and interrupt to create a 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 and interrupt to create a kHz square wave on P1.1 (Crystal 12 MHz) ORG 0000H LJMP CTCHINH ORG 000BH LJMP CTNGATTIMER0 ORG 0030H CTCHINH: MOV TMOD,#01H MOV TH0,#HIGH(-500) MOV TL0,#LOW(-500) SETB TR0 SETB EA SETB ET0 SJMP $ Ref I Scott Mackenzie sites.google.com/site/chithong CTNGATTIMER0: CPL P1.1 CLR TR0 MOV TH0,#HIGH(-500) MOV TL0,#LOW(-500) SETB TR0 RETI END Lê Chí Thơng 34 17 ĐH Bách Khoa TP.HCM Lê Chí Thơng Timer (16 bit) Interrupt Write a program using Timer and interrupt to create a kHz square wave on P1.1 (Crystal 12 MHz) ORG 0000H LJMP CTCHINH ORG 000BH LJMP CTNGATTIMER0 ORG 0030H CTCHINH: MOV TMOD,#01H SETB EA SETB ET0 SETB TF0 ; Ép ngắt SJMP $ Ref I Scott Mackenzie CTNGATTIMER0: CPL P1.1 CLR TR0 MOV TH0,#HIGH(-500) MOV TL0,#LOW(-500) SETB TR0 RETI END Lê Chí Thơng 35 Practice Problem Write a program using Timer and interrupt to create a 100 Hz square wave on P1.2 (Crystal 24MHz) Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 36 18 ĐH Bách Khoa TP.HCM Lê Chí Thơng Practice Problem Write a program using Timer and interrupt to create a 100 Hz square wave on P1.2 (Crystal 24MHz) ORG 0000H LJMP CTCHINH ORG 001BH LJMP CTNGATTIMER1 ORG 0030H CTCHINH: MOV TMOD,#10H SETB EA SETB ET1 SETB TF1 ; Ép ngắt SJMP $ Ref I Scott Mackenzie CTNGATTIMER1: CPL P1.2 CLR TR1 MOV TH1,#HIGH(-10000) MOV TL1,#LOW(-10000) SETB TR1 RETI END Lê Chí Thơng 37 Practice Problem Write a program using Timer and interrupt to complement P1.2 every sec (Crystal 12 MHz) Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 38 19 ĐH Bách Khoa TP.HCM Lê Chí Thơng Practice Problem Write a program using Timer and interrupt to complement P1.2 every sec (Crystal 12 MHz) ORG 0000H LJMP CTCHINH ORG 001BH LJMP CTNGATTIMER1 ORG 0030H CTCHINH: MOV TMOD,#10H SETB EA SETB ET1 SETB TF1 ; Ép ngắt SJMP $ Ref I Scott Mackenzie CTNGATTIMER1: CPL P1.2 CLR TR1 MOV TH1,#HIGH(-10000) MOV TL1,#LOW(-10000) SETB TR1 RETI END Lê Chí Thơng 39 Two Square Waves Using Timer Interrupts Write a program using interrupts to create kHz and 500 Hz square waves on P1.7 and P1.6 (Crystal 12 MHz) Ref I Scott Mackenzie sites.google.com/site/chithong Lê Chí Thơng 40 20 ĐH Bách Khoa TP.HCM Lê Chí Thơng Two Square Waves Using Timer Interrupts Write a program using interrupts to create kHz and 500 Hz square waves on P1.7 and P1.6 ORG 0000H LJMP MAIN ORG 000BH LJMP T0ISR ORG 001BH LJMP T1ISR ORG 0030H MAIN: MOV TMOD,#12H MOV IE,#8AH MOV TH0,#-71 SETB TR0 MOV TH1,#HIGH(-1000) MOV TL1,#LOW(-1000) SETB TR1 SJMP $ Ref I Scott Mackenzie T0ISR: CPL P1.7 RETI T1ISR: CPL P1.6 CLR TR1 MOV TH1,#HIGH(-1000) MOV TL1,#LOW(-1000) SETB TR1 RETI END 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 LJMP MAIN ORG 0023H LJMP SPISR ORG 0030H MAIN: MOV TMOD,#20H MOV TH1,#-26 SETB TR1 MOV SCON,#42H MOV IE,#90H MOV A,#20H SETB TI SJMP $ Ref I Scott Mackenzie sites.google.com/site/chithong SPISR: CJNE A,#7FH,SKIP MOV A,#20H SKIP: MOV SBUF,A INC A CLR TI RETI END Lê Chí Thơng 42 21 ĐH Bách Khoa TP.HCM Lê Chí Thơng Character Output Using Interrupts (2) 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) Ref I Scott Mackenzie Lê Chí Thơng 43 Character Output Using Interrupts (2) 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 LJMP MAIN ORG 0023H LJMP SPISR ORG 0030H MAIN: MOV SCON,#52H MOV TMOD,#20H MOV TH1,#-26 SETB TR1 MOV A,#20H MOV IE,#90H SJMP $ Ref I Scott Mackenzie sites.google.com/site/chithong SPISR: CLR TI MOV SBUF,A INC A CJNE A,#7FH,SKIP MOV A,#20H SKIP: RETI END Lê Chí Thơng 44 22 Đ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 /HOT = if T > 21oC /COLD = if T < 19oC P1.7 = : Furnace ON P1.7 = : Furnace OFF Ref I Scott Mackenzie P3.2 P3.3 Lê Chí Thơng 45 Furnace Controller Using interrupts, design an 8051 furnace controller that keeps a building at 20oC ± 1oC E0ISR: E1ISR: MAIN: SKIP: ORG 0000H LJMP MAIN ORG 0003H CLR P1.7 RETI ORG 0013H SETB P1.7 RETI ORG 0030H MOV IE,#85H SETB IT0 SETB IT1 SETB P1.7 JB P3.2,SKIP CLR P1.7 SJMP $ END Ref I Scott Mackenzie sites.google.com/site/chithong ;turn furnace off ;turn furnace on ;enable external & interrupts ;negative edge triggered for external ;negative edge triggered for external ;turn furnace on ;if T > 21 degrees, ; turn furnace off ;do nothing Lê Chí Thơng 46 23 ĐH Bách Khoa TP.HCM Lê Chí Thơng Intrusion Warning System (1) Design an intrusion warning system using interrupts that sounds a 400 Hz tone using loudspeaker connected to P1.7 whenever a door sensor connected /INT0 makes a high-to-low transition P3.2 Ref I Scott Mackenzie Lê Chí Thơng 47 Intrusion Warning System (1) Design an intrusion warning system using interrupts that sounds a 400 Hz tone using loudspeaker connected to P1.7 whenever a door sensor connected /INT0 makes a high-to-low transition ORG 0000H LJMP MAIN ORG 0003H LJMP E0ISR ORG 001BH LJMP T1ISR ORG 0030H MAIN: SETB IT0 MOV TMOD,#10H MOV IE,#81H SJMP $ E0ISR: SETB TF1 SETB ET1 RETI Ref I Scott Mackenzie sites.google.com/site/chithong T1ISR: CLR TR1 MOV TH1,#HIGH(-1250) MOV TL1,#LOW(-1250) CPL P1.7 SETB TR1 RETI END Lê Chí Thơng 48 24 Đ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 LJMP MAIN ORG 0003H LJMP E0ISR ORG 000BH LJMP T0ISR ORG 001BH LJMP T1ISR ORG 0030H MAIN: SETB IT0 MOV TMOD,#11H MOV IE,#81H SJMP $ E0ISR: MOV TH0,#HIGH(-50000) MOV TL0,#LOW(-50000) SETB TR0 SETB TF1 Ref I Scott Mackenzie sites.google.com/site/chithong SETB ET0 SETB ET1 RETI T0ISR: CLR TR0 CLR ET0 CLR ET1 RETI T1ISR: CLR TR1 MOV TH1,#HIGH(-1250) MOV TL1,#LOW(-1250) CPL P1.7 SETB TR1 RETI END Lê Chí Thơng 50 25 ĐH Bách Khoa TP.HCM Lê Chí Thơng Intrusion Warning System (3) Design an intrusion warning system using interrupts that sounds a 400 Hz tone for second (using loudspeaker connected to P1.7) whenever a door sensor connected /INT0 makes a high-to-low transition P3.2 Ref I Scott Mackenzie Lê Chí Thơng 51 Intrusion Warning System (3) Design an intrusion warning system using interrupts that sounds a 400 Hz tone for second (using loudspeaker connected to P1.7) whenever a door sensor connected /INT0 makes a high-to-low transition ORG 0000H LJMP MAIN ORG 0003H LJMP E0ISR ORG 000BH LJMP T0ISR ORG 001BH LJMP T1ISR ORG 0030H MAIN: SETB IT0 MOV TMOD,#11H MOV IE,#81H SJMP $ E0ISR: MOV R7,#20 SETB TF0 SETB TF1 SETB ET0 Ref I Scott Mackenzie sites.google.com/site/chithong SETB ET1 RETI T0ISR: CLR TR0 DJNZ R7,SKIP CLR ET0 CLR ET1 LJMP EXIT SKIP: MOV TH0,#HIGH(-50000) MOV TL0,#LOW(-50000) SETB TR0 EXIT: RETI T1ISR: CLR TR1 MOV TH1,#HIGH(-1250) MOV TL1,#LOW(-1250) CPL P1.7 SETB TR1 RETI Lê Chí Thơng 52 END 26 ĐH Bách Khoa TP.HCM Lê Chí Thơng References • I Scott Mackenzie, The 8051 Microcontroller • Các tài liệu Internet khơng trích dẫn khơng ghi tác giả Lê Chí Thơng sites.google.com/site/chithong 53 27

Ngày đăng: 13/04/2023, 08:09

w