📄 51单片机系统中运用电脑上的大键盘.c
字号:
/*=============================================================
Keyboard接线
PS/2--------51
1 DATA------P3.4
3 GND
4 VCC
5 CLK-------P3.3 接在51的外部中断,触发方式为低电平
AT89x51使用12M
Modified by Shen Peng!
Postgraduate of 2003, 7 Department
Harbin Engineering University
=============================================================*/
#include <at89x51.h>
#include "scancodes.h"
//#define Key_Data P3_4 //定义Keyboard引脚
//#define Key_CLK P3_3
sbit Key_Data=P3^4; //定义Keyboard引脚
sbit Key_CLK =P3^3;
sbit led1=P1^1; // use the led to test the system
sbit led2=P1^3;
//void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);
//void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);
//void Delay5Ms(void);
//void Delay400Ms(void);
void Decode(unsigned char ScanCode); //这是个键盘的译吗程序,并且显示出来,
void Init(void); //穿口的初始话‘
void sendtoPC(unsigned char); //发送程序;
static unsigned char IntNum = 0; //中断次数计数
static unsigned char KeyV; //键值
static unsigned char DisNum = 0; //显示用指针
static unsigned char Key_UP=0, Shift = 0; //Key_UP是键松开标识, Shift是Shift键按下标识
static unsigned char BF = 0; //标识是否有字符被收到
unsigned char command; //receive the command from PC
//static unsigned char BF = 1; //标识是否有字符被收到
void Init()
{
// the oc is 12MHZ
IE=0x98; //1001 1000 //
IP=0x10; //ps is the first class;
TMOD=0x21; //0010 0001
SCON=0x50; // 0101 0000
PCON=0x00; // 0000 0000 // set the SMOD 1
TL1=0xcc; // set the baud; 600 //串口600;
TH1=0xcc; // 204 should be set;
TR1=1; //1100 1100
//TL1=0xfd; // set the baud; 9600
// TH1=0xfd; // 253 should be set;
// TR1=1; //1110 0110
}
void main(void)
{
unsigned char TempCyc;
Init(); // init the serial port
sendtoPC(Shifted[34][1]); // test the system,and restet;
// Delay400Ms(); //启动等待,等LCM讲入工作状态
// Delay5Ms(); //延时片刻(可不要)
// for (TempCyc=0; TempCyc<10; TempCyc++)
// Delay400Ms(); //延时
//DisplayListChar(0, 1, Cls);
// IT1 = 0; //设外部中断1为低电平触发
IT1 = 1; //设外部中断1跳变触发;
EA = 1;
EX1 = 1; //开中断
do
{
led2=~led2; // test weather the mcu is death;
if (BF)
Decode(KeyV);
else
EA = 1; //开中断
/// EX1 = 1;
}
while(1);
}
/*
//5ms延时
void Delay5Ms(void)
{
unsigned int TempCyc = 5552;
while(TempCyc--);
}
//400ms延时
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
};
}
*/
void Keyboard_out(void) interrupt 2
{
// led2=~led2;
if ((IntNum > 0) && (IntNum < 9))
{
KeyV = KeyV >> 1; //因键盘数据是低>>高,结合上一句所以右移一位
if (Key_Data) KeyV = KeyV | 0x80; //当键盘数据线为1时为1到最高位
}
IntNum++;
while (!Key_CLK); //等待PS/2CLK拉高
if (IntNum > 10)
{
led1=~led1;
// led2=~led2;
IntNum = 0; //当中断11次后表示一帧数据收完,清变量准备下一次接收
BF = 1; //标识有字符输入完了
EA = 0; //关中断等显示完后再开中断 (注:如这里不用BF和关中断直接调Decode()则所Decode中所调用的所有函数要声明为再入函数)
// EX1 = 0;
}
}
void Decode(unsigned char ScanCode) //注意:如SHIFT+G为12H 34H F0H 34H F0H 12H,也就是说shift的通码+G的通码+shift的断码+G的断码
{
unsigned char TempCyc;
if (!Key_UP) //当键盘松开时
{
switch (ScanCode)
{
case 0xF0 : // 当收到0xF0,Key_UP置1表示断码开始
Key_UP = 1;
break;
case 0x12 : // 左 SHIFT
Shift = 1;
break;
case 0x59 : // 右 SHIFT
Shift = 1;
break;
default:
if (DisNum > 15)
{
// DisplayListChar(0, 1, Cls);//清LCD第二行
// sendtoPC("");
DisNum = 0;
}
if(!Shift) //如果SHIFT没按下
{
for (TempCyc = 0;(UnShifted[TempCyc][0]!=ScanCode)&&(TempCyc<59); TempCyc++); //查表显示
if (UnShifted[TempCyc][0] == ScanCode)
// DisplayOneChar(DisNum, 1, UnShifted[TempCyc][1]);
sendtoPC(UnShifted[TempCyc][1]);
DisNum++;
}
else //按下SHIFT
{
for(TempCyc = 0; (Shifted[TempCyc][0]!=ScanCode)&&(TempCyc<59); TempCyc++); //查表显示
if (Shifted[TempCyc][0] == ScanCode)
// DisplayOneChar(DisNum, 1, Shifted[TempCyc][1]);
sendtoPC(Shifted[TempCyc][1]);
}
break;
}
}
else
{
Key_UP = 0;
switch (ScanCode) //当键松开时不处理判码,如G 34H F0H 34H 那么第二个34H不会被处理
{
case 0x12 : // 左 SHIFT
Shift = 0;
break;
case 0x59 : // 右 SHIFT
Shift = 0;
break;
}
}
BF = 0; //标识字符处理完了
}
void UART_ISR(void) interrupt 4 //serial interrupt
{
unsigned char ch ;
ch = 0;
if(RI)
{
RI = 0;
ch = SBUF; /* 清除RS接收标志位*/
command = ch;
}
if(TI)
{
TI=0; //send this finished, can send another again ;
}
}
void sendtoPC(unsigned char dat)
{
if(TI)
TI = 0; /* clear transmit interrupt flag*/
SBUF = dat; /* start sending one byte*/
while (!TI); /* wait until sent*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -