View previous topic :: View next topic |
Author |
Message |
Hereoncapecod
Joined: 08 Dec 2003 Posts: 4
|
Serial Port problems using PIC18F8720 |
Posted: Tue May 25, 2004 8:00 pm |
|
|
I am using a PIC18F8720 with one USART connected to a PC (as a diagnostic tool ). The other port connected to a satellite module that runs at 9600 baud. I am getting intermittent performance from the interface.
I have defined the two uarts as two sepearate streams. On power up, I wait to see if an external PC is attached be using kbhit(). I am getting freqeunt random connections. The second port does not behave consistently either. Of interest, is that the prior prototype version of this system worked fine.
Is theier a way I can clear the Uarts transmitter and receive buffers prior to performing the required functions. This would eliminate spurioius characters. Is there a way in C to clear out the receiver UArt if more than 3 bytes have been loaded.
I am not running on interrupts, nor do do I need to for this application.
Thank you for your assistance.
Bruce A |
|
|
SteveS
Joined: 27 Oct 2003 Posts: 126
|
|
Posted: Wed May 26, 2004 7:33 am |
|
|
Are you using ERRORS in the #use RS232 statement? I think this is a must (unless you check for errors on your own). The reason is that an overrun error will halt the receiver, and must be cleared to allow futher reception. It is not uncommon on startup, or when plugging in a cable, to get this error. The CREN bit is used to reset this bit - and it also resets the recive logic. I assume (it's not explicit in the data sheet) that toggling CREN clears all the buffers and other flags as well. I don't think a framing error affects any operation.
You can also do a
Code: |
while(kbhit(STREAM)) fgetc(STREAM);
|
where STREAM is one of your streams defined in #use RS232.
this will clear out the buffers without resetting anything, but it will not clear any error flags (unless ERRORS is used).
An additional caution - be sure all RS232 related routines use the STREAM version (fgetc not getc, etc) and be sure to identify the stream. It's easy to miss one of these and the compiler will take it - but may not do what you wanted it to.
Finally, consider interupt driven reception. Not that it fixes the problems you are talking about, but as your code gets more complex I think you'll find it very useful; otherwise you are always under the gun of the RS232 Rx buffer.
- SteveS |
|
|
Guest
|
|
Posted: Wed May 26, 2004 7:50 pm |
|
|
Thanks for the input Steve. I will try the fix tomorrow. AM I to assume that the CREN bit is automatically cleared by ERRORS being set in USE RS-232?
As far as interrupts are concerend, I only use the UART at specified parts of my program and as there is a Data acquisition interrupt already running (which I shutoff during Uart operations ). I don;t see the need for Uart interrupts in this application.
Thanks for your help.
Bruce A |
|
|
Guest
|
|
Posted: Wed May 26, 2004 9:02 pm |
|
|
I had the exact same problem on the exact chip. My data is not comming in very fast though. Here is what I did... I turned the RCSTA bit off just after recieving a character.
Put this in the start of your code...
#bit Recieve_bit = 0xFAB.4 //bit to disable recieve
then put this or something in your recieve routine.
if(kbhit(com_a))
{
char_in = fgetc(com_a);
recieve_bit = 0;//disable receive
delay_ms(20);
recieve_bit = 1;//turn back on
bit_clear(flag3,7);
}
Thats all I did and have never had a problem since... TTY |
|
|
SteveS
Joined: 27 Oct 2003 Posts: 126
|
|
Posted: Thu May 27, 2004 6:54 am |
|
|
Yes, ERRORS will toggle the CREN bit, so that should take care of any problems due to serial 'noise'. Of course it adds a few instructions to each getc - so be aware of that. You can compile a short program each way to see the affect (in the LST file).
- SteveS |
|
|
|