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

65 Views Asked by At

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

#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);
        }

  }
}

proteus circuit image is here

1

There are 1 best solutions below

2
Tom V On

Move the config functions outside the while loop.

The main problem however is that you have no delay, so you are toggling the LED as fast as the processor can, which is faster than your eye can see.