Hello,
For every byte sent to the SPI, a data byte is returned via MISO (whether you actually make use of the data, or not). Because you are not explicitly clearing the SPRF flag, I assume a receive overrun error would be occurring after three bytes.
Modifying your function as follows should fix this problem.
unsigned char SPISendChar (unsigned char data)
{
while (!SPI1S_SPTEF); // Wait until transmit buffer is empty
SPI1D = data; // Send data
while (!SPI1S_SPRF); // Wait for flag set
return SPI1D // Also clears flag
}
Your code does not show the handling of the /SS signal to the remote slave. If you need to send a 16-bit value to the slave, this would be handled as two separate bytes, and with /SS set active prior to sending the first byte, and set inactive only after the second byte is completed. In this instance, you would need to use GP output as the SS signal, and not the "automatic" SS operation available within the SPI module.
Regards,
Mac