cancel
Showing results for 
Search instead for 
Did you mean: 

How to send and recevie data from serial print.

sangarius
Associate II

I'm using servo and driving it with servo motor. I want to print the servo degree via UART to serial. But im getting unknown characters. My code is;

/* USER CODE BEGIN 0 */
uint32_t potadc;
uint32_t servopot;
/* USER CODE END 0 */
 
/* USER CODE BEGIN 1 */
	char str[16];
  /* USER CODE END 1 */
 
 
 HAL_UART_Transmit(&huart2, (uint32_t)*str, sprintf(str, "Servo : %d\n", servopot), 0xFFFF);
 
 	  	  HAL_Delay(1);

Thanks in advance.

21 REPLIES 21
RomainR.
ST Employee

oups, sorry.

Not %ld but %lu is the correct format.

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

If still not work, check baudrate, data bit lenght and uart parity settings.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

RomainR.
ST Employee

Can you tell wich compiler / IDE tools you use ?

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

I'm using STM32CubeIDE.

It's still same. I just checked the settings.

RomainR.
ST Employee

here my test, it works on my side with trace using TeraTerm.

0693W0000059crVQAQ.png 

main.c

/* USER CODE BEGIN Includes */
#include <stdio.h>
 
/* USER CODE END Includes */

/* USER CODE BEGIN PV */
char str[16] = {0};
uint32_t servopot = 1234;
 
/* USER CODE END PV */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
 
  int size_len = sprintf (str, "Servo : %lu\n", servopot);
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  	HAL_UART_Transmit (&huart2, (uint8_t *)str, size_len, HAL_MAX_DELAY);
  	HAL_Delay(1000);
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

Can you shareyour code (UART2 setup) and hardware you use ?

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

I'm controlling the servo with potentiometer. "potadc" means the value came from ADC of the potentiometer. "servopot" means potadc/6, the rating of the servo 0-170. I want to see the value of the servopot live when i change the potentiometer.

Yeah, I think we all understood that, but there's a problem with the UART configuration and your hardware, and your posts lack any salient details to be able to diagnose.

Diagram what you have wired to where. And specify the STM32 part/board in question, several thousand different things out there, no idea what you're working with.

Stick a scope on the signal and figure out why you've got the levels or baud rate wrong,

Will save everyone wasting hours on this..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I'm using STM32F070RB. My connections are nearly same with this Arduino diagram;

0693W0000059hNLQAY.pngI'm using USART2 with asynchronous mode. I just changed the baud rate 9600bits/s. 8 bit word length. 1 stop bit. No parity bits. TIM settings are; 19 prescaler, 1000 ARR, PWM 500 Pulse. Clock speed at 1 Mhz.

/* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */
 
  /* USER CODE END USART2_Init 2 */

So a NUCLEO board?

M​ake sure that HSE_VALUE is set to 8000000 typically in the stm32f0xx_hal_conf.h file

M​ake sure the PLL settings for the clocks are set properly.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..