Ivan Rob
Ivan Rob

Reputation: 1

Reading IDR on STM32F411 Board

I'm currently trying to learn how to program these microcontrollers at a register level on my own and I've just hit a dead end

What I'm trying to accomplish is very simple. Basically the STM32F411 board that I have has a Led mapped on pin A5 and a push button mapped on PIN C13

I was trying to make a simple LED blink program where the LED on A5 would turn on when the button is pressed and turned off when the button is unpressed

Right now, as soon as I load my program, the LED turns on but no matter how many times I push the button, it's state does not change

I would appreciate if anybody could give me any insight here :C

I'm posting my code here below

#include "stm32f4xx.h"

/*
NOTES

USER LED IS ON PIN A5
USER BUTTON IS ON PIN C13

*/
uint8_t var=0;

int main(){


    RCC->AHB1ENR |=(1<<0); //Clock on port A
    RCC->AHB1ENR |=(1<<2); //Clock on port C

    //PORT A PIN 5 Config
    /*
        00: Input (reset state)
    01: General purpose output mode
    10: Alternate function mode
    11: Analog mode
    */
    GPIOA->MODER |= ~(1<<11);
    GPIOA->MODER |= (1<<10);
    GPIOA->OSPEEDR |= ((1<<11)|(1<<10));


    //PORT C PIN 13 Config
    /*
        00: Input (reset state)
    01: General purpose output mode
    10: Alternate function mode
    11: Analog mode
    */
    GPIOC->MODER |= ~(1<<27);
    GPIOC->MODER |= ~(1<<26);
    GPIOC->OSPEEDR |= ((1<<27)|(1<<26));
    GPIOC->PUPDR |= (1<<27);
    GPIOC->PUPDR |= ~(1<<26);

    while(1)
    {
        var = GPIOC->IDR;

        if(var==0)
        {
         GPIOA->BSRR = (1<<5);
        }       
        else
        {
         GPIOA->BSRR = 1<<(5+16);
        }

    }

}

Thank you in advance!

Upvotes: 0

Views: 861

Answers (1)

0___________
0___________

Reputation: 67476

this is wrong GPIOC->PUPDR |= ~(1<<26); it does not clear the 26th bit. you need to &= instead

this is also wrong

    var = GPIOC->IDR;

    if(var==0)
    {

you need to mask the correct bit. I did not check the correctness of the magic numbers

Upvotes: 1

Related Questions