top of page

14-STM32 UART RECEIVE INTERRUPT



Tutorial 14

Next Tutorial



This project demonstrates the use of UART interrupt method for controlling LED blinking patterns on an STM32L476 microcontroller. Three LEDs are 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 INTERRUPT method with HAL functions:

UART interrupt method involves configuring the UART peripheral to trigger interrupts upon receiving data. When data is received, an interrupt service routine (ISR) is executed to handle the received data. This method is more efficient than polling as it allows the microcontroller to perform other tasks while waiting for UART data.

In this example, we configure the UART peripheral to trigger interrupts upon receiving data. When data is received, the ISR is executed, and the received character is processed 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_IT(&huart2, &rx_buffer, 1, HAL_MAX_DELAY) == HAL_OK);

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
    // Handle UART receive interrupt
    if (huart->Instance == USART2) {
        // Process received data and trigger LED blinking
        // Code to handle LED blinking patterns based on received character
    }
}

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. Enable NVIC.

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

  5. Configure system clock settings.

SET UART UND GPIO:


SET NVIC:


9. "It does not matter how slowly you go, so long as you do not stop."- Confucius

CODE:


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);

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 interrupt 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 Interrupt communication: make the motor start and stop based on data received from the serial monitor.



Previous Tutorial

Tutorial 14

Next Tutorial





 
 
 

Recent Posts

See All

Yorumlar


bottom of page