Skip to main content
Test User
Associate II
November 24, 2019
Solved

Push button problem on STM32F407G-DISC1 board

  • November 24, 2019
  • 1 reply
  • 731 views

What is wrong with my code? The code never hits the turn on led part when I press the B1-USER1 button. From the board manual there is a LED on PD13 and the user push button on PA0.

#include "stm32f4xx.h"
 
 
GPIO_InitTypeDef GPIO_InitDef;
 
int main(void)
{
 
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	
 GPIO_InitDef.GPIO_Pin = GPIO_Pin_13;
 GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
 GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
 GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
 GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz;
 GPIO_Init(GPIOD, &GPIO_InitDef);
	
 GPIO_InitDef.GPIO_Pin = GPIO_Pin_0;
 GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN;
 GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
 GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_UP;
 GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz;
 GPIO_Init(GPIOA, &GPIO_InitDef);
 
 
 while(1) {
 // If button on PA0 is pressed 
 if (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))
 {
 // Turn on LED 
 GPIO_SetBits(GPIOD, GPIO_Pin_13);
 }
 else
 {
 // Turn off LED
 GPIO_ResetBits(GPIOD, GPIO_Pin_13);
 }
	}
}
 

This topic has been closed for replies.
Best answer by waclawek.jan

> GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_UP;

The button switches to VDD (and to GND, as is the "usual" case).

So, you have to set pull down, not pullup.

JW

1 reply

waclawek.jan
waclawek.janBest answer
Super User
November 24, 2019

> GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_UP;

The button switches to VDD (and to GND, as is the "usual" case).

So, you have to set pull down, not pullup.

JW