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

Problems with Double Serial Port in PIC18

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







Problems with Double Serial Port in PIC18
PostPosted: Thu Nov 04, 2004 6:40 am     Reply with quote

I'm working with the last version 3.212.

I use PIC18lf252 with two serial ports connected. I'm using streams to discriminate wich serial port.

#use rs232(STREAM=BT_id, baud=9600,parity=N,xmit=BT_TX,rcv=BT_RX,bits=8)
#use rs232(STREAM= HOST_id, baud=9600,parity=N,xmit=HOST_TX,rcv=HOST_RX,bits=8)

Pins used for each serial are diferent in any case.
Transmissions are always OK by the way : fprintf( BT_id , "%s" , BT_Command );
Receptions of bytes are sometimes no good.

<OPTION A>
if ( kbhit( BT_id ) ) {
setup_wdt(WDT_ON);
fgets( BT_Response , BT_id );
setup_wdt(WDT_OFF);
}

<OPTION B>
while ( New_BT_Char!=13 ) {
if ( kbhit( BT_id ) ) {
setup_wdt(WDT_ON);
New_BT_Char = fgetc( BT_id );
setup_wdt(WDT_OFF);
fputc (New_BT_Char,HOST_id); // echo caracteres HOST

BT_Response[Len_Rx_BT++] = New_BT_Char; // string comando/dato
}
}

I' using Watchdog timer because many times kbhit is true, but fgets or fgetc never ends.
String format is not problem beacuse I've tested by Hyperterminal and strings always finish with <RETURN>
I'm thinking about serial port is hang, because when Wachtdog make a RESET serial port running again.

Thaks in advance.

Alberto Campo
Photonic Engineering Group
University of Cantabria - SPAIN
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Nov 04, 2004 8:08 am     Reply with quote

You have not given enough of the configuration to determine which, if either, of the serial interfaces uses harware uart and which uses the software uart. For the hardware uart you should include the errors keyword in the corresponding #use rs232 statement. For the software UART you need to be calling kbhit() at at least 10 times the bit rate.

When the hardware handler is sending characters then the software handler will miss characters.

One approach you could do to handle the software uart is to setup a timer that causes an interrupt at > 10 times the bit rate. Then in the interrupt handler for the timer you call kbhit() and if a character is present you add it to a buffer (ring or linear but I prefer ring buffers). You could also test in the interrupt handler if the character received was a <CR> and set a flag accordingly. This way you main line code just needs to test the flag to see if the string is now in the buffer. Note that if you are using two software UARTS, the same timer and interrupt handler could be used to manage both receive handlers.

I have an application that uses multiple software and hardware UARTs each running at 115Kbps full duplex on a PIC18F252 at 40MHz but to do this the software UARTs are implemented by my own handlers and it consumes two external interrupts for start bit detection and three timers one to handle the common Tx bit clock and one timer for each of the SW UART Rx Bit clocks. This approach also requires multiple high and low priority interrupt tasks coded in assembler. You are using an LF version of the chip so you cannot run this fast. If you would like a copy of this code drop me a private message.

I wouldn't use the WDT this way but this is a personal preference. If I have a project that needs a WDT then I have it enabled for the entire program and issue preiodic resets. In the case of option B I would clear_wdt() inside the if ( kbhit( BT_id ){} segment.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
bercam4
Guest







doble UART in PIC18
PostPosted: Mon Nov 08, 2004 3:43 am     Reply with quote

Thanks for your explanation, I'm try to implement in this way. I'm don't understand you about hardware and software UART.
I think I have only one UART used by two diferent streams, isn't?

Regards
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Nov 08, 2004 4:41 am     Reply with quote

Quote:
I think I have only one UART used by two diferent streams, isn't?


The PIC you are using has one hadrware UART (actually a USART). The CCS compiler will automatically use this if the Tx/Rx PINs you have designated in the #use 232 statement are the same pins as the USART Tx/Rx pins.

The CCS compiler also support the emulation of the USART. If you select pins other than those used by the internal USART, then the compiler will emulate the serial port in software. You can have multiple serial ports emulated in software. In your code segment

Code:
#use rs232(STREAM=BT_id, baud=9600,parity=N,xmit=BT_TX,rcv=BT_RX,bits=8)
#use rs232(STREAM= HOST_id, baud=9600,parity=N,xmit=HOST_TX,rcv=HOST_RX


You did not show the definitions for the constants HOST_TX, BT_TX, HOST_RX, and BT_RX. SO I cannot tell if you are using hardware and/or software UARTs.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
ckielstra



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

View user's profile Send private message

PostPosted: Mon Nov 08, 2004 4:44 am     Reply with quote

You have only one hardware UART in the 18LF252. A UART can handle only a single stream! In your processor this is the stream physical connected to the pins RC6 and RC7. A stream connected to other physical pins will always be handled by software UARTs.

Software UARTs are functions generated by the CCS compiler. Software UARTs are useful but do have some severe limitations because they can not work in the background; for example automatic buffering of received data is not implemented and monitoring two software serial ports simultaneously is almost impossible.

It is possible to implement multiple serial ports, either in a combination of hardware and software UARTs, but this is not easy and requires a good understanding of what you are doing.

Please provide more information about your application like:
How much data are you transferring? What baud rates? Flow control? Full or half duplex?
bercam4
Guest







Double Port in PIC18
PostPosted: Tue Nov 09, 2004 4:01 am     Reply with quote

Thanks both for your help,

I'm using these pins conected to both serial ports

//--------------- TRANSMISION SERIE CON PC-HOST ----------------
#define HOST_TX PIN_C0
#define HOST_RX PIN_C1
//---------- TRANSMISION SERIE CON MODULO BLUETOOTH ------------
#define BT_RX PIN_C7
#define BT_TX PIN_C6

My application now is a PIC18 conected simultaneously to Host PC and Embedded Bluetooth Module. Both conections are Serial and I'm working with 9600, 8, n, 1, without flow control.

PIC are located between Host-PC and Bluetooth module in order to control comands and data.
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Nov 09, 2004 5:09 am     Reply with quote

The connection to the PC is using the software UART and the connection to the Bluetooth device is using the hardware uart. Add the errors to the following:

Code:
#use rs232(STREAM=BT_id, baud=9600,parity=N,xmit=BT_TX,rcv=BT_RX,bits=8, ERRORS)


The hardware UART requires less CPU cycles to support as the bit serialization in and out is performed by the hardware.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
bercam4
Guest







It works
PostPosted: Fri Nov 12, 2004 10:01 am     Reply with quote

I don't know why don't work before,

the only change I was made is to force Software UART by:

#use rs232(STREAM=BT_id, baud=9600,parity=N,xmit=BT_TX,rcv=BT_RX,bits=8,FORCE_SW)

And now both serial communication are well running.
ckielstra



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

View user's profile Send private message

PostPosted: Fri Nov 12, 2004 11:14 am     Reply with quote

It is very strange for your software to be working better with the FORCE_SW option than with using the hardware UART. To me this suggests a design problem in your software and you have to be very carefull !!!

Quote:
I'm thinking about serial port is hang, because when Wachtdog make a RESET serial port running again.

This was your original problem and it points to a common problem when using the hardware UART. As soon as the UART is enabled after power-on it will sample the rx-input for incomming data and store the data in it's input buffer. The input buffer has only space for 2 or 3 characters, when the buffer gets full the UART enters an error state and will stop reception until the error-bit is cleared. It looks like this is what happened in your application. Somehow data was sent to you before you started reading. Did you receive 2 or 3 characters before the reception stoppped?....

Have you tried the suggestion from asmallri by adding the ERRORS option? This will automatically clear the Error-bit in the hardware UART.

Again, the software UART working and the hardware UART failing seems like you are overlooking a potential problem in your design.[/quote]
bercam4
Guest







Double Port in PIC18
PostPosted: Mon Nov 15, 2004 3:30 am     Reply with quote

Hello ckielstra,

I'm agree with you, it's probably hardware UART is hang when more than 3 bytes incoming into the buffer, because after RESET it works properly again.

Also I followed asmallri suggestion in order to add ERRORS Clause. The problem wasn't fixed because sometime I missed bytes.

To solve this I reduced Baud Rate to the minimun (1200bps) in both devices PIC and Bluetooth and trouble remains.

Actually both ports living and working without problems at 9600bps, one connected to HOST_PC the other one connected to a Bluetooth module.
Surprising it works fine, but I know this isn't the logical way to do that.

I'm very pleasant with you and asmallri for your help.
Thanks a lot.

Alberto Campo
alberto@teisa.unican.es
Photonic Engineering Group
University of Cantabria - SPAIN
ckielstra



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

View user's profile Send private message

PostPosted: Mon Nov 15, 2004 4:25 am     Reply with quote

Quote:
Also I followed asmallri suggestion in order to add ERRORS Clause. The problem wasn't fixed because sometime I missed bytes.

This is a confirmation of the problem being an UART buffer overflow problem. The ERRORS clause clears the error-flag in the UART so transmission will continue, but it isn't a magical solution that can restore any missed data. So the fact that with the ERRORS clause your transmission continues now but with some missing characters is proof of the UART buffer overflowing.

Your code may be working now at 9600 baud, but you still have a big design problem in your software. Just a small change in your code will make the error pop up again.

The problem originates from your program communicating with the PC while simultaneously data is comming in from the Bluetooth module. Part of the solution will be to implement an interrupt routine that will read the incomming data from the UART and put it in a larger buffer for later processing.
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