The SPI module can be configured to operate in Receive-Only mode. This mode is useful in applications where the SPI master only needs to receive data and the contents of the data being transmitted to the slave device are not important. When configured in Receive-Only mode, the SPI will only begin receiving data when a non-zero value is written to the transfer counter. The SPI will then continue to receive bytes of data from the slave device until the transfer counter has decremented to zero or the SPI module has been disabled. Receive-Only mode performs the write to the transmit buffer in hardware, removing this responsibility from the user.
The TXR and RXR bits of the SPIxCON2 register determine the Transfer/Receive mode that the SPI module operates in. To operate in Receive-Only mode, the RXR bit must be set and the TXR bit needs to be cleared. The following example shows how to configure the SPI module for Receive-Only mode. In this example, the transfer counter has been set up to receive eight bytes of data from a slave device.
uint8_t SPI_Exchange8bit(uint8_t data) {
SPIxCON1 = 0x00;
SPIxCON2 = 0x01; // Receive Only Mode (TXR = 0; RXR = 1)
SPIxBAUD = 0x00;
SPIxCLK = 0x00;
SPIxTCNT = 0x08; // SPIxTCNT
SPIxCON0 = 0x82; // EN = 1, LSBF = 0, MST = 1, BMODE = 0
while (SPIxTCNT > 0) {
data_RX = SPIxRXB;
}
}