本帖最后由 厉飞雨 于 2024-1-5 20:56 编辑
使用串口DMA发送格式化字符串,分别使用两种方法,感觉两种方法差不多,一种没有问题,另外一种会出现硬件错误,有大佬能分析一下这是为啥吗?(使用了freeRTOS,开启了EventRecorder)
第一种没有问题代码如下
[C] 纯文本查看 复制代码 float temperature,humidity;
char message[50];
while(1){
if(aht20State ==0)
{
...
}
else if(aht20State ==1)
{
...
}
...
else if(aht20State ==4)
{
AHT20_Analysis(&temperature, &humidity);
LED1_TOGGLE();
sprintf (message,"温度:%.1f ℃,湿度: %.1f %%\r\n",temperature,humidity);
HAL_UART_Transmit_DMA(&huart1,(uint8_t*)message,strlen(message));
osDelay(1000);
aht20State = 0;
}
}
第二种发送一次数据就会出现硬件错误如下
[C] 纯文本查看 复制代码 float temperature,humidity;
char message[50];
while(1){
if(aht20State ==0)
{
...
}
else if(aht20State ==1)
{
...
}
...
else if(aht20State ==4)
{
AHT20_Analysis(&temperature, &humidity);
LED1_TOGGLE();
u1_printf("温度:%.1f ℃,湿度: %.1f %%\r\n",temperature,humidity);
osDelay(1000);
aht20State = 0;
}
}
其中u1_printf函数如下
[C] 纯文本查看 复制代码 void u1_printf(char *fmt,...)
{
char String[100];
va_list arg;
va_start(arg, fmt);
vsprintf(String, fmt, arg);
va_end(arg);
HAL_UART_Transmit_DMA(&huart1,(uint8_t*)String,strlen(String));
}
|