hi all,
How to clear SPISR_SPTEF bit?
DATASHHET the following description:
If set, this bit indicates that the transmit data register is empty. To clear
this bit and place data into the transmit data register, SPISR has to be read with SPTEF = 1, followed by a write
to SPIDR. Any write to the SPI Data Register without reading SPTEF = 1, is effectively ignored.
Can provide some routine? thank you!
已解决! 转到解答。
Hello,
When processing a single byte at a time, such as with the previous code snippet, the period that SPTEF flag remains low will be very short, typically a few bus cycles only. During debug, it is therefore unlikely that you would ever observe the flag being cleared.
However, you should not single step through the function for another reason. The monitoring of the SPISR and SPIDR registers by the debugger can inadvertently cause the SPIF flag to be cleared prior to the wait loop being exited. The function will then never exit.
Regards,
Mac
Hello,
The SPTEF flag will be cleared and will then become set during the normal course of sending a byte. You need to test the flag, to make sure it is set, prior to sending each byte. More important is the clearing of the SPIF flag following the completion of each byte transfer. The following code would be typical for the transfer of a single byte in both directions.
byte SPI_trans( byte value)
{
while ((SPISR & 0x20) == 0); // Wait until SPTEF flag is set
SPIDR = value;
while ((SPISR & 0x80) == 0); // Wait until SPIF flag is set
return SPIDR; // Also clears flag
}
Regards,
Mac
Hello,
When processing a single byte at a time, such as with the previous code snippet, the period that SPTEF flag remains low will be very short, typically a few bus cycles only. During debug, it is therefore unlikely that you would ever observe the flag being cleared.
However, you should not single step through the function for another reason. The monitoring of the SPISR and SPIDR registers by the debugger can inadvertently cause the SPIF flag to be cleared prior to the wait loop being exited. The function will then never exit.
Regards,
Mac