Hi everyone,
I am having a bit of an issue with the SPI RX(reading data). I have the following code:
1. while (!(SPI1_S & SPI_S_SPTEF_MASK))
{}
2. SPI1_D = 0xFF; //Generate clocks, this shift data in
3. //Wait until SPRF clears
while ((SPI1_S & SPI_S_SPRF_MASK))
{}
4. ucData_Read = SPI1_D; //Get the data
surprising enough, if I step through it, I get the data I need. If I let it run, then my data is 0xFF, which is what I initialize
ucData_Read with. This is what I understood from the manual section of the SPI. If the SPTEF is set, then the transmit buffer is empty and the D register is ready to receive data, this is what I am doing with step 1. In step 2, I generate clocks
to shift data in (I can see the clocks and data in my oscilloscope). If the SPRF is set data is waiting to be read, this is what
I am doing in step 3. Finally in step 4, I read the data.
Can someone tell me if I am missing something?
Thanks.
Hi Neil,
Please modify your code like this:
1. while (!(SPI1_S & SPI_S_SPTEF_MASK))
{}
2. SPI1_D = 0xFF; //Generate clocks, this shift data in
3. //Wait until SPRF clears
while (!(SPI1_S & SPI_S_SPRF_MASK))
{}
4. ucData_Read = SPI1_D; //Get the data
You use the wrong code configuration, it should be when the SPI Read Buffer Full Flag=0, it should wait for the received data comming, otherwise, SPIx_S[SPRF]=1 means the data available in the receive data buffer.
Please modify it and test it again.
If you still have question, please let me know!
Have a great day,
Kerry
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------