/******************************************/
/* 广州天河双龙电子公司 */
/* http://www.sl.com.cn */
/* PS2键盘驱动程序 */
/* 作者:ntzwq@wx88.net */
/* 2002年5月11日 */
/* 目标MCU:MEGA8 晶振:外部(EXT) 8MHZ */
/******************************************/
/******************************************/
/* 经过改动移植到51芯片上 */
/* 改动者:jifeng */
/******************************************/
#include <REG58.h>
#include "config.h"
//#include <REG61x2.H>
#include "KeyBoard.h"
#include "T6963.h"
//
#include "scancode.h"
#define BUFF_SIZE 64
uint8 keycode;
uint8 keyflag;
unsigned char bitcount;
uint8 xdata kb_buffer[BUFF_SIZE];
unsigned char input=0;
unsigned char output=0;
#define KEY_BUF_SIZE 32
uint8 KeyBuf[KEY_BUF_SIZE];
static uint8 KeyInCnt = 0;
static uint8 KeyOutCnt = 0;
unsigned char Key_OK=0;
unsigned char Temp_Key;
//使用软件模拟FIFO
void KeyPushBuf(uint8 keyv)
{
keyflag = 1;
KeyBuf[KeyInCnt] = keyv;
if( KeyInCnt<(KEY_BUF_SIZE-1) ){
KeyInCnt++;
}
else{
KeyInCnt = 0;
}
}
//从缓冲区读取键值
uint8 KeyPopBuf(void)
{
uint8 keyvalue;
if( KeyInCnt==KeyOutCnt ){
keyvalue = 0;
keyflag = 0;
}
else{
keyvalue = KeyBuf[KeyOutCnt];
if( KeyOutCnt<(KEY_BUF_SIZE-1) ){
KeyOutCnt++;
}
else{
KeyOutCnt = 0;
}
}
return keyvalue;
}
//初始化
void init_kb(void)
{
IT1 = 1;//下降延触发
EX1 = 1;
PX1 = 1;
//IPH = 1;//设置为最高级优先级
EA=1;
bitcount = 11;
}
//键值分析
void KeyDecode(uint8 ps2)
{
static uint8 shift; //这些要放在data里面,不然要出错
static uint8 shiftup;
static uint8 alt;
static uint8 altup;
static uint8 up;
uint8 i;
//shift = 0;
//alt = 0;
if(ps2==0xf0){
up = 1; //断码开始
return;
}
if(up==1){
up=0;
if((ps2==0x12)||(ps2==0x59)){
shift=0; //释放左右shift键标志
}
if(ps2==0x11){
alt=0; //释放左alt键标志
}
return;
}
switch(ps2){
case 0x11:
alt=1;
altup=1;
break;
case 0x12:
//按键PRINT出现的特殊情况
if((Temp_Key==0xe0)||(Temp_Key==0xf0)){
KeyPushBuf(27);
}
else{
shift=1;
shiftup=1;
}
break;
case 0x59:
shift=1;
shiftup=1;
break;
default:
if((shift==0)&&(alt==0)){
// Lcd_DisUint16(14,2,7777,4);
for(i = 0;i<90; i++){
if (unshifted[i][0] == ps2){
if((Temp_Key==0xe0)&&(ps2==0x71)){
KeyPushBuf(15);
Lcd_DisUint16(8,0,8888,4); //Delete
}
else{
KeyPushBuf(unshifted[i][1]);
Lcd_DisUint16(14,2,unshifted[i][1],4);
Lcd_DisUint16(18,2,KeyInCnt,4);
Lcd_DisUint16(18,2,KeyOutCnt,4);
//Lcd_Printf(1,2,"F1 KEY",0);
Key_OK=1;
}
i=90;
}
}
/*
if (unshifted[i][0] == ps2){
if(((Temp_Key==0xe0)||(Temp_Key==0xf0))&&(ps2==0x7c)){
//PRINT键
}
else{
if(((Temp_Key==0xe0)||(Temp_Key==0xf0))&&(ps2==0x71)){
KeyPushBuf(15); //Delete
}
else{
KeyPushBuf(unshifted[i][1]);
Key_OK=1;
}
i=90;
}
}
*/
// }
}
//使用SHIFT+字母的方式,输入大写字母
else if((shift==1)&&(alt==0)){
Lcd_DisUint16(14,4,8888,4);
for(i = 0;shifted[i][0]!=ps2 && shifted[i][0]; i++);
if (shifted[i][0] == ps2){
KeyPushBuf(shifted[i][1]);
Key_OK=1;
}
}
//使用ALT+字母的方式,用于焦点的跳转
else if((shift==0)&&(alt==1)){
Lcd_DisUint16(14,6,9999,4);
for(i = 0;alted[i][0]!=ps2 && alted[i][0]; i++);
if (alted[i][0] == ps2){
KeyPushBuf(alted[i][1]);
Key_OK=1;
}
}
break;
}
}
//键盘信息接收,使用中断 ,连接时钟线
void KeyIsr() interrupt 2
{
static unsigned char dat;
//Lcd_Printf(1,2,"F1 KEY",0);
switch (bitcount)
{
case 11:{
//if ((PIN_KB&(1<<DATAPIN))!=0)
if (PINDATA!=0)
return;
else
bitcount--;
break;
}
case 2:{
bitcount--;
break;
}
case 1:{
bitcount--;
//keyflag = 1;
//if ((PIN_KB&(1<<DATAPIN))!=0)
if (PINDATA==0)
{
bitcount=11;
return;
}
else
{
bitcount=11;
KeyDecode(dat);
Temp_Key=dat;
Lcd_DisUint16(1,0,dat,4);
// keycode = KeyPopBuf();
// Lcd_DisUint16(8,0,keycode,4);
}
break;
}
default:{
dat = (dat >> 1);
//if ((PIN_KB&(1<<DATAPIN))!=0)
if (PINDATA!=0)
dat|=0x80;
bitcount--;
}
}
}
/*
void put_esc(void) //模仿手动按下ESC键
{
//put_kbbuff(ESC);
KeyPushBuf(ESC);
}
void put_enter(void) //模仿手动按下ENTER键
{
//put_kbbuff(ENTER);
KeyPushBuf(ENTER);
}
*/