Information Technology Reference
In-Depth Information
else
{
// No collision occurred.
// When the BF bit in SSPSTAT = 1, the transmit operation is complete.
while (!SSPSTATbits.BF);
}
// Success.
return ( 0 );
}
Reading a Byte
When receiving a byte, the shift register stores bits received on
SDI/DataOut. When a byte has been received, the port hardware copies the
byte to SSPBUF. The BF bit SSPSTAT is set to 1 and the SPI interrupt flag
in PIR1 is set to 1 to indicate that a byte has arrived. Firmware can detect
received data with the SPI interrupt or by polling BF. After the hardware
copies a received byte to SSPBUF, the shift register can begin receiving
another byte. Reading a byte from SSPBUF clears the BF bit. Firmware
must clear the interrupt flag.
Microchip's C18 compiler libraries include a ReadSPI function, which reads
a byte from the SPI bus. However, the function isn't usable for MultiMedi-
aCard communications because ReadSPI holds DataIn low while reading a
byte, and MultiMediaCards require DataIn to be high. The ReadMedia
function reads a byte while holding DataIn high:
byte ReadMedia(void)
{
// Write FFh to the SPI buffer to hold the MultiMediaCard's DataIn line high
// while reading a byte.
SSPBUF = 0xFF;
// When the BF bit in SSPSTAT = 1, SSPBUF contains a byte read from the bus.
while (!SSPSTATbits.BF);
return(SSPBUF);
}
Search WWH ::




Custom Search