1 of 22

8051 Interrupts

Suresh.P.Nair , HOD - ECE , RCET

1

8051 Microcontroller

2 of 22

3 of 22

Hardware’s view of interrupts

3

Bus

4 of 22

Cascading the 8259 Chip

4

INTA*

5 of 22

General flow

Main

Program

Interrupt

Service

Routine

Process in execution

Process requires I/O service

I/O Interrupt

Returns

6 of 22

7 of 22

CPU Program Organisation

16:

MOV R0, 0x8000

17:

# modifies R0

18:

MOV 0x0001, R0

19:

RETI # ISR return

ISR

100:

101:

instruction

...

Main program

...

Program memory

instruction

Interrupt Service Routine

Main Program

8 of 22

Interrupt

8

External Device

Interrupt Routine

Instruction#1 execution

Instruction#2 execution

Instruction#3 execution

Instruction#4 execution

Instruction#5 execution

RTI

1. INTERRUPT

3. Enter interrupt service routine

2. Complete current

instruction execution

5. Return to main program and resume next instruction

(RTI: Return from Interrupt

6. Complete next

instruction execution

4. Execute interrupt routine

9 of 22

INTERRUPTS

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

  • A single microcontroller can serve several devices by two ways:
                • Interrupt
                • Polling

Suresh.P.Nair , HOD - ECE , RCET

9

8051 Microcontroller

10 of 22

Interrupt Vs Polling

  1. Interrupts
    • 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.
    • The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.
  2. Polling
    • The microcontroller continuously monitors the status of a given device.
    • When the conditions met, it performs the service.
    • After that, it moves on to monitor the next device until every one is serviced.

Suresh.P.Nair , HOD - ECE , RCET

10

8051 Microcontroller

11 of 22

Interrupt Vs Polling

  • The polling method is not efficient, since it wastes much of the microcontroller’s time by polling devices that do not need service.
  • The advantage of interrupts is that the microcontroller can serve many devices (not all at the same time).
  • Each devices can get the attention of the microcontroller based on the assigned priority.
  • For the polling method, it is not possible to assign priority since it checks all devices in a round-robin fashion.
  • The microcontroller can also ignore (mask) a device request for service in Interrupt.

Suresh.P.Nair , HOD - ECE , RCET

11

8051 Microcontroller

12 of 22

Steps in Executing an Interrupt

  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 (i.e: not on the stack).
  3. It jumps to a fixed location in memory, called the interrupt vector table, that holds the address of the ISR.
  4. The microcontroller gets the address of the ISR from the interrupt vector table and jumps to it.
  5. It starts to execute the interrupt service subroutine 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.

Suresh.P.Nair , HOD - ECE , RCET

12

8051 Microcontroller

13 of 22

Six Interrupts in 8051

Six interrupts are allocated as follows:

  1. Reset – power-up reset.
  2. Two interrupts are set aside for the timers.
    • one for timer 0 and one for timer 1
  3. Two interrupts are set aside for hardware external interrupts.
    • P3.2 and P3.3 are for the external hardware interrupts INT0 (or EX1), and INT1 (or EX2)
  4. Serial communication has a single interrupt that belongs to both receive and transfer.

Suresh.P.Nair , HOD - ECE , RCET

13

8051 Microcontroller

14 of 22

What events can trigger Interrupts?

  • We can configure the 8051 so that any of the following events will cause an interrupt:

    • Timer 0 Overflow.
    • Timer 1 Overflow.
    • Reception/Transmission of Serial Character.
    • External Event 0.
    • External Event 1.

  • We can configure the 8051 so that when Timer 0 Overflows or when a character is sent/received, the appropriate interrupt handler routines are called.

Suresh.P.Nair , HOD - ECE , RCET

14

8051 Microcontroller

15 of 22

8051 Interrupt Vectors

Suresh.P.Nair , HOD - ECE , RCET

15

8051 Microcontroller

16 of 22

8051 Interrupt related Registers

  • The various registers associated with the use of interrupts are:
    • TCON - Edge and Type bits for External Interrupts 0/1

    • SCON - RI and TI interrupt flags for RS232

    • IE - Enable interrupt sources

    • IP - Specify priority of interrupts

Suresh.P.Nair , HOD - ECE , RCET

16

8051 Microcontroller

17 of 22

Enabling and Disabling an Interrupt

  • Upon reset, all interrupts are disabled (masked), meaning that none will be responded to by the microcontroller if they are activated.

  • The interrupts must be enabled by software in order for the microcontroller to respond to them.

  • There is a register called IE (interrupt enable) that is responsible for enabling (unmasking) and disabling (masking) the interrupts.

Suresh.P.Nair , HOD - ECE , RCET

17

8051 Microcontroller

18 of 22

Interrupt Enable (IE) Register

Suresh.P.Nair , HOD - ECE , RCET

18

8051 Microcontroller

  • EA : Global enable/disable.
  • --- : Reserved for additional interrupt hardware.
  • ES : Enable Serial port interrupt.
  • ET1 : Enable Timer 1 control bit.
  • EX1 : Enable External 1 interrupt.
  • ET0 : Enable Timer 0 control bit.
  • EX0 : Enable External 0 interrupt.

MOV IE,#08h�or

SETB ET1

--

19 of 22

Enabling and Disabling an Interrupt

  • Example: Show the instructions to (a) enable the serial interrupt, timer 0 interrupt, and external hardware interrupt 1 and (b) disable (mask) the timer 0 interrupt, then (c) show how to disable all the interrupts with a single instruction.
  • Solution:
    • (a) MOV IE,#10010110B ;enable serial, timer 0, EX1
      • Another way to perform the same manipulation is:
        • SETB IE.7 ;EA=1, global enable
        • SETB IE.4 ;enable serial interrupt
        • SETB IE.1 ;enable Timer 0 interrupt
        • SETB IE.2 ;enable EX1
    • (b) CLR IE.1 ;mask (disable) timer 0 interrupt only
    • (c) CLR IE.7 ;disable all interrupts

Suresh.P.Nair , HOD - ECE , RCET

19

8051 Microcontroller

20 of 22

Interrupt Priority

  • When the 8051 is powered up, the priorities are assigned according to the following.

  • In reality, the priority scheme is nothing but an internal polling sequence in which the 8051 polls the interrupts in the sequence listed and responds accordingly.

Suresh.P.Nair , HOD - ECE , RCET

20

8051 Microcontroller

21 of 22

Interrupt Priority

  • We can alter the sequence of interrupt priority by assigning a higher priority to any one of the interrupts by programming a register called IP (interrupt priority).
  • To give a higher priority to any of the interrupts, we make the corresponding bit in the IP register high.

Suresh.P.Nair , HOD - ECE , RCET

21

8051 Microcontroller

22 of 22

Interrupt Priority (IP) Register

Suresh.P.Nair , HOD - ECE , RCET

22

8051 Microcontroller

PS PT1 PX1 PT0 PX0

Reserved

Serial Port

Timer 1 Pin

INT 1 Pin

Timer 0 Pin

INT 0 Pin

Priority bit=1 assigns high priority

Priority bit=0 assigns low priority