# Proper way to clear RCIF | PIC16F628A XC8

#### odm4286

Joined Sep 20, 2009
243
Well, I've learned a lot with this last project. For those that have helped me before I'm very close to getting this done thanks to you all! I'm curious about the RCIF bit what is the proper way to empty the RCREG buffer so I can avoid getting stuck in an ISR? Thanks!

Is this ok?
C:
while(RCREG);
Or do I need to actually put the data somewhere in order for the buffer to empty?
C:
char trash;
while(RCREG)
trash = RCREG;

#### jpanhalt

Joined Jan 18, 2008
8,507
You do not need to "put" RCREG data anywhere. Just reading it, clears the register. However, there are precautions described in 12.2.2 of the datasheet for the 16F62A.

John

#### dannyf

Joined Sep 13, 2015
2,197
Code:
while(RCREG);
I think the datasheet will give the most authoritative answer on that. In general, once RCIF is set, you want to read RCREG to retrieve the data being received - this will clear RCIF.

However, there are exceptions to that (and those are good / desirable exceptions). For devices with receiving buffers, reading RCREG will NOT clear RCIF if the buffer is more than one deep (meaning after reading RCREG once there are still data in the buffer).

In those cases, you will exit the ISR and jump right back into it again, until all data in the buffer is read - in most cases, that's what people want and what's what a buffer is there for.

If for some reason you want to clear RCIF in those cases, this will do:

Code:
  while (RCIF) RCREG;
I don't know of any practical purpose to use that but it does what you want - if I understood you correctly.