Basic reception is very simple, when a byte is received it is placed in the (RCREG) and the RCIF flag set. The RCREG is actually a buffer and allows 2 data bytes to be stored whilst anther byte is being received. The RCIF flag can be periodically checked to see if a data byte has been received (this is known as polling), or it can be used to trigger an interrupt†.
Code
The following code extract can be
used to receive a byte to to the PC. Note that this function
does not return until a data byte is received.
If other functionality needs to be done whilst waiting to receive a byte then the RCIF flag should be polled and if set, then the above function can be called to retrieve the data byte. Note that the polling should be done sufficiently often to ensure that no data bytes are lost (over-run error).
†See using interrupts in Advanced Tutorial |
