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

485 communication:gettin started

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



Joined: 02 May 2006
Posts: 16

View user's profile Send private message

485 communication:gettin started
PostPosted: Fri May 05, 2006 6:59 am     Reply with quote

I am pretty new to the PIC chips and have a project to swap out a 17C756 for an 18f4610. I have tried to match up the Registers as closely as I can, and same thing with the comm protocol. I have found however that my comm only works 90% of the time. I have a function which send a request to another chip, asking it to tell me if one of its inputs is on (there are several chips so i send out the address of the chip i want, the command(tell me if you input is on/off), and the specific input. It then responds back telling me if it is on or not. I found that once in a great while i would get the wrong response back, and later i found a certain combination (specific chip and input) that would give me the wrong response back about 50% of the time. I am wondering if this could be something set up wrong. I have pasted my configuration below.

SYNC = 0; // port asynchronous mode
// BRG16 = 0;
// BRGH = 0;
SPEN = 1; // serial port enable
TXIE = 0; // disable asynch TX interrupt
RCIE = 0; // disable asynch RC interrupt
SPBRG = 3; // 14.745 mhZ desired 57600 baud|FOSC/(64(SPBRG+1))
TXEN = 1; // enable transmit port
CREN = 1; // enable receive port
TX9 = 0;
RX9 = 0;

PORTC = 0x00; // clear portc
TRISC = 0x9D;

Any help or ideas would be greatly appreciated.
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Fri May 05, 2006 7:36 am     Reply with quote

That is not enough information.
Can you post the origonal C code for the 17C756?
new2pic



Joined: 02 May 2006
Posts: 16

View user's profile Send private message

PostPosted: Fri May 05, 2006 8:27 am     Reply with quote

this is the config for the 17c756, is this what you meant?

SYNC2 = 0; // port 2 asynchronous mode
SPEN2 = 1; // serial port 2 enable
TX2IE = 0; // disable asynch TX interrupt
RC2IE = 0; // disable asynch RC interrupt
SPBRG2 = 3; // 14.745 mhZ desired 57600 baud
TXEN2 = 1; // enable transmit port 2
CREN2 = 1; // enable receive port 2
TX92 = 0;
RX92 = 0;
BSR = 0x05; // select bank 5
ADCON1 = 0x0E; // bank 5 digital I/O
PORTG = 0xFF; // clear portg
DDRG = 0x40;
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Fri May 05, 2006 9:02 am     Reply with quote

I wanted the WHOLE code.
There could be problems all over the place.
or rather.. Places where, through time, there have been many improvments.
and the forum can describe the best way to handle these problems and suggest improvments.
new2pic



Joined: 02 May 2006
Posts: 16

View user's profile Send private message

PostPosted: Fri May 05, 2006 9:13 am     Reply with quote

i think this is the just of the 485 com in the 17. There is a timer that if exceeded sets "clear_485_fault" so that the transmission is repeated. "asynch_485_rec_busy" and "asynch_485_timeout_flt" are also part of a time-out scheme.There are different functions that load different value into slave_address, command, data_low, and data_high, but they all end up calling this function

do
{
clear_485_fault = FALSE;
RG5 = TRUE;
DelayIt(2);
Transmit(START_BYTE); DelayIt(20);
Transmit(START_BYTE); DelayIt(20);
Transmit(START_BYTE); DelayIt(20);
Transmit(START_BYTE); DelayIt(20);
Transmit(slave_address); DelayIt(20);
Transmit(command); DelayIt(20);
Transmit(data_high); DelayIt(20);
Transmit(data_low); DelayIt(20);
Transmit(STOP_BYTE); GLINTD = TRUE;
while(!TRMT2);
RG5 = FALSE;
GLINTD = FALSE;

// wait for full receive
serial_resp_counter = 0;
asynch_485_rec_busy = TRUE;
for (i = 0; i < MAX_ASYNCH_BYTES; i++)
asynch_serial_buffer[i] = Receive();
asynch_485_rec_busy = FALSE;
asynch_485_timeout_flt = FALSE;
}while(clear_485_fault);
void Transmit(unsigned char data)
{
BSR = 4;
while(!TRMT2);
TXREG2 = data;
}
unsigned char Receive(void)
{
unsigned char data_buffer;

BSR = 4;
while(!RC2IF)
{
if (clear_485_fault)
break;
}
data_buffer = RCREG2;
return (data_buffer);
}

also I have noticed that I am getting framing errors, but in the manual it talks about adjusting the internal clock, however since i am using an external clock i think that adjusting OSCTUNE would have no affect/ not be applicable, is this correct. I am also getting overrun errors but do not understand where the CKP bit should be set/reset. I think they are saying that it should be set after while(!RC2IF) and reset after data_buffer = RCREG2;

Is this everything?
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Fri May 05, 2006 9:30 am     Reply with quote

For future reference...when you post code, there is a button to push "code" button before you paste the text. and press it again after the paste.
It will then keep the formating. I don't like the way your sending a byte and then delay 20. I think the normal way is to put the send packet into a circular buffer. Then an interupt routeen read and sends the bytes untill they are all sent. Look at the RS485 example in the example directory. This is a perfect example of how this program can be made MUCH more reliable.
new2pic



Joined: 02 May 2006
Posts: 16

View user's profile Send private message

PostPosted: Fri May 05, 2006 10:33 am     Reply with quote

How can i get rid of framing errors?
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Fri May 05, 2006 11:54 am     Reply with quote

If you are using the hardwre USART and IRQ's I don't think there is much to try.

In the protocol there should be a spot that if you get a crc/checksum error you retransmit.
Then a framing error shouldn't be a problem.

But

I have products that talk at 19200 for 4,000 feet and I have only once seen a framing error once.
I use 18 gauge twisted wire, 1pair, 120ohm termination on each end, 1/8 load drivers/recievers.

Is the RS485 terminated correctly?
http://www.bb-elec.com/tech_articles/rs422_485_app_note/table_of_contents.asp


ps:
I had thought about sample_early option of RS232 but this is not for UART.
Quote:

getc() normally samples data in the middle of a bit time. This option causes the sample to be at the start of a bit time. May not be used with the UART.
new2pic



Joined: 02 May 2006
Posts: 16

View user's profile Send private message

PostPosted: Fri May 05, 2006 12:30 pm     Reply with quote

i dont see anything in the data sheet about CRC or checksum errors?
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Fri May 05, 2006 12:48 pm     Reply with quote

I am done
new2pic



Joined: 02 May 2006
Posts: 16

View user's profile Send private message

PostPosted: Fri May 05, 2006 12:56 pm     Reply with quote

what does that mean
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