[SOLVED] communication problem between two 8051 using RS-485

Status
Not open for further replies.

Briez

Member level 5
Joined
Nov 30, 2012
Messages
83
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,288
Activity points
1,835
hello,
I am implementing serial half duplex communication between two 8051 micro-controllers. My circuit is like this......


> I have wrote a "C" Program for master as well as Slave.

For master:>
Code:
#include<atmel\at89x51.h>
#include"lcd.h"

#define rs P1_0
#define en P1_1

void delay(unsigned int time);
void Uart_init();
void transmit(unsigned char talk);
void trans_string(char *string);
unsigned char sent=0,value3,value1,value2,value4,value5,value6,value7;
unsigned int i=0,j,l,m,n,o,p,q,r,s;

void main()
{  
	unsigned char  DATA_tx[8]={0x01,0x03,0x00,0x78,0x00,0x01,0x04,0x13},DATA_rx[7];
	Uart_init();
	lcd_init();
lcd_cmd(0x88);
lcd_puts("Transmit");
P1_7=0;
while(1)
{ 	
	
if(sent==0)
{ 	
	delay(5);
	P1_7=1;
  for(j=0;j<8;j++)
	{	
  		transmit(DATA_tx[j]);
	}
		delay(5);
		sent++;
}
P1_7=0;
while(RI==0);
	DATA_rx[i]=SBUF;
	i++;
	RI=0;
if(i==7)
{
value1=DATA_rx[0];
value2=DATA_rx[1];
value3=DATA_rx[2];
value4=DATA_rx[3];
value5=DATA_rx[4];
value6=DATA_rx[5];
//value7=DATA_rx[6];

		 lcd_cmd(0xC0);
		 for(j=0;j<7;j++)
		 lcd_data(DATA_rx[j]);
	delay(25);
	lcd_cmd(0x80);
		l=value4%10;	//0
		m=value4/10;	//0
		n=value5/10;	//7
		o=value5%10;	//7


			 lcd_data(m+0x30);
			 lcd_data(l+0x30);
			 lcd_data(n+0x30);
			 lcd_data(o+0x30);
		 i=0;
		 sent=0;	
}

	}


}

void Uart_init()
{
	TMOD=0x20;
	TH1=0xFD;
	SCON=0x50;
	TR1=1;	
}
void transmit(unsigned char talk)
{
	SBUF=talk;
	while(TI==0);
	TI=0;
}	 

void delay(unsigned int time)
{
	unsigned int j,k;
	for(j=0;j<time;j++)
		for(k=0;k<1275;k++);
}

For Slave:>
Code:
#include<atmel\at89x51.h>
#include"lcd.h"

#define rs P2_1
#define en P2_0

void delay(unsigned int time);
void Uart_init();
void transmit(unsigned char talk);
unsigned char sent=0,value3,value1,value2,value4,value5,value6,value7;
unsigned int i=0,j,l,m,n,o,p,q,r,s;

void main()
{  
	unsigned char  DATA_tx[7]={0x01,0x03,0x00,0x00,0x64,0x04,0x13},DATA_rx[8];
	Uart_init();
	lcd_init();
lcd_cmd(0x88);
lcd_puts("Receive");
P1_7=0;
while(1)
{
P1_7=0; 	
while(RI==0);
	DATA_rx[i]=SBUF;
	i++;
	RI=0;
if(i==8)
{
value1=DATA_rx[0];
/*value2=DATA_rx[1];
value3=DATA_rx[2];
value4=DATA_rx[3];
value5=DATA_rx[4];
value6=DATA_rx[5];*/
//value7=DATA_rx[6];

		 lcd_cmd(0xC0);
		 for(j=0;j<8;j++)
		 lcd_data(DATA_rx[j]);
if(value1==0x01)
	{
	   delay(10);
		P1_7=1;
	  for(j=0;j<7;j++)
		{	
 	 		transmit(DATA_tx[j]);
		}
			delay(5);
			
	}
		 i=0;
}
   P1_7=0;
	}


}

void Uart_init()
{
	TMOD=0x20;
	TH1=0xFD;
	SCON=0x50;
	TR1=1;	
}

void transmit(unsigned char talk)
{
	SBUF=talk;
	while(TI==0);
	TI=0;
}	 

void delay(unsigned int time)
{
	unsigned int j,k;
	for(j=0;j<time;j++)
		for(k=0;k<1275;k++);
}

> Please give me solution, where is i am making mistake.........??
 
Last edited:

hai, do your transmitter is transmitting ?.
to check that connect led on the to pin of 485 tx and rx. if it blinks it is working.

then check the receiver by writing simple code . eg: make some led ON when you receiver any character.
 

Hi,

you must provide some delay after transmitting the each character,And serial communication interrupt technique is very useful to you project.

you just wrote the code for RX in interrupt mode,And for TX you can use your "transmit(DATA_tx[j]);" function.
 

Hi,

If i transmit 1 character then receiver receives that character and replies with another one, so my transmission and receive seems okay.

If i transmit "DATA_Tx[8]={01,03,00,78,00,04,13}" then also been received right on receiver side. The problem accours only when i transmit frame like "DATA_Tx[8]={0x01,0x03,0x00,0x78,0x00,0x04,0x13}".

Please tell me whats the reason?
 

Thanks For Reply Guys,
I Tried This.

My data type is unsigned char DATA_tx[8]
 

Hi Briez,
I saw you schematic,In that you didn't inter change the RXD and TXD pins of your controller.Without inter changing that pins how you got the output.
see the below image.
 

Attachments

  • 17_1323661923.jpg
    53.8 KB · Views: 134
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…