cancel
Showing results for 
Search instead for 
Did you mean: 

Changing environmental settings.

PLee.3
Associate III

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?

5 REPLIES 5
Peter BENSCH
ST Employee

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

In order 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.
Andrew Neil
Evangelist III

0693W000008xsqBQAQ.pngAlso, once your question is resolved:

0693W000008y9fZQAQ.png 

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.

Peter BENSCH
ST Employee

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

In order 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.

@PLee.3​  and also mark your previous thread on the same topic:

https://community.st.com/s/question/0D53W00001cqLcHSAU/stm32cube-writing-global-variables-possible