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

need help with RS485

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



Joined: 09 Mar 2009
Posts: 6

View user's profile Send private message

need help with RS485
PostPosted: Mon Mar 09, 2009 11:32 am     Reply with quote

Hi,
I can not make it work for some reason.
The hardware: http://www.modtronix.com/product_info.php?cPath=108_109&products_id=111
Controller: PIC16F876A
RS485 driver: SP485E connected to PIC as: RX-RC7, TX-RC6 RX/TX - A0
And yes, I did my homework reading all previous RS485 posts on this forum.

The problem:
When I use RS485.C library - it is not working at all. No RX, No TX.

Simple code work for TX:
Code:
#include <16F876A.h>
#device *=16
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=18432000, xtal=18432000)

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,enable=PIN_A0, stream=RS485)

void main()
{
 
  while(1){
 
    printf("Test");

  }
}

How to receive data over RS485?
Please, help.
Thank you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 09, 2009 12:13 pm     Reply with quote

Have you ever made this board do anything (such as blinking an LED) ?
Do you know that the board works ?

How do you know there is no output ? Are you looking at the Tx pin
with an oscilloscope ?

If you have no output on the RS485 pins (the 3-pin Molex connector), then:
1. The PIC may not be running.
2. You might be using the wrong connector. Only the 3-pin Molex is
setup to be used, when the board is shipped from the factory.
3. The RS-485 enable may not be working. Is the board jumpered to
use Pin A0 for the Enable pin ?
4. What is your CCS compiler version ?
w6bvb



Joined: 09 Mar 2009
Posts: 6

View user's profile Send private message

PostPosted: Mon Mar 09, 2009 1:39 pm     Reply with quote

PCM programmer, thank you for reply.

The hardware is ok. Output of SP485 routed to the 5 pin terminal block (SJ5 and SJ4 are soldered). The terminated resistor R7 is connected (Jumper J3 is ON).
Rest of my program is working fine with this board. There are no problems with PIC or other hardware.
This "Hello World" version is working fine:
Code:
#include <16F876A.h>
#device *=16
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=18432000, xtal=18432000)

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define DE  PIN_A0
#define HF  PIN_A1

//======================================
void main()
{

output_low(HF);
output_high(DE);

printf("Hello World\n\r");

while(1);
 
}

But if I try to use getc() it returning 0x000

For PC connection I use this USB-RS485/422 converter: http://www.usconverters.com/index.php?main_page=product_info&cPath=67&products_id=197
Converter is working fine. I tested it in loop mode.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 09, 2009 1:43 pm     Reply with quote

Quote:
But if I try to use getc() it returning 0x000

Post a simple test program that shows the getc() problem.
w6bvb



Joined: 09 Mar 2009
Posts: 6

View user's profile Send private message

PostPosted: Mon Mar 09, 2009 1:54 pm     Reply with quote

Compiler version 4.068

Code:
#include <16F876A.h>
#device *=16
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=18432000, xtal=18432000)

//#use rs232(baud=9600, xmit=PIN_C2, rcv=PIN_C3)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define DE  PIN_A0
#define HF  PIN_A1

//======================================
void main()
{
int8 ci;
output_low(HF);
output_high(DE);



while(1){
  if (kbhit()) {
     ci =  getc();
    delay_ms(50);
    putc(ci);
 }
}
 
}

If I switch to RS232 port (commented code) then it works. But no echo if I use RS485 port.
w6bvb



Joined: 09 Mar 2009
Posts: 6

View user's profile Send private message

PostPosted: Mon Mar 09, 2009 1:56 pm     Reply with quote

One more thing. kbhit() returning TRUE constantly in RS485 mode.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 09, 2009 2:08 pm     Reply with quote

Quote:
void main()
{
int8 ci;
output_low(HF);
output_high(DE);

Look at the schematic on page 13 of the data sheet for your board:
http://www.modtronix.com/products/sbc28pc/sbc28pcr2_ir4.pdf
The transmitter enable signal (DE) is "high true". You are setting it high
and are enabling the transmit driver.

You need to disable DE (set it low) when you want to receive.
Normally, it is kept at a low level. It's only set to a high level during
tranmission. This is done automatically by using the ENABLE parameter
in the #use rs232() statement.
w6bvb



Joined: 09 Mar 2009
Posts: 6

View user's profile Send private message

PostPosted: Mon Mar 09, 2009 2:28 pm     Reply with quote

Yes, the RE/DE line of SP485E connected to RA0 (J1 in position 1_2).
And this is the code. No echo.
When I hit keyboard on PC, pin A0 changing state from 1 to 0 for 1ms.
I can see data on pin C7, but getc() still returning 0x000
Code:
#include <16F876A.h>
#device *=16
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=18432000, xtal=18432000)


#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, enable=PIN_A0, ERRORS)

//======================================
void main()
{
int8 ci;

while(1){
  if (kbhit()) {

     ci =  getc();
   
    delay_ms(50);
 
    putc(ci);
 }
}
 
}
Ttelmah
Guest







PostPosted: Mon Mar 09, 2009 2:54 pm     Reply with quote

One other thing about RS485, is the 'idle' state.
With TTL RS232, the line idles high, when nothing is being sent. With RS485, this will only happen, if there is a bias applied to the bus. Without this, the bus can idle to any state, and when reversed to receive data, if the other end hasn't yet started, the receiver will see garbage.
You can get receivers that see an undriven bus as the idle state, or your termination network has to apply a little voltage bias to the lines, to ensure they go to the idle state when undriven. So for 100R termination, you use a 1K2R to +5v, 120R between the lines, and 1K2R to 0v, giving the correct 100R as a match, but ensuring the slight bias needed.

Best Wishes
w6bvb



Joined: 09 Mar 2009
Posts: 6

View user's profile Send private message

PostPosted: Mon Mar 09, 2009 3:27 pm     Reply with quote

Thank you Ttelmah.
It was physical layer problem. Start working after biasing.
daniel.mtech803



Joined: 27 Mar 2014
Posts: 1

View user's profile Send private message Send e-mail

PostPosted: Thu Mar 27, 2014 6:19 am     Reply with quote

hi friends i'm new to pic32 i wanna to develope c code for rs485 please any one can send me the c code thanq........
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Thu Mar 27, 2014 6:27 am     Reply with quote

daniel.mtech803 wrote:
hi friends i'm new to pic32 i wanna to develope c code for rs485 please any one can send me the c code thanq........


You already have it in your CCS Drivers directory.
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