cancel
Showing results for 
Search instead for 
Did you mean: 

Push button problem on STM32F407G-DISC1 board

Test User
Associate III

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);
        }
	}
}
 

1 ACCEPTED SOLUTION

Accepted Solutions

> 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

View solution in original post

1 REPLY 1

> 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