2 *3键盘
//=========================================================================
// 工程名称: Demo2440_key23
// 组成文件: key23.c
// 功能描述: 2*3键盘,采用查询行列扫描,键值通过串口输出,行列独立扫描
// 硬件连接: GPF0~GPF1与键盘行相连,GPF2~GPF4与键盘列相连
//=========================================================================
#include "2440addr.h" //声明S3C2440的寄存器及其常用变量
#include "2440lib.h"
#include "uart.h"
#define KEY1 0x1a
#define KEY2 0x16
#define KEY3 0x0e
#define KEY4 0x19
#define KEY5 0x15
#define KEY6 0x0d
void delay(unsigned long dly)
{
for(; dly > 0; dly--);
}
//行扫IO状态
void RowScan_IOInit(void)
{
//将GPF低2位设为输入,GPF2~GPF4设为输出 0
rGPFCON &= ~((0x03<<0*2)|(0x03<<1*2)|(0x03<<2*2)|(0x03<<3*2)|(0x03<<4*2));
rGPFCON |= ((0x01<<2*2)|(0x01<<3*2)|(0x01<<4*2));
rGPFUP &=~((0x01<<0)|(0x01<<1));
rGPFDAT &=~((0x01<<2)|(0x01<<3)|(0x01<<4));
}
//列扫IO状态
void ColScan_IOInit(void)
{
//有键按下将GPF低2位设为输出 0,GPF2~GPF4设为输入
rGPFCON &= ~((0x03<<0*2)|(0x03<<1*2)|(0x03<<2*2)|(0x03<<3*2)|(0x03<<4*2));
rGPFCON |= ((0x01<<0*2)|(0x01<<1*2));
rGPFUP &=~((0x01<<2)|(0x01<<3)|(0x01<<4));
rGPFDAT &=~((0x01<<0)|(0x01<<1));
}
//解析键值
int key23_TranslateKey(int key_v)
{
int key = 0;
switch(key_v)
{
case KEY1:
key = 1;
break;
case KEY2:
key = 2;
break;
case KEY3:
key = 3;
break;
case KEY4:
key = 4;
break;
case KEY5:
key = 5;
break;
case KEY6:
key = 6;
break;
default:
key = 0;
break;
}
return key;
}
//行列扫描
int key23_Scan(void)
{
static unsigned char key;
unsigned char row;
unsigned char col;
RowScan_IOInit();
delay(1000);
row = rGPFDAT&0x03;//行扫 先读取行值
if(row != 0x03 )//有无键按下
{
ColScan_IOInit();
delay(1000);
col = rGPFDAT&0x1c; //列扫 先读取列值
if(col != 0x1c)
{
key = row | col; //确实有键按下,得到扫描键值
return key;
}
else
return 0;
}
else
return 0;
}
//查询按键
int key23_GetKey(void)
{
int key = key23_Scan();
if(key != 0)
{
delay(200*1000);// 延时去抖动
if(key == key23_Scan())
{
return key23_TranslateKey(key); // 如果经过长时间延时之后按键仍然存在,认为按键有效
}
}
return 0;
}
int Main(void)
{
unsigned int uiKey;
ChangeMPllValue(0x5c,1,1);
ChangeClockDivider(14,12);
Uart_Select(0);
myUart_Init(50*1000000,115200);
Uart_Printf("\nkey23 test\n");
Uart_Printf("2*3键盘测试程序\n");
while(1)
{
uiKey = key23_GetKey();
if (uiKey !=0)
Uart_Printf("the key is %d\n\n",uiKey);
}
}