Serial Interrupt Programming in 8051 Microcontroller

Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest
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.

Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest

Related Designs and Schematics

The following two tabs change content below.

Frank Donald

Junior Network Engineer at Tata Communications
Frank Donald is an Electronics and Communication Engineer who is very passionate about his subject. Reading, Surfing and Blogging are the things he loves.

Ask your query

4 Comments on "Serial Interrupt Programming in 8051 Microcontroller"

Notify of
avatar
Sort by:   newest | oldest | most voted
Basheer
Guest

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

Frank Donald
Guest

Welcome.

Basheer
Guest

Thanks great tutorial

Frank Donald
Guest

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

wpDiscuz