高级会员

- 积分
- 934
- 金钱
- 934
- 注册时间
- 2017-4-8
- 在线时间
- 111 小时
|
发表于 2018-1-19 16:59:22
|
显示全部楼层
[mw_shl_code=c,true]/*--------------------------------------------------------------------------------------* 矩阵键盘驱动
* 文件: keyboard.c
* 编写人:
* 描述: 扫描4x4 矩阵键盘输入,并返回键值
* 适用范围: 驱动采用 ST3.5 库编写,适用于 STM32F10x 系列单片机
* 所用引脚: PA0-PA7
* 编写时间: 2018 年1 月11 日
--------------------------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "key.h"
#include "delay.h"
/*-------------------------------- 矩阵键盘初始化 ----------------------------------------* 功能: 初始化
stm32 单片机GPIO //PA0-PA7
* 参数传递:
* 输入: 无
* 返回值:无
--------------------------------------------------------------------------------------*/
//uint8_t Key[128]={0};
//
//uint8_t get = 0;
//static uint8_t i = 0; //输入密码计数
//static uint8_t m = 0; //设定密码计数
//uint8_t Len;
//uint8_t Password[128]={0};
//uint8_t set_ok = 0;
//extern setted;
void KeyBoard_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //开启AFIO始终
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); //禁止JTAG功能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = Key_PIn_1;
GPIO_Init(Key_PIN1_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Key_PIn_2;
GPIO_Init(Key_PIN2_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Key_PIn_3;
GPIO_Init(Key_PIN3_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Key_PIn_4;
GPIO_Init(Key_PIN4_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = Key_POut_1;
GPIO_Init(Key_POut1_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Key_POut_2;
GPIO_Init(Key_POut2_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Key_POut_3;
GPIO_Init(Key_POut3_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Key_POut_4;
GPIO_Init(Key_POut4_GPIO, &GPIO_InitStructure);
}
/*------------------------------ 矩阵键盘扫描 --------------------------------------------* 功能:扫描矩阵
键盘,并返回键值
* 参数:
* 输入: 无
* 返回: 有键按下返回该键值
*
--------------------------------------------------------------------------------------*/
#include "USART2.h"
#include "stdlib.h"
//static uint8_t Lock = 0;
// + - * # Esc Ent
// 10 11 12 13 14 15
uint8_t Read_KeyValue(void)
{
static uint8_t Lock = 0;
if(((u8) Get_All_PIN & 0x0f) == 0x0f) //没有按键按下
{
Lock = 0;
}
if (((u8) Get_All_PIN & 0x0f) != 0x0f) //读取KeyBoard_POART端口输入,不为0x0f,有按键按下
{
Put_All_PIN_Height; //P1,2,3,4 置高
POut_1(0); //P1置0
switch ((u8) Get_All_PIN & 0x0f)
{
case 0b1110:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:#\r\n");
Lock = 1;
return 13;// #
}
break;
case 0b1101:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:-\r\n");
Lock = 1;
return 11;//-
}
break;
case 0b1011:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:0\r\n");
Lock = 1;
return 0;//0
}
break;
case 0b0111:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:+\r\n");
Lock = 1;
return 10;//+
}
break;
}
Put_All_PIN_Height;
POut_2(0);
switch ((u8) Get_All_PIN & 0x0f)
{
case 0b1110:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:ent\r\n");
Lock = 1;
return 15;//ent
}
break;
case 0b1101:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:9\r\n");
Lock = 1;
return 9;//9
}
break;
case 0b1011:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:8\r\n");
Lock = 1;
return 8;//8
}
break;
case 0b0111:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:*\r\n");
Lock = 1;
return 12;//*
}
break;
}
Put_All_PIN_Height;
POut_3(0);
switch ((u8) Get_All_PIN & 0x0f)
{
case 0b1110:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:6\r\n");
Lock = 1;
return 6;//6
}
break;
case 0b1101:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:5\r\n");
Lock = 1;
return 5;//5
}
break;
case 0b1011:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:7\r\n");
Lock = 1;
return 7;//7
}
break;
case 0b0111:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:4\r\n");
Lock = 1;
return 4;//4
}
break;
}
Put_All_PIN_Height;
POut_4(0);
switch ((u8) Get_All_PIN & 0x0f)
{
case 0b1110:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:esc\r\n");
Lock = 1;
return 14;//esc
}
break;
case 0b1101:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:3\r\n");
Lock = 1;
return 3;//3
}
break;
case 0b1011:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:2\r\n");
Lock = 1;
return 2;//2
}
break;
case 0b0111:
if(Lock == 0)
{
Put_All_PIN_Low;
Myprintf(USART1, "Get_PIN:1\r\n");
Lock = 1;
return 1;//1
}
break;
}
Put_All_PIN_Low;
}
return 0xff;
}
/*--------------------------------THE END--------------------------------------------*/
[/mw_shl_code] |
|