cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 External button not working !

monta
Associate II
Posted on July 20, 2015 at 23:30

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 :

0690X00000605IEQAY.png

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.

7 REPLIES 7
Posted on July 20, 2015 at 23:39

Should be PinSource1 for Pin1

SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
monta
Associate II
Posted on July 20, 2015 at 23:48

Same thing still happening, I click the button nothing happens, I unplug the jumper afterwards the LED start blinking (function executed )

Posted on July 20, 2015 at 23:59

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 21, 2015 at 00:00

Print out the level you see via GPIOA->IDR as you press and release the button.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
monta
Associate II
Posted on July 21, 2015 at 01:02

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
Posted on July 21, 2015 at 01:43

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)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
monta
Associate II
Posted on July 21, 2015 at 09:51

Done, working perfectly, Thank you !! You're a boss.