独立按键

–Key.h

#ifndef __Key_h
#define __Key_h
#include "Led.h"

#define  KEY1    GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_8)
#define  KEY2    GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_9)
#define  KEY3    GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_10)
#define  KEY4    GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_11)

#define KVALUE1  LED1 = ~LED1_IN
#define KVALUE2  LED2 = ~LED2_IN
#define KVALUE3  LED3 = ~LED3_IN
#define KVALUE4  LED4 = ~LED4_IN

void Scan_Key_Configuration(void);
int Key_Scan();
void Key_Delay(int ms);
#endif

–Key.c

#include <stdio.h>
#include "stm32f10x.h"
#include "Key.h"

void Scan_Key_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 
                                                | GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 
    //上拉输入模式
    GPIO_Init(GPIOD ,&GPIO_InitStructure);

}
int Key_Scan()
{
    if(KEY1 == 0 || KEY2 == 0 || KEY3 == 0 || KEY4 == 0)
    //如果按下键盘
    {
        Key_Delay(2);
        if(KEY1 == 0)
        {           
            while(!KEY1);
            //去抖
            return KVALUE1;
        }
        else if(KEY2 == 0) 
        { 
            while(!KEY2);
          return KVALUE2;
        }
        else if(KEY3 == 0)
        {           
            while(!KEY3);
            return KVALUE3;
        }
        else if(KEY4 == 0) 
        {
            while(!KEY4);
            return KVALUE4;
        }
    }else if((KEY1 == 1) && (KEY2 == 1) && (KEY3 == 1) && (KEY4 == 1))
    {
        return 0;
    }
}

void Key_Delay(int ms)
{
        int i = 0;
        int j = 0;
        for( i = 0 ; i < 100 ; i++)
        for(j = 0 ; j < ms ; j++);
}

void Set_LED_Reverse(int t)
{
    if(t == 1) LED1 = ~LED1_IN;
    //小灯亮灭
    else if(t == 2) LED2 = ~LED2_IN;
    else if(t == 3) LED3 = ~LED3_IN;
    else if(t == 4) LED4 = ~LED4_IN;
    else if(t == 0) return;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值