8051 Has Serial -Universal Asynchronous Transiever(UART) i.e it works in Full Duplex Mode
The register SBUF is used to hold the data. The special function register SBUF is physically two registers. One is, write-only and is used to hold data to be transmitted out of the 8051 via TXD. The other is, read-only and holds the received data from external sources via RXD. Both mutually exclusive registers have the same address 099H. SCON REGISER:
SM0 SM1 MODE 0 0 0 SHIFT REGISTER(BAUDRATE IS 1/12 OF OSC FREQ) MODE 1 0 1 10-BIT UART(DETERMINED BY TIMER 1) MODE 3 1 0 11-BIT UART(1/32,1/64) MODE 3 1 1 11-BIT UART(DETERMINED BY TIMER 1) SM2-MULTIPROCESSOR COMMUNICATION ENABLE BIT
| ||||||||||||||||||
SOURCE CODE FOR MULTIPROCESSOR COMMUNICATION MASTER MODE
#include<lcd.h>
void tx_delay(int);
void transmit_string(char *);
void transmit_addr(unsigned char);
void main()
{
char *r1="MASTER:TX TO";
char *r2="SLAVE ONE RCVING...";
char *r3="SLAVE TWO RCVING...";
TMOD=0X21;
SCON=0XF0;
TH1=0XFD;
TR1=1;
init_8bit();
hide_cursor;
string_8bit(r1);
while(1)
{
xy_8bit(1,0);
string_8bit("SLAVE2");
transmit_addr(0x32);
tx_delay(100);
transmit_string(r2);
tx_delay(100);
xy_8bit(1,0);
string_8bit("SLAVE1");
transmit_addr(0x31);
tx_delay(100);
transmit_string(r3);
tx_delay(100);
}
}
void transmit_string(char *s)
{
TB8=0;
while(*s!='\0')
{
SBUF=*s;
while(TI==0);
TI=0;
s++;
tx_delay(50);
}
SBUF=0x01;
while(TI==0);
TI=0;
tx_delay(50);
}
void transmit_addr(unsigned char adr)
{
TB8=1;
SBUF=adr;
while(TI==0);
TI=0;
tx_delay(50);
}
void tx_delay(int val)
{
int i,j;
for(i=0;i<val;i++)
{
for(j=0;j<500;j++)
{;}
}
}
SOURCE CODE FOR MULTIPROCESSOR COMMUNICATION SLAVE 1
#include<lcd.h>
const unsigned char my_addr=0x31;
char slave1;
sbit comp=P1^7;
void tx_delay(int);
int i;
void main()
{
char *r1="SLAVE 1";
TMOD=0X21;
SCON=0XF0;
TH1=0XFD;
TR1=1;
init_8bit();
hide_cursor;
string_8bit(r1);
xy_8bit(1,0);
IE=0x90;
while(1)
{
comp=1;
tx_delay(100);
comp=0;
}
}
void ser_int()interrupt 4
{
if(RI==1)
{
if(SBUF==my_addr)
{
slave1=my_addr;
SM2=0;
clear_8bit(2,1);
cmd_8bit(0xc0);
}
else if(SBUF==0x01)
{
for(i=1;i<=16;i++)
{
clear_8bit(2,i);
}
cmd_8bit(0xc0);
SM2=1;
}
else if(slave1==my_addr){
disp_8bit(SBUF);
}
RI=0;
}
}
void tx_delay(int val)
{
int i,j;
for(i=0;i<val;i++)
{
for(j=0;j<500;j++)
{;}
}
}
SOURCE CODE FOR MULTIPROCESSOR COMMUNICATION SLAVE 2
#include<lcd.h>
const unsigned char my_addr=0x32;
char slave2;
sbit comp=P1^7;
void tx_delay(int);
int i;
void main()
{
char *r1="SLAVE 2";
TMOD=0X21;
SCON=0XF0;
TH1=0XFD;
TR1=1;
init_8bit();
hide_cursor;
string_8bit(r1);
xy_8bit(1,0);
IE=0x90;
while(1)
{
comp=1;
tx_delay(100);
comp=0;
}
}
void ser_int()interrupt 4
{
if(RI==1)
{
if(SBUF==my_addr)
{
slave2=my_addr;
SM2=0;
clear_8bit(2,1);
cmd_8bit(0xc0);
}
else if(SBUF==0x01)
{
for(i=1;i<=16;i++)
{
clear_8bit(2,i);
}
cmd_8bit(0xc0);
SM2=1;
}
else if(slave2==my_addr){
disp_8bit(SBUF);
}
RI=0;
}
}
void tx_delay(int val)
{
int i,j;
for(i=0;i<val;i++)
{
for(j=0;j<500;j++)
{;}
}
}