The SPI module found on newer PIC18 devices feature the ability to operate in Transmit-Only mode. This mode may be useful in applications where the SPI master only needs to transmit data and the data being received either does not matter or does not exist. When the SPI operates in Full-Duplex (Legacy) mode, received data is stored in the receive buffer and the data must be read from the RXFIFO before the SPI will continue data transfers. In Transmit-Only mode, data that is received from the slave device is not stored in the Receive FIFO and the SPI master will continue transmitting data via the transmit buffer (TXFIFO) without the need to clear the receive FIFO with every data transfer.
The TXR and RXR bits of the SPIxCON2 register determine the Transfer/Receive mode that the SPI module operates in. To operate in Transmit-Only mode, the TXR bit must be set and the RXR bit needs to be cleared. The following example shows how the SPI module can be used in Transmit-Only mode to send eight bytes of data. Please note that in this code snippet it is not necessary to poll the SPI Receive Buffer Interrupt flag because the SPI module is configured to only transmit data. In full-duplex operation, the user would need to intentionally clear the receive buffer with every transmission to keep the transaction going.
uint8_t SPI_Exchange8bit(uint8_t data) {
SPIxCON1 = 0x00;
SPIxCON2 = 0x02; // Transmit Only Mode (TXR = 1; RXR = 0)
SPIxBAUD = 0x00;
SPIxCLK = 0x00;
SPIxINTEbits.TCZIE = 1; // Transfer Counter is Zero Interrupt Enabled
SPIxTCNTL = 0x08; // SPIxTCNT
SPIxTCNTH = 0x00;
SPIxTWIDTH = 0x00; // SPIxTWIDTH
SPIxCON0 = 0x82; // EN = 1, LSBF = 0, MST = 1, BMODE = 0
SS1_SetHigh();
while (SPIxINTFbits.TCZIF == 0x0) {
SPIxTXB = data;
}
SS1_SetLow();
SPIxINTFbits.TCZIF = 0;
}