Introduction to Micro Controllers Embedded System Design Interrupt
Introduction to Micro Controllers & Embedded System Design Interrupt Department of Electrical & Computer Engineering Missouri University of Science & Technology hurson@mst. edu A. R. Hurson 1
Introduction to Micro Controllers & Embedded System Design • An interrupt is the occurrence of an event out of normal sequence of machine’s operation. • It causes a temporary suspension of a program while the event is serviced by another program known as interrupt handlers or interrupt service routine, very similar to a subroutine call. A. R. Hurson 2
Introduction to Micro Controllers & Embedded System Design • In microcontroller, interrupt allows the system to respond asynchronously to an event and deal with the event. • An interrupt driven system gives the illusion of doing many things simultaneously. A. R. Hurson 3
Introduction to Micro Controllers & Embedded System Design Time Main Program Main ISR A. R. Hurson Main ISR 4
Introduction to Micro Controllers & Embedded System Design • There are five interrupt sources in 8051: Two external, two timers, and a serial port interrupt. • Note: it is possible that more than one interrupt happen simultaneously or an interrupt occurs while another interrupt being serviced. A. R. Hurson 5
Introduction to Micro Controllers & Embedded System Design • Timer CONtrol register: Bit Symbol Address Semantic TCON. 7 TF 1 8 FH Timer 1 overflow flag. Set by hardware upon overflow; cleared by software or hardware when processor vectors to interrupt service routine TCON. 6 TR 1 8 EH Timer 1 run-control bit. Set/cleared by software to turn timer on/off TCON. 5 TF 0 8 DH Timer 0 overflow flag TCON. 4 TR 0 8 CH Timer 0 run-control bit A. R. Hurson 6
Introduction to Micro Controllers & Embedded System Design • Timer CONtrol register: Bit Symbol Address TCON. 3 IE 1 8 BH TCON. 2 IT 1 8 AH External interrupt 1 type flag. Set/cleared by software TCON. 1 IE 0 89 H External interrupt 0 flag. TCON. 0 IT 0 88 H External interrupt 0 type flag A. R. Hurson Semantic 7
Introduction to Micro Controllers & Embedded System Design • Serial port control register BIT Symbol Address Semantics SCON. 7 SM 0 9 FH Serial port mode bit 0 SCON. 6 SM 1 9 EH Serial port mode bit 1 SCON. 5 SM 2 9 DH Serial port mode bit 2. Enables multiprocessor communications in modes 2 and 3. SCON. 4 REN 9 CH Receiver enable (must be set to receive) SCON. 3 TB 8 9 BH Transmit bit 8. 9 th bit transmitted in modes 2 and 3. Set/cleared by software SCON. 2 RB 8 9 AH Receive bit 8 SCON. 1 TI 99 H Transmit interrupt flag. Set at the end of character transmission. Cleared by software SCON. 0 RI 98 H Receive interrupt flag. Set at the end of character reception. Cleared by software A. R. Hurson 8
Introduction to Micro Controllers & Embedded System Design • Interrupt flag bits INTERRUPT FLAG External 0 IE 0 TCON. 1 External 0 IE 1 TCN. 3 Timer 1 TF 1 TCON. 7 Timer 0 TF 0 TCON. 5 Serial Port TI SCON. 1 Serial Port RI SCON. 0 A. R. Hurson SFR and bit position 9
Introduction to Micro Controllers & Embedded System Design • Enabling and disabling Interrupts • Each interrupt source can be individually enabled or disabled through the bit addressable special function register IE (Interrupt Enable) at address 0 A 8 H. • Note: There is a global enable/disable bit that can be cleared to disable all interrupts or set to turn on interrupts. A. R. Hurson 10
Introduction to Micro Controllers & Embedded System Design Byte address 90 P 1 F 0 B E 0 ACC D 0 PSW B 8 IP B 0 P 3 A 8 IE A 0 P 2 DPH DDL SP 99 11 SBUF P 0 98 SCON 8 D TH 1 8 C TH 0 8 B TL 1 8 A TL 0 89 TMOD TCON PCON 88 87 83 82 81 80 A. R. Hurson FF
Introduction to Micro Controllers & Embedded System Design • Interrupt bits BIT Symbol Bit address Semantics 1 = Enable, 0 = Disable IE. 7 EA AFH Global enable/disable interrupt IE. 6 - AEH Unused IE. 5 ET 2 ADH Enable timer 2 interrupt(8052) IE. 4 ES ACH Enable Serial port interrupt IE. 3 ET 1 ABH Enable timer 1 interrupt IE. 2 EX 1 AAH Enable external 1 interrupt IE. 1 ET 0 A 9 H Enable timer 0 interrupt IE. 0 EX 0 A 8 H Enable external 0 interrupt A. R. Hurson 12
Introduction to Micro Controllers & Embedded System Design • Note: Two bits must be set to enable any interrupt: The individual interrupt bit and the global interrupt bit. Example: SETB ET 1 EA ; Enable timer 1 interrupt bit ; Enable global interrupt bit Alternatively, we can write: MOV IE, #1000 B • Question: do the above solutions have exactly the same effect? A. R. Hurson 13
Introduction to Micro Controllers & Embedded System Design • Interrupt Priority • Each interrupt source is individually programmed to one of two priority levels via bit addressable special function register IP (Interrupt Priority) at address 0 B 8 H. A. R. Hurson 14
Introduction to Micro Controllers & Embedded System Design • Interrupt priority bits BIT Symbol Bit address Semantics 1 = Higher level, 0 = Lower level IP. 7 - - Unused IP. 6 - - Unused IP. 5 PT 2 0 BDH Priority for timer 2 interrupt(8052) IP. 4 PS 0 BCH Priority for Serial port interrupt IP. 3 PT 1 0 BBH Priority for timer 1 interrupt IP. 2 PX 1 0 BAH Priority for external 1 interrupt IP. 1 PT 0 0 B 9 H Priority for timer 0 interrupt IP. 0 PX 0 0 B 8 H Priority for external 0 interrupt A. R. Hurson 15
Introduction to Micro Controllers & Embedded System Design • Order of handling several interrupts: • ISR is interrupted if a higher priority interrupt occurs while servicing a lower priority interrupt. • A high-priority interrupt cannot be interrupted. • The main program can always be interrupted regardless of the priority of the interrupt. A. R. Hurson 16
Introduction to Micro Controllers & Embedded System Design • Order of handling several interrupts: • If two interrupts of different priorities happen simultaneously, the higher priority interrupt will be serviced first. • If two interrupts of the same priority occurs simultaneously, a fixed polling sequence determines which is serviced first, as follows. External 0, Timer 0, External 1, Timer 1, Serial Port A. R. Hurson 17
Introduction to Micro Controllers & Embedded System Design • Processing Interrupts: In case of an interrupt the system follows the following steps: • • • The current instruction completes execution, The content of PC is saved on the stack, The current interrupt status is saved internally, The interrupts are blocked at the level of interrupt, The PC is loaded with the vector address of the ISR, The ISR executes • • • ISR takes action in response to the interrupt ISR finishes with a RETI instruction Execution of main program continues from where it left off. A. R. Hurson 18
Introduction to Micro Controllers & Embedded System Design • Interrupt vector • When an interrupt is accepted the value loaded into the PC is called the interrupt vector which is the address of the start of ISR routine for the interrupting source. • When vectoring to an interrupt, the flag that initiated the interrupt is automatically cleared by hardware, except RI and TI flags which trigger the serial port interrupt. A. R. Hurson 19
Introduction to Micro Controllers & Embedded System Design • Interrupt vector Interrupt Flag Vector address System reset RST 0000 H External 0 IE 0 0003 H Timer 0 TF 0 000 BH External 1 EE 1 0013 H Timer 1 TF 1 001 BH Serial port RI or TI 0023 H • Note: System reserves eight bytes for each interrupt. A. R. Hurson 20
Introduction to Micro Controllers & Embedded System Design Example: MAIN: ORG 0000 H LJMP MAIN ORG 0030 H A. R. Hurson ; Reset entry point ; Main program entry point 21
Introduction to Micro Controllers & Embedded System Design • Graphical representation FFFF Main program 0030 002 F A. R. Hurson 0000 LJMP MAIN Reset and Interrupt Entry points 22
Introduction to Micro Controllers & Embedded System Design Example: Assume we have just one interrupt source (say Timer 0) ORG 0000 H LJMP MAIN ORG 000 BH TOISR: RETI MAIN: A. R. Hurson ; Reset entry point ; Timer 0 ISR begins ; Return to main program : Main program 23
Introduction to Micro Controllers & Embedded System Design Example: If an ISR is longer than 8 bytes, then its code must be somewhere else: ORG LJMP ORG MAIN: TOISR: RETI A. R. Hurson 0000 H MAIN 000 BH TOISR; 0030 H ; Reset entry point : Main program ; Timer 0 ISR begins ; Return to main program 24
Introduction to Micro Controllers & Embedded System Design • Example: Write a program using Timer 0 and interrupt to create a 10 Khz square wave on P 1. 0 A. R. Hurson 25
Introduction to Micro Controllers & Embedded System Design 0000 000 B 000 D 0030 0033 0036 0038 003 B ORG 0000 H 020030 LJMP ORG 000 BH B 290 T 0 ISR: CPT 32 RETI ORG 0030 H 758902 MAIN: MOV 758 CCE MOV D 28 C SETB 75 A 882 MOV ; interrupt 80 FE SJMP A. R. Hurson MAIN P 1. 0 A, R 5 TMOD, #02 H TH 0, #-50 TR 0 IE, #82 H ; Timer 0, Mode 2 ; 50 s delay ; Start Timer ; Enable Timer 0 $ 26
Introduction to Micro Controllers & Embedded System Design • Serial Port interrupts • Serial port interrupts trigger when either the transmit interrupt flag (TI) or the receive interrupt flag (RI) is set. • A transmit interrupt occurs when transmission of the previous character written to SBUF has finished. • A receive interrupt occurs when a character has been completely received and is waiting in SBUF to be read. • Serial port interrupts are slightly different from other types of interrupts in the sense that the serial port interrupt is cleared by software rather than hardware. A. R. Hurson 27
- Slides: 27