STM32循环读取按键
#include "stm32f10x.h"
#include "Delay.h"
#include "led.h"
#include "key.h"
#include "OLED.h"
int main(void) {
OLED_Init();
Key_Init();
LED_Init();
OLED_ShowString(1, 1, "Welcome Jun..");
OLED_ShowString(2, 1, "Led Status:");
OLED_ShowString(3, 1, "Key Count:");
uint8_t key_press = 0, led = 0, cnt = 0;
while(1) {
key_press = 0;
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 0)
{
Delay_ms(20);
while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 0);
Delay_ms(20);
key_press = 1;
cnt ++;
}
if(key_press) led = (led == 0)? 1 : 0;
if(led == 0) LED_A0_OFF();
else LED_A0_ON();
OLED_ShowString(2, 12, (led == 1)? "ON " : "OFF");
OLED_ShowNum(3, 11, cnt, 2);
}
}
#include "stm32f10x.h"
#include "Delay.h"
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}