Few general notes abour SPI module:
We can say that all SPI devices typically work as simply shift register. If clock at SCLK pin is presented, data are transferred. SPI Master produce clock signal.
In fact there are two buffers behind SPIDR register in MCU. When you read SPIDR register, you always read receiver buffer. When you write to SPIDR register, you always write to transmitter buffer.
When MCU is master, write to SPIDR register also start SCLK signal generation.
Settings SPI1BR = 0x00 (=fBUS/2) presents the maximum SCI speed for master mode. In slave mode is maximum communication speed fBUS/4. In fact, in slave mode have value of SPIBR no meaning, because baud rate divider is disabled to decrease IDD current.
There is example code for SPI read and SPI send:
#define SPIReadStatReg() (void)SPI0SR; // Clear Status reg.
#define SPIReadDataReg() (void)SPI0DR; // Clear Data reg.(Dummy read)
#define SPIWaitReceiveDone() while (!(SPI0SR_SPIF)); // Wait for full buf
#define SPIWaitTransferDone() while (!(SPI0SR_SPTEF));//Wait for buf empty
/*******************************************************************************/
void SPI_send_byte(unsigned char data)
{
SPIWaitTransferDone(); // Wait for buf empty
SPIReadStatReg(); // Read status register (possible SPRF, SPTEF)
SPIReadDataReg(); // Read receive data register. It clears flags
SPI0DR = data; // Start transfer - send byte
SPIWaitTransferDone(); // Wait for transfer complete
SPIWaitReceiveDone(); // Wait for receive complete
}
/*******************************************************************************/
unsigned char SPI_read_byte()
{
unsigned char value=0; // function cannot directly return register value
SPIReadStatReg(); // Read status register (possible SPRF, SPTEF)
SPIReadDataReg(); // Read receive data register. It clears flags
SPI0DR = 0x00; // Generate clocks for reading 1 byte of data!
SPIWaitReceiveDone(); // Wait for receive complete
value = SPI0DRL; // read received data and store to result
return(value);
}