Serial Interrupt Programming in 8051 Microcontroller

serial-interrupt-programming-in-8051-microcontroller
Serial Interrupt using 8051

Serial interrupt programming in 8051 plays a significant role since it was used to perform interrupt operation through UART protocol. It plays a significant role in Embedded system Design where the controller has to perform a certain tasks based on the incoming character through the UART. Before going through this tutorial you must be familiar with Serial Communication in 8051 Microcontroller to get a clear idea about Serial Communication.

IE REGISTER:

IE register-8051

IE= 1001 0000This IE register is responsible for all activating all the interrupts in 8051. For serial Interrupt we need to activate EA which is enabling activating usage of interrupts and ES for activating Serial Interrupts. So the value of IE will be
IE= 0x90
 
Every Interrupt Service Routine must be inside a sub routine followed by the respective interrupt numbers. The below table will shows the interrupt numbers for respective interrupts in 8051.
Interrupt number-table

STEPS TO PROGRAM SERIAL INTERRUPT:

  1. Initiate Timer to generate Baud rate for the Serial communication, and TH1 register should be loaded with the value 0xFD to generate baud rate of 9600 bps.
  2. Initiate SCON register to select the mode of serial communication, i have chosen Mode 1 since i have used timer 1 for Baud rate generation.
  3. Load the value 0x90 into the IE register to initiate the Serial interrupt.
  4. Write sub routine for the interrupt with the interrupt number 4.

Usually every key you press in your hyper terminal reaches the controller as a Hex value. This will also allow you to place conditional statements inside the subroutine to make the controller to respond only to specific key press from the terminal rather than from all the keys.

CODE:

The code was built using Keil C Software
#include<regx51.h>
void main()
  {
   TMOD=0x20;                                //Choosing Timer mode
   TH1=0xFD;                                   //Selecting Baud Rate
   SCON=0x50;                               //Serial mode selection
   TR1=1;
   IE=0x90;                                      //Enabling Serial Interrupt
   while(1);
   }
void ser_intr(void)interrupt 4        //Subroutine for Interrupt
 {
  char c;
  c=SBUF;  
  IE=0x00;                      //Turning off interrupt to prevent recursion
   if(c==0x0d)
   {
   P0=~P0;                                
   SBUF='A';                 //Sending back "ACK" as   Acknowledgement 
   while(TI==0);
   TI=0;
   SBUF='C';
   while(TI==0);
   TI=0;
   SBUF='K';
    while(TI==0);
    TI=0;
    }
   RI=0;
   IE=0x90;                            //Reactivating the interrupt
  }

All the initialization of the Timers and Interrupts took place in the main() function of the program. In the subroutine “ser_intr” i have used a condition to check received variable from interrupt as “0x0D“. This “0x0D” is a equivalent of “Enter” key in the keyboard, so whenever you press enter the Microcontroller toggle the port 0 and Send ACK back to the terminal.

You can use condition for any key in the keyboard by identifying the HEX values of the respective keys in your keypad. This will help you to perform specific tasks with each key press.


7 Comments

  1. sourabh sharma

    you have given zero value to IE for the prvention of recursion , but how it’s going to call ISR further .., and what will be the effect- if i do not make IE=0x00..plz explain.

    Reply
  2. ragahv

    when i enter a character from keyboard code works ,but i cannot see it on the hyperterminal what i entered when using uart interrupts

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Hi Raghav, You mean to say that character from Mcu is not displayed in Hyperterminal? If so kindly check the hardware connections of your MCU TX.

      Reply
  3. Basheer

    are we able to use the incoming character the one which causes the interrupt?

    Reply
    1. Frank Donald

      Of course you can, read the value like i did “c=SBUF” in the 14th line and then use it for any purpose.

      Reply
    2. Basheer

      Thanks great tutorial

      Reply
    3. Frank Donald

      Welcome.

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *