赞
踩
买了个矩阵键盘,原理图如下:
左边的为行,右边的为列,扫描的原理大概如下:行为输出,列为输入。先让第一行为0,其他行为高,读取列的电平。
key.c如下:#include "key.h"
#include "delay.h"
//键值表
/*
1:0x01 2:0x02 3:0x03 A:0x04
4:0x05 5:0x06 6:0x07 B:0x08
7:0x09 8:0x0A 9:0x0B C:0x0C
*:0x0D 0:0x0E #:0x0F D:0x10
*/
uint8_t keyNum = 0x00; //键值
void keyInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
//key row1 output
GPIO_InitStructure.GPIO_Pin = KEY_ROW1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(KEY_ROW1_PORT,&GPIO_InitStructure);
//key row2 output
GPIO_InitStructure.GPIO_Pin = KEY_ROW2_PIN;
GPIO_Init(KEY_ROW2_PORT,&GPIO_InitStructure);
//key row3 output
GPIO_InitStructure.GPIO_Pin = KEY_ROW3_PIN;
GPIO_Init(KEY_ROW3_PORT,&GPIO_InitStructure);
//key row4 output
GPIO_InitStructure.GPIO_Pin = KEY_ROW4_PIN;
GPIO_Init(KEY_ROW4_PORT,&GPIO_InitStructure);
//key col1 input
GPIO_InitStructure.GPIO_Pin = KEY_COL1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(KEY_COL1_PORT,&GPIO_InitStructure);
//key col2 input
GPIO_InitStructure.GPIO_Pin = KEY_COL2_PIN;
GPIO_Init(KEY_COL2_PORT,&GPIO_InitStructure);
//key col3 input
GPIO_InitStructure.GPIO_Pin = KEY_COL3_PIN;
GPIO_Init(KEY_COL3_PORT,&GPIO_InitStructure);
//key col4 input
GPIO_InitStructure.GPIO_Pin = KEY_COL4_P
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。