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)

IE register in 8051

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.

tcon register in 8051

  • 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)

interrupt priority register in 8051

  • 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.

Interrupts in 8051

ISR examples

External Interrupt 0

Timer 0

External Interrupt 1

Timer 1

Serial Interrupt

Example programs for Interrupts in 8051

Blinking an LED using Timer interrupt in 8051

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



 

Liked this article! You'll love our video courses.
Checkout our comprehensive pre-recorded Video Courses for 8051 Microcontrollers
More details
Free Embedded C course with this course for a short period of time
Spread the love, share this