// File Name : keyboard.h
// Author : Coolbor Xie
// Created : 2009-11-13
// Modified :
// Revision : V0.0
//***************************************************************
#ifndef _keyboard_h_
#define _keyboard_h_
#include <reg52.h>
#include "DataType.h"
//***************************************************************
//端口定义
//PIN DESCRIPTION
//P17 P16 P15 P14
// | | | |
// \---\---\---\---P10 1 2 3 F1
// \---\---\---\---P11 4 5 6 F2
// \---\---\---\---P12 7 8 9 F3
// \---\---\---\---P13 * 0 # F4
//***************************************************************
//global functions declaration
extern uchar KeyBoardScan(void);
#endif
//===============================END OF FILE==================================//
//***************************************************************
// File Name : keyboard.c
// Author : Coolbor Xie
// Created : 2009-11-13
// Modified :
// Revision : V0.0
//***************************************************************
#ifndef _keyboard_c_
#define _keyboard_c_
#include <reg52.h>
#include <intrins.h>
#include "keyboard.h"
//***************************************************************
//端口定义
//PIN DESCRIPTION
//P17 P16 P15 P14
// | | | |
// \---\---\---\---P10 1 2 3 F1
// \---\---\---\---P11 4 5 6 F2
// \---\---\---\---P12 7 8 9 F3
// \---\---\---\---P13 * 0 # F4
//***************************************************************
#define KeyPort P1
#define key_state_0 0 //定义按键状态(基于状态机)
#define key_state_1 1
#define key_state_2 2
#define key_state_3 3
#define NO_key 0
//***************************************************************
// Function : KeyBoardScan
// Input : none
// Output : Key Number
// 0~9、*、#:ASCII code
// F1、F2、F3、F4: (A、B、C、D) ASCII code
// Description : 4*4 matrix keyboard scanning Function
//
//***************************************************************
uchar KeyBoardScan(void)
{
uchar key_temp;
uchar key_num = NO_key;
static uchar key_status = key_state_0; //按键状态
//线反转法扫描键盘
KeyPort = 0x0f;
key_temp = KeyPort;
KeyPort = 0xf0;
key_temp |= KeyPort;
KeyPort = 0xff;
switch(key_status)
{
case key_state_0:
if(key_temp != 0xff)
key_status = key_state_1;
break;
case key_state_1:
if(key_temp == 0xff)
key_status = key_state_0;
else
{
key_status = key_state_2;
switch(key_temp)
{
case 0x7e: //0111 1110
key_num = '1';
break;
case 0xbe: //1011 1110
key_num = '2';
break;
case 0xde: //1101 1110
key_num = '3';
break;
case 0x7d: //0111 1101
key_num = '4';
break;
case 0xbd: //1011 1101
key_num = '5';
break;
case 0xdd: //1101 1101
key_num = '6';
break;
case 0x7b: //0111 1011
key_num = '7';
break;
case 0xbb: //1011 1011
key_num = '8';
break;
case 0xdb: //1101 1011
key_num = '9';
break;
case 0xb7: //1011 0111
key_num = '0';
break;
case 0x77: //0111 0111
key_num = '*'; //*
break;
case 0xd7: //1101 0111
key_num = '#'; //#
break;
case 0xee: //1110 1110
key_num = 'A'; //F1
break;
case 0xed: //1110 1101
key_num = 'B'; //F2
break;
case 0xeb: //1110 1011
key_num = 'C'; //F3
break;
case 0xe7: //1110 0111
key_num = 'D'; //F4
break;
}
}
break;
case key_state_2:
if(key_temp == 0xff)
key_status = key_state_3;
break;
case key_state_3:
if(key_temp == 0xff)
key_status = key_state_0;
else
key_status = key_state_2;
break;
}
return key_num;
}
#endif
//===============================END OF FILE==================================//
文章评论(0条评论)
登录后参与讨论