STM32F407实现矩阵键盘程序
来源:互联网 发布:有招聘在家淘宝客服吗 编辑:程序博客网 时间:2024/05/29 15:10
由于最近在做一个有关按键的项目,有用到矩阵键盘,因为网上有关矩阵键盘的程序很少,所以我就自己写一个发上去供大家参考,该程序所用到的GPIO口是PD0--PD7,至于矩阵键盘的原理就不一一描述了,直接上程序,具体程序如下:
//GPIO初始化函数
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructuress;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOA, ENABLE); //打开时钟
GPIO_InitStructuress.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; //初始化PD0-PD3
GPIO_InitStructuress.GPIO_Mode = GPIO_Mode_OUT ;
GPIO_InitStructuress.GPIO_OType = GPIO_OType_PP; //设置为推挽输出
GPIO_InitStructuress.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructuress.GPIO_PuPd =GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOD, &GPIO_InitStructuress);
GPIO_InitStructuress.GPIO_Pin =GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; //初始化PD4-PD7
GPIO_InitStructuress.GPIO_Mode =GPIO_Mode_OUT ;
GPIO_InitStructuress.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructuress.GPIO_PuPd = GPIO_PuPd_DOWN ;//Ï下拉
GPIO_Init(GPIOD, &GPIO_InitStructuress);
GPIO_SetBits( GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
GPIO_ResetBits( GPIOD,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
}
//按键函数
extern void Keyspress()
{
int KeyValue;
int y=0;
int f=5;
while(f)
{
if((GPIO_ReadInputData(GPIOD)&0xff)!=0x0f)
{
Delay10ms(20);
if((GPIO_ReadInputData(GPIOD)&0xff)!=0x0f)
{
GPIO_SetBits(GPIOD,GPIO_Pin_0);
GPIO_ResetBits(GPIOD,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
switch(GPIO_ReadInputData(GPIOD)&0xff)
{
case 0x11:KeyValue=12;break;
case 0x21:KeyValue=13;break;
case 0x41:KeyValue=14;break;
case 0x81:KeyValue=15;break;
}
GPIO_SetBits(GPIOD,GPIO_Pin_1);
GPIO_ResetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3);
switch(GPIO_ReadInputData(GPIOD)&0xff)
{
case 0x12:KeyValue=8;break;
case 0x22:KeyValue=9;break;
case 0x42:KeyValue=10;break;
case 0x82:KeyValue=11;break;
}
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_ResetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_3);
switch(GPIO_ReadInputData(GPIOD)&0xff)
{
case 0x14:KeyValue=4;break;
case 0x24:KeyValue=5;break;
case 0x44:KeyValue=6;break;
case 0x84:KeyValue=7;break;
}
GPIO_SetBits(GPIOD,GPIO_Pin_3);
GPIO_ResetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2);
switch(GPIO_ReadInputData(GPIOD)&0xff)
{
case 0x18:KeyValue=0;break;
case 0x28:KeyValue=1;break;
case 0x48:KeyValue=2;break;
case 0x88:KeyValue=3;break;
}
GPIO_SetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2 |GPIO_Pin_3);
GPIO_ResetBits(GPIOD, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7);
while((GPIO_ReadInputData(GPIOD)&0xff)!=0x0f);
switch(KeyValue)
{
case 0:f=3;break;
case 1:f=3;break;
case 2:f=3;break;
case 4: f=3;break;
case 5: f=3;break;
case 6: f=3;break;
case 8:f=3;break;
case 9: f=3;break;
case 10: f=3; break;
case 12: f=3;break;
case 13: f=3;break;//由于项目只用到12个按键,所以有4个按键的扫描程序是没有的
}
}
}
}
}
//主函数:
int main
{
GPIO_Configuration();
Keyspress();
}
- STM32F407实现矩阵键盘程序
- 基于STM32L151//STM32F407的矩阵键盘程序(不规则接口):
- 矩阵键盘扫描程序
- 矩阵键盘扫描程序
- STM32F4实现矩阵键盘
- C51矩阵键盘扫描程序
- 4X4矩阵键盘程序
- 矩阵键盘扫描程序实例
- VHDL实现矩阵键盘检测
- 单片机矩阵键盘的实现
- 【stm32f407】SysTick实现延时
- 4*4矩阵键盘C51程序
- 数码管显示矩阵键盘扫描程序
- 51单片机(AT89C52)矩阵键盘检测程序
- AVR单片机(ATMEGA16)矩阵键盘扫描程序
- 51单片机矩阵键盘检测程序
- 4X4矩阵键盘扫描程序
- 4X4矩阵键盘扫描程序
- BFS-胜利大逃亡
- STM32的TIM3产生的PWM
- Http与https区别(转发)
- SpringMVC学习(八)之view-controller标签
- ThreadLocal源码探索
- STM32F407实现矩阵键盘程序
- CS107-Assignment 1
- 转 C++ 标准库 vector list map使用方法
- hive 配置参数说明
- LevelListDrawable的简单使用
- 理解Android安全机制
- Maven常用仓库地址以及手动添加jar包到仓库
- 模板与泛型编程
- 如何优雅的为你的Recyclerview添加分割线