STM32F407ZGT6矩阵键盘驱动程序(4*4)

定义头文件

#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;
 
}

由于时间较长,程序来源无法考证,如有侵权,请联系删除。
程序仅用于学习交流,不做其他任何用途。
如有错误或者更好的方法,欢迎指出交流。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值