Proper way to clear RCIF | PIC16F628A XC8

Discussion in 'Embedded Systems and Microcontrollers' started by odm4286, Apr 14, 2016.

  1. odm4286

    Thread Starter Active Member

    Sep 20, 2009
    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?
    Code (C):
    2. while(RCREG);
    Or do I need to actually put the data somewhere in order for the buffer to empty?
    Code (C):
    2. char trash;
    3. while(RCREG)
    4.     trash = RCREG;
  2. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    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.

  3. dannyf

    Well-Known Member

    Sep 13, 2015
    Code (Text):
    2. 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 (Text):
    2.   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.