top of page

13-STM32 UART RECEIVE POLLING



Previous Tutorial

Tutorial 13

Next Tutorial



This project demonstrates the use of UART polling method on the STM32L476 microcontroller to receive data from a PC.

  • The microcontroller continuously polls the UART receive buffer to check for incoming data.

  • Upon receiving data, the microcontroller processes it accordingly, in this case, adjusting an LED's brightness based on the received value from the PC.

  • This project showcases a UART polling method for controlling LED blinking patterns on an STM32L476 microcontroller. It involves three LEDs connected to PC7, PC8, and PC9 pins. Upon receiving specific characters ('1', '2', or '3') via UART, the microcontroller triggers different LED blinking patterns and sends corresponding messages back to the UART terminal.


UART RECEIVING POLLING method with HAL functions:

UART receiving polling method involves continuously checking the receive buffer for incoming data using HAL_UART_Receive function. This function waits until data is available in the receive buffer or until a timeout occurs. Once data is received, it can be processed accordingly.

In this example, we use HAL_UART_Receive function inside a while loop to continuously poll the UART receive buffer. When data is received, we check its value to determine the desired LED blinking pattern. Based on the received character ('1', '2', or '3'), different LED blinking patterns are triggered.

if (HAL_UART_Receive(&huart2, &rx_buffer, 1, HAL_MAX_DELAY) == HAL_OK);

Required Parts For Example:

  1. STM32L476 microcontroller board

  2. Three LEDs

  3. Three 220 OHM Resistors

  4. UART terminal (such as PuTTY or Tera Term...)

  5. Connecting wires


CUBEIDE Configuration Steps:

  1. Configure GPIO pins as OUTPUT for LED connections (PC7, PC8, PC9).

  2. Initialize UART peripheral for communication.

  3. Set up UART communication parameters (baud rate, data bits, stop bits, etc.).

  4. Configure system clock settings.

SET UART UND GPIO:

"All our dreams can come true if we have the courage to pursue them."- Walt Disney

CODE:

#include<string.h>

ADC_HandleTypeDef hadc1;
TIM_HandleTypeDef htim2;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_TIM2_Init(void);

uint8_t rx_buffer;
char tx_buffer[50]={'\0'};

#define SYSTICK_LOAD (SystemCoreClock/1000000U)
#define SYSTICK_DELAY_CALIB (SYSTICK_LOAD >> 1)

#define DELAY_US(us) \
	do { \
	      uint32_t start = SysTick->VAL; \
	      uint32_t ticks = (us * SYSTICK_LOAD)-SYSTICK_DELAY_CALIB;  \
	      while((start - SysTick->VAL) < ticks); \
	   } while (0)

#define DELAY_MS(ms) \
	  do { \
	      for (uint32_t i = 0; i < ms; ++i) { \
	         DELAY_US(1000); \
	        } \
	     } while (0)

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
	if(hadc->Instance == ADC1)
	{
		adc_valid=1;
		adc_value=HAL_ADC_GetValue(&hadc1);
	}
}

int main(void) {

    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_ADC1_Init();
    MX_TIM2_Init();

  HAL_ADC_Start_DMA(&hadc1, &adc_value, 1);

  while (1)
  {
	  rx_buffer=0;

  if (HAL_UART_Receive(&huart2, &rx_buffer, 1, HAL_MAX_DELAY) == HAL_OK) 
	{
	  switch (rx_buffer) {
		case '1':
		{
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, SET);
			sprintf(tx_buffer, "\r\n%c LED BLINK\r\n...",rx_buffer);
			HAL_UART_Transmit(&huart2, (uint8_t *)tx_buffer, sizeof(tx_buffer), HAL_MAX_DELAY);
		}
			break;
		case '2':
		{
		sprintf(tx_buffer, "\r\n%c 2 LED BLINK\r\n...",rx_buffer);
		HAL_UART_Transmit(&huart2, tx_buffer, sizeof(tx_buffer), 100);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, SET);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, SET);
		}
			break;
		case '3':
		{
		sprintf(tx_buffer, "\r\n%c 3 LED BLINK\r\n...",rx_buffer);
		HAL_UART_Transmit(&huart2, tx_buffer, sizeof(tx_buffer), 100);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, SET);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, SET);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, SET);
		}
			break;
		case '0':
		{
		sprintf(tx_buffer, "\r\nALL LED RESET\r\n...");
		HAL_UART_Transmit(&huart2, tx_buffer, sizeof(tx_buffer), 100);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, RESET);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, RESET);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, RESET);
		}
			break;
		default:
		{
		sprintf(tx_buffer, "PRESS 1 - 2 - 3 - OR 4 \r\n...");
		HAL_UART_Transmit(&huart2, tx_buffer, sizeof(tx_buffer), 100);
			HAL_Delay(1000);
		}
			break;
	}
   }
  }
  }

void SystemClock_Config(void) {
    // System Clock Configuration
    // Add code to configure system clock if needed
}

static void MX_GPIO_Init(void) {
    // GPIO Initialization
    // Add code to initialize GPIO pins if needed
}

static void MX_USART2_UART_Init(void){
    // GPIO Initialization
    // Add code to initialize GPIO pins if needed
}

Result

Upon running the code and opening the UART terminal, you can send characters '1', '2', or '3' to trigger different LED blinking patterns. For each character sent, the corresponding LED blinks, and a message indicating the number of LED blinks is sent back to the UART terminal. This project demonstrates the use of UART polling method to control LED blinking patterns on the STM32L476 microcontroller.





Now it's your turn.
Now it's your turn. What I'm asking from you is to control one DC motor using UART polling communication: make the motor start and stop based on data received from the serial monitor.



Previous Tutorial

Tutorial 13

Next Tutorial





 
 
 

Recent Posts

See All

Bình luận


bottom of page