简体   繁体   English

STM32 usart中断无法将正确的数据转换为其他功能

[英]STM32 usart interrupt cannot translate correct data to other functions

I have some problem when I use a stm32 discovery board send data to another one and it can get correct data and print in callback function, but cannot print correctly in other function. 使用stm32发现板将数据发送到另一个时,我遇到一些问题,它可以获取正确的数据并在回调函数中打印,但在其他函数中无法正确打印。

void UART7_IRQHandler()
{
    HAL_UART_IRQHandler(&huart7);
    HAL_UART_Receive_IT(&huart7, (uint8_t *)UART7RxBuffer, 16);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart)  
{   
    if(huart->Instance == UART7) {
        X = (UART7RxBuffer[1]-48) + (UART7RxBuffer[3]-48)*0.1 + (UART7RxBuffer[4]-48)*0.01 + (UART7RxBuffer[5]-48)*0.001;           
    }
}

But I receive wrong data like this 但是我收到这样的错误数据

void controller(){
    printf("%.3f\t\n", X);
}

It should be 0.012, and it correct in HAL_UART_RxCpltCallback(), I receive -38.02, -0.009, 0.512, 0.012, -1.948 and so on in controller. 它应该是0.012,并且在HAL_UART_RxCpltCallback()中正确,我在控制器中收到-38.02,-0.009、0.512、0.012,-1.948等。 How should I do to prevent this situation? 我应该如何预防这种情况?

Without knowing if your MCU actually supports floating point etc, I would probably do something like this in order to diagnose/debug what is happening: 在不知道您的MCU是否真正支持浮点等的情况下,我可能会执行以下操作以诊断/调试正在发生的事情:

void UART7_IRQHandler()
{
    HAL_UART_IRQHandler(&huart7);
    HAL_UART_Receive_IT(&huart7, (uint8_t *)UART7RxBuffer, 16);
}

char d1;
char d2;
char d3;
char d4;
float X1;
float X2;
float X3;
float X4;
float X;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart)  
{   
    if(huart->Instance == UART7) {
        d1 = UART7RxBuffer[1]-48;  /* add breakpoint here, and single step from here while inspecting variables */
        d2 = UART7RxBuffer[3]-48;
        d2 = UART7RxBuffer[4]-48;
        d3 = UART7RxBuffer[5]-48;
        X1 = d1;
        X2 = d2 * 0.1;
        X3 = d3 * 0.01;
        X4 = d4 * 0.001;
        X = X1 + X2 + X3 + X4;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM