Question
Toggle led using external interrupt from push button
I want to toggle a led when an external interrupt happened. But I could not.
That's my code using keil mdk for stm32f103c8t6
Any help, please?
#include "stm32f10x.h"
void GPIO_Config (void)
{
RCC->APB2ENR |= (1<<2); // Enable GPIOA clock
GPIOA->CRL &= ~(0xf<<4); // clear bits (7:6:5:4)
GPIOA->CRL |= (8<<4); // Bits (7:6:5:4) = 1:0:0:0 --> PA1 in Input Mode in Pull-up/ Pull-down mode
GPIOA->ODR |= (1<<1); // --> PA1 is in Pull UP mode
}
void EXTI1_IRQHandler(void)
{
if (EXTI->PR & (1<<1)) // If the PA1 triggered the interrupt
{
EXTI->PR |= (1<<1); // Clear the interrupt flag by writing a 1
toggle_GP(PC,13);
}
}
void Interrupt_Config (void)
{
RCC->APB2ENR |= (1<<0); // Enable AFIO CLOCK
AFIO->EXTICR[0] &= ~(0xf<<4); // Bits[7:6:5:4] = (0:0:0:0) -> configure EXTI1 line for PA1
EXTI->IMR |= (1<<1); // Bit[1] = 1 --> Disable the Mask on EXTI 1
EXTI->RTSR |= (1<<1); // Enable Rising Edge Trigger for PA1
EXTI->FTSR &= ~(1<<1); // Disable Falling Edge Trigger for PA1
NVIC_SetPriority (EXTI1_IRQn, 1); // Set Priority
NVIC_SetVector(EXTI1_IRQn, (uint32_t)&EXTI1_IRQHandler);
NVIC_EnableIRQ (EXTI1_IRQn); // Enable Interrupt
}
int main()
{
GPIO_Config();
init_GP(PC,13,OUT50,O_GP_PP); // initialize PC13 as output for toggle
toggle_GP(PC,13);
Interrupt_Config();
}
