这个STM8的SPI通信也够奇葩的,跟STM32有大区别。光判断发送空和接收空的标志还不行,还必须判断忙标志位,而且读数的时候要等忙标志完毕才能读,废话少说上代码: 这是一段发2字节收2字节的代码,注意如果第二个字节接收才有效的话,也需要发送第二个字节才能提供时钟进行接收: GPIO_WriteLow(GPIOA, GPIO_PIN_3);
while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET){}
SPI_SendData(0x80 | 0x75);
while(SPI_GetFlagStatus(SPI_FLAG_BSY) == SET){}
while(SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET){} /* ¸Ã×Ö½Ú¶ªÆú */
ret1 = SPI_ReceiveData();
while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET){}
SPI_SendData(0x75);
while(SPI_GetFlagStatus(SPI_FLAG_BSY) == SET){}
while(SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET){}
ret2 = SPI_ReceiveData();
GPIO_WriteHigh(GPIOA, GPIO_PIN_3);
|