下面的代码:
按一下按键,led一直保持闪烁。
怎样才能改成,按一下按键,led保持闪烁,然后再按一下,led停止闪烁。以此类推
我试过在while(1)里面加上一个另外一个while(GPIOC->IDR & (1 << BUTTON_PIN)); 去检测按键是否按下,但是没有按上述要求工作
谢谢
while(1){
GPIOA->ODR |= 1 << LED_PIN; //led亮
delayMs(50); // Call the delay function
GPIOA->ODR &= ~(1 << LED_PIN); //led 灭
delayMs(50); // Call the delay function
while(GPIOC->IDR & (1 << BUTTON_PIN));
GPIOA->ODR &= ~(1 << LED_PIN); //led灭
}
下面是按一下键可以让led循环闪烁
#include "stm32L476xx.h"
#define LED_PIN 5
#define BUTTON_PIN 13
void delayMs(int n);
void toggleLed();
int main(void) {
//enable clock for GPIOA and GPIOC
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN;
// Configure LED2 pin as output
GPIOA->MODER &= ~(3UL << (2 * LED_PIN));
GPIOA->MODER |= (1UL << (2 * LED_PIN));
// Configure button pin as input
GPIOC->MODER &= ~(3UL << (2 * BUTTON_PIN));
// Configure button with no pull-up or pull-down
GPIOC->PUPDR &= ~(3UL << (2 * BUTTON_PIN));
while(GPIOC->IDR & (1 << BUTTON_PIN)); //wait for button press
while(1){
GPIOA->ODR |= 1 << LED_PIN;
delayMs(50); // Call the delay function
GPIOA->ODR &= ~(1 << LED_PIN);
delayMs(50); // Call the delay function
}
}
/* 16 MHz SYSCLK */
void delayMs(int n) {
int i;
for (; n > 0; n--)
for (i = 0; i < 3195; i++);
}