I want to use a button connected to PA0 as an external interrupt to toggle LED on PE14 on button press. However calling the configure_PA0
function doesn't seem to work.
I did a simple blinking instruction in while loop to test and it turns out when I call configure_PA0
the LED stays ON all the time.
Without calling it, the LED will blink just fine so I think it must be something wrong with this function.
#include "stm32f30x.h"
void delay(volatile uint32_t count){
while(count > 0 )
count--;
}
void init_LED(){ //init led on PE14
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE,&GPIO_InitStructure);
}
void configure_PA0(void) {
GPIO_InitTypeDef GPIO_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
//PA0 as button init
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
//EXTI init
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
EXTI_InitStruct.EXTI_Line = EXTI_Line0;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStruct);
//NVIC init
NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
void toggle_PE14(){
if(GPIO_ReadOutputDataBit(GPIOE,GPIO_Pin_14) == 0)
GPIO_SetBits(GPIOE,GPIO_Pin_14);
else
GPIO_ResetBits(GPIOE,GPIO_Pin_14);
}
//handle pa0 interrupt
void EXTI0_IRQHandler(void) {
if(EXTI_GetITStatus(EXTI_Line0) != RESET){
toggle_PE14();
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
int main(void) {
init_LED();
configure_PA0();
while (1) {
delay(400);
}
return 0;
}
UPDATE
I fixed it by putting handler definition into extern "C" { .. }
brackets. Apparently you have to do that if you code in C++
.
configure_PA0
return? Set breakpoints onEXTI0_IRQHandler
andtoggle_PE14
. Do those functions ever execute? Do the conditionals within those functions work like you expect? – kkrambodelay(400); toggle_PE14();
in the while loop it will crash when entering delay for the first time and jump intoDefault_Handler(void)
. If deleteconfigure_PA0
call the toggling and delay works as expected. The function returns fine it seems, but it causes this infinite loop. – Viral