top of page

5-STM32 PWM2



Previous Tutorial

Example 3

Next Tutorial



This project demonstrates an LED blinking project using the STM32L476 microcontroller, with the LED toggling its state in response to an interrupt generated.


It showcases the usage of interrupts to handle external events and provides a practical example of event-driven programming in embedded systems.


In this session, I will give you homework to solve two examples and make one of your own.



What are Interrupts?

Interrupts are signals generated by hardware or software events that temporarily halt the normal execution flow of a microcontroller to handle the event. When an interrupt occurs, the microcontroller suspends its current task, executes a predefined interrupt service routine (ISR) to handle the event, and then resumes normal operation.


Types of Interrupts in STM32 Microcontrollers:

  1. External Interrupts: These interrupts are triggered by external events such as a button press, a sensor signal, or a peripheral status change. STM32 microcontrollers typically have multiple external interrupt lines that can be configured to trigger interrupts on rising edges, falling edges, or both.

  2. Peripheral Interrupts: These interrupts are generated by internal peripherals such as timers, UART (Universal Asynchronous Receiver-Transmitter), SPI (Serial Peripheral Interface), and I2C (Inter-Integrated Circuit). Peripheral interrupts are triggered by events such as timer overflow, data received, or transmission complete.

  3. System Interrupts: These interrupts are generated by system events such as a system tick (used for timekeeping in real-time operating systems), memory management faults, or system faults. System interrupts help manage system-level operations and handle exceptional conditions.


Interrupt Handling in STM32 Microcontrollers: In STM32 microcontrollers, interrupt handling involves several steps:

  1. Interrupt Request (IRQ): When an interrupt event occurs, an interrupt request (IRQ) signal is sent to the microcontroller's NVIC (Nested Vectored Interrupt Controller). The NVIC prioritizes interrupts based on their importance and decides which interrupt to handle first.

  2. Interrupt Service Routine (ISR): Each interrupt has a corresponding interrupt service routine (ISR), a predefined function in the firmware that handles the interrupt event. When an interrupt is triggered, the microcontroller automatically jumps to the corresponding ISR to execute the required actions.

  3. Interrupt Priority: Interrupts in STM32 microcontrollers can have different priority levels. The NVIC allows programmers to assign priority levels to interrupts based on their criticality. Higher priority interrupts can preempt lower priority interrupts, ensuring that the most important tasks are handled promptly.

  4. Interrupt Clearing: After the ISR completes its execution, it clears the interrupt flag associated with the interrupt source. This informs the microcontroller that the interrupt event has been handled, allowing it to resume normal program execution.

void EXTI0_IRQHandler(void) {
  if (EXTI->PR1 & EXTI_PR1_PIF0) { // Check if EXTI0 interrupt flag is set
        HAL_GPIO_TogglePin(LED_PORT, LED_PIN); // Toggle LED state
        EXTI->PR1 |= EXTI_PR1_PIF0; // Clear EXTI0 interrupt flag
    }
}
  1. Interrupt Callback Function:

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin==GPIO_PIN_0)
	{
	HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
	}
}

Benefits of Interrupts:

  • Improve responsiveness: Interrupts allow microcontrollers to respond quickly to external events, reducing latency and improving system responsiveness.

  • Efficient resource utilization: By temporarily suspending the execution of non-critical tasks, interrupts help optimize resource utilization and ensure that system resources are allocated effectively.

  • Simplify event-driven programming: Interrupt-driven programming simplifies the handling of asynchronous events, making it easier to develop applications that interact with external devices and respond to real-world stimuli.

In summary, interrupts are a powerful mechanism for handling events in STM32 microcontrollers, enabling efficient event-driven programming and improving system responsiveness. Understanding how to use interrupts effectively is essential for developing robust and responsive embedded systems applications.


Required Parts For Example:

  1. STM32L476 development board.

  2. LED (for blinking).

  3. Button

  4. 220 Ohm

  5. Breadboard (optional).


CUBEIDE Configuration Steps:


CUBEIDE Configuration Steps:

  • Create a new project in CUBEIDE.

  • Select the STM32L476 microcontroller variant.

  • Configure GPIO pin PA5 for the LED (output) and PA0 button (input).

  • Configure system clock settings.

  • Generate initialization code and project configuration.



"Things work out best for those who make the best of how things work out." - John Wooden

CODE:

void delay(uint32_t time_ms) {
    // Simple delay function
    uint32_t tickstart = HAL_GetTick();
    while ((HAL_GetTick() - tickstart) < time_ms) {}
}

int main(void)
{
  HAL_Init();
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();

  while (1)
  {
			//////////
  }
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin==GPIO_PIN_0)
	{
	HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
	}
}

Result


  • The STM32L476 LED blink project with interrupt usage demonstrates the toggling of an LED in response to a button press.

  • When the button is pressed, an interrupt is generated, triggering the LED to toggle its state.

  • This project highlights the utilization of interrupts for event-driven programming in embedded systems, providing a more efficient and responsive approach compared to polling-based methods.

  • By incorporating interrupt handling, the microcontroller can efficiently respond to external events while conserving CPU resources, making it suitable for real-time applications and systems requiring rapid event processing.


Same Example wıth dıfferent Method


Now let's solve the above example using the same class but a different method and function.

void EXTI0_IRQHandler(void) {
    if (EXTI->PR1 & EXTI_PR1_PIF0) { // Check if EXTI0 interrupt flag is set
        HAL_GPIO_TogglePin(LED_PORT, LED_PIN); // Toggle LED state
        EXTI->PR1 |= EXTI_PR1_PIF0; // Clear EXTI0 interrupt flag
    }
}

Explanation:

  • void EXTI0_IRQHandler(void): This function is the interrupt service routine (ISR) for the EXTI0 (External Interrupt 0) line. It is automatically called when an interrupt event occurs on the EXTI0 line.


  • if (EXTI->PR1 & EXTI_PR1_PIF0): This line checks if the interrupt flag for EXTI0 is set. The EXTI peripheral has multiple interrupt lines, each represented by a bit in the PR1 (Pending Register 1) register. EXTI_PR1_PIF0 is a mask representing the interrupt flag for EXTI0.


  • HAL_GPIO_TogglePin(LED_PORT, LED_PIN): If the interrupt flag is set, this line toggles the state of the LED connected to the specified GPIO port and pin using the HAL function HAL_GPIO_TogglePin().


  • EXTI->PR1 |= EXTI_PR1_PIF0: After handling the interrupt, this line clears the interrupt flag for EXTI0 by setting the corresponding bit in the PR1 register. This prevents the ISR from being called repeatedly for the same interrupt event.


#define LED_PIN     GPIO_PIN_5   // Pin PA5
#define LED_PORT    GPIOA

#define BUTTON_PIN  GPIO_PIN_0   // Pin PA0
#define BUTTON_PORT GPIOA

void EXTI0_IRQHandler(void) {
    if (EXTI->PR1 & EXTI_PR1_PIF0) { // Check if EXTI0 interrupt flag is set
        HAL_GPIO_TogglePin(LED_PORT, LED_PIN); // Toggle LED state
        EXTI->PR1 |= EXTI_PR1_PIF0; // Clear EXTI0 interrupt flag
    }
}

int main(void) {
    HAL_Init();
    GPIO_Init();

    EXTI_Config();

    while (1) {
        // Main program loop, LED toggling is handled by the interrupt
    }
}

Now it's your turn.
Now it's your turn. Above, we changed the state of a LED with a button interactively. What you'll do is different: you'll change the state of two LED with a different pin sequentially. If you get stuck, feel free to write in the comments, and we'll help you out. Those who solve it can share their solutions in the comments to assist others.






Previous Tutorial

Example 3

Next Tutorial





 
 
 

Recent Posts

See All

Comments


bottom of page