Categories: Firmware Guide-8051

Interrupts in 8051

Interrupts are the events that temporarily suspend the main program, pass the control to other functions or sources and execute their task. It then passes the control to the main program where it had left off.
As code size increases and your application handles multiple modules, sequential coding would be too long and too complex. The interrupt mechanism helps to embed your software with hardware in a much simpler and efficient manner. In this topic, we will discuss the interrupts in 8051 using AT89S52 microcontroller.

When an interrupt is received, the controller stops after executing the current instruction. It transfers the content of the program counter into the stack.

Interrupts in 8051 includes :

  • Reset
  • External Interrupt 0
  • Timer/Counter 0
  • External Interrupt 1
  • Timer/Counter 1
  • Serial Interrupt

Each interrupt can be enabled or disabled by setting bits of the IE register and the whole interrupt system can be disabled by clearing the EA bit of the same register.

Reset interrupt

When the RESET interrupt is received, the controller restarts executing code from 0000H location. This interrupt is not available to the programmer.

Timer interrupts

Each Timer is associated with a Timer interrupt. When a timer has finished counting, the Timer interrupt will notify the microcontroller by setting the required flag bit.

External interrupt

8051 based AT89S52 microcontroller has two active-low external interrupts, INT0 and INT1.
See more information here: External Interrupt in 8051 microcontroller

Serial interrupt

This interrupt is used for serial communication. When enabled, it notifies the controller when a byte has been received or transmitted according to how the interrupt is configured.

Registers in AT89S52 for interrupts.

Now let’s go through the registers used for configuring interrupt in AT89S52 microcontroller. All the above-mentioned interrupts except reset interrupt can be individually enabled or disabled by setting or clearing a bit in Special Function Register IE. IE also contains a global disable bit, EA, which disables all interrupts at once. The priority of each interrupt can be configured using the IP register.

IE register (Interrupt Enable Register)

EA bit enables or disables all interrupt sources (globally):

  • 0 – disables all interrupts (even enabled).
  • 1 – enables specific interrupts.

EX0 bit enables or disables External interrupt 0:

  • 0 – External interrupt 0 disabled.
  • 1 – External interrupt 0 enabled.

ET0 bit enables or disables Timer T0 interrupt:

  • 0 – Timer T0 interrupt disabled.
  • 1 – Timer T0 interrupt enabled.

EX1 bit enables or disables External interrupt 1:

  • 0 – External interrupt 1 disabled.
  • 1 – External interrupt 1 enabled.

ET1 bit enables or disables Timer 1 overflow interrupt:

  • 0 – Timer 1 overflow interrupt disabled.
  • 1 – Timer 1 overflow interrupt enabled.

ES bit enables or disables serial port interrupt :

  • 0 – serial port interrupt disabled.
  • 1 – serial port interrupt enabled.

ET2 bit enables or disables Timer 2 overflow interrupt :

  • 0 – Timer 2 overflow interrupt disabled.
  • 1 – Timer 2 overflow interrupt enabled.

TCON

TCON register is also one of the registers whose bits are directly in control of timer operation. The 4 bits in LSB is used for interrupt control.

  • TF1 bit is automatically set when the Timer 1 overflow.
  • TR1 bit enables the Timer 1.
    • 1 – Timer 1 is enabled.
    • 0 – Timer 1 is disabled.
  • TF0 bit is automatically set when the Timer 0 overflow.
  • TR0 bit enables the timer 0.
    • 1 – Timer 0 is enabled.
    • 0 – Timer 0 is disabled.
  • IE1 – External Interrupt 1 edge detection flag.
    • This bit is set by the processor when there is an interrupt at INT1.
    • It is cleared by the processor when there is a jump to Interrupt Service Routine (ISR), i.e. interrupt is processed.
  • IT1
    • 1 – interrupt triggered by a falling edge.
    • 0 – interrupt triggered by a low level.
  • IE0 – External Interrupt 0 edge detection flag.
    • This bit is set by the processor when there is an interrupt at INT0.
    • It is cleared by the processor when there is a jump to Interrupt Service Routine (ISR), i.e. interrupt is processed.
  • IT0
    • 1 – interrupt triggered by a falling edge.
    • 0 – interrupt triggered by a low level.

IP Register (Interrupt Priority Register)

  • PT2: It defines the Timer 2 interrupt priority level (8052 only).
  • PS: It defines the serial port interrupt priority level.
  • PT1: It defines the Timer 1 interrupt priority level.
  • PX1: It defines the external interrupt priority level.
  • PT0: It defines the Timer 0 interrupt priority level.
  • PX0: It defines the external interrupt 0 priority level.

Interrupt Subroutine ( ISR )

Once all the configurations are done, the next step is to write the functions to execute when an interrupt occurs. And this is done by writing ISR functions. This function gets called automatically when an interrupt occurs.

While writing the ISR, The function definition must have the keyword ‘Interrupt’ followed by the interrupt number. Interrupt number is unique for each interrupt signal and a subroutine for a particular interrupt is identified by this number.

ISR examples

External Interrupt 0

Void ISR(void) interrupt 0
{
    <Body of ISR>
}

Timer 0

Void timer0 (void) interrupt 1
{
    <Body of ISR>
}

External Interrupt 1

Void ex0 (void) interrupt 2
{
    <Body of ISR>
}

Timer 1

Void timer1_ISR (void) interrupt 3
{
    <Body of ISR>
}

Serial Interrupt

Void serial (void) interrupt 4
{
    <Body of ISR>
}

Example programs for Interrupts in 8051

Blinking an LED using Timer interrupt in 8051

#include<reg51.h>
#define LED P1;

Void main()
{
    TMOD = 0x01;        
    TH0 = 0xFC;     
    TL0 = 0x66;
    IE = 0x82;      // enable interrupt
    TR0 = 1;        //start timer
    while(1);       
}

void timer0(void) interrupt 1        //interrupt number 1 for Timer 0
{
    LED = ~LED;       //Toggles the LED on interrupt
    TH0=0xFC;       
    TL0=0x66;
}

External Interrupt

Visit: External Interrupt in 8051 microcontroller

Serial Interrupt

The below example will display the ASCII value of character received serially using LEDs connected in port 0.

  • In the main function, the value of the variable LED is given to port 0 inside the while(1) loop.
  • When a character is received, the interrupt occurs and the ISR function will be executed.
  • The received data is copied to variable ‘LED’. So the LEDs in port 0 turns on accordingly.

Serial Interrupt Example Program


[fusion_global id=”18383″]

 

Share