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







Clearing RX buffer and TX Buffer
PostPosted: Sun Feb 09, 2003 2:16 pm     Reply with quote

Hello,
I need to use the Hardware UART (RS232) with no buffer, even on the RX line. The problem is that I have to connect the RX and TX pin to a bidirecional interfase, and everything I send is Echo on the RX Pin, and appears on the buffer. And I cannot connect with the other Terminal as I cannot see what it sends because of this problem.
Does anybody know how to disable both buffers, or how to "clear" the RX and TX buffer. I cannot use another pin for TX and RX as I need to use the Set_Uart_Speed() function.
thank you
Ezequiel
___________________________
This message was ported from CCS's old forum
Original Post ID: 11452
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 3:32 pm     Reply with quote

:=Hello,
:= I need to use the Hardware UART (RS232) with no buffer, even on the RX line. The problem is that I have to connect the RX and TX pin to a bidirecional interfase, and everything I send is Echo on the RX Pin, and appears on the buffer. And I cannot connect with the other Terminal as I cannot see what it sends because of this problem.
:= Does anybody know how to disable both buffers, or how to "clear" the RX and TX buffer. I cannot use another pin for TX and RX as I need to use the Set_Uart_Speed() function.
:=thank you
-------------------------------------------------

So, your PIC is talking to an RS-232 device that echoes back
everything your PIC sends to it. Also, the device will send
its own data to the PIC, as a reply. You're having trouble
telling the difference between the echoed characters, and
the real data from the RS-232 device.

You don't want to use a software receive fifo in your PIC,
for some reason. So, you have several getc() statements
which receive the data, and then you interpret the data.
The echoed characters are messing up your sequence of getc()
statements, so you're not getting the real data from the
device when you expect it.

Is that a proper statement of your problem ?

Since you know the device will echo your transmitted data,
can't you just put in additional getc() statements to receive,
and then throw away those extra chars ?





___________________________
This message was ported from CCS's old forum
Original Post ID: 11455
Ezequiel
Guest







Re: Clearing RX buffer and TX Buffer
PostPosted: Sun Feb 09, 2003 4:32 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?
Ezequiel

P.D.: I was thinking of using a Hex Inverter with Tri-State, and a Enable Pin to enable Tx and Rx on PIC.

:=:=Hello,
:=:= I need to use the Hardware UART (RS232) with no buffer, even on the RX line. The problem is that I have to connect the RX and TX pin to a bidirecional interfase, and everything I send is Echo on the RX Pin, and appears on the buffer. And I cannot connect with the other Terminal as I cannot see what it sends because of this problem.
:=:= Does anybody know how to disable both buffers, or how to "clear" the RX and TX buffer. I cannot use another pin for TX and RX as I need to use the Set_Uart_Speed() function.
:=:=thank you
:=-------------------------------------------------
:=
:=So, your PIC is talking to an RS-232 device that echoes back
:=everything your PIC sends to it. Also, the device will send
:=its own data to the PIC, as a reply. You're having trouble
:=telling the difference between the echoed characters, and
:=the real data from the RS-232 device.
:=
:=You don't want to use a software receive fifo in your PIC,
:=for some reason. So, you have several getc() statements
:=which receive the data, and then you interpret the data.
:=The echoed characters are messing up your sequence of getc()
:=statements, so you're not getting the real data from the
:=device when you expect it.
:=
:=Is that a proper statement of your problem ?
:=
:=Since you know the device will echo your transmitted data,
:=can't you just put in additional getc() statements to receive,
:=and then throw away those extra chars ?
:=
:=
:=
:=
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 11457
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