CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

RS 485 communication between two pic micro controllers

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
chandrshekar411



Joined: 14 Feb 2013
Posts: 9

View user's profile Send private message

RS 485 communication between two pic micro controllers
PostPosted: Thu Feb 14, 2013 10:47 pm     Reply with quote

Hi. I am trying rs485 communication between two pic micro controllers 16f876A. I am sending data from master to the slave. I am using rs485.c file provided by the ccs but it is not working. Can anyone provide sample code for building the communication between two micro controllers using rs485 in ccs compiler ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Feb 15, 2013 1:50 am     Reply with quote

The example does work.
Problem is probably hardware. Several 'gotchas' on RS485:

1) The termination or the type of buffer used, needs to ensure that when the bus is not driven, the buffer output's a 'high' to the processor. TI and Maxim, both do buffers that guarantee this, or alternatively the termination needs to provide a little bias to the bus to ensure this happens. Most common buffers do not do this, so bus biasing is required.
2) The incoming data line from the buffer, needs a pull up resistor, so when the buffer is turned 'off' there is again a 'high' at this point.
3) Remember RS485, is a 1:1 connection, not a crossover connection like RS232. So the A line on each device needs to connect together.
4) General thing, before starting, check each processor works, and is working at the right clock speed. As with any asynchronous bus, timing is vital.

Best Wishes
chandrshekar411



Joined: 14 Feb 2013
Posts: 9

View user's profile Send private message

PostPosted: Fri Feb 15, 2013 2:54 am     Reply with quote

what modifications i have to do for rs385.c sample code for sending "chandu"string as data to the slave how i can retrieve that data in display at slave side.I did not understand how to use the function rs485_send_message(),res485_get_message for sending and reeving.
my code for transmitter.
#include <16f886.h> // transmitter
#fuses HS,WDT,NOPROTECT,BROWNOUT
#use delay(clock=20000000,restart_wdt)
#use rs232(baud=9600, xmit=Pin_b7, rcv=Pin_B6,enable=Pin_B5, bits=8,errors, stream=RS485)
#use rs232(baud=9600, xmit=Pin_b7, rcv=Pin_B6,enable=Pin_B5, bits=8,errors, stream=RS485_CD)

#byte PORTA=0X05
#byte PORTB=0X06
#byte PORTC=0X07

#include<Define.c>
#include<stdio.h>
#include<math.h>
#include<stddef.h>
#include<string.h>
#include<stdlib.h>

#include<LCDLEDOP.c>
#include<rs485.c>

char ch[7]="chandu"
void init_io()

{
set_tris_a(0Xff);
set_tris_b(0x40); //0100 0000
set_tris_c(0x00);
}
VOID SERVOMAX()
{
lcd_gotoxy(1,1);
printf (lcd_putc, "SERVOMAX INDIA LTD.");

delay_ms (1000);
SETUP_WDT(WDT_2304MS);


}

Void main()
{

init_io();
delay_ms(10);
lcd_init();
delay_ms(500);
// SERVOMAX();
while(1)
{
restart_wdt();
rs485_send_message(0x11,3,ch);
lcd_gotoxy(1,2);
printf (lcd_putc, "RS 485 Comm");


}
}

receiver code:
#include <16f876a.h> // reciever
#fuses HS,WDT,NOPROTECT,BROWNOUT
#use delay(clock=20000000,restart_wdt)
#USE RS232(baud=9600,xmit=pin_b7,rcv=pin_b6,ENABLE=pin_b5,bits=8,stop=1,parity=n,ERRORS,STREAM=RS485)
#USE RS232(baud=9600,xmit=pin_b7,rcv=pin_b6,ENABLE=pin_b5,bits=8,stop=1,parity=n,ERRORS,STREAM=RS485_CD)

#byte PORTA=0X05
#byte PORTB=0X06
#byte PORTC=0X07

#include<stdio.h>
#include<math.h>
#include<stddef.h>
#include<string.h>
#include<stdlib.h>
#include<Define.c>
#include<LCDLEDOP.c>
#include<mydriverrx.c>

int8 msg[32];

void init_io()

{
set_tris_a(0Xff);
set_tris_b(0x40);
set_tris_c(0x00);
}
VOID SERVOMAX()
{
// lcd_gotoxy(1,1);
// printf (lcd_putc, "SERVOMAX INDIA LTD.");
lcd_gotoxy(1,2);
printf (lcd_putc, "RS 485 Comm");
delay_ms (1000);
SETUP_WDT(WDT_2304MS);
}

Void main()
{

init_io();
delay_ms(10);
lcd_init();
delay_ms(500);
SERVOMAX();
rs485_init() ;

while(1)
{
restart_wdt();
rs485_get_message(msg);
lcd_gotoxy(1,1);
printf (lcd_putc, "A:%c%c%c%c%c%c", msg[0],msg[1],msg[2],msg[3],msg[4],msg[5]);

}
}

please help me what mistake am doing
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Fri Feb 15, 2013 7:15 am     Reply with quote

Hi,

Ttelmah was kind enough to give you a detailed reply, and you essentially ignored 100% of what he said. Basically, he said that you need to validate your hardware before considering any software issues. Because you ignored this advise can we assume this is not a hardware based project, but simulation only, ie. Proteus?

Please post your schematic for the project, and a photo of your hardware so that we know what we are dealing with!

John

PS I know you are new to the forum, but didn't you think it was a little bit strange that the code you posted doesn't look anything like the the code posted by other users? When you post code, you should use the 'Code Tag' feature of the forum message editor. If you do this your code will be much easier to read!

Code:

This is what your code should look like!
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Feb 15, 2013 7:44 am     Reply with quote

Cart before horse applies here.

Start by just turning the output buffer control on on board one, and use standard RS232 commands to send a repeated message. Have the second device set it's buffer to receive, and have it read the message. Do something simple like turn an LED on/off according to the message.

Once you have the buffer and transmission working in one direction, reverse the buffers, and get it working in the other direction.

For simple one device to another communications, this is really all that is needed.

The point about the RS485 drivers, is they are designed to allow you to have multiple addressed devices on a single bus. If you want this, then move on to trying these, _but get the hardware working first_.

I see ezflyr posted while I was typing. Smile

Best Wishes
chandrshekar411



Joined: 14 Feb 2013
Posts: 9

View user's profile Send private message

RS 485 communication between two pic micro controllers
PostPosted: Mon Feb 18, 2013 5:05 am     Reply with quote

Am sending data using fputs function like below. when am sending single character the data is received at receiver.if i send string in fputs function the data is not received .
transmitter:
Code:
fputs("a",RS485) ; // char a is the data is received at another end && RS485 is the stream declared in #use rs232

fputs("chandu",RS485);// data is not received at another end

receiver:
Code:
char data[10]';
fgets(data,PC); // data receiving in to char array && PC is the stream declared in #use rs232

lcd_gotoxy(1,1);
printf(lcd_putc,"d:%s".data);

Please tell me how i can send the string or bytes of data to the receiver.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Feb 22, 2013 5:51 am     Reply with quote

Computers are stupid, they only do exactly what you tell them to do. When you make only a tiny mistake in telling them what to do, the outcome will be different from what you want it to be.
Your answers are full of the tiny mistakes that make them useless. This is not a language translation problem but is an attitude problem.
Quote:
My compiler is CCS compiler version 4.1
This is an invalid version number. Version numbers have the format x.yyy. All digits are important.
Post the version number again.

chandrshekar411 wrote:
I'm trying rs485 serial communication. When I'm sending character using fgets() the data is received at the receiver but when I'm sending string using fgets() the data is not receiving.

Transmitter:
Code:

fgets("a",RS485); // RS 485 is the stream used in #use RS232 and the data is received.
fgets("ab",RS485); // the data is not recevied.
Where did this bullshit come from?
I guess you typed it just from your mind instead of copying it from a real program? fgets() is used to 'get' data. For sending you use fputs().

You didn't give a direct answer to many of the earlier questions, but let's try again:
Are you testing with real hardware? Simple answer yes or no.

About your transmission problem:
Make things simpler. Break it down into smaller parts that you can proof to be working. Only when you are sure all sub components are working you put them together, one by one, and test again after each addition. This way, when it fails you know it is in the combination of last added components.

For a good starting point on RS485 problems see Ttelmah's post. That really is the way to go.
Don't come back here just repeating the same question again and again. Tell us what you did to find the problem cause and what results this returned.

Another hint: keep things easy. Do you really need a watchdog? A wrong coded watchdog can create hard to find bugs. 99% of the programs I've seen on this forum don't need a watchdog. For now, remove the watchdog, you can always add it later when the rest of your product has proofed itself to be working.
apsua



Joined: 12 Feb 2016
Posts: 1

View user's profile Send private message

PostPosted: Fri Feb 12, 2016 10:05 am     Reply with quote

Hello,
I would ask, why the #use rs232 directive defined two times? The stream is different yes, but why is it necessary?
Thanks.
_________________
Sara sapsoup.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group