Information
© 2008 Microchip Technology Inc. DS80387A-page 3
PIC18F2458/2553/4458/4553
5. Module: MSSP
If the application firmware is expecting to receive
valid data – in either SPI Slave or Master mode –
the firmware must read from the SSPBUF register
before writing the next byte to transmit to SSPBUF.
If the firmware does not read from SSPBUF, the
BF bit (SSPSTAT<0>) can still be set from the pre-
vious transaction. If the BF bit is set, the incoming
data byte is blocked from transferring from the
SSPSR Shift register to the SSPBUF register. If
the firmware then reads from SSPBUF, the data
read will not match the data most recently received
on the SDI pin.
In the earlier silicon revision (B5), incoming data
bytes received on the SDI pin are always trans-
ferred from SSPSR to SSPBUF, regardless of the
state of the BF bit.
Work around
If the firmware expects to receive valid data,
always clear the BF bit by reading from SSPBUF
prior to writing to SSPBUF, even when the current
data in SSPBUF is not important. Sample work
around code, suitable for all silicon revisions, is
given in Example 1 (Assembly language) and
Example 2 (C language).
Date Codes that pertain to this issue:
All engineering and production devices.
EXAMPLE 1: SAMPLE ASSEMBLY CODE FOR TRANSFERRING SPI DATA
EXAMPLE 2: SAMPLE C CODE FOR TRANSFERRING SPI DATA
WriteSPI:
BCF PIR1, SSPIF
MOVF SSPBUF, w ;Perform read, even if the data in SSPBUF is not important
MOVLW 0xA5 ;In this example, let’s send “0xA5” to the other SPI device.
MOVWF SSPBUF
WaitXmitComplete:
BTFSS PIR1, SSPIF
BRA WaitXmitComplete
MOVF SSPBUF, w ;The data received should be valid.
unsigned char WriteSPI(unsigned char ByteToSend)
{
unsigned char TempVariable;
PIR1bits.SSPIF = 0;
TempVariable = SSPBUF; // Reads from SSPBUF, ensures BF bit is clear before
SSPBUF = ByteToSend; // sending the next byte.
while(!PIR1bits.SSPIF); // Wait until the transmission is complete.
return SSPBUF; // The data received should be valid.
}