//Key.c文件
/* Includes ---------------------------------------------------------------*/
#include "stm32f10x.h"
#include "Key.h"
#include <stdio.h>
#include"LCD_12864.h"
/****************************************************************************
* 名 称:void Key_Init(void)
* 功 能:按键初始化
* 入口参数:无
* 出口参数:无
* 说 明:
* 调用方法:Key_Init();
****************************************************************************/
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 打开端口时钟--------------------------------------------------------*/
RCC_APB2PeriphClockCmd(RCC_GPIO_B,ENABLE);
/* 配置端口B y输入口-------------------------------------------------*/
GPIO_InitStructure.GPIO_Pin=Keyy;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* 配置端口B x输出口------------------------------------------------*/
GPIO_InitStructure.GPIO_Pin=Keyx;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/****************************************************************************
* 名 称:u16 Key_Read(void)
* 功 能:检测按键,读出具体按键
* 入口参数:无
* 出口参数:Num
* 说 明:
* 调用方法:Key_Read();
****************************************************************************/
u16 Key_Read(void)
{
u16 Num=20,Temp;
Temp=GPIO_ReadOutputData(GPIOB); // 输出口全部置1
GPIO_Write(GPIOB,Temp|0X000F);
//第一行 //按键检测
GPIO_ResetBits(GPIOB, Keyx0);
if(!GPIO_ReadInputDataBit(GPIOB, Keyy0))
Num=0;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy1))
Num=1;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy2))
Num=2;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy3))
Num=3;
GPIO_SetBits(GPIOB, Keyx0);
//第二行
GPIO_ResetBits(GPIOB, Keyx1);
if(!GPIO_ReadInputDataBit(GPIOB, Keyy0))
Num=4;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy1))
Num=5;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy2))
Num=6;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy3))
Num=7;
GPIO_SetBits(GPIOB, Keyx1);
//第三行
GPIO_ResetBits(GPIOB, Keyx2);
if(!GPIO_ReadInputDataBit(GPIOB, Keyy0))
Num=8;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy1))
Num=9;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy2))
Num=10;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy3))
Num=11;
GPIO_SetBits(GPIOB, Keyx2);
//第四行
GPIO_ResetBits(GPIOB, Keyx3);
if(!GPIO_ReadInputDataBit(GPIOB, Keyy0))
Num=12;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy1))
Num=13;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy2))
Num=14;
else if(!GPIO_ReadInputDataBit(GPIOB, Keyy3))
Num=15;
GPIO_SetBits(GPIOB, Keyx3);
return Num;
}
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
Key.h文件
/****************************************************************************
* 4*4矩阵按键对应的端口引脚宏定义
****************************************************************************/
#define Keyx0 GPIO_Pin_0
#define Keyx1 GPIO_Pin_1
#define Keyx2 GPIO_Pin_2
#define Keyx3 GPIO_Pin_3
#define Keyy0 GPIO_Pin_4
#define Keyy1 GPIO_Pin_5
#define Keyy2 GPIO_Pin_6
#define Keyy3 GPIO_Pin_7
/****************************************************************************
* 所有矩阵按键在此汇总
****************************************************************************/
#define Keyx Keyx0|Keyx1|Keyx2|Keyx3 //输出口
#define Keyy Keyy0|Keyy1|Keyy2|Keyy3 //输入口
/****************************************************************************
*使用到的端口的时钟定义
****************************************************************************/
#define RCC_GPIO_B RCC_APB2Periph_GPIOB
/****************************************************************************
* Key.c用到的函数在此声明
****************************************************************************/
void Key_Init(void); //按键使用到的端口初始化
//void Delayms(uint32_t ms); //延时函数
u16 Key_Read(void); //检测按键,读出具体按键
///////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
main函数里面 我用一下语句已经关掉了jtag,但是还是检测不到第一列,这是什么原因啊
RCC->APB2ENR |= 0x00000001; //使能复用口时钟
AFIO->MAPR = (0x00FFFFFF & AFIO->MAPR)|0x04000000; //这两句话是关闭jtag
/////////////////////////////////
|