2015-07-20 02:30 PM
I'm trying to interface a push button to my stm32F4 discovery via PA1 , this is the code I got :
void EXTI1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line1);
// Code goes here.
BlinkLED();
}
}
void initButton(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIOA clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
And the circuit I'm using :When I clicked nothing happened :\What's even weirder when I unplugged the jumper that connect this to PA1 the function suddenly worked and the LED started blinking !!! I tried again and seems that connecting the jumper alone to the pin PA1 triggers the function !!! like it's just a wire, why would this happen ?Can anyone tell me what I'm doing wrong ?? Thank you.
2015-07-20 02:39 PM
Should be PinSource1 for Pin1
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);2015-07-20 02:48 PM
Same thing still happening, I click the button nothing happens, I unplug the jumper afterwards the LED start blinking (function executed )
2015-07-20 02:59 PM
The ground symbol for 3.3V is a bit odd.
The DISCO has 3V supply, I'd probably configure the pin as an input with a pull down, seeing as you don't have one externally.2015-07-20 03:00 PM
Print out the level you see via GPIOA->IDR as you press and release the button.
2015-07-20 04:02 PM
Thank you , I had to set GPIO_InitStructure.GPIO_PuPd to GPIO_PuPd_DOWN.
Do you know how I can initialize 4 of these buttons on PA1 PA2 PA3 & PA4.I'm using 4 of these in fact.Thank you
2015-07-20 04:43 PM
You can create a pin list for GPIO_Init(), for SYSCFG_EXTILineConfig() you'll need one function per pin. One NVIC_Init() for each. Check the library for the EXTI
You'd need multiple IRQ handlers. If you use pin 5..9 those would go to a single IRQ (EXTI9_5)2015-07-21 12:51 AM
Done, working perfectly, Thank you !! You're a boss.