Enthernet Code
Enthernet Code
DIIDevHeads IoT Integration Server
Created by techielew on 9/9/2024 in #firmware-and-baremetal
Need Help with NVIC Pending Register Not Reflecting Button Press on STM32L476RG
@lokii first try trouble shooting to make sure everything works and is on the right places then try this code
#include "stm32f4xx.h"

void EXTI15_10_IRQHandler(void) {
// Check if EXTI line 13 triggered the interrupt
if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_13) != RESET) {
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_13); // Clear the interrupt flag
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_5); // Toggle the LED pin
}
}

int main(void) {
HAL_Init();

// Enable the GPIOC clock for the LED
__HAL_RCC_GPIOC_CLK_ENABLE();

// Configure LED pin (PC5) as output
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

// Enable the SYSCFG clock for EXTI
__HAL_RCC_SYSCFG_CLK_ENABLE();

// Configure button pin (PC13) as input with EXTI
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

// Set priority and enable EXTI line 15-10 interrupt in NVIC
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);

while (1) {
// Main loop
}
}
#include "stm32f4xx.h"

void EXTI15_10_IRQHandler(void) {
// Check if EXTI line 13 triggered the interrupt
if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_13) != RESET) {
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_13); // Clear the interrupt flag
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_5); // Toggle the LED pin
}
}

int main(void) {
HAL_Init();

// Enable the GPIOC clock for the LED
__HAL_RCC_GPIOC_CLK_ENABLE();

// Configure LED pin (PC5) as output
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

// Enable the SYSCFG clock for EXTI
__HAL_RCC_SYSCFG_CLK_ENABLE();

// Configure button pin (PC13) as input with EXTI
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

// Set priority and enable EXTI line 15-10 interrupt in NVIC
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);

while (1) {
// Main loop
}
}
The code sets up the LED on GPIOC pin 5 and configures an interrupt on GPIOC pin 13, using HAL functions to ensure proper setup of the NVIC and EXTI. The interrupt handler toggles the LED and clears the interrupt flag when triggered.
17 replies