1. CS过短导致写SPI出现问题
spi_i2s_data_transmit(SPI1, byte);
while(RESET == spi_i2s_flag_get(SPI1, SPI_FLAG_TBE));
LCD_CS是用户软件控制的芯片使能脚。
实际发送了1.5bit后,SPI_FLAG_TBE已置位。因此判断SPI_FLAG_TBE会导致 LCD_CS过短。
2. 改善
while(RESET == spi_i2s_flag_get(SPI1, SPI_FLAG_TBE));
spi_i2s_data_transmit(SPI1, byte);
while(SET == spi_i2s_flag_get(SPI1, SPI_FLAG_TRANS)); //加此行,改善
3. 完整代码
void SPI1_SendByte(uint8_t byte)
{
while(RESET == spi_i2s_flag_get(SPI1, SPI_FLAG_TBE));
spi_i2s_data_transmit(SPI1, byte);
while(SET == spi_i2s_flag_get(SPI1, SPI_FLAG_TRANS));
}
uint8_t SPI1_ReadByte(uint8_t byte)
{
while(RESET == spi_i2s_flag_get(SPI1, SPI_FLAG_TBE));
spi_i2s_data_transmit(SPI1, byte);
while(RESET == spi_i2s_flag_get(SPI1, SPI_FLAG_RBNE));
return (uint8_t)spi_i2s_data_receive(SPI1);
}
void SPI1_SendBytes(const uint8_t *buf, uint16_t length)
{
uint16_t i;
for(i=0;i<length;i++)
{
while(RESET == spi_i2s_flag_get(SPI1, SPI_FLAG_TBE));
spi_i2s_data_transmit(SPI1, buf[i]);
}
while(SET == spi_i2s_flag_get(SPI1, SPI_FLAG_TRANS));
}
版权声明:本文为lljss1980原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。