Hello,
For C programs, the typical function for the master to transfer a single byte in each direction would be -
byte SPI_trans( byte val)
{
while (!SPI1S_SPTEF);
SPI1D = val; // Send byte value
while (!SPI1S_SPRF); // Wait for completion of transfer
return SPI1D;
}
The final read of SPI1D is necessary under all circumstances, to clear the SPRF flag and prevent an overrun condition. The manipulation of the SS output is external to the function. You probably will not be able to single step this code without disrupting its operation. This is because it is possible for the debugger itself to clear the hardware flags before the code can test the flag status.
To send each byte to the SPI slave device (and ignore the returned value), use the following call. The cast avoids a compiler warning because the returned value is not used.
(void)SPI_trans( byte_value);
To read the returned data from the slave, it is necessary to send a byte, usually a dummy value.
return_value = SPI_trans( 0); // Dummy send
Regards,
Mac
Message Edited by bigmac on
2008-09-28 04:30 PM