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 support@ccsinfo.com

Can't tx correct data in the 1st time tx of RS-485

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



Joined: 09 May 2011
Posts: 6

View user's profile Send private message

Can't tx correct data in the 1st time tx of RS-485
PostPosted: Thu May 19, 2011 2:55 am     Reply with quote

I can not transmit the correct data in the 1st transmission. It means if I transmit the same data 2 times, I can see the correct data was sent in the 2nd transmission.

Why is the reason that I can transmit the correct data in the 1st transmission?

Code:

#include <16F1947.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT
#Fuses INTRC_IO
#use delay(clock=32000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7, bits=8)


unsigned int uchCRCHi, uchCRCLo;
unsigned int8 TXDBuffer[8];
unsigned int8 TXCnt=0;

void main (void)
{
 while(1)
 {
     
    if (TXCnt<=1)                         
      {                     
        TXCnt++;   // if (TXCnt<=2). I can see the correct data in TXCnt=2
        TXDBuffer[0]=0xB5;
        TXDBuffer[1]=0x00;
        TXDBuffer[2]=0x74;
        TXDBuffer[3]=0x65;
        TXDBuffer[4]=0x73;
        TXDBuffer[5]=0x74;   
        TXDBuffer[6]=0x00;
        TXDBuffer[7]=0x00;

        output_bit(PIN_G0,1);
        putc( TXDBuffer[0] );     
        putc( TXDBuffer[1] );
        putc( TXDBuffer[2] );
        putc( TXDBuffer[3] );
        putc( TXDBuffer[4] );
        putc( TXDBuffer[5] );
        putc( TXDBuffer[6] );
        putc( TXDBuffer[7] );
         delay_us(2000);
        output_bit(PIN_G0,0);
      }
 }
}
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu May 19, 2011 5:12 am     Reply with quote

Are you using RS-485 as in the subject line , or RS-232 as your program indicates ?

If really using RS-485, you must specify the 'control pin' in the USE RS232(options...) statement.

Also you should add the 'errors' option to the USE RS232(...) in all cases of using the internal hardware UART.

Also, confirm that the device that you're transmitting the data to is setup correctly as the PIC is only 1/2 of the 'system'.
che_chia



Joined: 09 May 2011
Posts: 6

View user's profile Send private message

PostPosted: Thu May 19, 2011 5:32 am     Reply with quote

Dear all,

how can I clear the buffer of RX and TX?
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu May 19, 2011 7:01 am     Reply with quote

If using the internal hardware UART..

You have to read the RX buffer, twice to 'clear' it...

You have to send the TX buffer, once to clear it...

as there is no command to 'clear' the internal shift registers used for serial I/O, not that I'm aware of.

Maybe newer PICs, but unlikely.
Ttelmah



Joined: 11 Mar 2010
Posts: 19339

View user's profile Send private message

PostPosted: Thu May 19, 2011 10:22 am     Reply with quote

Forget about 'clearing buffers'.
The RX buffer you can clear by reading characters, and the TX buffer will clear with time, or you can just poll the INT_TBE line, if you want to know when it is empty. You don't have to 'send the TX buffer once', it clears itself. Once loaded, it _automatically sends_.
However these should not be a problem.

Key thing is to describe the RS485 hardware. How are you handling the enable pin?. Is the bus 'biased' when not in use?. Is it going through other transceiver devices?.
I'd suspect that the bus is floating when not driven, so an infinite stream of garbage is being seen by the receiving device, which takes time to recover from this.
Describe the hardware.

Best Wishes
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu May 19, 2011 4:52 pm     Reply with quote

Make sure the 485 bus is in a valid stop bit state when no one is driving it. If it is already in a start bit state when someone tries to put a start bit on it, the receiver will never see the beginning of the start bit.
_________________
The search for better is endless. Instead simply find very good and get the job done.
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