新手上路
- 积分
- 36
- 金钱
- 36
- 注册时间
- 2019-2-15
- 在线时间
- 6 小时
|
6金钱
STM32f103串口解析字符型数据变为整型,代码如下:
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
while(i<=1)
{
temp_data[i] = USART_ReceiveData(USART1);
temp_data[i] = temp_data[i] - 0x30;
i++;
}
printf("%d\r\n",temp_data[0]);
printf("%d\r\n",temp_data[1]);
arr = temp_data[0]*10+temp_data[1];
i = 0;
这样temp_data[0]和temp_data[1]总是取同样的值,比如说我发19,arr就等于99;
代码我感觉有问题,可就是没找出来!!
有没有大神
|
最佳答案
查看完整内容[请看2#楼]
换成这样试下
static u8 i = 0;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
temp_data = USART_ReceiveData(USART1);
temp_data = temp_data - 0x30;
i++;
printf("%d\r\n",temp_data);
if (i == 2)
{
arr = temp_data[0]*10+temp_data[1];
i = 0;
printf("%d\r\n",arr);
}
|