cancel
Showing results for 
Search instead for 
Did you mean: 

Why led isn't blinking when i press the button?

YagciOnur
Associate II

I am tring to write a code for stm32f401cb which will blink the led when i press the led and when i leave pressing the button,it will turn out the led.I have set channel ahb1 which connects to port a and b in my microprocessor f401cb and i chose input type pull down in port a and led is connected to port b.

But my led is not blinking in anyway.

where is my mistake you think?

My led is not blinking when i press the button.it is always turned out

 

As you can see in the picture am holding the button but there is no response on the led.

Note:Port a is selected as input and port b is selected as output and both ports are controlled with their 0 (zero) pins.

 

my code is like that

 

Why i cant get a response on the led?hold button.PNG

 

#include "stm32f4xx.h"

void ahb1_config(void) {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
}

void gpio_config(void) {
    GPIO_InitTypeDef gpio_init;
    gpio_init.GPIO_Mode=0x00;
    gpio_init.GPIO_Pin=GPIO_Pin_0 ;
    gpio_init.GPIO_OType=0x00;
    gpio_init.GPIO_PuPd=0x02;
    gpio_init.GPIO_Speed=0x03;
    GPIO_Init(GPIOA,&gpio_init);

    gpio_init.GPIO_Mode=GPIO_Mode_OUT;
    gpio_init.GPIO_Pin=GPIO_Pin_0 ;
    gpio_init.GPIO_OType=GPIO_OType_PP;
    gpio_init.GPIO_Speed=GPIO_Speed_100MHz;
    GPIO_Init(GPIOB,&gpio_init);


}







int main(void)
{







  while (1)
  {
        ahb1_config();
        gpio_config();
        if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)) {
            GPIO_SetBits(GPIOB,GPIO_Pin_0);
        }
        else {
            GPIO_ToggleBits(GPIOB,GPIO_Pin_0);
        }

  }
}

 

3 REPLIES 3
Ozone
Lead

The LED is blinking, you just can't see it.

        if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)) {
            GPIO_SetBits(GPIOB,GPIO_Pin_0);
        }
        else {
            GPIO_ToggleBits(GPIOB,GPIO_Pin_0);
        }

Try to measure how fast that loop executes.

Or else, simply insert a delay of 50 ... 1000ms. 

Start by having it set or clear based on the switch. Confirm that the switch and LED work.

Not sure is schematic is supposed to be complete, but have a pull down resistor on BOOT0 and a 4u7 capacitor on VCAP

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

LED needs a current limiting resistor otherwise it will instantly burn out.

Schematic is missing power and other things.

Debug, set a breakpoint within GPIO_ReadInputDataBit and see if it gets triggered when button is down.

GPIO_ToggleBits should be GPIO_ClearBits if you want the LED to turn off and stay off when button isn't held.

If you feel a post has answered your question, please click "Accept as Solution".