Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Programming Interrupts in 8051

The document outlines the programming of timer, external, and serial interrupts for microcontrollers, detailing the necessary steps and example code for each type. It explains how to configure registers, enable interrupts, and write interrupt service routines (ISRs). Additionally, it provides specific instructions for handling timer modes, edge/level triggering for external interrupts, and clearing flags for serial communication.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Programming Interrupts in 8051

The document outlines the programming of timer, external, and serial interrupts for microcontrollers, detailing the necessary steps and example code for each type. It explains how to configure registers, enable interrupts, and write interrupt service routines (ISRs). Additionally, it provides specific instructions for handling timer modes, edge/level triggering for external interrupts, and clearing flags for serial communication.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Timer Interrupts

1. Programming Timer Interrupts

The timer interrupts IT0 and IT1 are related to Timers 0 and 1, respectively. The interrupt
programming for timers involves following steps :

1. Configure TMOD register to select timer(s) and its/their mode.

2. Load initial values in THx and TLx for mode 0 and 1; or in THx only for mode 2.

3. Enable Timer Interrupt by configuring bits of IE register.

4. Start timer by setting timer run bit TRx.

5. Write subroutine for Timer Interrupt. The interrupt number is 1 for Timer0 and 3 for
Timer1.

Note that it is not required to clear timer flag TFx.

6. To stop the timer, clear TRx in the end of subroutine. Otherwise, it will restart from 0000H
in case of modes 0 or 1 and from initial values in case of mode 2.

7. If the Timer has to run again and again, it is required to reload initial values within the
routine itself (in case of mode 0 and 1). Otherwise after one cycle timer will start counting from
0000H.

Example code:
Timer interrupt to blink an LED; Time delay in mode1 using interrupt method

// Use of Timer mode0 for blinking LED using interrupt method

// XTAL frequency 11.0592MHz

#include<reg51.h>

sbit LED = P1^0; //LED connected to D0 of port 1

void timer(void) interrupt 1 //interrupt no. 1 for Timer 0

led=~led; //toggle LED on interrupt

TH0=0xFC; // initial values loaded to timer

TL0=0x66;

main()

TMOD = 0x01; // mode1 of Timer0

TH0 = 0xFC; // initial values loaded to timer

TL0 = 0x66;

IE = 0x82; // enable interrupt


TR0 = 1; //start timer

while(1); // do nothing

Programming External Interrupts


2. Programming External Interrupts

The external interrupts are the interrupts received from the (external) devices interfaced with the
microcontroller. They are received at INTx pins of the controller. These can be level triggered or edge
triggered. In level triggered, interrupt is enabled for a low at INTx pin; while in case of edge
triggering, interrupt is enabled for a high to low transition at INTx pin. The edge or level trigger is
decided by the TCON register. The TCON register has following bits:

Fig. 3: Bit Values of TCON Register of 8051 Microcontroller

Setting the IT0 and IT1 bits make the external interrupt 0 and 1 edge triggered respectively. By
default these bits are cleared and so external interrupt is level triggered.

Note : For a level trigger interrupt, the INTx pin must remain low until the start of the ISR and should
return to high before the end of ISR. If the low at INTx pin goes high before the start of ISR, interrupt
will not be generated. Also if the INTx pin remains low even after the end of ISR, the interrupt will be
generated once again. This is the reason why level trigger interrupt (low) at INTx pin must be four
machine cycles long and not greater than or smaller than this.

Following are the steps for using external interrupt:

1. Enable external interrupt by configuring IE register.

2. Write routine for external interrupt. The interrupt number is 0 for EX0 and 2 for EX1
respectively.

Example code:
//Level trigger external interrupt

void main()

IE = 0x81;

while(1);

void ISR_ex0(void) interrupt 0

{
<body of interrupt>

Example code:
//Edge trigger external interrupt

void main()

IE = 0x84;

IT1 = 1;

while(1);

void ISR_ex1(void) interrupt 2

<body of interrupt>

Programming Serial Interrupt


3. Programming Serial Interrupt

To use the serial, interrupt the ES bit along with the EA bit is set. Whenever one byte of data is sent
or received, the serial interrupt is generated and the TI or RI flag goes high. Here, the TI or RI flag
needs to be cleared explicitly in the interrupt routine (written for the Serial Interrupt).

The programming of the Serial Interrupt involves the following steps:

1. Enable the Serial Interrupt (configure the IE register).

2. Configure SCON register.

3. Write routine or function for the Serial Interrupt. The interrupt number is 4.

4. Clear the RI or TI flag within the routine.

Example code:
Send ‘A’ from serial port with the use of interrupt

// Sending ‘A’ through serial port with interrupt

// XTAL frequency 11.0592MHz

void main()

TMOD = 0x20;
TH1 = -1;

SCON = 0x50;

TR1 = 1;

IE = 0x90;

while(1);

void ISR_sc(void) interrupt 4

if(TI==1)

SBUF = ‘A’;

TI = 0;

else

RI = 0;

Example code:
// Receive data from serial port through interrupt

// XTAL frequency 11.0592MHz

void main()

TMOD = 0x20;

TH1 = -1;

SCON = 0x50;

TR1 = 1;

IE = 0x90;

while(1);

void ISR_sc(void) //interrupt 4

unsigned char val;

if(TI==1)

TI = 0;
}

else

val = SBUF;

RI = 0;

You might also like