0
)In this mode of operation, the SPIxTCNTH/L register pair and the SPIxTWIDTH register are concatenated to determine the total number of bits to be transferred. The total number of bits that will be transmitted equals (SPIxTCNT * 8) + SPIxTWIDTH. When using the SPI module in this mode of operation, the user can control the total number of bits being transmitted by writing the appropriate values to the SPIxTCNT and SPIxTWIDTH registers. The following example shows how the SPI module can be configured to send 45 bits of data, demonstrating Total Bit Count mode. Using these configuration settings in this example, the SPI module will transmit five 8-bit bytes of data along with one final packet that is five bits wide as specified by the SPIxTWIDTH register setting.
uint8_t SPI_Exchange8bit(uint8_t data) {
SPIxCON1 = 0x00;
SPIxCON2 = 0x03; // Full-Duplex Mode (TXR = RXR = 1)
SPIxBAUD = 0x00;
SPIxCLK = 0x00;
SPIxINTEbits.TCZIE = 1; // Transfer Counter is Zero Interrupt Enabled
SPIxTCNTL = 0x05; // SPI1TCNT
SPIxTCNTH = 0x00;
SPIxTWIDTH = 0x05; // SPI1TWIDTH
SPIxCON0 = 0x82; // EN = 1, LSBF = 0, MST = 1, BMODE = 0
SS1_SetHigh();
while (SPIxINTFbits.TCZIF == 0x0) {
SPIxTXB = data;
while (PIR2bits.SPIxRXIF == SPI_RX_IN_PROGRESS) {
}
data_RX = SPIxRXB;
}
SS1_SetLow();
SPIxINTFbits.TCZIF = 0;
}