0
votes

I just started to learn how to program with Nucleo board and decided to do a simple task. When I press my User button I want my LED to turn on (Off at the start). I have the following issues and I am not quite sure of what is the answer. I am wondering if it is specific to STM32 or it is a subtlety that I do not understand.

I am running the following code. My interrogation is that currently my LED is always turned on and Only when I press the user button does it go off. It does not make sense to me that When I press my button the value of my PinState = 0. I thought it should be one.

Thank you in advance

    #include "stm32f4xx.h"
    #include "stm32f4xx_nucleo.h"
    #include "system_stm32f4xx.h"
    #include "stm32f4xx_hal_gpio.h"
    #include "stm32f4xx_hal_rcc.h"

    GPIO_InitTypeDef GPIO_InitStructure; 

    int main(void) {

        HAL_Init(); 

        __GPIOA_CLK_ENABLE();
        GPIO_InitStructure.Pin   = GPIO_PIN_5;
        GPIO_InitStructure.Mode  = GPIO_MODE_OUTPUT_PP;    
        GPIO_InitStructure.Pull  = GPIO_PULLUP;
        GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;  
        HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);    

        __GPIOC_CLK_ENABLE();
        GPIO_InitStructure.Pin   = GPIO_PIN_13;
        GPIO_InitStructure.Mode  = GPIO_MODE_INPUT;
        GPIO_InitStructure.Pull  = GPIO_PULLDOWN;
        GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
        HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
        int PinState;
       while (1)
       {
         PinState = HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13);

         if(PinState == 1){
              HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5, GPIO_PIN_SET);
         }
         else {
             HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5, GPIO_PIN_RESET);
         }
      }
  }
2
Which nucleo board? There is no reason to assume that a pin state of 0 means led off and 1 means led on. You have to look at the schematic. Parts (I/O pads) can often sink more than they can source.old_timer
so looking at one of the boards. a high on PA5 will turn on the led. a low off. PC13 pin switch is normally open and pulls the pin low when pressed, so when pressed pc13 is a 0 and your code says to turn the led off in that case.old_timer
setting an output to 1 normally means the VCC or VDD voltage (or pretty close to it) is applied to the pin, and a 0 means GND (or pretty close to it) is applied. For a push-pull, open drain, etc. That changes things it can only sink not source unless there is a pull up. to sink you set the output to 0 to let it float you set it to 1 if you have a pull up then it tries to pull up or a pull down or tries to pull down, likewise with an external pull up or pull down.old_timer
The connection between a 1 and the source voltage and a 0 and ground is not necessarily documented (by every vendor every part) but is assumed.old_timer
I use a NucleoF446RE, I figured it was something like that, but it felt strange that is why I asked the question. I am not very familiar with these board/infrastructure just yet. I would have looked at a schematic, but they are not as available as Arduino datasheet.Hawk_08

2 Answers

0
votes

If you take a look at page 64 of the documentation of the f401re (see image bellow) you can see the B1 button (the blue one) has a pull-up resistor. For the programmer this means that the digital signal will allways be 1 as long as the button is unpressed, and 0 when it is pressed.

enter image description here

0
votes

In electronic, the signal is known as "active low", and represented by a __ bar over the symbol.