Search code examples
cserial-portstm32uart

STM32 sending value via UART to serial port


I'm using potentiometer to control servo motor. I want to send the servo angle value to serial port via UART. First, i was getting unknown characters. Now, it prints "Servo: 0 " everytime but i'm changing the value.

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);
        
        }


Solution

  • sprintf has to be inside the loop otherwise it will be executed only once before entering the loop/.

    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);        
    }