Skip to main content
PLee.3
Associate II
June 22, 2022
Question

Changing environmental settings.

  • June 22, 2022
  • 3 replies
  • 1813 views

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?

This topic has been closed for replies.

3 replies

Peter BENSCH
Technical Moderator
June 22, 2022

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.
PLee.3
PLee.3Author
Associate II
June 22, 2022

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.

Andrew Neil
Super User
June 22, 2022

0693W000008xsqBQAQ.pngAlso, once your question is resolved:

0693W000008y9fZQAQ.png 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Peter BENSCH
Technical Moderator
June 22, 2022

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.
Andrew Neil
Super User
June 22, 2022

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

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

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.