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

Using Streams with RS232 and RS485

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







Using Streams with RS232 and RS485
PostPosted: Mon Aug 04, 2003 3:32 am     Reply with quote

Dear all,

I am having some difficulty using streams with RS232 and RS485. I am using the PIC16F874 at 4MHz. My complier is 3.126. The code is inserted below. The RS485 (stream bus) will happily recieve data packets from a second PIC and pass them onto the host PC. This will happen without any problems until I start to send data over the RS232 connection (stream controller). I can send data from the PC to the PIC via RS232 and that to will be recieved and transfered to the remote PIC application. The problems occur once I have sent the first packet over RS232. The RS485 input pin (C7) on the PIC floats, whereas it normally sits high until data comes in. It is as though the RS232 #use command has been forgotten about and the pin just goes back to normal use.

Could this be a problem with the complier I have or am I using the #use command wrong. I need to have both streams available all of the time and so the top of the program seems like the best place to put them. Any help in solving this problem would be greatly received.

Regards,

Jason James.

//-----------------------------------------------
// Data packet configuration
//-----------------------------------------------
// Start Byte
// Source
// Destination
// Instruction
// LED1
// LED2
// LED3
// Jack1
// Jack2
// Jack3
// CheckSum

#include <16F874.h>

#use delay(clock=4000000)
#use RS232 (baud=4800, xmit=PIN_C6, rcv=PIN_C7, BITS=8, PARITY=N, STREAM=bus)
#use RS232 (baud=4800, xmit=PIN_B1, rcv=PIN_B0, BITS=8, PARITY=N, STREAM=controller)

#fuses NOWDT, XT, NOPROTECT, NOPUT, NOBROWNOUT, NOLVP

int displaystring[32];

#define ALL_IN 0x0f
#define ALL_OUT 0x00
#define WRITE_ENABLE PIN_C5
#define TRAFFIC PIN_E1

#include "lcdlib.c"

#byte PORTA=5 // Used for debugging with LEDs on each pin to monitor program progression
#byte PORTB=6
#byte PORTC=7

void initialiseports(void);

void initialiseports(void)
{
// Initialise IO pins

// DATA = 0;
set_tris_a(ALL_OUT);
// DATA = 0;

CTRL = 0;
set_tris_d(ALL_OUT);
CTRL = 0;
}

void main(void)
{
//-----------------------------------------------
// Set up an variables needed during the main loop
//-----------------------------------------------

byte address, mask, oldaddress;
byte data[11];
byte y;
int x;
int thischar;
byte singlechar;
delay_ms(100);

//-----------------------------------------------
// Setup direction of data byte
//-----------------------------------------------
initialiseports();

//-----------------------------------------------
// Send hello packet in RS485
//-----------------------------------------------
output_high(WRITE_ENABLE);
delay_ms(10);
fprintf(bus, "RS485RS48\n\r");
delay_ms(20);
output_low(WRITE_ENABLE);

//-----------------------------------------------
// Setup LCD
//-----------------------------------------------
delay_ms(100);
setup8();
delay_ms(10);

//-----------------------------------------------
// Display welcome message
//-----------------------------------------------
strcpy(displaystring, "Hellermann Tyton Vision Patch ");
display8();
delay_ms(100);

//-----------------------------------------------
// Send RS232 welcome message
//-----------------------------------------------
fprintf(controller, "012345678\n\r");
delay_ms(100);
output_a(0xff);
delay_ms(1000);
output_a(0x00);
delay_ms(100);

//-----------------------------------------------
// Set the RS485 to read
//-----------------------------------------------
output_low(WRITE_ENABLE);
delay_ms(2);
while(1)
{
bit_set(PORTA,0);
//-----------------------------------------------
// Check for byte on RS232 input
//-----------------------------------------------
if(kbhit(controller))
{
bit_set(PORTA,1);
//-----------------------------------------------
// Retrieve 11 bytes from the RS232
//-----------------------------------------------
for(x=0; x<=10; x++)
{
data[x] = fgetc(controller);
}
delay_ms(2);
//-----------------------------------------------
// Set the RS485 to write
//-----------------------------------------------
output_high(WRITE_ENABLE);
delay_ms(5); // was 2ms
//-----------------------------------------------
// Send 11 bytes out on RS485
//-----------------------------------------------
for(x=0; x<=10; x++)
{
fputc(data[x],bus);
delay_ms(20);
}
delay_ms(100); // 100ms
//-----------------------------------------------
// Set the RS485 to read
//-----------------------------------------------
output_low(WRITE_ENABLE);

delay_ms(20);
bit_set(PORTA,2);
delay_ms(10);
//-----------------------------------------------
// Empty any characters that are on the RS485 bus
//-----------------------------------------------
output_high(WRITE_ENABLE);
delay_ms(2);
while(kbhit(bus))
{
x = fgetc(bus);
bit_set(PORTA,2);
delay_ms(10);
bit_clear(PORTA,2);
delay_ms(10);
}
delay_ms(20);
output_low(WRITE_ENABLE);
delay_ms(2);
bit_set(PORTA, 3);
delay_ms(10);
}
//-----------------------------------------------
// Check for bytes on RS485 bus
//-----------------------------------------------
if(kbhit(bus))
{
//-----------------------------------------------
// Read 11 bytes from RS485 bus
//-----------------------------------------------
for(x=0; x<=10; x++)
{
data[x] = fgetc(bus);
}
delay_ms(20);
bit_set(PORTA,5);
//-----------------------------------------------
// Send 11 bytes over RS232
//-----------------------------------------------
for(x=0; x<=10; x++)
{
fputc(data[x],controller);
delay_ms(2);
}
delay_ms(2);
}
output_a(0x00);
}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516640
R.J.Hamlett
Guest







Re: Using Streams with RS232 and RS485
PostPosted: Mon Aug 04, 2003 4:12 am     Reply with quote

:=Dear all,
:=
:=I am having some difficulty using streams with RS232 and RS485. I am using the PIC16F874 at 4MHz. My complier is 3.126. The code is inserted below. The RS485 (stream bus) will happily recieve data packets from a second PIC and pass them onto the host PC. This will happen without any problems until I start to send data over the RS232 connection (stream controller). I can send data from the PC to the PIC via RS232 and that to will be recieved and transfered to the remote PIC application. The problems occur once I have sent the first packet over RS232. The RS485 input pin (C7) on the PIC floats, whereas it normally sits high until data comes in. It is as though the RS232 #use command has been forgotten about and the pin just goes back to normal use.
:=
:=Could this be a problem with the complier I have or am I using the #use command wrong. I need to have both streams available all of the time and so the top of the program seems like the best place to put them. Any help in solving this problem would be greatly received.
:=
:=Regards,
:=
:=Jason James.
3.126, is pretty old. On the Version '3' compilers, there were quite a few major bugs up to about 3.14x, including at one point, some with the streams handling. Unfortunately (for some reason), the 'download' page appears not to be working at the moment, since (though CCS have a habit of missing 'fixes' out of the list), it would have been worthwhile having a look at their list of versions, and what was fixed, and I was going to see if anything was mentioned about streams, after 3.126...
As a general 'comment', I'd really switch to handling the hardware serial receive using interrupts (unless you are 100\% certain that data cannot arrive between the other routines), since otherwise one obvious thought, is that the hardware UART, has received some data, gone into hardware 'overrrun', and hence is giving odd behaviour...
It would perhaps otherwise be worth adding the 'errors' option, and reading the error variable, to see if something like this has happened to the port.

Best Wishes

:=//-----------------------------------------------
:=// Data packet configuration
:=//-----------------------------------------------
:=// Start Byte
:=// Source
:=// Destination
:=// Instruction
:=// LED1
:=// LED2
:=// LED3
:=// Jack1
:=// Jack2
:=// Jack3
:=// CheckSum
:=
:=#include <16F874.h>
:=
:=#use delay(clock=4000000)
:=#use RS232 (baud=4800, xmit=PIN_C6, rcv=PIN_C7, BITS=8, PARITY=N, STREAM=bus)
:=#use RS232 (baud=4800, xmit=PIN_B1, rcv=PIN_B0, BITS=8, PARITY=N, STREAM=controller)
:=
:=#fuses NOWDT, XT, NOPROTECT, NOPUT, NOBROWNOUT, NOLVP
:=
:=int displaystring[32];
:=
:=#define ALL_IN 0x0f
:=#define ALL_OUT 0x00
:=#define WRITE_ENABLE PIN_C5
:=#define TRAFFIC PIN_E1
:=
:=#include "lcdlib.c"
:=
:=#byte PORTA=5 // Used for debugging with LEDs on each pin to monitor program progression
:=#byte PORTB=6
:=#byte PORTC=7
:=
:=void initialiseports(void);
:=
:=void initialiseports(void)
:={
:=// Initialise IO pins
:=
:=// DATA = 0;
:= set_tris_a(ALL_OUT);
:=// DATA = 0;
:=
:= CTRL = 0;
:= set_tris_d(ALL_OUT);
:= CTRL = 0;
:=}
:=
:=void main(void)
:={
:=//-----------------------------------------------
:=// Set up an variables needed during the main loop
:=//-----------------------------------------------
:=
:= byte address, mask, oldaddress;
:= byte data[11];
:= byte y;
:= int x;
:= int thischar;
:= byte singlechar;
:= delay_ms(100);
:=
:=//-----------------------------------------------
:=// Setup direction of data byte
:=//-----------------------------------------------
:= initialiseports();
:=
:=//-----------------------------------------------
:=// Send hello packet in RS485
:=//-----------------------------------------------
:= output_high(WRITE_ENABLE);
:= delay_ms(10);
:= fprintf(bus, "RS485RS48\n\r");
:= delay_ms(20);
:= output_low(WRITE_ENABLE);
:=
:=//-----------------------------------------------
:=// Setup LCD
:=//-----------------------------------------------
:= delay_ms(100);
:= setup8();
:= delay_ms(10);
:=
:=//-----------------------------------------------
:=// Display welcome message
:=//-----------------------------------------------
:= strcpy(displaystring, "Hellermann Tyton Vision Patch ");
:= display8();
:= delay_ms(100);
:=
:=//-----------------------------------------------
:=// Send RS232 welcome message
:=//-----------------------------------------------
:= fprintf(controller, "012345678\n\r");
:= delay_ms(100);
:= output_a(0xff);
:= delay_ms(1000);
:= output_a(0x00);
:= delay_ms(100);
:=
:=//-----------------------------------------------
:=// Set the RS485 to read
:=//-----------------------------------------------
:= output_low(WRITE_ENABLE);
:= delay_ms(2);
:= while(1)
:= {
:= bit_set(PORTA,0);
:=//-----------------------------------------------
:=// Check for byte on RS232 input
:=//-----------------------------------------------
:= if(kbhit(controller))
:= {
:= bit_set(PORTA,1);
:=//-----------------------------------------------
:=// Retrieve 11 bytes from the RS232
:=//-----------------------------------------------
:= for(x=0; x<=10; x++)
:= {
:= data[x] = fgetc(controller);
:= }
:= delay_ms(2);
:=//-----------------------------------------------
:=// Set the RS485 to write
:=//-----------------------------------------------
:= output_high(WRITE_ENABLE);
:= delay_ms(5); // was 2ms
:=//-----------------------------------------------
:=// Send 11 bytes out on RS485
:=//-----------------------------------------------
:= for(x=0; x<=10; x++)
:= {
:= fputc(data[x],bus);
:= delay_ms(20);
:= }
:= delay_ms(100); // 100ms
:=//-----------------------------------------------
:=// Set the RS485 to read
:=//-----------------------------------------------
:= output_low(WRITE_ENABLE);
:=
:= delay_ms(20);
:= bit_set(PORTA,2);
:= delay_ms(10);
:=//-----------------------------------------------
:=// Empty any characters that are on the RS485 bus
:=//-----------------------------------------------
:= output_high(WRITE_ENABLE);
:= delay_ms(2);
:= while(kbhit(bus))
:= {
:= x = fgetc(bus);
:= bit_set(PORTA,2);
:= delay_ms(10);
:= bit_clear(PORTA,2);
:= delay_ms(10);
:= }
:= delay_ms(20);
:= output_low(WRITE_ENABLE);
:= delay_ms(2);
:= bit_set(PORTA, 3);
:= delay_ms(10);
:= }
:=//-----------------------------------------------
:=// Check for bytes on RS485 bus
:=//-----------------------------------------------
:= if(kbhit(bus))
:= {
:=//-----------------------------------------------
:=// Read 11 bytes from RS485 bus
:=//-----------------------------------------------
:= for(x=0; x<=10; x++)
:= {
:= data[x] = fgetc(bus);
:= }
:= delay_ms(20);
:= bit_set(PORTA,5);
:=//-----------------------------------------------
:=// Send 11 bytes over RS232
:=//-----------------------------------------------
:= for(x=0; x<=10; x++)
:= {
:= fputc(data[x],controller);
:= delay_ms(2);
:= }
:= delay_ms(2);
:= }
:= output_a(0x00);
:= }
:=}
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516643
jamesjl
Guest







Re: Using Streams with RS232 and RS485
PostPosted: Mon Aug 04, 2003 4:38 am     Reply with quote

Thanks for the info. I am about to get the latest complier, just in case!

I have been putting off using interupts through a general lack of knowledge and experience but maybe now is as good a time as any to get them sorted.

Since the RS232 input uses B0, would it be worth using interupts on this pin also to trap inputs to the software UART? Is there any sample code out there that would offer me some help?

Many thanks,

Jason.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516644
R.J.Hamlett
Guest







Re: Using Streams with RS232 and RS485
PostPosted: Mon Aug 04, 2003 5:43 am     Reply with quote

:=Thanks for the info. I am about to get the latest complier, just in case!
:=
:=I have been putting off using interupts through a general lack of knowledge and experience but maybe now is as good a time as any to get them sorted.
:=
:=Since the RS232 input uses B0, would it be worth using interupts on this pin also to trap inputs to the software UART? Is there any sample code out there that would offer me some help?
:=
:=Many thanks,
:=
:=Jason.
I wouldn't 'get the latest'. Get the 3.148, that is on the site as the 'latest tested' version. The current 3.17x versions, have some nice improvements to the IDE, but have introduced some new bugs, and it'll be worth waiting a while till these are 'ironed out'....
3.148, is pretty good, and the newest versions seem to have concentrated on changes to the enviroment, rather than to the 'core' compiler.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516646
jamesjl
Guest







Re: Using Streams with RS232 and RS485
PostPosted: Mon Aug 04, 2003 6:10 am     Reply with quote

Thanks,

I will bear that in mind. Hope the CCS web site sorts itself out soon. I will need to download today to stay on target. Does anyone know what is wrong with it and when I will be back online?

Thanks again,

Jason
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516648
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