矩阵键盘222

h文件

#ifndef __keyn_H__
#define  __keyn_H__
#include "sys.h"
#define KEY_CLO0_OUT_LOW  GPIO_WriteBit(GPIOE,GPIO_Pin_8,RESET)
#define KEY_CLO1_OUT_LOW  GPIO_WriteBit(GPIOE,GPIO_Pin_9,RESET)
#define KEY_CLO2_OUT_LOW  GPIO_WriteBit(GPIOE,GPIO_Pin_10,RESET)
#define KEY_CLO3_OUT_LOW  GPIO_WriteBit(GPIOE,GPIO_Pin_11,RESET)
 
#define KEY_CLO0_OUT_HIGH  GPIO_WriteBit(GPIOE,GPIO_Pin_8,SET)
#define KEY_CLO1_OUT_HIGH  GPIO_WriteBit(GPIOE,GPIO_Pin_9,SET)
#define KEY_CLO2_OUT_HIGH  GPIO_WriteBit(GPIOE,GPIO_Pin_10,SET)
#define KEY_CLO3_OUT_HIGH  GPIO_WriteBit(GPIOE,GPIO_Pin_11,SET)

int Key_Row_Scan(void);
int Key_Scan(void);

int Key_Col_Scan(void);

#endif

c文件

#include "keyn.h"
#include "sys.h"
#include "delay.h"
#include "stdio.h"
uint8_t Key_row[1]={0x00};   //定义一个数组,存放行扫描状态
uint8_t Key_col[1]={0x00};   //定义一个数组,存放列扫描状态
/***
 *函数名:KEY_ROW_SCAN
 *功  能:按键行扫描
 *返回值:1~4,对应1~4行按键位置
 */
//如果为1,代表没有按键被按下,如果为0,代表有按键被按下

int Key_Row_Scan(void)
{
    //读出行扫描状态
    Key_row[0] = GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_0)<<3;
    Key_row[0] = Key_row[0] | (GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_1)<<2);
    Key_row[0] = Key_row[0] | (GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_2)<<1);
    Key_row[0] = Key_row[0] | (GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_3));

    if(Key_row[0] != 0x00)         //不是0000,代表肯定有一个0行
    {
      delay_ms(10);                   //消抖
      if(Key_row[0] != 0x00)
        {   
              
                switch(Key_row[0])
                {
                    case 0x08:         //1000 判断为该列第1行的按键按下
                        return 0;
                    case 0x04:         //0100 判断为该列第2行的按键按下
                        return 4;
                    case 0x02:         //0010 判断为该列第3行的按键按下
                        return 8;
                    case 0x01:         //0001 判断为该列第4行的按键按下
                        return 12;
                    default :
                        return 0;
                }
        }
        else return 0;
    }
    else return 0;
}

int Key_Col_Scan(void)
{
        int row_value = Key_Row_Scan();
    //读出列扫描状态
    Key_col[0] = GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_4)<<3;
       
    Key_col[0] = Key_col[0] | (GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_5)<<2);
       
    Key_col[0] = Key_col[0] | (GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_6)<<1);
       
    Key_col[0] = Key_col[0] | (GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_7));

    if(Key_col[0] != 0x00)         //不是0000,代表肯定有一个0行
    {
      delay_ms(10);                   //消抖
      if(Key_col[0] != 0x00)
          
        {   
               
                switch(Key_col[0])
                {
                    case 0x08:         //1000 判断为该列第1列的按键按下
                        return row_value+1;
                    case 0x04:         //0100 判断为该列第2列的按键按下
                        return row_value+2;
                    case 0x02:         //0010 判断为该列第3列的按键按下
                        return row_value+3;
                    case 0x01:         //0001 判断为该列第4列的按键按下
                        return row_value+4;
                    default :
                        return 0;
                }
        }
        else return 0;
    }
    else return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值