1 chapter 11 interrupts programming in assembly and c

90
1 Chapter 11 Interrupts Programming in Assembly and C

Upload: reynold-harvey

Post on 27-Dec-2015

274 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: 1 Chapter 11 Interrupts Programming in Assembly and C

1

Chapter 11 Interrupts Programming in

Assembly and C

Page 2: 1 Chapter 11 Interrupts Programming in Assembly and C

2

Objective

• 8051 提供插斷 (interrupt) 的功能,當 I/O 需服務時, CPU 會中斷目前的工作,而跳躍到此 I/O 的插斷服務程式 (interrupt service routine) ,做完後才又回到原本的工作地方繼續執行。

• 8051 的插斷 (interrupt) 的功能,共針對 6 種 I/O : Reset 、 timers 、 external hardware 、 serial communication ,我們將會分別討論。

• 我們首先要瞭解 8051 插斷的運作方式,進而探討相關程式的撰寫。希望透過插斷的使用,讓 8051 工作得更有效率。

Page 3: 1 Chapter 11 Interrupts Programming in Assembly and C

3

Review of Call A Subroutine

LCALL DELAY;

MOV A,#0AAH;

DELAYROM addr.

0004

0300

0304

00070009

MOV R5,#0FFH;

RET;

0000

ROM addr.

return address

Opcode of LCALL DELAY: 120300

Page 4: 1 Chapter 11 Interrupts Programming in Assembly and C

4

Sections

11.1 8051 Interrupts

11.2 Programming timer interrupts

11.3 Programming external hardware interrupts

11.4 Programming the serial communication interrupt

11.5 Interrupt priority in the 8051/52

11.6 Interrupt programming in C

Page 5: 1 Chapter 11 Interrupts Programming in Assembly and C

5

Section 11.18051 Interrupts

Page 6: 1 Chapter 11 Interrupts Programming in Assembly and C

6

Inside Architecture of 8051

CPU

On-chip RAM

On-chip ROM for program code

4 I/O Ports

Timer 0

Serial Port

Figure 1-2. Inside the 8051 Microcontroller Block Diagram

OSC

Interrupt Control

External interrupts

Timer 1

External T/C

Bus Control

TxD RxDP0 P1 P2 P3

Address/Data

Counter Inputs

Timer/Counter

Page 7: 1 Chapter 11 Interrupts Programming in Assembly and C

7

Interrupt of the 8051

• Each component runs individually. • Whenever any component needs CPU’s service, the

component notifies the interrupt control by sending it an signal, referred to the interrupt signal.

• CPU stops its regular work and jumps to serve this component.

– Execute the Interrupt Service Routine (ISR) or interrupt handler.

• After finishing the work of this component, CPU returns to its regular work.

Page 8: 1 Chapter 11 Interrupts Programming in Assembly and C

8

Six Interrupts in the 8051

• Reset• Two interrupt for the timers

– TF0, TF1

• Two interrupt for external hardware interrupts– INT0, INT1

• Serial communication– TI or RI

Page 9: 1 Chapter 11 Interrupts Programming in Assembly and C

9

Figure 4-1. 8051 Pin DiagramPDIP/Cerdip

123

4567891011121314151617181920

403938

3736353433323130292827262524232221

P1.0P1.1P1.2

P1.3P1.4P1.5P1.6P1.7RST

(RXD)P3.0(TXD)P3.1

(T0)P3.4(T1)P3.5

XTAL2XTAL1

GND

(INT0)P3.2

(INT1)P3.3

(RD)P3.7(WR)P3.6

VccP0.0(AD0)P0.1(AD1)

P0.2(AD2)P0.3(AD3)P0.4(AD4)P0.5(AD5)P0.6(AD6)P0.7(AD7)

EA/VPPALE/PROG

PSENP2.7(A15)P2.6(A14)P2.5(A13)P2.4(A12)P2.3(A11)P2.2(A10)P2.1(A9)P2.0(A8)

8051(8031)

external hardware interrupt

Page 10: 1 Chapter 11 Interrupts Programming in Assembly and C

10

Create A Square Wave on P2.1 without Timer

DJNZ R0,HERE

CPL P2.1

YES

NO

R0=#08H• To create a square wave

on P2.1 and complement P1

• We use DJNZ to generate a time delay.

• The 8051 CPU is busy for DJNZ!

• The 8051 CPU can only do one task.

CPU

XRL P1,#0FFH

HERE:

Page 11: 1 Chapter 11 Interrupts Programming in Assembly and C

11

Create A Square Wave on P2.1 with Timer

Timer 0, mode 2

TH0=#0F0H, SETB TR0

If TF0=1

CPL P2.1

YES

NO

CLR TF0

• The 8051 CPU is busy for monitoring TF0!

CPU

XRL P1,#0FFH

Timer 0

TH0

TL0 TF0

auto reload

roll over

Page 12: 1 Chapter 11 Interrupts Programming in Assembly and C

12

Create A Square Wave on P2.1 with Interrupt

Enable Interrupt

Regular Work

If TF0=1NO

ISR

CPL P2.1

YES

XRL P1,#0FFH

CPU Interrupt Control

An Interrupt Event

Timer 0

TH0

TL0 TF0

auto reload

roll over

Timer 0, mode 2

TH0=#0F0H, SETB TR0

RETI

Page 13: 1 Chapter 11 Interrupts Programming in Assembly and C

13

Steps in Executing an 8051 Interrupt (1/2)

• Upon activation of an interrupt, the microcontroller goes through the following steps:1. It finishes the instruction it is executing and saves the

address of the next instruction (PC) on the stack.

2. It also saves the current status of all the interrupts internally.

3. It jumps to a fixed location in memory based on the interrupt vector table.

Page 14: 1 Chapter 11 Interrupts Programming in Assembly and C

14

Steps in Executing an 8051 Interrupt (2/2)

• Executing steps (continuous):4. The microcontroller gets the address of the ISR from the

interrupt vector table and jumps to it.

5. The microcontroller starts to execute the interrupt service routine until it reaches the last instruction of the subroutine which is RETI (return from interrupt).

6. Upon executing the RETI instruction, the microcontroller returns to the place where it was interrupted. • First, it gets the program counter (PC) address from the stack by

popping the top two bytes of the stack into the PC.

• Then it starts to execute from that address.

Page 15: 1 Chapter 11 Interrupts Programming in Assembly and C

15

Interrupt Service Routines

• ISRs are similar to normal subroutines.• ISRs are generated by programs to handle interrupt

events.• For interrupt event, its ISR is hold at a fixed

location in memory.

1

TF0

jumps to

0000 .... 000B.... .... 0030 ....

ISR of timer0

main program

Reserved for Interrupt

Page 16: 1 Chapter 11 Interrupts Programming in Assembly and C

16

The Addresses of ISRs

• The group of memory locations set aside to hold the addresses of ISRs is called the interrupt vector table.

• Table 11-1 is the interrupt vector table for 8051.– 3 bytes for reset

– 8 bytes for timers and external hardware interrupts

– If the service routine is too short to fit the ISR, an SJMP/LJMP instruction is placed in the vector table to point to the address of the ISR.

Page 17: 1 Chapter 11 Interrupts Programming in Assembly and C

17

Table 11-1: Interrupt Vector Table for the 8051

Interrupt ROM Location

(Hex) Pin

Reset 0000 9

External hardware interrupt 0 (INT0)

0003 P3.2

(12)

Timer 0 interrupt (TF0) 000B

External hardware interrupt 1 (INT1)

0013 P3.3

(13)

Timer 1 interrupt (TF1) 001B

Serial COM interrupt (RI or TI) 0023

Page 18: 1 Chapter 11 Interrupts Programming in Assembly and C

18

Figure 11-1: Programs with Interrupts;---- the first instruction is executed as the 8051 powers up ORG 0 ;ROM reset location LJMP MAIN ;by-pass interrupt vector table;---- ISR for INT0 ORG 0003H : RETI ;---- ISR for Timer 0 ORG 000BH : RETI ....;---- the wake-up program ORG 30HMAIN: .... END

001B

ISR of Serial Communication

Main Program

Reserved for Interrupt

ISR of Timer1

ISR of INT1

ISR of Timer0

ISR of INT0

LJMP MAIN0000

0003

000B

0013

0023

0030

Page 19: 1 Chapter 11 Interrupts Programming in Assembly and C

19

Notices

• Interrupt is disable upon RESET.• You can open the functionality of interrupt or not.• You can choose to disable some interrupt events,

– You do not need to write ISRs for them.

• Programmers must enable these interrupts before using them.

Page 20: 1 Chapter 11 Interrupts Programming in Assembly and C

20

IE (Interrupt Enable)

• In the 8051, the IE (interrupt enable) register denotes the usage of these interrupts.

– Figure 11-2 : IE register – Upon reset, all interrupts are disabled (masked).

– The interrupts must be enabled by software.

– IE is bit-addressable.

– If we want to enable a special interrupt, set EA=1 first.

– Enable each interrupt by setting its corresponding bit in IE.

EA -- ET2 ES ET1 EX1 ET0 EX0

D7 D0

Page 21: 1 Chapter 11 Interrupts Programming in Assembly and C

21

Enable/Disable Interrupts

TF0Timer/

Counter 0

INT1External

Interrupt 1

TF1Timer/

Counter 1

RI/TISerialPort

TF2Timer/

Counter 2

INT0External

Interrupt 0

CPUEA

ET2

ES

ET1

EX1

ET0

EX0

IE register

Page 22: 1 Chapter 11 Interrupts Programming in Assembly and C

22

Figure 11-2. IE (Interrupt Enable) Register

EA IE.7 Disables all interrupts.

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.

--- IE.6 Not implemented, reserved for future use. *

ET2 IE.5 Enables or disables timer 2 overflow or capture interrupt

(8052).

ES IE.4 Enables or disables the serial port interrupt. RI or TI

ET1 IE.3 Enables or disables timer 1 overflow interrupt. TF1

EX1 IE.2 enables or disables external interrupt 1. INT1

ET0 IE.1 Enables or disables timer 0 overflow interrupt. TF0

EX0 IE.0 enables or disables external interrupt 0. INT0

Page 23: 1 Chapter 11 Interrupts Programming in Assembly and C

23

Example 11-1 (1/2)

Show the instructions to (a) enable the serial interrupt, timer 0 interrupt, and external

hardware interrupt 1 (EX1)(b) disable (mask) the timer 0 interrupt(c) show how to disable all the interrupts with a single instruction.

Solution:

(a) MOV IE,#10010110B EA serial INT1 timer 0

Another way to perform the “MOV IE,#10010110B” instruction is by using single-bit instructions as shown below.

SETB IE.7 ;EA-1, Global enable (D2 AF)

SETB IE.4 ;enable serial interrupt (D2 AC)

SETB IE.2 ;enable hardware interrupt 1 (D2 AA)

SETB IE.1 ;enable Timer 0 (D2 A9)

Page 24: 1 Chapter 11 Interrupts Programming in Assembly and C

24

Example 11-1 (2/2)

Or you can write SETB EA ;EA-1, Global enable (D2 AF) SETB ES ;enable serial interrupt (D2 AC) SETB EX1 ;enable hardware interrupt 1 (D2 AA) SETB ET0 ;enable Timer 0 (D2 A9)

Since IE is a bit-addressable register, we can use the following instructions to access individual bits of the register.

(b) CLR IE.1 ;mask (disable) timer 0 interrupt (C2 A9)(c) CLR IE.7 ;disable all interrupts (C2 AF)

Page 25: 1 Chapter 11 Interrupts Programming in Assembly and C

25

Section 11.2Programming Timer Interrupts

Page 26: 1 Chapter 11 Interrupts Programming in Assembly and C

26

Roll-over Timer Flag and Interrupt

• In polling TF, we have to wait until the TF is raised. HERE: JNB TF0, HERE

• Using interrupt, whenever TF is raised, the microcontroller is interrupted and jumps to the interrupt vector table to service the ISR.

1

TF0

jumps to

0000 .... 000B.... ....

ISR of timer0

Timer 0 Interrupt Vector: 000BH

1

TF1

jumps to

0000 .... .... 001B....

ISR of timer1

Timer 1 Interrupt Vector: 001BH

Page 27: 1 Chapter 11 Interrupts Programming in Assembly and C

27

Example 11-2 (1/3)Write a program that continuously gets 8-bits data from P0 and

sends it to P1 while simultaneously creating a square wave of 200

µs period on pin P2.1. Use timer 0 to create the square wave.

Assume that XTAL = 11.0592 MHz.

Solution:

We use timer 0 in mode 2 and interrupt to generate half period.

TH0 = 100 µs /1.085 µs = 92 for half clock.

We must avoid using memory space allocated to interrupt vector table. Therefore, we place the main memory in 0030H

1

TF0

jumps to

0000 .... 000B.... .... 0030 ....

ISR of timer0

main program

interrupt vector tableP2.1

interrupt

100 µs 100 µs

200 µs

Page 28: 1 Chapter 11 Interrupts Programming in Assembly and C

28

Example 11-2 (2/3)

;LJMP redirects the controller away from the interrupt vector table.

ORG 0000H

LJMP MAIN ;by-pass interrupt vector table

;The ISR for Timer 0 to generate square wave

;The ISR is small enough to fit in the 8 bytes.

ORG 000BH ;Timer 0 interrupt vector table

CPL P2.1 ;toggle P2.1 pin

RETI ;return from ISR

;In the ISR for timer 0, notice that there is no need for “CLR TF0” before “RETI”. The reason for this is that the 8051 clears the TF flag internally by the execution of RETI.

Page 29: 1 Chapter 11 Interrupts Programming in Assembly and C

29

Example 11-2 (3/3)

;The main program for initialization ORG 0030H ;after vector table spaceMAIN: MOV P0,#0FFH ;make P0 an input port MOV TMOD,#02H ;Timer 0,mode 2(auto reload) MOV TH0,#0A4H ;TH0=A4H for -92 MOV IE,#82H ;IE=100000010(bin) enable Timer 0 SETB TR0 ;Start Timer 0BACK: MOV A,P0 ;get data from P0 and put it to P1 MOV P1,A ;loop unless interrupted by TF0 SJMP BACK END

Page 30: 1 Chapter 11 Interrupts Programming in Assembly and C

30

Example 11-3 (1/4)

Rewrite Example 11-2 to create a square wave that has a high portion of 1085 µs and a low portion of 15 µs. Assume XTAL = 11.0592 MHz. Use timer 1.

Solution:

Low: 15 µs =1.085 µs × 14 ← by DJNZ

High: 1085 µs =1.085 µs × 1000 ← by timer 1

we need to use mode 1 of timer 1.

1

TF1

jumps to

0000 .... 001B.... .... 0030 ....

ISR of timer1

main program

interrupt vector table

P2.1

1085 µs15 µs

interruptinterrupt

one period

Page 31: 1 Chapter 11 Interrupts Programming in Assembly and C

31

Example 11-3 (2/4) ORG 0000H ;by-pass interrupt vector table

LJMP MAIN

;ISR for Timer 1 to generate square wave

ORG 001BH ;timer 1 interrupt vector table

LJMP ISR_T1 ;jump to ISR

;The main program for initialization

ORG 0030H ;after vector table

MAIN: MOV TMOD,#10H ;timer 1, mode 1

MOV P0,#0FFH ;make P0 an input port

MOV TL1,#018H ;TL1=18 the low byte of -1000

MOV TH1,#0FCH ;TH1-FC the high byte of -1000

MOV IE,#88H ;IE=10001000 enable timer 1.

SETB TR1 ;start timer 1

Page 32: 1 Chapter 11 Interrupts Programming in Assembly and C

32

Example 11-3 (3/4)

;The main program for initialization

BACK: MOV A,P0 ;get data from P0 and put it to P1

MOV P1,A ;loop unless interrupted by TF0

SJMP BACK

Page 33: 1 Chapter 11 Interrupts Programming in Assembly and C

33

Example 11-3 (4/4)

;Timer 1 ISR. Must be reloaded since not auto-reload

;The low portion of the clock is created by the 14 MC

ISR_T1:CLR TR1 ;stop Timer 1

CLR P2.1 ;end of high portion

MOV R2,#3 ; low portion(2 MC)

HERE: DJNZ R2,HERE ; (3 × 2 MC)

MOV TL1,#18H ;load T1 low byte value (2 MC)

MOV TH1,#0FCH ;load T1 high byte value (2 MC)

SETB TR1 ;starts timer 1 (1 MC)

SETB P2.1 ;P2.1=1, back to high (1 MC)

RETI ;start of high portion

END

Page 34: 1 Chapter 11 Interrupts Programming in Assembly and C

34

Example 11-4 (1/2)

Write a program to generate a square wave of 50 Hz frequency on pin P1.2. This is similar to Example 9-12 except that it uses an interrupt for timer 0. Assume that XTAL=11.0592 MHz.

Solution:

(a) The period of the square wave = 1 / 50 Hz = 20 ms.

(b) The half square wave = 10 ms = 1.085 s × 9216

65536 – 9216 = 56320 in decimal = DC00H in hex.

ORG 0 LJMP MAIN ORG 000BH CPL P1.2 MOV TL0,#00 MOV TH0,#0DCH RETI

50% 50%

20ms

P1.2

50%

interrupt

Page 35: 1 Chapter 11 Interrupts Programming in Assembly and C

35

Example 11-4 (2/2)

;--main program for initialization ORG 30HMAIN: MOV TMOD,#00000001B ;timer 0, mode 1

MOV TL0,#00

MOV TH0,#0DCH

MOV IE,#10000010B ;enable timer 0 interrupt

SETB TR0

HERE: SJMP HERE

END

P1.2

8051

TL0

TH0

50 MHz square wave

Page 36: 1 Chapter 11 Interrupts Programming in Assembly and C

36

Section 11.3Programming External Hardware Interrupts

Page 37: 1 Chapter 11 Interrupts Programming in Assembly and C

37

External Hardware Interrupts (1/2)

• The 8051 has two external hardware interrupts:– EX0: INT0, Pin 12 (P3.2)

– EX1: INT1, Pin 13 (P3.3)

• These two pins are used in timer/counter.– INT is a trigger for hardware control (GATE=1).

– Timer/counter is enabled only while the INT pin is high and the TR control pin is set.

• They are enabled and disabled using the IE register.– EX0 by IE.0

– EX1 by IE.2

Page 38: 1 Chapter 11 Interrupts Programming in Assembly and C

38

External Hardware Interrupts (2/2)

• Upon activation of these pins, the 8051 gets interrupted and jumps to the vector table to perform the ISR.

• There are two activation levels for the external hardware interrupts:

– Low level triggered (default)– Falling edge triggered

• This is chosen by IT0/IT1 in TCON.– On Reset, IT0 and IT1 are both low, making external

interrupts low level-triggered.

Page 39: 1 Chapter 11 Interrupts Programming in Assembly and C

39

Low Level-triggered Interrupt

• Also called as level-activated interrupt.• After the hardware interrupts in the IE register are

enabled, the controller keeps sampling the INT pin for a low-level signal once each machine cycle.

– INT0 and INT1 pins are normally high.

– If a low-level signal is applied to them, it triggers the interrupt.

1 MC

1.085s

4 MC

1.085s × 4

to INT0 or INT1 pins

minimum duration of logic 0

Page 40: 1 Chapter 11 Interrupts Programming in Assembly and C

40

Sampling the Low Level-triggered

• The duration of the low level-triggered interrupt:– The pin must be held in a low state until the start of the

execution of ISR. If the INT pin is bought back to a logic high before the start of the execution of ISR, there will be no interrupt.

– The low-level signal at the INT pin must be removed before the execution of RETI; otherwise, another interrupt will be generated after one instruction is executed.

• See Example11-5.

Page 41: 1 Chapter 11 Interrupts Programming in Assembly and C

41

Falling Edge-triggered Interrupt

• IT is set to specify falling edge triggered external interrupt.

– The controller keeps sampling the INT pin once each machine cycle. When a high-to-low signal is applied to INT0 (INT1) pin, the controller will be forced to jump to location 0003H (0013H) to service the ISR.

– The external source must be held high for at least one MC, and then held low for at least one MC.

1 MC

1.085s 1 MC

1.085s to INT0 or INT1 pins

Page 42: 1 Chapter 11 Interrupts Programming in Assembly and C

42

Sampling the Falling Edge-triggered Interrupt

• When a falling edge-triggered interrupt occurs:– The IE0 and IE1 of TCON register goes high whenever a

falling edge is detected. – When IE0/IE1=1, it indicates to the external world that the

interrupt is begin serviced now and on this INT pin no new interrupt will be responded to until this service is finished.

• The IE0 and IE1 function as interrupt-in-service flags.

• The RETI clears IE0 (IE1) flag.

• During the time that the ISR is being executed, the INT pin is ignored, no matter how many times it makes a high-to-low transition.

Page 43: 1 Chapter 11 Interrupts Programming in Assembly and C

43

Figure 11-4. Activation of INT0 (1/2)

IE0 (TCON.1)

INT0 (Pin 3.2)

Interrupt Service Table with addr.0003H

Edge-triggered

level-triggeredIT 0 =0

IT 0 =1

INT0

INT0

in ISR IE0=1

RETI IE0=0

Page 44: 1 Chapter 11 Interrupts Programming in Assembly and C

44

Figure 11-4. Activation of INT1 (2/2)

IE1 (TCON.3)

INT1 (Pin 3.3)

Interrupt Service Table with addr.0013H

Edge-triggered

level-triggeredIT 1 =0

IT 1 =1

INT1

INT1

in ISR IE1=1

RETI IE1=0

Page 45: 1 Chapter 11 Interrupts Programming in Assembly and C

45

TCON Register (1/3)

• Timer control register: TCON– Upper nibble for timer/counter, lower nibble for

interrupts

– Bit-addressable

– TR0/TR1 are used to start or stop timers.

– TF0/TF1 indicate if the timer has rolled over.

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0Timer 1 Timer0 for Interrupt

(MSB) (LSB)

Page 46: 1 Chapter 11 Interrupts Programming in Assembly and C

46

TCON Register (2/3)

• IT (Interrupt type control bit) – IT0 for external interrupt 0; IT1 for external interrupt 1.

– IT is set/cleared by software to specify falling edge/low-level triggered external interrupt.

• IT=0 : low-level triggered interrupt

• IT=1: falling edge triggered interrupt

– In the 8051, once IT0/IT1 are set to 0 or 1, they will not be altered again since the designer has fixed the interrupt either as edge- or level-triggered.

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0Timer 1 Timer0 INT1 INT0

(MSB) (LSB)

Page 47: 1 Chapter 11 Interrupts Programming in Assembly and C

47

TCON Register (3/3)

• IE (External interrupt edge flag)– IE0 for external interrupt 0; IE1 for external interrupt 1.

– IEs indicate whether or not an interrupt is in use for falling edge-triggered interrupt.

– It does not latch level-triggered interrupts.

– IE is set by CPU when the external interrupt edge (H-to-L transition) is detected.

– Cleared by CPU when the ISR is finished.

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0Timer 1 Timer0 INT1 INT1

(MSB) (LSB)

Page 48: 1 Chapter 11 Interrupts Programming in Assembly and C

48

Example 11-5 (1/2)

Assume that the INT1 pin is connected to a switch that is normally high. Whenever it goes low, it should turn on an LED.

The LED is connected to P1.3 and is normally off.

When it is turned on, it should stay on for a fraction of a second.

As long as the switch is pressed low, the LED should stay on.

Solution:

Pressing the switch will cause the

LED to be turned on.

If it is kept activated,

the LED stays on.

To LED

8051

P1.3

INT1

Vcc

Page 49: 1 Chapter 11 Interrupts Programming in Assembly and C

49

Example 11-5 (2/2)

ORG 0000H LJMP MAIN;--ISR for hardware interrupt INT1 to turn on the LED ORG 0013H SETB P1.3 MOV R3,#255 ;stay on for a fraction of a secondBACK: DJNZ R3,BACK CLR P1.3 RETI;--main program for initialization ORG 30HMAIN: MOV IE,#10000100B ;enable INT1

HERE: SJMP HERE ;stay here until get interrupt

END

Page 50: 1 Chapter 11 Interrupts Programming in Assembly and C

50

Example 11-6 (1/2)

Assuming that pin 3.3 (INT1) is connected to a pulse generator, write a program in which the falling edge of the pulse will send a high to P1.3 which is connected to an LED (or buzzer). In other words, the LED is turned on and off at the same rate as the pulses are applied to the INT1 pin.

This is an edge-triggered version of Example 11-5. But in this example, to turn on the LED again, the INT1 pulse must be brought back high and then forced low to create a falling edge to activate the interrupt.

INT1

toLED

P3.3

P1.3

8051

pulse generator

interrupt

Page 51: 1 Chapter 11 Interrupts Programming in Assembly and C

51

Example 11-6 (2/2) ORG 0000H LJMP MAIN;--ISR for hardware interrupt INT1 to turn on the LED ORG 0013H SETB P1.3 MOV R3,#255 ;keep the buzzer on for a whileBACK: DJNZ R3,HERE CLR P1.3 RETI ;--MAIN program for initialization ORG 30H MAIN: SETB TCON.2 ;make INT1 edge-trigger interrupt MOV IE,#10000100B ;enable External INT 1HERE: SJMP HERE END

Page 52: 1 Chapter 11 Interrupts Programming in Assembly and C

52

Example 11-7What is the difference between the RET and RETI instructions?

Explain why we cannot use RET instead of RETI as the last instruction of an ISR.

Solution:

Both perform the same actions of popping off the top two bytes of the

stack into the program counter, and making the 8051 return to where it

left. However, RETI also performs an additional task of clearing the

interrupt-in-service flag, indicating that the servicing of the interrupt is

over and the 8051 now can accept a new interrupt on that pin. If you

use RET instead of RETI as that last instruction of the interrupt service

routine, you simply block any new interrupt on that pin after the first

interrupt, since the pin status would indicate that the interrupt is still

being serviced. In the cases of TF0, TF1, TCON.1, and TCON.3, they

are cleared due to the execution of RETI.

Page 53: 1 Chapter 11 Interrupts Programming in Assembly and C

53

Section 11.4Programming the Serial Communication Interrupt

Page 54: 1 Chapter 11 Interrupts Programming in Assembly and C

54

Serial Communication Interrupt

• In Chapter 10, the 8051 CPU waits for TI or RI to be raised. While we wait, we cannot do anything else.

• In this section, interrupt-based serial communication are introduced.

– This allows the 8051 to do many things, in addition to sending and receiving data from the serial communication port.

Page 55: 1 Chapter 11 Interrupts Programming in Assembly and C

55

RI and TI Flags and Interrupts

• In the 8051 these is only one interrupt set aside for serial communication.

– This interrupt is used to both send and receive data.

– The interrupt is used mainly for receiving data.

• If the interrupt bit IE.4 is enabled, when RI or TI is raised, the 8051 jumps to memory 0023H to execute the ISR.

• You have to clear of the RI or TI flags yourself.– Because the 8051 does not know who generated it.

– RETI does not clear RI or TI.

Page 56: 1 Chapter 11 Interrupts Programming in Assembly and C

56

Example 11-8 (1/3)

Write a program in which the 8051 reads data from P1 and writes it

to P2 continuously while giving a copy of it to the serial COM port

to be transferred serially. Assume that XTAL=11.0592MHz. Set the baud rate at 9600Hz.

Solution:

The moment a byte is written into SBUF, it is framed and

transferred serially. As a result, when the last bit (stop bit) is

transferred the TI is raised, and that causes the serial interrupt to

be invoked since the corresponding bit in the IE register is high.

We check for both TI and RI since both could have invoked interrupt (but do noting about RI=1).

P1 P2 TxD

Page 57: 1 Chapter 11 Interrupts Programming in Assembly and C

57

Example 11-8 (2/3)

ORG 0

LJMP MAIN

ORG 23H

LJMP SERIAL ;jump to serial interrupt ISR

; ------ main program, initialization ------

ORG 30H

MAIN: MOV P1,#OFFH ;make P1 an input port

MOV TMOD,#20H ;timer 1,mode 2 (auto reload)

MOV TH1,#OFDH ;9600 baud rate

MOV SCON,#50H ;8-bit, 1 stop, REN enabled

MOV IE,#10010000B ;enable serial interrupt

SETB TR1 ;start timer 1

Page 58: 1 Chapter 11 Interrupts Programming in Assembly and C

58

Example 11-8 (3/3);------ stay in loop indefinitely ------ MOV A,P1 MOV SBUF,ABACK: MOV A,P1 MOV P2,A SJMP BACK;------ Serial communication ISR ------ ORG 100HSERIAL: JB TI,TRANS ;jump if TI is high CLR RI ;do nothing but clear RI RETI TRANS: CLR TI ;clear TI MOV SBUF,A ;transmit the copy of P1 again RETI END

Page 59: 1 Chapter 11 Interrupts Programming in Assembly and C

59

Example 11-9 (1/3)

Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0.

Assume that XTAL = 11.0592MHz.

Set the baud rate at 9600Hz.

Solution:

The main program is the same as the main program in Example 11-8.

Only the ISR of serial communication has a little different.

P1 P2 RxD P0

Page 60: 1 Chapter 11 Interrupts Programming in Assembly and C

60

Example 11-9 (2/3)

ORG 0

LJMP MAIN

ORG 23H

LJMP SERIAL ;jump to serial ISR

ORG 30H

; ------ main program, initialization ------

MAIN: MOV P1,#0FFH ;make P1 an input port

MOV TMOD,#20H ;timer 1, mode 2 (auto reload)

MOV TH1,#OFDH ;9600 baud rate

MOV SCON,#50H ;8-bit,1 stop, REN enabled

MOV IE,#10010000B ;enable serial interrupt

SETB TR1 ;start timer 1

Page 61: 1 Chapter 11 Interrupts Programming in Assembly and C

61

Example 11-9 (3/3)

;------ stay in loop indefinitely ------

BACK: MOV A,P1

MOV P2,A

SJMP BACK ;------ Serial communication ISR ------ ORG 100H

SERIAL: JB TI,TRANS ;jump if TI is high

MOV A,SBUF

MOV P0,A

CLR RI

RETI

TRANS: CLR TI ;do nothing

RETI

END

Page 62: 1 Chapter 11 Interrupts Programming in Assembly and C

62

Example 11-10 (1/4)Write a program using interrupts to do the following:(a) Receive data serially and sent it to P0.(b) Have P1 port read and transmitted serially, and a copy given to

P2.(c) Make timer 0 generate a square wave of 5 kHz frequency on

P0.1.Assume that XTAL = 11.0592MHz. Set the baud rate at 4800Hz.Solution:Two interrupts (a)+(b) and (c) must be set:TF0 (address 000BH) for square wave by timer 0: toggle P0.1 (c)RI (address 0023H) for receive data P0 (a)P1 TI (address 0023H) for transmit data (b) (notice: Timer 1 is used for baud rate. But it is not necessary to

enable TF1)An indefinitely loop in main program: P1 P2 (b)

Page 63: 1 Chapter 11 Interrupts Programming in Assembly and C

63

Example 11-10 (2/4)

ORG 0 LJMP MAIN ORG 000BH ;ISR for Timer 0 CPL P0.1 ;toggle P0.1 RETI ORG 0023H LJMP SERIAL ;jump to serial ISR

(c)

(a)+(b)

Page 64: 1 Chapter 11 Interrupts Programming in Assembly and C

64

Example 11-10 (3/4); ------ main program, initialization ------ ORG 30HMAIN:MOV P1,#0FFH ;make P1 as an input port MOV TMOD,#22H ;Timer 0 &1,mode 2, auto-reload MOV SCON,#50H ;8-bit,1 stop bit, REN enabled MOV TH1,#0FAH ;4800 baud rate for (a) MOV TH0,#-92 ;5KHz square wave for (c) MOV IE,#10010010B ;enable serial, timer 0 interrupt

SETB TR1 ;start Timer 1 for (a) SETB TR0 ;start Timer 0 for (c);------ stay in loop indefinitely for (b) : P1 P2 ------ MOV A,P1 MOV SBUF,A BACK: MOV A,P1 MOV P2,A SJMP BACK

Page 65: 1 Chapter 11 Interrupts Programming in Assembly and C

65

Example 11-10 (4/4)

;------ Serial communication ISR ------ ORG 100HSERIAL:JB TI,TRANS ;jump if TI is high MOV A,SBUF ;for (a) MOV P0,A CLR RI RETI TRANS: CLR TI MOV SBUF,A ;for (b) RETI END

Page 66: 1 Chapter 11 Interrupts Programming in Assembly and C

66

Section 11.5Interrupt Priority in the 8051/52

Page 67: 1 Chapter 11 Interrupts Programming in Assembly and C

67

Interrupt Priority upon Reset

• When the 8051 is powered up, the priorities are assigned according to Table 11.3:

– If some interrupts are activated, they are latched and kept internally. The 8051 checks all five interrupts according to the sequence listed in Table 11-3. If any is activated, it services it in sequence.

External Interrupt 0 (INT0) high priority

Timer Interrupt 0 (TF0)

External Interrupt 1 (INT1)

Timer Interrupt 1 (TF1)

Serial Communication (RI+TI)

Timer 2 (8052 only) (TF2) low priority

Page 68: 1 Chapter 11 Interrupts Programming in Assembly and C

68

Example 11-11

Discuss what happens if interrupts INT0, TF0, and INT1 are

activated at the same time. Assume priority levels were set by the

power-up reset and that the external hardware interrupts are edge-

triggered.

Solution:

If these three interrupts are activated at the same time, they are latched and kept internally. Then the 8051 checks all five interrupts according to the sequence listed in Table 11-3.

INT 0 > TF 0 > INT 1 > TF 1 > RI or TI

If any is activated, it services it in sequence. Therefore, when the above three interrupts are activated, INT0 (external interrupt 0) is serviced first, then timer 0 (TF0), and finally INT1 (external interrupt 1).

Page 69: 1 Chapter 11 Interrupts Programming in Assembly and C

69

Setting Interrupt Priority with the IP (Interrupt Priority) Register

• To give a higher priority to any of the interrupts, we make the corresponding bit in the IP high.

– Upon power-up reset, IP contains all 0s, making the priority sequence based on Table 11.3.

– IP bit = 1: high priority

– IP bit = 0: low priority

– First, high-priority and low-priority: 2 levels

– Second, if some interrupts have the same priority than others, they are serviced according to the sequence of Table 11.3.

Page 70: 1 Chapter 11 Interrupts Programming in Assembly and C

70

Figure 11-8. Interrupt Priority Register (Bit Addressable)

-- IP.7 Reserved

-- IP.6 Reserved

PT2 IP.5 Timer 2 interrupt priority bit (8052 only).

PS IP.4 Serial port interrupt priority bit.

PT1 IP.3 Timer 1 interrupt priority bit.

PX1 IP.2 External interrupt 1 priority bit.

PT0 IP.1 Timer 0 interrupt priority bit.

PX0 IP.0 External interrupt 0 priority bit.

User software should never write 1s to unimplemented bits, since they may be used in

future products.

-- -- PT2 PS PT1 PX1 PT0 PX0

Page 71: 1 Chapter 11 Interrupts Programming in Assembly and C

71

Example 11-12

(a) Program the IP register to assign the highest priority to INT1

(external interrupt 1), then (b) discuss what happens if INT0, INT1,

and TF0 are activated at the same time. Assume that the interrupts

are both edge-triggered.

Solution:

(a) MOV IP,#00000100B or SETB IP.2

IP.2=1 to assign INT1 higher priority.

(b) The instruction is Step (a) assigned a higher priority to INT1 than the others:

INT1 > INT 0 > TF 0 > TF 1 > RI or TI

When INT0, INT1, and TF0 interrupts are activated at the same time, the 8051 services INT1 first, then it services INT0, then TF0.

Page 72: 1 Chapter 11 Interrupts Programming in Assembly and C

72

Example 11-13

Assume that after reset, the interrupt priority is set by the instruction

“MOV IP,#00001100B”. Discuss the sequence in which the

interrupts are serviced.

Solution:

The instruction “MOV IP,#00001100B” sets the external interrupt 1 (INT1) and timer 1 (TF1) to a higher priority level compared with the rest of the interrupts.

They will have the following priority.

High priority: INT 1 > TF 1

Low priority: INT 0 > TF 0 > RI or TI

That is: INT 1 > TF 1 > INT 0 > TF 0 > RI or TI

Page 73: 1 Chapter 11 Interrupts Programming in Assembly and C

73

Interrupt inside an Interrupt

• What happens if the 8051 is executing an ISR belonging to an interrupt and another interrupt is activated?

– A high-priority interrupt can interrupt a low-priority interrupt.

– This is an interrupt inside an interrupt.

ISR of Timer 1

ISR of INT0

Timer 1 occurs

INT 0 occurs

main mainA

B

C

The order is ABC

Page 74: 1 Chapter 11 Interrupts Programming in Assembly and C

74

Triggering the Interrupt by Software

• Sometimes we need to test ISR.• We can cause an interrupt with an instruction

which raises the interrupt flag.– For example, if the IE bit for timer 1 is set, “SETB TF1” will interrupt the 8051 and force it jump to the interrupt vector table.

Page 75: 1 Chapter 11 Interrupts Programming in Assembly and C

75

I/O Services

• A single microcontroller can serve several devices.• Two ways:

– Polling method

– Interrupt method

Page 76: 1 Chapter 11 Interrupts Programming in Assembly and C

76

Polling Method

• The microcontroller continuously monitors the status of a given device.

• When the condition is met, it performs the service.

• After that, it moves on to monitor the next device until every one is serviced.

• The microcontroller check all devices in a round-robin fashion.

CPU

If INT0

Handle INT 0

YES

NO

If RI/TI=1

Handle Serial

YES

NO

If TF0=1NO

YES

Page 77: 1 Chapter 11 Interrupts Programming in Assembly and C

77

Interrupt Method

• An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service.

• Whenever any device needs its service, the device notifies the microcontroller by sending it an interrupt signal.

• Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves the device by executing the Interrupt Service Routine.

Page 78: 1 Chapter 11 Interrupts Programming in Assembly and C

78

The Advantage of Interrupts

• The use of microcontroller is more efficient.– Ex In polling system, HERE: JNB TI, HERE

wastes much of the microcontroller’s time.

• The microcontroller can monitor many devices simultaneously.

• Each device can get service based on the priority assigned to it.

• The microcontroller can ignore (mask) a device request.

Page 79: 1 Chapter 11 Interrupts Programming in Assembly and C

79

Section 11.6Interrupt Programming in C

Page 80: 1 Chapter 11 Interrupts Programming in Assembly and C

80

8051C Interrupt Numbers

• The 8051 C compilers have extensive support for the 8051 interrupts with two major features as follows:

– 1. They assign a unique number to each of the 8051 interrupts, as shown in Table 11-4.

– 2. It can also assign a register bank to an ISR. This avoids code overhead due to the pushes and pops of the R0-R7.

Page 81: 1 Chapter 11 Interrupts Programming in Assembly and C

81

Table 11–4 8051/52 Interrupt Numbers in C

Page 82: 1 Chapter 11 Interrupts Programming in Assembly and C

82

Example 11-14 (1/3)

Write an 8051 C program that continuously gets a single bit of data form P1.7 and sends it to P1.0. While simultaneously creating a square wave of 200 s period on pin P2.5.

Use timer 0 to create the square wave. Assume XTML=11.0592MHz.

Solution:

Using Timer 0, mode 2.

100s /1.085s =92

TH0=256-92=164=A4H

Switch

LED

P1.7P2.5

8051

P1.0

200s

Page 83: 1 Chapter 11 Interrupts Programming in Assembly and C

83

Example 11-14 (2/3)

#include <reg51.h>

sbit SW=P1^7;

sbit IND=P1^0;

sbit WAVE=P2^5;

//Interrup subroutine for creating WAVE

void timer0(void) interrupt 1

{

WAVE=~WAVE; //toggle pin

}

Page 84: 1 Chapter 11 Interrupts Programming in Assembly and C

84

Example 11-14 (3/3)

void main(void) {

//setup Timer Interrupt 1

TMOD=0x02;

TH0=0xA4; //TH0=-92

IE=0x82; //enable interrupts for timer 0

TR0=1; //start the timer 0

//setup SW IND SW=1; //make P1.7 an input pin

while(1) {

IND=SW; //send switch to LED

}}

Page 85: 1 Chapter 11 Interrupts Programming in Assembly and C

85

Example 11-15 (1/4)

Write a C program that continuously gets a single bit of data from P1.7 and sends it to P1.0 in the main, while simultaneously

(a) creating a square wave of 200 s period on pin P2.5.

(b) sending letter ‘A’ to the serial port.

Use timer 0 to create the

square wave.

Assume XTML=11.0592MHz.

Use the 9600 baud rate

Switch

LED

P1.7P2.5

8051

P1.0

200s

as same as Ex 11-14

TxDSerial Port

Page 86: 1 Chapter 11 Interrupts Programming in Assembly and C

86

Example 11-15 (2/4)

Solution:

#include <reg51.h>

sbit SW=P1^7;

sbit IND=P1^0;

sbit WAVE=P2^5;

//Interrup subroutine for creating WAVE

void timer0(void) interrupt 1

{

WAVE=~WAVE; //toggle pin

}

Page 87: 1 Chapter 11 Interrupts Programming in Assembly and C

87

Example 11-15 (3/4)

//Interrup subroutine for sending ‘A’

void serial0() interrupt 4

{

if (TI==1) {

SUBF = ‘A’; //send ‘A’

TI=0; //clear interrupt

}

else {

RI=0; //clear interrupt

}

}

Page 88: 1 Chapter 11 Interrupts Programming in Assembly and C

88

Example 11-15 (4/4)

void main(void) {

SW=1; //make P1.7 an input pin

TH1= -3; //9600 baud rate

TMOD=0x22;//mode 2 for both timers

TH0=0xA4; //TH0=-92

SCON=0x50;

TR0=1; TR1=1;

IE=0x92; //enable interrupts for timer 0

while(1) {

IND=SW; //send switch to LED

}}

Page 89: 1 Chapter 11 Interrupts Programming in Assembly and C

89

You are able to (1/2)

• Contrast and compare interrupts versus polling• Explain the purpose of the ISR (interrupt service

routine)• List the interrupts of the 8051• Enable or disable 8051 interrupts• Program the 8051 timers using interrupts• Describe the two external hardware interrupts of

the 8051/52

Page 90: 1 Chapter 11 Interrupts Programming in Assembly and C

90

You are able to (2/2)

• Contrast edge-triggered with level-triggered interrupts

• Program the 8051 for interrupt-based serial communication

• Define the interrupt priority of the 8051• Program 8051/52 interrupts in C