Wednesday 13 March 2013

program for serial interrupt using 8051

------------------------------------------------------------------------------------------------------------
/************************serial interrupt*******************************/

#include<reg51.h>

static unsigned char a[20],i;
bit flag;
void delay1(unsigned int );                       //Declaration of delay function
void send_data_tx(unsigned char *);        //Declaration of Transmit function for string
void serial_data(unsigned char);              // Declaration of Transmit function for char             
void sconfig();                                         // Declaration of serial configuration function   
void clear();
                 
void serial(void) interrupt 4 using 1
{
      if(RI)                                         //by receiving character into SBUF, RI will                         {                                                // become 1

             a[i]=SBUF;                  // moving Data from SBUF to array
         
                  RI=0;                            //clear the RI bit
                             
if(a[i]==0x0d)             //until SBUF Data become 0x0d(Enter key)
                  {
                              a[i]='\0';                

                              flag=0;             //clearing flag
                  }
                  i++;                              //incrementing the index to store new char      
      }
      RI=0;
}

void main()
{
           sconfig();                               //serial configuration
           clear();                                  //clearing array
           EA=0;ES=1;                         // interrupt is not activated but serial is enabled
           while(1)
            {
                    EA=1;                             //interrupt is enabled
                    while(flag==0)                //chking for interrupt to happen
                     {
                        flag=1;
                        EA=0;                         //disabling interrupt
                        send_data_tx(a);          // transmitting array
                        clear();                         //clearing array
                     }
             }

}
void sconfig()
{
      TMOD =0x20;     //Declaring or initialization of timer 1 in mode 2
      TH1=0xfd;           // loading the value to generate baud rate 9600
      SCON=0x50;      // Declaring or initialization of SCON
      TR1=1;               // to start the timer
}
 /********function used for clearing the array( a[] )***/
void clear()
{
      for(i=0;i<20;i++)
      {
a[i]='\0';               // Placing Null character in array
}
      i=0;                                                   
}   
/***function used for Transmitting the string data serially***/
 void send_data_tx(unsigned char *msg)
 {
         while ( *msg!='\0')                                  //checking up to null char
           {
                     serial_data(*msg);
                     msg++;
                     delay1(10);
            }
}
void serial_data(unsigned char p)
   {
           SBUF=p;                    //transmitting data serially through SBUF
           while(TI==0);            //monitoring TI bit until TI equal to 1
           TI=0;                          //clear the TI bit
   }            
void delay1(unsigned int t)
{
unsigned int i,j;
for(i=0; i<t; i++)
for(j=0; j<1275; j++);
}
-------------------------------------------------------------------------------------------------------

No comments:

Post a Comment