2020-10-28 03:21 AM
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.
Solved! Go to Solution.
2020-10-28 07:21 AM
here my test, it works on my side with trace using TeraTerm.
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.
2020-10-28 03:49 AM
Levels output by the STM32 are not directly compatible with RS232, you need to use a different adapter.
Check that your baud rate and settings are consistent.
Check that HSE_VALUE is consistent with your actual clock source.
2020-10-28 03:53 AM
(uint8_t *)str
2020-10-28 04:01 AM
I did it but still getting 999999 or ===== in different values. Baud rate and settings are OK.
2020-10-28 04:06 AM
Is 16 characters enough? Does setting servopot to 1234 give consistent reporting?
2020-10-28 04:23 AM
.
2020-10-28 04:37 AM
How many characters should i set?
2020-10-28 04:48 AM
Hi sangarius,
Set str before calling HAL_UART_Transmit(), and I think sprintf cannot use %d with unsigned long integer.
HAL_UART_Transmit(&huart2, (uint32_t)*str, sprintf(str, "Servo : %d\n", servopot), 0xFFFF);
Try:
uint8_t srt[16] = {0};
uint32_t servopot = 1234;
int size_len = sprintf (str, "Servo : %ld\n", servopot);
HAL_UART_Transmit (&huart2, (uint8_t)*str, size_len, HAL_MAX_DELAY);
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.
2020-10-28 05:00 AM
32 would give you some margin, the %d could output 11 digits and sign, you need space for the NUL and newline characters.
2020-10-28 05:20 AM
I tried this but still getting words like A A A A, not my servopot value.