This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Working with buttons

Hello) Please, tell me what I'm doing wrong. I want to make the LED (PE1) work when the (PB13) button is pressed, but it does not work, as I understand that I am not correctly initializing the mode of the button port, there must be Input mode, but I don’t understand how to do it. I just have an LED on, but nothing happens when the button is pressed. My board is stm32h743zi. Can you please tell me what I'm doing wrong, I checked all the sites, but I couldn't find anything. p.s. I tried to write the same thing through HAL, everything naturally works there, but I want to understand how to work with a button without HAL.

#include "stm32h7xx.h"

void GPIO_Init(void);

int main(void) {

GPIO_Init();

if((GPIOC->IDR & GPIO_IDR_ID13) != 0) {

GPIOE->BSRR |= GPIO_BSRR_BR1;

}else{

GPIOE->BSRR |= GPIO_BSRR_BS1;

}

}

void GPIO_Init ()

{

RCC->AHB4ENR |= RCC_AHB4ENR_GPIOEEN;

RCC->AHB4ENR |= RCC_AHB4ENR_GPIOCEN;

GPIOE->MODER = GPIO_MODER_MODE1_0;

GPIOC->MODER &= ~GPIO_MODER_MODE13;

}

   
Parents
  • Thank you, I figured out how it works. It was not easy to understand. Maybe someone will need this code. I wish you all good luck)

    #include "stm32h7xx.h"

    #define LED_PORT GPIOE
    #define LED_PORT_1 GPIOB
    #define LED_ORANGE (1 << 1)
    #define LED_GREEN (1 << 0)

    //void GPIO_Init(void);

    static inline void setup_leds(void){
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIOEEN;
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIOBEN;
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIOCEN;

    GPIOC->MODER &= ~GPIO_MODER_MODE13;
    //GPIOC->MODER |= GPIO_MODER_MODE13_1;
    LED_PORT->MODER = GPIO_MODER_MODE1_0;
    LED_PORT_1->MODER = GPIO_MODER_MODE0_0;
    }

    static inline void switch_leds_off(void){
    LED_PORT->ODR = 0;
    }


    int main(void)
    {
    setup_leds();
    //GPIO_Init();

    while (1) {
    if((GPIOC->IDR & GPIO_IDR_ID13) == 0)
    {
    LED_PORT->ODR |= LED_ORANGE;
    //LED_PORT->BSRR |= GPIO_BSRR_BR1;
    //LED_PORT->ODR |= LED_ORANGE;

    }else{
    //LED_PORT_1->ODR |= LED_GREEN;
    //LED_PORT_1->BSRR |= GPIO_BSRR_BR0;
    //LED_PORT_1->ODR |= LED_GREEN;
    LED_PORT->BSRR |= GPIO_BSRR_BR1;

    }
    }

    }

    /*void GPIO_Init () {
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIOEEN;
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIOCEN;
    GPIOE->MODER = GPIO_MODER_MODE1_0;
    //GPIOC->MODER = GPIO_MODER_MODE13;

    GPIOC->MODER |= GPIO_MODER_MODE13_0;
    GPIOC->MODER |= GPIO_MODER_MODE13_1;
    }*/

     

Reply
  • Thank you, I figured out how it works. It was not easy to understand. Maybe someone will need this code. I wish you all good luck)

    #include "stm32h7xx.h"

    #define LED_PORT GPIOE
    #define LED_PORT_1 GPIOB
    #define LED_ORANGE (1 << 1)
    #define LED_GREEN (1 << 0)

    //void GPIO_Init(void);

    static inline void setup_leds(void){
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIOEEN;
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIOBEN;
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIOCEN;

    GPIOC->MODER &= ~GPIO_MODER_MODE13;
    //GPIOC->MODER |= GPIO_MODER_MODE13_1;
    LED_PORT->MODER = GPIO_MODER_MODE1_0;
    LED_PORT_1->MODER = GPIO_MODER_MODE0_0;
    }

    static inline void switch_leds_off(void){
    LED_PORT->ODR = 0;
    }


    int main(void)
    {
    setup_leds();
    //GPIO_Init();

    while (1) {
    if((GPIOC->IDR & GPIO_IDR_ID13) == 0)
    {
    LED_PORT->ODR |= LED_ORANGE;
    //LED_PORT->BSRR |= GPIO_BSRR_BR1;
    //LED_PORT->ODR |= LED_ORANGE;

    }else{
    //LED_PORT_1->ODR |= LED_GREEN;
    //LED_PORT_1->BSRR |= GPIO_BSRR_BR0;
    //LED_PORT_1->ODR |= LED_GREEN;
    LED_PORT->BSRR |= GPIO_BSRR_BR1;

    }
    }

    }

    /*void GPIO_Init () {
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIOEEN;
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIOCEN;
    GPIOE->MODER = GPIO_MODER_MODE1_0;
    //GPIOC->MODER = GPIO_MODER_MODE13;

    GPIOC->MODER |= GPIO_MODER_MODE13_0;
    GPIOC->MODER |= GPIO_MODER_MODE13_1;
    }*/

     

Children