problem in using rs485.

Status
Not open for further replies.

kuluvale

Junior Member level 1
Joined
Feb 18, 2012
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,412
I have a problem in serial communication between At89s52 micro controllers. I thought to use rs485 for connecting more devices in network. for testing I connect two controller alone.
I assume one as a master and another as slave. If I initiate transmission from master it reaches slave correctly. And when I write a code for slave to response master data means it give response but that was not a expected data(garbage value)View attachment rs-485_hardware1_204.pdf.
I attached a circuit.

I used a coding in c.

"// for slave side
#include<reg51.h>
#include <string.h>
#include <stdio.h>
sbit RE_DE=P1^0;
void ini() // Initialize Timer 1 for serial communication
{
TMOD=0x21; //Timer1, mode 2, baud rate 9600 bps
TH1=0XFD;
SCON=0x50;
TR1=1;
}

void delay_50ms(unsigned char x)
{
unsigned char i;
for(i=0;i<x;i++)
{
TH0=0x3c;
TL0=0xb0;
TR0=1;
while(!TF0);
TF0=0;
TR0=0;
}
}
void transmit(unsigned char value) // Funtion to transmit serial data
{
RE_DE=1;
SBUF=value;
while(TI==0);
TI=0;
}

void main()
{
unsigned char value;
ini();
while(1)
{
RE_DE=0;
while(RI==0);
value=SBUF;
RI=0;
delay_50ms(1);
transmit(value);
}
}
"

"// for master
#include<reg51.h>
#include <string.h>
#include <stdio.h>
sbit RE_DE=P3^4;
sbit SW1=P0^4;
sbit SW2=P0^5;
sbit SW3=P0^6;
sfr LCD=0xa0;
sbit EN=P3^6;
sbit RS=P3^7;
void initial_lcd(void);
void nop(void);
void string_to_lcd(unsigned char *s);
void write_lcd(unsigned char dat,unsigned int com);
void delay_50ms(unsigned char x);
unsigned char value;

void ini() // Initialize Timer 1 for serial communication
{
TMOD=0x21; //Timer1, mode 2, baud rate 9600 bps
TH1=0XFD;
SCON=0x50;
TR1=1;
}

void recieve() //Function to receive serial data
{
RE_DE=0;
nop();
while(RI==0);
value=SBUF;
RI=0;
RE_DE=1;
P1=value;
write_lcd(value,1);// it will show received value
}
void transmit(unsigned char y) // Funtion to transmit serial data
{
SBUF=y;
RE_DE=1;
while(TI==0);
TI=0;
}

void initial_lcd(void)
{
write_lcd(0x38,0); delay_50ms(20);
write_lcd(0x0f,0); delay_50ms(20);
write_lcd(0x01,0);
}
void write_lcd(unsigned char dat,unsigned int com)
{
RS=com;
LCD=dat; nop();
EN=1; nop();
EN=0; nop();
}
void string_to_lcd(unsigned char *s)
{
unsigned char i,l,u=0;
l=strlen(s);
write_lcd(0x01,0);
write_lcd(0x80,0);
for(i=0;i<l;i++)
{
if(i>=15)
{
write_lcd(0xc0+u,0);
write_lcd(*s,1); delay_50ms(1);
s++;
u++;
}
else
{
write_lcd(*s,1); delay_50ms(1);
s++;
}
}
}
void delay_50ms(unsigned char x)
{
unsigned char i;
for(i=0;i<x;i++)
{
TH0=0x3c;
TL0=0xb0;
TR0=1;
while(!TF0);
TF0=0;
TR0=0;
}
}

void nop(void)
{
unsigned char n;
for(n=0;n<20;n++);
}
void main()
{
initial_lcd();
ini();
string_to_lcd("press switch");
while(1)
{
if(SW1==0)
{
string_to_lcd("msg 1=");
transmit('1');
delay_50ms(8);
recieve();
}
if(SW2==0)
{
string_to_lcd("msg 2=");
transmit('2');
delay_50ms(2);
recieve();
}
if(SW3==0)
{
string_to_lcd("msg 3=");
transmit('3');
delay_50ms(5);
recieve();
}
}
}
"
 

Use spacing between words. And [ code] .. [/code] tags please.
 

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…