INTERRUPTS IN 8051

Page 4

Programming an Interrupt

For example if we are programming interrupt INT0, then first we have enable flags in registers as given below

Enable global interrupt i.e. EA = 1 in the Interrupt Enable (IE) Register. This will allow us to enable or work with any interrupt. After enabling this flag we have to enable specific interrupt like INIT0 or INT1 etc..

Enable external interrupt i.e. EX0 = 1 in the Interrupt Enable(IE) Register.

Enable interrupt trigger mode i.e. whether the interrupt is edge triggered or level triggered, here we will use falling edge trigger interrupt, so make IT0 = 1 in the TCON register. These steps will enable INT0 as falling triggered Interrupt, if want to make INT0 as Edge triggered Interrupt then make IT0 = 0 in the TCON register.

Example for  external interrupt ISR for 8051

#include <reg51.h>
sbit LED = P1^0;                       /* set LED on port1 */
void External_INT0_ISR() interrupt 0
{
LED = ~LED;         /* Toggle LED pin ON or OFF for falling edge on INT0 pin */
} 
/* Other Interrupt ISR has to be defined as below */
/*
void Timer0_ISR() interrupt 1
{
}
void External_INT1_ISR() interrupt 2
{
}
void Timer1_ISR() interrupt 3
{
}
void Serial_INT_ISR() interrupt 4
{
}
*/
void main()
{
EA  = 1;   /* In the Interrupt Register, Enable global interrupt */
EX0 = 1;   /* In the Interrupt Register Enable External interrupt0 */
IT0 = 1;   /* In the TCON Register make External interrupt0 on falling edge */
/*Initialize Interrupt registers */ 
Ext_int_Init(); /* Call Ext. interrupt initialize */
while(1);
} 

Programming Timer Interrupt

As we know there are two Timer interrupts Timer0 and Timer1. To program a timer interrupt we need to do following configurations.

Selecting the timer by configuring TMOD register and its mode of operation.

Choosing and loading the initial values of TLx and THx for appropriate modes.

Enabling the IE registers and corresponding timer bit in it.

Setting the timer run bit to start the timer. Writing the subroutine for the timer for time required and clear timer value TRx at the end of subroutine.

#include <reg51.h>
sbit LED = P2^0;         /* set LED on port2 0th pin */
void Timer0_ISR() interrupt 1   // Interrupt 1 for Timer 0
{
LED = ~LED;  // Blink Led on Interrupt
TH0 = 0xFC;   // Loading Initial values to timer
TL0 = 0x66;
}
void main()
{
TM0D = 0x01;     // Mode 1 of Timer 0
TH0  = 0xFC;   // Loading Initial values to timer
TL0  = 0x66;
EA   = 1;   // In the Interrupt Register, Enable global interrupt 
ET0  = 1;   // Enable Timer 0 Interrupt
TR0  = 1;   //  Start Timer  
while(1);
} 

Goto Page 3

Leave a Reply