分享一个基于STM32F0按键松手检测并且不堵塞主程序的方法

#include "stm32f0xx.h"                  // Device header
#include "Delay.h"
#include "BEE.h"
#include "Key.h"
#include "main.h"
uint8_t KZFH=0;
void Key_Init()
{
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN;
    //GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_0;
    GPIO_InitStruct.GPIO_PuPd= GPIO_PuPd_UP;
    GPIO_InitStruct.GPIO_Speed= GPIO_Speed_Level_3;//50
    GPIO_Init(GPIOB,&GPIO_InitStruct);
    
    GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN;
    //GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_6;
    GPIO_InitStruct.GPIO_PuPd= GPIO_PuPd_UP;
    GPIO_InitStruct.GPIO_Speed= GPIO_Speed_Level_3;//50
    GPIO_Init(GPIOA,&GPIO_InitStruct);

//返回按键变量
uint8_t Key_Z(void)
{
    uint8_t KeyNum=0;
    if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
    {
        Delay_ms(10);
        KeyNum=1;
    }
    if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0)
    {
        Delay_ms(10);
        KeyNum=2;
    }
    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6)==0)
    {
        Delay_ms(10);
        KeyNum=3;
    }
    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)==0)
    {
        Delay_ms(10);
        KeyNum=4;
    }
    return KeyNum;
}
uint8_t Key_GetNum(void)
{
    uint8_t KZ,KeyNum=0;
    KZ=Key_Z();//接收按键按下的值
    if(KZ!=0)
    {
        KZFH=KZ;//检测到按键按下
    }
    else if(KZFH!=0)//检测到按键松开并且按下过
    {
        Delay_ms(10);
        return KZFH;
    }
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值