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

USART with more then 1 char

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



Joined: 25 Jan 2005
Posts: 5

View user's profile Send private message

USART with more then 1 char
PostPosted: Tue Jan 25, 2005 7:51 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 25, 2005 8:34 am     Reply with quote

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







PostPosted: Tue Jan 25, 2005 9:11 am     Reply with quote

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
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