2022-06-22 12:48 AM
Hi, Is there anyway to use a global variable to change the USART interface configuration such as the BaudRate :
static void MX_USART1_UART_Init(void)
{
If(A = 1)
{
huart1.Init.BaudRate = 38400;
}
If(A = 2)
{
huart1.Init.BaudRate = 9600;
}
}
Not sure if this is possible?
2022-06-22 01:16 AM
Of course you can do it this way, but you are working outside the user code. By the way, you can also assign the baud rate (9600, 115200, etc) directly to the global variable instead of a number and then set the global variable as the baud rate in the init routine.
With HAL you have to go a slightly different way, because the assignment
huart1.Init.BaudRate = xyz;
is not contained in a USER CODE block and is overwritten by CubeMX the next time it is regenerated. You could subsequently overwrite the initialisation in the following block
/* USER CODE BEGIN USART1_Init 2 */
[...]
/* USER CODE END USART1_Init 2 */
and carry it out again.
Regards
/Peter
2022-06-22 01:36 AM
Also, once your question is resolved:
2022-06-22 01:43 AM
Thanks Peter you are a star I will try this, it looks like it should work meaning I can change Communication settings using a user assigned variable.
2022-06-22 01:46 AM
Great!
As mentioned by @Andrew Neil, if the problem is resolved, please mark this topic as answered by selecting Select as best. This will help other users find that answer faster.
Regards
/Peter
2022-06-22 02:02 AM
@PLee.3 and also mark your previous thread on the same topic:
https://community.st.com/s/question/0D53W00001cqLcHSAU/stm32cube-writing-global-variables-possible