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

Clearing RX buffer and TX Buffer

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Clearing RX buffer and TX Buffer
PostPosted: Sun Feb 09, 2003 5:18 pm     Reply with quote

:=Hello,
:= my program works if I don't use a Hardware UART, but I need the Set_uart_speed() function. So isn't any way to "clear" the RX buffer, so before a getc() I clear the buffer and everything is okey. Isn't any bit in a register that do this?
--------------------------------------------------------------

I tried the SPEN and CREN bits, and they didn't work.

The method that does work, requires reading the RCREG three
times. See the following demo program. I think the
"clear_usart_receiver()" function will do what you want.
Try it.

Code:
#include "c:\program files\picc\devices\16F877.h"
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT, NOLVP
#use Delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#byte RCREG = 0x1A

void clear_usart_receiver(void);
//==============================

main()
{
char c;

printf("Press 3 or more keys within 5 seconds.\n\r");

delay_ms(5000);

clear_usart_receiver();

if(kbhit() == 0)
  {
   printf("The receive buffer was cleared.\n\r");
  }
else
  {
   printf("The receive buffer was not cleared.\n\r");
   printf("It contains these chars: \n\r");

   while(1)
     {
      if(kbhit())
        {
         c = getc();
         putc(c);
        }   
     }
  }

while(1);
}

//=================================
// Clear the USART receive register and its fifo
// by reading it three times.

void clear_usart_receiver(void)
{
char c;
c = RCREG;
c = RCREG;
c = RCREG;
}

___________________________

Edited to put the program in a code block so it looks better.


Last edited by PCM programmer on Thu Sep 18, 2008 4:37 pm; edited 1 time in total
Ezequiel
Guest







Re: Clearing RX buffer and TX Buffer
PostPosted: Sun Feb 09, 2003 7:08 pm     Reply with quote

Hello,
Two more things. First, why read it three times and not more or less. Second, why do you use the Error parameter in #use RS232.
I think I'll modify my circuit and use two buffers with Tri State, anyway-
Thanks
Ezequiel

___________________________
This message was ported from CCS's old forum
Original Post ID: 11463
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Clearing RX buffer and TX Buffer
PostPosted: Sun Feb 09, 2003 7:50 pm     Reply with quote

:=Hello,
:= Two more things. First, why read it three times and not more or less. Second, why do you use the Error parameter in #use RS232.
---------------------------------------------------------

Look at Figure 10-4 in the 16F877 data sheet. This is a
block diagram of the USART receiver. It consists of the
RSR register, and the RCREG register. The RCREG register
is a "2-deep" fifo. So, you could have 2 chars in the
RCREG fifo, and have another char being shifted into the
RSR register. That's a total of 3 chars. So that's why
you need to read the receiver 3 times to make sure you clear it.

The ERRORS parameter is used to clear any overrun or framing
errors that may occur. It's rare to get such an error, if
your baud rate is correct, and if you read the chars from
the receiver quickly enough. But if you do get an error,
it can lock the receiver, and your program will lock up.
By using the ERRORS parameter, you cause the compiler to
put in code that checks for these conditions and then clears
the error if it occurs. It's a safety measure.


___________________________
This message was ported from CCS's old forum
Original Post ID: 11464
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