2020-01-07 10:19 PM
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.
2020-01-08 12:20 AM
Turning the LED off (low) is wrong, use
GPIOA->ODR &= ~0x00000001;
2020-01-08 12:34 AM
I would suggest to use the BSRR register, instead of ODR.
2020-01-08 01:31 AM
> if(GPIOB->IDR & 0x00000400)
That's PB10, not PB6.
> GPIOA->ODR = 0x00000001;
That's PA0, not PA1.
JW
2020-01-10 09:19 PM
right