PDA

View Full Version : How to clear RCREG



xapmanis
- 13th November 2014, 18:15
Hello everyone,

I would like to ask:
--How can i clear the RCREGx buffer ?!
--Is RCREGx a writable register or a readable only ?
I'm asking because i am using a "Hserin [Str message\50\13]" command and if there are more bytes incoming after the "13" character they are saved right into the RCREGx and reappearing in the next Hsering command as the first received Bytes. And my Message array gets messed up.
After a lot of testing and playing with the PBP hyperterminal i ended up with this solution:


RCREG1= 0
RCREG1= 0
Hserin [Str Message\50\13]

It seems it is working perfectly and RCREGx cleared but i would like to know if there is anything else.
There aren't many info about RCREGx in PIC's manual so any information is welcome!

HenrikOlsson
- 13th November 2014, 21:20
Hi,
I thought HSERIN flushed the buffer first thing - apparently not.... Or are you just concerned that what you describe MIGHT happen if you DON'T clear the buffer before issuing a HSERIN?

A quick look in the datasheet for a 18F25K22 doesn't really say (or I missed it) if RCREG is writable so I can't really answer that with certainty but how it's "usually" done is to read it untill there's no more data available.



RC1IF VAR PIR1.5 ' Alias to USART receive interrupt flag

WHILE RC1IF
Dummy = RCREG1
WEND


Or, since the FIFO is only two bytes deep, simply

Dummy = RCREG1
Dummy = RCREG1

/Henrik.

xapmanis
- 13th November 2014, 23:21
Thank you for the info Henrik.

I thought HSERIN flushed the buffer first thing - apparently not
I used my pic as a Command Line Editor to communicate from my PC Hyperterminal with many Devices. In the past i did care only for debuging those devices so i never went into trouble looking "why the HECK" those two Bytes pop up into my array and as a result to my Hyperterminal.
Since i have to make a more stable program with many "Command Messages" comming via SMS i tried to debug those two Bytes.
After a lot of search and testing the result was that Hserin [Str Arrayvar\n\c] saves the string until the "c" char received but it doesn't clear the RCREGx after so if Hserin is used again, the last 2 Bytes that remain in the RCREGx become the 2 first received Bytes !

I believe RCREGx is both Readable/Writable register because all the codes bellow worked fine.

WHILE RC1IF
Dummy = RCREG1
WEND

Dummy = RCREG1
Dummy = RCREG1

RCREG1= 0
RCREG1= 0


Best Regards, Kostas.

richard
- 14th November 2014, 02:43
I tend to use

RCSTA.4=0 ; (cren)
RCSTA.4=1

to clear buffer overrun etc

but now I wonder if it clears rcreg