我试图通过按下并释放一次按钮来打开 LED,然后通过再次按下并释放它来关闭它,依此类推。我一起写了一堆代码,但我不确定听起来是否正确。我的意思是我不知道这是我的面包板还是连接松动,但不确定它是否按预期工作。我知道接线是正确的,只需在按下按钮时将其打开,然后通过释放按钮将其关闭即可。任何帮助深表感谢。谢谢你。
这是代码:
//Reset and clock control - Advanced high-performance bus - Enabling GPIO Port C pin 6 and Port B pin 1
RCC -> AHBENR |= RCC_AHBENR_GPIOCEN;
RCC -> AHBENR |= RCC_AHBENR_GPIOBEN;
//Setup Control Registers for the LED output
//Mode register as Output
GPIOC -> MODER |= GPIO_MODER_MODER6_0;
GPIOC -> MODER &= ~(GPIO_MODER_MODER6_1);
//OtypeR - Push pull
GPIOC -> OTYPER &= ~(GPIO_OTYPER_OT_6);
//OspeedR - High
GPIOC -> OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6;
//PUPDR
GPIOC -> PUPDR &= ~(GPIO_PUPDR_PUPDR6);
//Setup control registers for the push button input
//Mode register as input
GPIOB -> MODER &= ~(GPIO_MODER_MODER1);
//Pull up pull down register
GPIOB -> PUPDR &= ~(GPIO_PUPDR_PUPDR1); // Connected to ground externally (no need for internal pupdr
int counter = 0;
while (1) {
//If the button is pressed (IDR - input data register)
if((GPIOB -> IDR & (GPIO_IDR_1)) && counter == 0) //If button is pressed
{
GPIOC -> BSRR |= GPIO_BSRR_BS_6; //Turn ON the LED
if(~(GPIOB->IDR &(GPIO_IDR_1))) // If the button is released
{
GPIOC -> BSRR |= GPIO_BSRR_BS_6; //LED stays ON
}
}
counter = 1;
if((GPIOB -> IDR & (GPIO_IDR_1)) && counter == 1) //If button is pressed
{
GPIOC -> BRR |= GPIO_BRR_BR_6; //Turn OFF the LED
if(~(GPIOB -> IDR &(GPIO_IDR_1))) // If the button is released
{
GPIOC -> BRR |= GPIO_BRR_BR_6; //LED stays OFF
}
}
counter = 0;
}