Information
PIC18F2455/2550/4455/4550
DS80478A-page 16 © 2009 Microchip Technology Inc.
38. 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 earlier silicon revisions (A3, B4 and B5), incom-
ing data bytes received on the SDI pin are always
transferred 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 7 (Assembly language) and
Example 8 (C language).
Affected Silicon Revisions
EXAMPLE 7: SAMPLE ASSEMBLY CODE FOR TRANSFERRING SPI DATA
EXAMPLE 8: SAMPLE C CODE FOR TRANSFERRING SPI DATA
A3 B4 B5 B6 B7
X
X
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.
}