#include //Define 8051 Registers void serial(void); //Serial Communication Register void DelayMs(unsigned int); //Delay Function unsigned int i,j; unsigned char b[25],d; //--------------------------- // Main Program //--------------------------- void main() { EA=1; //Enable All Interrupt ES=1; //Enable Serial Port Interrupt serial(); //Serial Communication while(1); //Loop Forever } //------------------------------------------ // Serial Communication Register Initialisation
//------------------------------------------
void serial(void) { TMOD=0X20; //Timer1, Mode2 SCON=0X50; //Serial Mode1, Receive Enable TH1=0XFD; //Baud Rate 9600bps TR1=1; //Timer1 ON } //----------------------------------------- // Serial Interrupt Function //----------------------------------------- void serin (void) interrupt 4 //Serial Port Interrupt { if(RI==1) //Receive Interrupt Gets Enabled { //after Stop Bit get Received d=SBUF; //Serial Buffer value moved to a variable b[j]=d; SBUF=b[j]; DelayMs(20); //Delay Function j++; } SCON=0X50; //Initialising Receive and Transmit Interrupt } //--------------------------------- // Delay Function //--------------------------------- void DelayMs(unsigned int k) { unsigned int i; for(i=0;i<=k;i++); }
|
#include<string.h> int a,b,ans; char rec[4]; void main() { a=80; b=40; ans=a+b; sprintf(rec, "%d", ans); 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 { IE=0x00; short int i; for(i=0;i<=2;i++) //Transmitting data { SBUF=rec[i]; while(TI==0); TI=0; } IE=0x90; }
|