I am going to give you an introduction to STM32's USART communication in this introduction. The USART is a way of communication of Microcontroller with computer. For example RS232 is widely used protocol for communication with PC.
With STM's Cube MX it is really simple to get started. The CubeMX is a graphical tool which can be used to generate the driver code required to get started in for of a complete project which has HAL(Hardware abstraction layer libraries etc) and other required project files. This project can then be edited in Attolic true studio which is eclipse based IDE.
First step is to download and install the latest CubeMX from the following website.
https://www.st.com/en/development-tools/stm32cubemx.html?sc=stm32cubemx
Next step, is to download and install Attolic true studio from the following website
https://atollic.com/truestudio/
Open the CubeMX software and select from file> New project. From the top left corner select the boards tab and then select the board you want to use. In my case it is STM32 Nucleo F334R8.
There are total 3 USARTs supported by this device and I am going to use USART 2 because it is by default connected to PC via ST link debuger and hence I can see the data I send to my PC without need of extra hardware(i.e. RS232 connector). Select the USART2 and in mode select Asynchronous mode then in hardware flow control select CTS/RTS mode.
After this is done next step is to generate the code. From the top right click on Generate code. It will prompt you to the projects setting window. Select the right tools, in my case I am using Attolic IDE so it will generate code for that IDE. If you are using for example ARM Keil you can select that too.
After the code is generated, open the folder where code is located. It will have a project solution. Open the project with true studio IDE and expland the /src folder where you will be able to see the main.c file. Have a look on the file it will have function something as follows.
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 38400;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_RTS_CTS;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
Now you can use this method to init the USART. similarly you will be able to see several other useful functions for communication.
Under drivers folder you will have HAL libraries for UART's.
In the main.c write the following
while (1)
{
/* USER CODE END WHILE */
char First[]="I am Ujjval";
HAL_UART_Transmit(&huart1, (uint8_t*)First, sizeof(First), 100);
/* USER CODE BEGIN 3 */
}
Now, be sure your board is conected to your PC, build the project and then go to the debug mode. open a serial terminal with right post and board rate selected. then run the project, you will be able to see the string on your terminal.. Enjoy!!
Comments