Receiving serial data in STM32


We have already discussed data transmission using UART where we sent the data from microcontroller to the computer. Today I will show you how to receive data using UART and display it on LCD.

Here I will connect LCD with PCF8574 using I2C. You can check my previous tutorial here if you haven’t did it yet.
Basically what I am going to do is, i will send data using a serial monitor from the computer and that data will be displayed on the LCD. We can do this using two ways:-
1.) We continuously check for incoming data and when data is received, display it on the lcd.
2.) Use interrupt. Microcontroller does it’s normal operations, and whenever the data receiving completes, an interrupt is generated and ISR is executed, after completing ISR, control returns back to normal operation.
STM32CubeMx setup is as follows:-

Some insight into the code:-

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) // is a receive complete callback function. 
                                                           When the data is received via UART, this function is called. 
                                    So in order to display data on LCD, we have to write our code inside this function.

if (huart->Instance == USART2)//  checks whether the data received is from UART2 ?

HAL_UART_Receive_IT (&huart2, Rx_data, 1) // enables the interrupt receive function. 
              Whenever the data is received, an interrupt will be generated and ISR will be served. 
This function takes the following arguments:-

UART_HandleTypeDef *huart //is the handletype of UART we are using 
uint8_t *pData // is the pointer to the variable, where data need to be stored
uint16_t Size // is the size in bytes

I am going to use I2C to connect LCD with the Nucleo using PCF8574. Also I will use COMPIM in proteus to communicate with Nucleo. Here is my proteus setup



Data is sent from Computer

Received data is displayed on LCD

Check out the VIDEO Below


You can buy me a coffee sensor 🙂

download the CODE below

0 0 vote
Article Rating
, , , , , , ,
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x