19 Interrupt Programming in Embedded C

Dr K. Vani

 

In this lecture embedded C program for interrupt handling will be discussed. Timer interrupt programming in Embedded C will be visited. Embedded C program for external hardware interrupt will be discussed in detail with examples.

 

1.   Interrupt Vs. Polling

 

External or internal event interrupts the microcontroller to inform it that a device needs its service. There are two ways to serving the device, they are interrupt and polling. Interrupt service begins when the device notifies the microcontroller by sending it an interrupt signal. After receiving an interrupt signal, the microcontroller stops whatever it is doing and serves the device. Program associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler. Polling happens while the microcontroller continuously monitors the status of a given device. When the condition is met, it performs the service. Next, it moves on to monitor the next device until everyone is serviced. Polling is not efficient, because it wastes much of the microcontroller’s time by checking devices that do not need service. Interrupt can serve many devices, each device can get the service of microcontroller based on priority; but in polling, there is no priority, only on round robin they can get the attention of microcontroller. Microcontroller can also ignore a device request for service, because each interrupt is having an interrupt service routine (ISR), or interrupt handler. This is not possible in the polling method

 

1.1 Interrupt Service Routine(ISR):

 

Each interrupt has an interrupt service routine (ISR), or interrupt handler. When an interrupt is called, the microcontroller runs the interrupt service routine. Every interrupt has a fixed location in memory that holds the address of the ISR. The group of memory locations that hold the addresses of ISRs is called interrupt vector table. Interrupt vector table for 8051 is shown in Table 1 below.

1.2 Six interrupts in the 8051.

 

Six interrupts in the 8051 are allocated as follows:

 

● Reset – When the reset pin is activated, the 8051 jumps into memory location 0000. This is the power-up reset.

 

● Two interrupts are set aside for the timers:One for timer 0 and one for timer 1.

 

● Two interrupts are set aside for hardware external interrupts. Pin numbers 12 (P3.2) and 13 (P3.3) in port 3 are for the external hardware interrupts INT0 (or EX1), and INT1 (or EX2).

 

● Serial communication has a single interrupt that belongs to both receive and transmit. The interrupt vector table location 0023H belongs to this interrupt.

 

1.3 Steps in enabling the interrupt

 

The following are the steps to enable the interrupt in 8051:

 

1.    Bit D7 of the IE register (EA) must be set to high to allow the rest of the register to take effect.

 

2.    If EA=1 enable all interrupts, EA=0 disable all interrupts even if the associated bit in the IE register is high.

 

The figure 1 shown below gives the details about IE (Interrupt Enable) registers.

 

1.4 8051 C interrupt numbers

 

The 8051 C compilers have extensive support for 8051 interrupt with two features. They are

 

●     Assign a unique number to each of the 8051 interrupts. It is shown in Table 2 shown below.

●     It can also assign a register bank to ISR. This avoids code overhead due to push and pop operation of the R0-R7 registers.

 

1.5 Programmable Timer Interrupt

 

The timer flag (TF) is raised when the timer rolls over.

 

● In polling mode, we have to wait until the TF is raised. The microcontroller is tied down while waiting for TF to be raised, and cannot do anything else.

 

● If the timer interrupt in the IE register is enabled, whenever the timer rolls over, the Timer Flag is raised. After that the microcontroller is interrupted in whatever it is doing, and jumps to the interrupt vector table to service the ISR. In this way, the microcontroller can do other work until it is notified that the timer has rolled over.

 

The figure 2 shown below gives the details about TF interrupt. If the timer interrupt is enabled, whenever TF=1, the microcontroller is interrupted in whatever it is doing, and jumps to the interrupt vector table to service the ISR.

 

The Example 1 gives details about generating a square wave using Timer 0 assuming that XTAL value is 11.0592 MHz

 

Example 1

 

Write a C program that continuously gets a single bit of data from 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 that XTAL = 11.0592 MHz

 

Solution:

 

We will use timer 0 in mode 2 (auto-reload). One half of the period is100 μs. 100/1.085 μs = 92, and TH0 = 256 – 92 = 164 or A4H.

 

#include <reg51.h>

sbit SW =P1^7;

  sbit IND =P1^0;

sbit WAVE =P2^5;

 

void timer0(void) interrupt 1

 

{

WAVE=~WAVE; //toggle pin

}

void main()

{

SW=1; //make switch input

TMOD=0x02;

TH0=0xA4; //TH0=-92

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

while (1)

{

IND=SW; //send switch to LED

}

}

 

1.6 Programming the Serial Communication Interrupt

 

There is only one interrupt for serial communication. It is used to both send and receive the data. If the interrupt bit in the IE register (IE.4) is enabled, when RI (Received interrupt) or TI (transfer interrupt) is raised, the 8051 gets interrupted and jumps to memory location 0023H to execute the ISR. In ISR, the programmer has to examine the TI and RI flags to see which one has caused the interrupt and take action accordingly. Serial interrupt mainly used for receiving and not for sending the data. The following figure 3 es Single interrupt for both TI and RI flag.

 

 

Example 2 described below is used for creating a square wave of 200 us period on pin P2.5 and sending letter ‘A’ to the serial port. It uses Timer0 to create the square wave.

 

Example 2:

 

Write a C program that continuously gets a single bit of data from P1.7 and sends it to P1.0, while simultaneously (a) creating a square wave of 200 us period on pin P2.5, and (b) sending letter ‘A’ to the serial port. Use Timer 0 to create the square wave. Assume that XTAL = 11.0592 MHz. Use the 9600 baud rate.

 

Solution:

 

2. Programming the External hardware interrupt

 

Two external hardware interrupts are available in 8051.They are Level triggered and Edge triggered interrupt. The interrupt vector table locations 0003H and 0013H are set aside for INT0 and INT1.

 

Level Triggered Interrupt:

 

The level-triggered or level-activated interrupt is the default mode upon reset of the 8051. INT0 and INT1 pins are normally high in the level trigger mode, while the low-level signal on INT0 or INT1 triggers the interrupt. Once the interrupt occurs, the microcontroller stops whatever it is doing and jumps to the interrupt vector table to service that interrupt. The low-level interrupt signal is to be removed before the execution of the last instruction of the ISR, RETI. If low level signal is not removed it results in another interrupt being generated.

 

Edge Triggered Interrupt:

 

To make INT0 and INT1 edge triggered interrupts, we must program the bits of the TCON register. The details of TCON register is given in Table 3.

 

2.1 Sampling the edge-triggered interrupt

 

The external source must be held high for at least one machine cycle, and then held low for at least one machine cycle. The falling edge of pins INT0 and INT1 are latched by the 8051 and are held by the TCON.1 and TCON.3 bits of TCON register. These function as interrupt-in-service flags. It indicates that the interrupt is being serviced now. On this INTn pin, no new interrupt will be responded to until this service is finished. When the ISRs are finished, TCON.1 and TCON.3 are cleared. The interrupt is finished and the 8051 is ready to respond to another interrupt on that pin. During the time that the interrupt service routine is being executed, the INTn pin is ignored, no matter how many times it makes a high-to-low transition. RETI clears the corresponding bit in TCON register (TCON.1 or TCON.3). There is no need for instruction CLR TCON.1 before RETI in the ISR associated with INT0.

 

Example 3 described below shows a C program to Light all LEDS connected to Port 0 if the switch is pressed and display “y” at port2.

 

Example 3:

 

A switch is connected to pin P3.2. When switch is pressed the corresponding line goes low. Write a C program to Light all LEDS connected to Port 0 if the switch is pressed. Display “y” at port2.

 

Solution:

 

#  include <reg51.h> Sbit switch = P3^2;

void extint0() // interrupt 0

{

P0=0xFF;

}

 

void main()

{

Switch=1;

IE=0x81;

While(1)

{

P2=“y”;

}

}

 

2.2 Interrupt Flag Bits for 8051

 

The TCON register holds four of the interrupt flags in the 8051. The interrupt flag bits for 8051 are shown in Table 4. The SCON register has the RI and TI flags.

 

Example 4 shown below gives the C program to receive data serially and send it to P0.

 

Example 4

 

Write a C program using interrupts to do the following: (a) Receive data serially and send it to P0 (b) Read port P1, transmit data serially, and give a copy to P2 (c) Make timer 0 generate a square wave of 5 kHz frequency on P0.1. Assume that XTAL = 11.0592 MHz. Set the baud rate at 4800.

 

Solution:

 

void main()

{

unsigned char x;

P1=0xFF; //make P1 an input

TMOD=0x22;

TH1=0xF6; //4800 baud rate

SCON=0x50;

TH0=0xA4; //5 kHz has T=200us

 

IE=0x92; //enable interrupts

TR1=1; //start timer 1

TR0=1; //start timer 0

while (1)

{

x=P1; //read value from pins

SBUF=x; //put value in buffer

P2=x; //write value to pins

}

}

 

Example 5 gives the details about how to write an Embedded C program to generate 10 KHz frequency wave and use timer 1 as an event counter to count up to 1-Hz pulse.

 

Example 5

 

Write a C program using interrupts to do the following:

(a) Generate a 10 KHz frequency on P2.1 using T0 8-bit auto-reload

 

(b)  Use timer 1 as an event counter to count up a 1-Hz pulse and display it on P0. The pulse is connected to EX1. Assume that XTAL = 11.0592 MHz. Set the baud rate at 9600.

Solution:

 

#include <reg51.h>

sbit WAVE =P2^1;

Unsigned char cnt;

void timer0() interrupt 1

{

WAVE=~WAVE; //toggle pin

}

void timer1() interrupt 3

{

cnt++; //increment counter

P0=cnt; //display value on pins

}

void main()

{

cnt=0; //set counter to 0

TMOD=0x42;

TH0=0x-46; //10 KHz

IE=0x86; //enable interrupts

TR0=1; //start timer 0

while (1); //wait until interrupted

}

 

3. Summary

 

In this lecture interrupt handling in 8051 has been discussed. Timer interrupt programming in 8051 is discussed with examples. External interrupt programming in embedded C is discussed in detail.

  1. References

 1. The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edition Muhammad Ali Mazidi, Janice Gillispie, Mazidi and Rolin D. McKinlay.