cancel
Showing results for 
Search instead for 
Did you mean: 

Push button is not turning on the LED

MRamy.1
Associate II

Simple program. Trying to make a push button turn on an LED. I have connected on pin of the push button to 5V and the other pin to the STM32F411E-Disco board directly without any resistors. The anode of the LED is connected to a 330 ohm resistor and connected to the board and the cathode pin is connected to the GND. On the board PORTA Pin 1 is the output and PORTB Pin 6 is the input. In the PUPDR Register I made Pin 6 to have a Pull Down Resistor.

#include "stm32f4xx.h"                  // Device header
 
int main(void){
	
	RCC->AHB1ENR |= 0x00000001; // Clock to PORTA
	RCC->AHB1ENR |= 0x00000002; // Clock to PORTB
	
	GPIOA->MODER = 0x00000004; // Port A pin 1 output
	GPIOB->MODER = 0x00000000; // Port B pin 6 input
	GPIOB->PUPDR = 0x00002000; // Pull Down Resistor on Port B pin 6
	
	while(1){
			if(GPIOB->IDR & 0x00000400)
				GPIOA->ODR = 0x00000001;
			else
				GPIOA->ODR ^= 0x00000001;
	}
}

The program isn't working, how can I fix the code for the LED to turn on when I press the push button and off when the push button isn't pressed.

4 REPLIES 4
KnarfB
Principal III

Turning the LED off (low) is wrong, use

GPIOA->ODR &= ~0x00000001;

Ozone
Lead

I would suggest to use the BSRR register, instead of ODR.

> if(GPIOB->IDR & 0x00000400)

That's PB10, not PB6.

> GPIOA->ODR = 0x00000001;

That's PA0, not PA1.

JW

dbgarasiya
Senior II

right