STM32通过UART向串口发送值

STM32 sending value via UART to serial port

我正在使用电位器来控制伺服电机。我想通过 UART 将伺服角度值发送到串口。首先,我得到了未知字符。现在,它每次都打印“伺服:0”,但我正在更改值。

uint32_t potadc;
uint32_t servopot;
char str[32];

int size_len = sprintf (str, "Servo : %lu\n", servopot, 0xFFFF);


while {
servopot = potadc/6;

HAL_UART_Transmit (&huart2, (uint8_t *)str, size_len, HAL_MAX_DELAY);
          HAL_Delay(1000);
        
        }

sprintf 必须在 内部 循环否则它只会在进入循环之前执行一次 /.

int size_len;

while(1) {
    servopot = potadc/6;
    size_len = sprintf (str, "Servo : %u\n", servopot);
    HAL_UART_Transmit (&huart2, (uint8_t *)str, size_len, HAL_MAX_DELAY);
    HAL_Delay(1000);        
}

我搬了

int size_len = sprintf (str, "Servo : %lu\n", servopot, 0xFFFF);

循环代码,成功了。感谢所有试图回答的人。