当前位置:文档之家› STM32_4x4矩阵键盘

STM32_4x4矩阵键盘

*参数:
*输入:无
*返回:有键按下返回该键值
*无键按下时则返回0
--------------------------------------------------------------------------------------*/
u8Read_KeyValue(void){Βιβλιοθήκη u8KeyValue=0;
/*--------------------------------------------------------------------------------------*矩阵键盘驱动
*文件:keyboard.c
*编写人:LiuHui
*描述:扫描4x4矩阵键盘输入,并返回键值
*适用范围:驱动采用ST3.5库编写,适用于STM32F10x系列单片机
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);
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);
GPIO_ResetBits(GPIOA, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
}
/*------------------------------矩阵键盘扫描--------------------------------------------*功能:扫描矩阵键盘,并返回键值
switch(GPIO_ReadInputData(GPIOA)&0xff)
{
case 0x14:KeyValue= 3; break;
case 0x24:KeyValue= 7; break;
case 0x44:KeyValue= 11;break;
case 0x84:KeyValue= 15;break;
GPIO_ResetBits(GPIOA, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 |
GPIO_Pin_7);
while((GPIO_ReadInputData(GPIOA)&0xff)!=0x0f);
returnKeyValue;
}
}
return 0;
}
/*--------------------------------THE END--------------------------------------------*/
#include "dealy.h"
/*--------------------------------矩阵键盘初始化----------------------------------------*功能:初始化stm32单片机GPIO //PA0-PA7
*参数传递:
*输入:无
*返回值:无
--------------------------------------------------------------------------------------*/
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);
}
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= 2; break;
/*--------------------------------------------------------------------------------------*矩阵键盘驱动
*文件:keyboard.h
*编写人:LiuHui
*描述:扫描4x4矩阵键盘输入,并返回键值
*适用范围:驱动为ST3.5库编写,适用于STM32F10x系列单片机
}
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;
voidKeyBoard_Init(void);
u8Read_KeyValue(void);
#endif
/*----------------------------------THE END------------------------------------------*
voidKeyBoard_Init(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_10MHz;
*所用引脚:PA0-PA7
*编写时间:2013年11月22日
*版本:1.0
--------------------------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "keyboard.h"
switch(GPIO_ReadInputData(GPIOA)&0xff)
{
case 0x11:KeyValue= 1; break;
case 0x21:KeyValue= 5; break;
case 0x41:KeyValue= 9; break;
case 0x81:KeyValue= 13;break;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
*所用引脚:PA0-PA7
*编写时间:2013年11月22日
*版本:1.0
--------------------------------------------------------------------------------------*/
#ifndef__KEYBOARD_H
#define __KEYBOARD_H
case 0x28:KeyValue= 8; break;
case 0x48:KeyValue= 12;break;
case 0x88:KeyValue= 16;break;
}
GPIO_SetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
相关主题