Lab5 Interupt 8051 2023
Lab5 Interupt 8051 2023
1. Objectives:
To learn interrupt programming of 8051 microcontroller
2. Theory
There are five interrupt sources for the 8051. Since the main RESET input can also be
considered as an interrupt, six interrupts in the order of priority can be listed as follows:
When an interrupt is received, the controller stops after executing the current instruction.
It transfers the content of the program counter into the stack. It also stores the current
status of the interrupts internally but not on the stack.
RESET interrupt
This is also known as Power-on Reset (POR). When the RESET interrupt is received, the
controller restarts executing code from the 0000H location. This is an interrupt that is not
available to or, better to say, need not be available to the programmer.
Timer interrupts
Each Timer is associated with a Timer interrupt. A timer interrupt notifies the
microcontroller that the corresponding Timer has finished counting.
External interrupts
There are two external interrupts EX0 and EX1 to serve external devices. Both these
interrupts are active low. In AT89C51, P3.2 (INT0) and P3.3 (INT1) pins are available
for external interrupts 0 and 1 respectively. An external interrupt notifies the
microcontroller that an external device needs its service.
Serial interrupt
This interrupt is used for serial communication. When enabled, it notifies the controller
whether a byte has been received or transmitted.
Before going into programming we have to go through the registers used in the Interrupt.
Registers used for Interrupt
IE Register (Interrupt Enable Register)
IP Register (Interrupt Priority Register)
TCON Register (Timer Control Register) – This is used for External Interrupts only.
IE Register (Interrupt Enable Register)
This register is responsible for enabling and disabling the interrupt. EA register is set to
one for enabling interrupts and set to 0 for disabling the interrupts. Its bit sequence and its
meanings are shown in the following figure.
To enable any of the interrupts, first, the EA bit must be set to 1. After that, the bits
corresponding to the desired interrupts are enabled. ET0, ET1, and ET2 bits are used to
enable the Timer Interrupts 0, 1, and 2, respectively. In AT89C51, there are only two
timers, so ET2 is not used. EX0 and EX1 are used to enable the external interrupts 0 and
1. ES is used for the serial interrupt.
EA bit acts as a lock bit. If any of the interrupt bits are enabled but EA bit is not set, the
interrupt will not function. By default, all the interrupts are in disabled mode.
Note that the IE register is a bit addressable and individual interrupt bits can also be
accessed.
For example –
IE = 0x81; enables External Interrupt0 (EX0)
IE = 0x88; enables Serial Interrupt
IP (Interrupt Priority) Register
The 8051 offers two levels of interrupt priority: High and Low. By using interrupt
priorities you may assign higher priority to certain interrupt conditions. We can change
the priority levels of the interrupts by changing the corresponding bit in the Interrupt
Priority (IP) register as shown in the following figure.
A low priority interrupt can only be interrupted by the high priority interrupt, but not
interrupted by another low priority interrupt.
If two interrupts of different priority levels are received simultaneously, the request of a
higher priority level is served.
If the requests of the same priority levels are received simultaneously, then the internal
polling sequence determines which request is to be serviced.
Here MSB four bits are used for Timers. But LSB four bits are used for External
Interrupts. We will see that bits.
Setting the IT0 and IT1 bits makes the external interrupt 0 and 1 edge-triggered
respectively. By default, these bits are cleared and so the 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 the ISR. If the low at INTx pin goes high
before the start of ISR, an 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 the level trigger interrupt (low) at the INTx pin must be four machine cycles
long and not greater than or smaller than this.
So, these all are the registers used in the Interrupt. These registers are not enough for play
with interrupt. We have to write the ISR or Interrupt Handler.
ISR or Interrupt Handler – 8051 Interrupts
Setting the bits of IE register is necessary and sufficient to enable the interrupts. The next
step is to specify to the controller what to do when an interrupt occurs. This is done by
writing a subroutine or function for the interrupt. This is the ISR and gets automatically
called when an interrupt occurs. It is not required to call the Interrupt Subroutine
explicitly in the code.
An important thing is that the definition of a subroutine must have the
keyword interrupt followed by the interrupt number. A subroutine for a particular
interrupt is identified by this number.
Programming (Timer 0 Interrupt) – 8051 Interrupts
Timer interrupts to blink an LED; Time delays in mode 1 using interrupt method.
#include<reg51.h>
sbit LED = P1^0; //LED connected to P1.0
Void 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
}
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;
}
Programming (Serial Interrupt) – 8051 Interrupts
Send ‘A’ from the serial port when it receives anything via Rx.
#include<reg51.h>
void main()
{
TMOD = 0x20;
TH1 = TL1=0xfd;
SCON = 0x50;
TR1 = 1;
IE = 0x90;
while(1);
}
#include<reg51.h>
void delay();
int i,a;
void main()
{
P1=P2=P0=0x00;
IP=0x04;
EA=1;
EX0=1;
ET0=1;
EX1=1;
TMOD=0X01;
TH0=0X4b;
TL0=0Xfd;
TR0=1;
while(1);
}
3. Lab exercise
3.1. Use Timer1 interrupt to blink the LED every 2ms - 1 time.
3.2. Use a push button to simulate a product counter with a 7-segment LED display.
Name: Student Code: Class: Lab:
1. Circuit
2. Algorithm flowchart
3. Code and explanation
4. Summary