8051 Interrupts Programming 1 8051 Interrupts An interrupt

  • Slides: 13
Download presentation
******* 8051 Interrupts Programming 1

******* 8051 Interrupts Programming 1

8051 Interrupts An interrupt is the occurrence of a condition – an event -

8051 Interrupts An interrupt is the occurrence of a condition – an event - which interrupts normal program flow ◦ Interrupts allow a system to respond to asynchronous events (not in program flow) and handle the events while another task is running ◦ An interrupt driven system gives the illusion of doing many things simultaneously (multitasking on one CPU) ◦ The routine that deal’s with a specific interrupt is called an Interrupt Service Routine (ISR) or an “interrupt handler” ◦ Interrupts routines are activated by the occurrence of either an external or an internal event (a. k. a. exceptions) ◦ Interrupt routines are said to run in the “background” while the main system program runs in the “foreground” 2

Interrupt Service Interrupts allow the 8051 to respond to asynchronous events (external or internal)

Interrupt Service Interrupts allow the 8051 to respond to asynchronous events (external or internal) only when required. o � � � The alternative is called “polling” – testing status bits - which can be time consuming, wasting precious CPU resources/cycles Interrupts introduce the concept of “priority” where one interrupt is given preference over another simultaneous interrupt Interrupt Vectors--where is the address of the ISR when a particular interrupt occurs? only when interrupts are enabled 8051 Interrupt Vector Table Interrupt Vector Address Number Bytes Reset 0000 H 3 INT 0 0003 H 8 TF 0 000 BH 8 INT 1 0013 H 8 TF 1 001 BH RI/TI TF 2/EXF 2 (Atmel 89 C 51 RD 2 has 3 more – not covered) Pin Flag Clearing Comment Auto Power-on Auto External Hardware Auto Timer 0 Auto External Hardware 8 Auto Timer 1 0023 H 8 Programmer Serial Communication 002 Bh 3 Programmer Timer 2 P 3. 3 Each has 2 possible reasons for interrupt 3

Interrupt Service Sequence When an interrupt is activated the 8051… ◦ Finishes the instruction

Interrupt Service Sequence When an interrupt is activated the 8051… ◦ Finishes the instruction currently being executed ◦ Saves the current status of all the interrupts and stores the current PC Stack ◦ Vectors (i. e. jumps) to the corresponding location within the interrupt vector table, i. e. PC Vector Table address �Option 1: If the ISR fits in the available space you can immediately service the interrupt �Option 2: If the ISR is too large then the vector table contains a long jump (ljmp) to the Interrupt Service Routing (ISR) �The last instruction of the ISR is a “reti” (Return from Interrupt) �Responsibility of ISR to save/restore any registers that it uses, including the PSW, having the same number of pushes and pops to/from the stack for the “reti” instruction to work correctly ◦ Original PC is popped off the stack – returning to where program was when the interrupt occurred 4

Enabling/Disabling Interrupts All interrupts are disabled (masked) at system reset � Software enables those

Enabling/Disabling Interrupts All interrupts are disabled (masked) at system reset � Software enables those interrupts required � IE (A 8 h) Interrupt Enable SFR is used to enable/disable interrupts 7 6 EA -- EA IE. 7 - IE. 6 IE. 5 IE. 4 ET 2 ES ET 1 IE. 3 EX 1 IE. 2 ET 0 IE. 1 EX 0 IE. 0 5 ET 2 4 3 ES ET 1 2 EX 1 1 ET 0 0 EX 0 Global enable. If EA = 0, no interrupt is acknowledged. If EA = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit Not implemented. Don’t set. Enables/disables Timer 2 TF 2 or EXF 2 interrupt (8052) Enables/disables Serial Port RI or TI interrupt Enables/disables Timer 1 overflow interrupt Enables/disables External interrupt 1 Enables/disables Timer 0 overflow interrupt Enables/disables External interrupt 0 5

Interrupt Priority Levels Each interrupt source is individually programmed as being a “high” or

Interrupt Priority Levels Each interrupt source is individually programmed as being a “high” or “low” priority interrupt ◦ Prioritized allows resolution of simultaneous interrupts ◦ Prioritized interrupts allow for “preemptive” interrupt handlers ◦ IP (B 8 h) – Interrupt Priority SFR ◦ Priority is either high (1) or low (0) ◦ If an ISR is active and a higher priority interrupt occurs, it is interrupted (i. e. preempted). A high level ISR can not be interrupted. -- IP. 7 Undefined -- IP. 6 Undefined PT 2 IP. 5 Priority for Timer 2 interrupt PS IP. 4 Priority for serial port interrupt PT 1 IP. 3 Priority for Timer 1 interrupt PX 1 IP. 2 Priority for external 1 interrupt PT 0 IP. 1 Priority for Timer 0 interrupt PX 0 IP. 0 Priority for external 0 interrupt 6

7

7

Interrupt Priority Levels (enhanced) Each interrupt source is individually programmed to one of four

Interrupt Priority Levels (enhanced) Each interrupt source is individually programmed to one of four priority levels (00, 01, 10, 11) ◦ IPH (B 7 h) – Interrupt Priority HIGH ◦ IPL (B 8 h) – Interrupt Priority LOW ◦ Priority from highest to lowest (3, 2, 1, 0) ◦ If an ISR is active and a higher priority interrupt occurs, it is interrupted. A higher level ISR can not be interrupted. Equal priorities yield to polling sequence (cf. page 75, AT 8951 RD 2 spec) -- IP (H/L). 7 Undefined -- IP (H/L). 6 PCA PT 2 IP (H/L). 5 Priority for Timer 2 interrupt PS IP (H/L). 4 Priority for serial port interrupt PT 1 IP (H/L). 3 Priority for Timer 1 interrupt PX 1 IP (H/L). 2 Priority for external 1 interrupt PT 0 IP (H/L). 1 Priority for Timer 0 interrupt PX 0 IP (H/L). 0 Priority for external 0 interrupt (Programmable Counter Array) 8

External Interrupts 8051 has two external interrupts: INT 0 and INT 1 ◦ Tied

External Interrupts 8051 has two external interrupts: INT 0 and INT 1 ◦ Tied to external pins: INT 0 (P 3. 2), INT 1 (P 3. 3) ◦ P 3. 2 and P 3. 3 are general purpose I/O pins until the respective Interrupt Enable bits are set (i. e. EX 0 and EX 1, respectively) � Activation of external interrupts ◦ 8051 external interrupts activated in one of two ways ◦ (1) Level-triggered of (2) edge-triggered ◦ TCON. 0 (IT 0) and TCON. 2 (IT 1) defines “interrupt type“ � IT 0/IT 1 = 0 for level-triggered interrupts (default) � IT 0/IT 1 = 1 for edge triggered interrupts 7 6 5 4 3 2 1 0 TF 1 TR 1 TF 0 TR 0 IE 1 IT 1 IE 0 IT 0 TCON (88 h) Vcc Pull-up INT 1 8051 9

External Interrupts � Level-triggered (cont. ) interrupts ◦ INT 0/INT 1 held normally HIGH.

External Interrupts � Level-triggered (cont. ) interrupts ◦ INT 0/INT 1 held normally HIGH. A LOW level on one of these signals will trigger the respective interrupt ◦ The 8051 keeps sampling INTn for a LOW once each machine cycle � Some 8051’s specify that “the pin must be held in a LOW state until the start of the execution of the ISR. If the INTn pin is brought back to a HIGH before the start of the ISR, there will be no interrupt. ” � Thus, to ensure activation of an external interrupt, it must remain LOW for at least 4 machine cycles ◦ The LOW on the pin must be removed before the last instruction in the ISR (reti) is executed else another interrupt will occur 10

External Interrupts � Edge-triggered (cont. ) interrupts ◦ Setting IT 1 or IT 0

External Interrupts � Edge-triggered (cont. ) interrupts ◦ Setting IT 1 or IT 0 to “ 1” programs the 8051 to detect edge-triggered signals ◦ INT 1/INT 0 held normally HIGH. A HIGH-to-LOW transition on one of these signals will trigger the respective interrupt ◦ In edge-triggered interrupts, the INT 1/INT 0 signal must be held HIGH for at least one machine cycle, and then held LOW for at least one machine cycle to ensure that the transition is “seen” by the 8051 ◦ The 8051 automatically sets the respective “External Interrupt Edge Flag” (IE 1/IE 0) in the TCON register when using edge-trigerring ◦ Regarding the “Interrupt Type” bits (IT 1/IT 0) in the TCON register, the following two points must be emphasized: � Upon execution of a “RETI” instruction, the respective IEn bit will be cleared automatically, indicating the edge-triggered interrupt has been serviced � While the ISR is being executed, the 8051 ignores all transitions on the external interrupt signals INT 1/INT 0 7 6 5 4 3 2 1 0 TF 1 TR 1 TF 0 TR 0 IE 1 IT 1 IE 0 IT 0 TCON (88 h) 11

Review of Timer Control register - TCON (88 h) Bit Symbol TCON. 7 TF

Review of Timer Control register - TCON (88 h) Bit Symbol TCON. 7 TF 1 TCON. 6 TR 1 TCON. 5 TF 0 TCON. 4 TR 0 TCON. 3 IE 1 TCON. 2 IT 1 TCON. 1 IE 0 TCON. 0 IT 0 • • • Comment T 1 Overflow Flag T 1 Run Control (1=ON, 0=OFF) T 0 Overflow Flag T 1 Run Control (1=ON, 0=OFF) EI 1 edge flag. SET on H 2 L transition. Cleared by CPU I 1 type control. 1=falling edge, 0=low-level activated EI 0 edge flag. SET on H 2 L transition. Cleared by CPU I 0 type control. 1=falling edge, 0=low-level activated Edge-detect: the input must be held HIGH for one cycle and LOW for another. IE 0/IE 1 automatically cleared when CPU vectors to interrupt Level-activated: the input must be held until interrupt generated. Must also be de-activated before the ISR is completed. Usually the ISR “acknowledges” the interrupt and the interrupting device removes the interrupt request External Interrupts are sampled once each machine cycle so input should be held for at least 12 oscillator periods to ensure proper sampling 12

Programming the Serial Comm. Interrupt � TI (Transmit Interrupt) is set when the last

Programming the Serial Comm. Interrupt � TI (Transmit Interrupt) is set when the last bit of framed data, the stop bit, is transmitted – indicating that SBUF is empty and ready for another byte RI (Receive Interrupt) is set when an entire frame of data is correctly received indicating that SBUF now has a byte ready to be read � Behavior of TI and RI is the same whether we are polling or using interrupts. Difference is how we detect and respond to its occurrence � TI and RI are OR’d to generate a single interrupt ◦ User must determine which is the source in the ISR ◦ Typical use relies on RI for data received but will poll TI to ensure data sent ◦ Analogous to receiving and generating a phone call � Always clear TI or RI prior to execution of reti � 13