Interrupts in 8051
What is an Interrupt?
An interrupt is a signal that temporarily halts the normal execution of a program so that the microcontroller can respond to an urgent task (like a button press ). After handling the interrupt, it resumes normal execution.
Main Interrupt-Related Registers and Their Functions:
1. IE(Interrupt Enable):- IE stands for Interrupt Enable Register. It is an 8-bit special function register that enables or disables individual interrupts as well as all interrupts globally.
EA |
---- |
---- |
ES |
ET1 |
EX1 |
ET0 |
EX0 |
EA:- EA is the master enable.
If EA = 0
, all interrupts are disabled, no matter the other bits.
If EA = 1
, interrupts are enabled based on other IE bits.
Bit 6 and Bit 5:- These bits are not used in the standard 8051 microcontroller. They are reserved.
ES:- ES means Enable Serial Interrupt.
Triggered when either the TI (Transmit Interrupt) or RI (Receive Interrupt) flag is set in UART.
ET1:- ET1 means Enable Timer 1 Interrupt.
When ET1 = 1, it enables the interrupt for Timer 1. If Timer 1 overflows and ET1 is set, the microcontroller will jump to the Timer 1 ISR (Interrupt Service Routine). The overflow is detected by the TF1 (Timer 1 Flag) in the TCON register.
EX1:- Ex1 means Enable External Interrupt 1.
Enables the external interrupt 1 (connected to pin P3.3 / INT1). It works with the TCON register bit IT1 to determine whether the interrupt is edge-triggered or level-triggered. If IT1 is set to 0, it will go to the interrupt block when the switch is fully pressed and held down (level-triggered mode). If IT1 is set to 1, then as soon as the switch starts moving down (i.e., when the pin goes from HIGH to LOW), it will immediately jump to the interrupt block.
ET0:- ET0 means Enable Timer 0 Interrupt.
When ET0 = 1, it enables the interrupt for Timer 0. If Timer 0 overflows and ET0 is set, the microcontroller will jump to the Timer 0 ISR (Interrupt Service Routine). The overflow is detected by the TF0 (Timer 0 Flag) in the TCON register.
EX0:- Ex0 means Enable External Interrupt 0.
Enables the external interrupt 0 (connected to pin P3.2 / INT0). It works with the TCON register bit IT0 to determine whether the interrupt is edge-triggered or level-triggered. If IT0 is set to 0, it will go to the interrupt block when the switch is fully pressed and held down (level-triggered mode). If IT0 is set to 1, then as soon as the switch starts moving down (i.e., the pin goes from HIGH to LOW), it will immediately jump to the interrupt block.
2. IP(Interrupt Priority):- IP stands for Interrupt Priority register in the 8051 microcontroller. It is an 8-bit Special Function Register (SFR) that determines which interrupt has higher priority when multiple interrupts occur at the same time.
---- |
---- |
---- |
PS |
PT1 |
PX1 |
PT0 |
PX0 |
PS:- PS stands for serial port Interrupt priority. Controls the priority of Serial Interrupt (RI/TI).
PS = 1
: Serial interrupt gets high priority.
PS = 0
: Serial interrupt has low priority.
PT1:- PT1 stands for Timer 1 Interrupt priority. Sets the priority level for Timer 1 interrupt.
PT1 = 1
: Timer 1 has high priority.
PT1 = 0
: Timer 1 has low priority.
PX1:- PX1 stands for External Interrupt 1 Priority. Controls priority of External Interrupt 1 (INT1, P3.3).
PX1 = 1
: INT1 has high priority.
PX1 = 0
: INT1 has low priority.
PT0:- PT0 stands for Timer 0 Interrupt priority. Sets the priority level for Timer 0 interrupt.
PT0 = 1
: Timer 0 has high priority.
PT0 = 0
: Timer 0 has low priority.
PX0:- PX0 stands for External Interrupt 0 Priority. Controls the priority of External Interrupt 0 (INT0, P3.2).
PX0 = 1
: INT0 has high priority.
PX0 = 0
: INT0 has low priority.
Task 1:- Using an external interrupt, toggle the LED.
Code:-
#include<reg51.h>
sbit led=P2^0;
void interruptt();
void delay(unsigned int t);
void main()
{
IE=0x81;// or EA=1; EX0=1
IT0=0;
led=1;
while(1);
}
void interruptt() interrupt 1
{
led=!led;
delay(1);
}
void delay(unsigned int t)
{
int i,j;
for(i=0;i<100*t;i++)
{
for(j=0;j<1257;j++)
{
}
}
}
Output:-
0 Comments