定义头文件
#ifndef __JIANPAN_H
#define __JIANPAN_H
#include "sys.h"
#define Read_Lie1 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0)
#define Read_Lie2 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1)
#define Read_Lie3 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)
#define Read_Lie4 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3)
void jianpan_Init(void);
u8 jianpan_Scan(void);
#endif
对相关引脚进行初始化
void jianpan_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|
RCC_AHB1Periph_GPIOF,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|
GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);//输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|
GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);//输出
}
对键盘进行扫描
u8 jianpan_Scan(void)
{
static u8 key_flag1=1,key_flag2=1,key_flag3=1,key_flag4=1;
u8 KeyVal;
GPIO_Write(GPIOF,(( GPIOF->ODR & 0xFFC3 )|0x0004)); //0100 1111 1111 1100 0011 检测第一行
if(key_flag1&&(Read_Lie1==1||Read_Lie2==1||Read_Lie3==1||Read_Lie4==1))
{
key_flag1=0;
if (Read_Lie1==1) {KeyVal= 1; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie2==1) {KeyVal= 2; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie3==1) {KeyVal= 3; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie4==1) {KeyVal= 4; printf("key:%d\r\n",KeyVal);return KeyVal;}
}
else if(Read_Lie1==0&&Read_Lie2==0&&Read_Lie3==0&&Read_Lie4==0)key_flag1=1;
else return 0;
GPIO_Write(GPIOF,(( GPIOF->ODR & 0xFFC3 )|0x0008));//1000检测第二行
if(key_flag2&&(Read_Lie1==1||Read_Lie2==1||Read_Lie3==1||Read_Lie4==1))
{
key_flag2=0;
if (Read_Lie1==1){ KeyVal= 5; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie2==1) {KeyVal= 6; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie3==1) {KeyVal= 7; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie4==1) {KeyVal= 8; printf("key:%d\r\n",KeyVal);return KeyVal;}
}
else if(Read_Lie1==0&&Read_Lie2==0&&Read_Lie3==0&&Read_Lie4==0)key_flag2=1;
else return 0;
GPIO_Write(GPIOF,(( GPIOF->ODR & 0xFFC3 )|0x0010));// 0001 0000检测第三行
if(key_flag3&&(Read_Lie1==1||Read_Lie2==1||Read_Lie3==1||Read_Lie4==1))
{
key_flag3=0;
if (Read_Lie1==1){ KeyVal= 9; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie2==1) {KeyVal= 10; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie3==1) {KeyVal= 11; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie4==1) {KeyVal= 12; printf("key:%d\r\n",KeyVal);return KeyVal;}
}
else if(Read_Lie1==0&&Read_Lie2==0&&Read_Lie3==0&&Read_Lie4==0)key_flag3=1;
else return 0;
GPIO_Write(GPIOF,(( GPIOF->ODR & 0xFFC3 )|0x0020));
//0010 0000检测第四行
if(key_flag4&&(Read_Lie1==1||Read_Lie2==1||Read_Lie3==1||Read_Lie4==1))
{
key_flag4=0;
if (Read_Lie1==1){ KeyVal= 13; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie2==1) {KeyVal= 14; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie3==1) {KeyVal= 15; printf("key:%d\r\n",KeyVal);return KeyVal;}
else if (Read_Lie4==1) {KeyVal= 16; printf("key:%d\r\n",KeyVal);return KeyVal;}
}
else if(Read_Lie1==0 &&Read_Lie2==0 &&Read_Lie3==0 &&Read_Lie4==0 )key_flag4=1;
else return 0;
return KeyVal;
}
由于时间较长,程序来源无法考证,如有侵权,请联系删除。
程序仅用于学习交流,不做其他任何用途。
如有错误或者更好的方法,欢迎指出交流。