楼主这么写很是蛋疼。UART的发送部分用中断方式就好的。而且为啥要用HAL_Delay硬等五秒?用定时器定时,主程序去干别的事情,五秒钟到了再切换回来继续执行不好吗?不用RTOS用状态机也很容易实现的。
#define MAX_SENSOR_BUF_LEN 64
#define SENSOR_RXBUF_LEN 7
#define SENSOR_TXBUF_LEN 8
static uint8_t sensorUartBuffer[MAX_SENSOR_BUF_LEN];
static uint8_t sensorUartBufferIndex = 0;
static uint8_t sensorReportEnabled = 0;
static uint8_t prxbuff = 0;
//static uint8_t ptxbuff = 0;
static uint8_t rxbuff[SENSOR_RXBUF_LEN];
static uint8_t txbuff[SENSOR_TXBUF_LEN] = {0x30,0x03,0x00,0x06,0x00,0x01,0x60,0x2A}; //PM2.5
uint16_t i=0;
uint8_t *pSend;
pSend = txbuff;
HAL_Delay(5000);
while(i++ < SENSOR_TXBUF_LEN)
{
if(HAL_UART_Transmit(&MyDebugUart,(uint8_t *)(pSend++),1,5000) != HAL_OK)
{
debug_print("HAL_UART_Transmit fail!");
}
}
if( HAL_UART_Receive_IT(&MyDebugUart,(uint8_t *)sensorUartBuffer,1) != HAL_OK )
{
debug_print("HAL_UART_Receive fail!");
}
for(size_t i=0;i<=sizeof(rxbuff)-1;i++)
{
debug_print("@%d %x@",rxbuff[i],rxbuff[i]);
}
中断处理函数:
void sensorIRQHandler(void)
{
uint8_t data;
if((MyDebugUart.Instance->SR) & UART_FLAG_RXNE)
{
CLEAR_BIT(MyDebugUart.Instance->SR,UART_FLAG_RXNE|UART_FLAG_ORE);
data = MyDebugUart.Instance->DR;
/*
2关注 丨 2人回答
来自( 测试设备 )
3关注 丨 3人回答
来自( 医疗健康 )
1关注 丨 1人回答
来自( Mouser )
7关注 丨 6人回答
来自( 测试设备 )
5关注 丨 5人回答
来自( ST )