stm32-矩阵键盘程序44

上传人:小** 文档编号:90069395 上传时间:2019-06-07 格式:DOC 页数:7 大小:46.50KB
返回 下载 相关 举报
stm32-矩阵键盘程序44_第1页
第1页 / 共7页
stm32-矩阵键盘程序44_第2页
第2页 / 共7页
stm32-矩阵键盘程序44_第3页
第3页 / 共7页
stm32-矩阵键盘程序44_第4页
第4页 / 共7页
stm32-矩阵键盘程序44_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《stm32-矩阵键盘程序44》由会员分享,可在线阅读,更多相关《stm32-矩阵键盘程序44(7页珍藏版)》请在金锄头文库上搜索。

1、/*-* 矩阵键盘驱动* 文 件: keyboard.c* 编写人: LiuHui* 描述: 扫描4x4 矩阵键盘输入,并返回键值* 适用范围: 驱动采用ST3.5 库编写,适用于STM32F10x 系列单片机* 所用引脚: PA0-PA7* 编写时间: 2014 年5 月20 日-*/#include stm32f10x.h#include keyboard.h#include dealy.h/*-矩阵键盘初始化-* 功能: 初始化stm32 单片机GPIO /PA0-PA7* 参数传递:* 输入: 无* 返回值:无-*/void KeyBoard_Init(void)GPIO_InitTy

2、peDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOA, &GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO

3、_Pin_6 | GPIO_Pin_7;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;GPIO_Init(GPIOA, &GPIO_InitStructure);GPIO_SetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);GPIO_ResetBits(GPIOA, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);/*-矩

4、阵键盘扫描-* 功能: 扫描矩阵键盘,并返回键值* 参数:* 输入: 无* 返回: 有键按下返回该键值* 无键按下时则返回0-*/u8 Read_KeyValue(void)u8 KeyValue=0;if(GPIO_ReadInputData(GPIOA)&0xff)!=0x0f)Delay_ms(10);if(GPIO_ReadInputData(GPIOA)&0xff)!=0x0f)GPIO_SetBits(GPIOA, GPIO_Pin_0);GPIO_ResetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);switch(GPIO_

5、ReadInputData(GPIOA)&0xff)case 0x11: KeyValue = 1; break;case 0x21: KeyValue = 5; break;case 0x41: KeyValue = 9; break;case 0x81: KeyValue = 13;break;GPIO_SetBits(GPIOA, GPIO_Pin_1);GPIO_ResetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3);switch(GPIO_ReadInputData(GPIOA)&0xff)case 0x12: KeyValue

6、= 2; break;case 0x22: KeyValue = 6; break;case 0x42: KeyValue = 10;break;case 0x82: KeyValue = 14;break;GPIO_SetBits(GPIOA, GPIO_Pin_2);GPIO_ResetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3);switch(GPIO_ReadInputData(GPIOA)&0xff)case 0x14: KeyValue = 3; break;case 0x24: KeyValue = 7; break;case

7、 0x44: KeyValue = 11;break;case 0x84: KeyValue = 15;break;GPIO_SetBits(GPIOA, GPIO_Pin_3);GPIO_ResetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2);switch(GPIO_ReadInputData(GPIOA)&0xff)case 0x18: KeyValue = 4; break;case 0x28: KeyValue = 8; break;case 0x48: KeyValue = 12;break;case 0x88: KeyValue

8、 = 16;break;GPIO_SetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);GPIO_ResetBits(GPIOA, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7);while(GPIO_ReadInputData(GPIOA)&0xff)!=0x0f);return KeyValue;return 0;/*-THE END-*/*-* 矩阵键盘驱动* 文 件: keyboard.h* 编写人: LiuHui* 描述: 扫描4x4 矩阵键盘输入,并返回

9、键值* 适用范围: 驱动为ST3.5 库编写,适用于STM32F10x 系列单片机* 所用引脚: PA0-PA7* 编写时间: 2013 年11 月22 日* 版本: 1.0-*/#ifndef _KEYBOARD_H#define _KEYBOARD_Hvoid KeyBoard_Init(void);u8 Read_KeyValue(void);#endif/*-THE END-*#include stm32f10x.hvoid KeyBoard_Init(void)GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pi

10、n = GPIO_Pin_All;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOA, &GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;GPIO_Init(GPI

11、OB, &GPIO_InitStructure);GPIO_SetBits(GPIOB, GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6);GPIO_ResetBits(GPIOB, GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10);/PA,PBvoid Delay_ms(int time)int i=0;while(time-)i=12000;while(i-);u8 Read_KeyValue(void)u8 KeyValue=1;if(GPIO_ReadInputData(GPIOB

12、)&0xff)!=0x0f)Delay_ms(10);if(GPIO_ReadInputData(GPIOB)&0xff)!=0x0f)GPIO_SetBits(GPIOB, GPIO_Pin_3);GPIO_ResetBits(GPIOB, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6);switch(GPIO_ReadInputData(GPIOB)&0xff)case 0x11: KeyValue = 7; break;case 0x21: KeyValue = 4; break;case 0x41: KeyValue = 1; break;case 0x81: KeyValue = 0; break;GPIO_SetBits(GPIOB, GPIO_Pin_4);GPIO_ResetBits(GPIOB, GPIO_Pin_3 | GPIO_Pin_5 | GPIO_Pin_6);switch(GPIO_ReadInputData(GPIOB)&0xff)case 0x12: KeyValue = 8; break;case 0x22: Key

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号