سؤال

I am trying to turn on the LEDs when the user button is pressed

I think I have enabled the peripheral clocks right and the right register. The button is on porta bit 0

Here is my code...any help would be great. Sorry if it is a bit simple, I am still learning the board.

int main (void)
{
RCC->AHB1ENR=0x9;           
GPIOA->MODER = 0x00000002; 
GPIOD->MODER = 0x55000000;  
GPIOD->OTYPER = 0;          
GPIOD->OSPEEDR = 0;         
GPIOD->PUPDR = 0;           
GPIOA->PUPDR = 0;
GPIOA->OTYPER = 0;
GPIOA->OSPEEDR = 0;

    while(1)
{
    if(GPIOA->IDR == 0x0001){
    GPIOD->ODR = 0xF000;                        
    }
    else{
        GPIOD->ODR = 0;         

        }   
    }   
}
هل كانت مفيدة؟

المحلول

I don't know the STM32f4 but I'm guessing that instead of

if(GPIOA->IDR == 0x0001)

You want

if ((GPIOA->IDR & 0x0001) != 0)

The original checks that the low bit is on AND all other bits are off while the new version just checks the low bit and ignores the rest.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top