Hello,
Even when sending data using a single wire, the SPI data register needs to be read as part of the SPRF flag clearing process, to prevent an overrun condition, even though the returned data will be meaningless. Similarly, the receiving of return data needs to be initiated by the sending of a dummy byte. The data transfer direction will be determined by the BIDIROE control bit.
The procedure for the transfer of a byte in either direction can use a common function, similar to the following -
byte SPI_trans( byte val)
{
(void)SPIS; // Read status register
SPID = val;
while (!SPIS_SPRF); // Wait until transfer is complete
return SPID; // Also clears flag
}
Prior to each transfer, the setting of the BIDIROE bit will determine the desired data direction.
To send a command byte to the slave device -
SPIC2_BIDIROE = 1;
(void)SPI_trans( command);
To receive a response from the slave device -
SPIC2_BIDIROE = 0;
resp = SPI_trans( 0); // Send dummy byte
The use of the void casts should eliminate compiler warnings.
Regards,
Mac
Message Edited by bigmac on
2008-07-02 10:30 PM