|
|
View previous topic :: View next topic |
Author |
Message |
rmderbes
Joined: 25 Apr 2007 Posts: 5
|
RS232 missing chars |
Posted: Mon May 07, 2007 2:28 pm |
|
|
For a project, using 16F877@20Mhz, I need to read 210 bytes from a software serial interface, and send them to another software serial interface. I cannot use PIC's UART (pins C6 & C7) because it´s already used for communications with another device.
I have no room to buffer as much characters, (210), so I must send & receive chars on the fly.
As a test, I started with this snippet of code:
Code: |
#case
#nolist
#include <16F877.h>
#device *=16 ADC=10
#list
#opt 9
#fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#ZERO_RAM
// For the bootloader
#ORG 0x1F00,0x1FFF {}
#define XTAL 20000000
#define BRATE 9600
#use delay(clock=XTAL)
#use rs232 (baud=BRATE,parity=N,xmit=PIN_C0,rcv=PIN_C1,bits=8,stream=FNG)
#use rs232(baud=BRATE, parity=N, xmit=PIN_C5, rcv=PIN_C2, bits=8, stream=HOST)
#use rs232(baud=BRATE, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=DBG)
void main( void )
{
disable_interrupts( GLOBAL );
output_d( 0b00000000 );
while (TRUE)
{
if( kbhit( HOST ) )
{
fputc( fgetc( HOST ), FNG );
}
}
}
|
I miss about 1 each two transmitted chars... And after a while, all hangs.
As a test, if I set the hardware UART as the transmitter, it works like a charm if i send few chars (about 80), but it hangs later...
Thanks for your help. |
|
|
Ttelmah Guest
|
|
Posted: Mon May 07, 2007 2:36 pm |
|
|
Of course you will...
While you are transmitting a character, you will miss a character if it arrives. Hence just about every second character.
You _need_ a hardware UART on the one of the other direction. Solutions are:
Use a PIC with a second UART.
Add a second UART, on the SPI interface for example. There are I2C, and SPI UART chips, wich will solve this.
It is possible, for such a simple application, to perform both transmission and reception at the same time, provided the processor doesn't have to do anything much else. However this is _serious_ programming (you would probably have to implement a state machine, using one of the hardware timers, and look for 'events', based on a number of timer counts from the start of transmission/reception, to time the reception amd transmission)....
Best Wishes |
|
|
Steve H. Guest
|
|
Posted: Mon May 07, 2007 2:53 pm |
|
|
I once saw a clever application that received data strings at a rate and sent them along at a higher rate – exactly double.
This allowed the PIC to be reading data in and sending out (keeping track of the bit time by itself) data and never be caught in s bind because the send data was always done before the next receive data.
This will involve a home brew approach to the code – you won’t be able to get away with the built in routines – but you might be able to eliminate extra hardware – if you can send and receive at different rates.
HTH - Steve H. |
|
|
rmderbes
Joined: 25 Apr 2007 Posts: 5
|
RS232 missing chars |
Posted: Tue May 08, 2007 11:32 am |
|
|
Thank you very much for your answers.
As Steve H. suggested, I tried to lower the speed of the incoming serial interface, with success at 600bps at the incoming serial and 9600 bps at the outgoing serial interface, using the built-in picc routines.
I also tried a bitbanging solution, built around the timer0 interrupt, which also worked, at 2400bps on one interface and 9600 on the other.
Both are software serial interfaces, as I said before, I cannot employ the hardware uart, because how is the board built.
Thank you again, and I apologize for my bad english. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue May 08, 2007 3:21 pm |
|
|
I had a project where a long data string came periodically from one device and went out to another, and my PIC (16C54) had to add data to the message. I had no RAM to store the long message so I just let it go through an AND gate from input to output. The PIC listened in on this message and when it heard the <CRLF> go by it sent its own data to the AND gate. So the output data was:
<long data><CRLF><PIC data><long data><CRLF><PIC data><long data....
It was especially nice that there was zero latentcy for the long data. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
rmderbes
Joined: 25 Apr 2007 Posts: 5
|
RS232 missing chars |
Posted: Wed May 09, 2007 6:34 am |
|
|
Nice solution! So incoming stream was routed to the AND gate and to the PIC? And one pin of the PIC was connected to the other input of the AND gate? |
|
|
|
|
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
|