Serial Transmission in Stm32

As the title says, today we will see how to use UART for serial communication in STM32 devices. I am going to use STM32CubeMx to generate necessary code and than edit the code in Keil uvision 5.

UART is widely used for serial communication between two devices. It is easy to use protocol, because the clock sync is not involved. All you have to do is take care of BAUD RATE.  BAUD RATE must be same for both devices, amongst which communication is done. I am going to use 9600 as it is standard.

CubeMX Setup


1.)  Select the UART you want to use for communication. I am using UART2.



2.) In the ‘configuration’ tab, make sure USART settings are as follows.



Some Insight into the Code

uint8_t data[] = "HELLO WORLD!\n";

int main(void)
{

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_USART2_UART_Init();

  while (1)
  {
	HAL_UART_Transmit (&huart2, data, 14, 50);
	HAL_Delay (1000);    // wait for 1 sec
  }
}
  • Here we will use HAL_UART_Transmit to transmit the given data every second.
  • The parameters of this function are :-
  • The pointer to the handle of the UART i.e huart2
  • Pointer to the data that you want to send i.e data
  • The size of the data i.e 14 bytes
  • And finally the timeout (in ms) in case of any errors i.e 50 ms


Result

“hello World!” is transmitted from microcontroller to the computer and is being displayed on a serial window.
Check out the VIDEO Below
100%
100%

DOWNLOAD

You can buy me a coffee Sensor by clicking DONATE OR Just click DOWNLOAD to download the code

controllerstech

100%
100%

Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Menu