|
|
View previous topic :: View next topic |
Author |
Message |
firefox78
Joined: 25 Jan 2005 Posts: 5
|
USART with more then 1 char |
Posted: Tue Jan 25, 2005 7:51 am |
|
|
PIC16F628 with a HARDWARE USART.
Hi all, how can read all chars in a USART BUFFER (I read that the USART can handle 3 chars) ?
I read the 1st char with :
x = getch();
and the next 2 chars ?
now how can I know how many chars are in RX buffer?
Best regards |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Tue Jan 25, 2005 8:34 am |
|
|
Code: |
char x[3];
int8 i;
if (kbhit())
{
i = 0;
do {
x[i++] = getch();
} while(kbhit() && i < 3);
}
|
is one possibility (pre-coffee and probably poorly though out). On exit, "i" tells you how many characters were read (up to 3)
However if you are expecting a stream of characters I suggest you look at the examples for the serial port receive and transmit interrupts and use those to create a FIFO. You can modify the FIFO code examples to also provide you a count of characters in the FIFO. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
Ttelmah Guest
|
|
Posted: Tue Jan 25, 2005 9:11 am |
|
|
The characters, are two in the receive register, and one in the process of receipt (it is not all here yet). How it works, is that when a character is complete in the receive shift register (RSR), it is transfered to the readable 'RCREG', and the flag is set to say that a character is waiting. 'RCREG', is a double buffered register. Hence another character can arrive, and not be lost, if the first has not been read. If you read RCREG, and it is holding another character, the 'received character' flag, will immediately be reset. So if two characters are waiting, and you code:
Code: |
if (kbhit()) c1=getc();
if (kbhit()) c2=getc();
|
This will return the two waiting characters. Only two characters are stored (not three). The 'third' character, is the one currently being received. Effectively there can be 2.9999 characters in the hardware, but if the third character completes, you lose one.
Realistically, the 'extra character', is there to help you avoid overrun conditions. You should read characters as soon as they are received, and not rely on more being held by the hardware. If you want to retrieve an entire sequence, then use an interrupt driven receive routine, and write the characters to your own buffer, processing when you have the expected number of characters.
Best Wishes |
|
|
|
|
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
|