Receiving

Back | Up

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.

  1. Read RCIF
  2. If TXIF is low (TSR full) goto 1.
  3. data = RCREG
  4. END

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. 
 
unsigned char getc(void)
{
    while(!RCIF)    //wait for a byte to be put in the RCREG...
        continue;

    return RCREG;   //return the data byte received in RCREG
}

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).

void main(void)
{
    while(1)
    {
        if(RCIF)   //check for data,
             data = getc();
        //do other code....
    }
}

See using interrupts in Advanced Tutorial

 

Send mail to webmaster@avitresearch.co.uk with questions or comments about this web site.
Copyright © 2007 AVIT Research Ltd
Last modified: 10/02/07