51单片机模拟单片机模拟PS2协议制作协议制作5X5矩阵工业键盘矩阵工业键盘
本文给大家而分享了51单片机模拟PS2协议制作5X5矩阵工业键盘。
根据客户的要求利用单片机制作一个小的工控键盘,将下面对应的键值发送到电脑显示,利用的协议就是PS2,单片机型号为
stc89c52rc,晶振为12M;
1 0
6 5 4 3 2
7 8 9 减号 等号
y u i o p
q w e r t
#include
#include "PS2.H"
BYTE PS2RecChar=0xCC;
BOOL KeyBoardFlag=FALSE;
#define Key_line P0 //键盘行入口
#define Key_list P2 //键盘列入口
#define PS2_1 0 //16
#define PS2_0 1 //45
#define PS2_6 2 //36
#define PS2_5 3 //2e
#define PS2_4 4 //25
#define PS2_3 5 //26
#define PS2_2 6 //1e
#define PS2_7 7 //3d
#define PS2_8 8 //3e
#define PS2_9 9 //46
#define PS2_dec 10 //4e
#define PS2_eq 11 //55
#define PS2_y 12 //35
#define PS2_u 13 //3c
#define PS2_i 14 //43
#define PS2_o 15 //44
#define PS2_p 16 //4d
#define PS2_q 17 //15
#define PS2_w 18 //1d
#define PS2_e 19 //24
#define PS2_r 20 //2d
#define PS2_t 21 //2c
//第二套键盘码
unsigned char PS2Value[22]={0x16,0x45,0x36,0x2e,0x25,0x26,0x1e,0x3d,0x3e,0x46,0x4e,0x55,0x35,0x3c,
0x43,0x44,0x4d,0x15,0x1d,0x24,0x2d,0x2c};
unsigned char Key_Press(void)
{
unsigned temp3;
unsigned char flag=0; //设定标志位
Key_line=0xe0; //将P0口低5位全部设置位0
temp3=Key_list; //读取P2口的状态,若果P2口的值temp3:(temp3&0xff)!=0xff成立,表示有键按下
if(((temp3&0xff)!=0xff)) //有键按下条件判断
{
flag=1;
//Key_line=0XFF; //清零键盘行端口
Key_list=0xff; //清零键盘列端口
}
else
flag=0; //无键按下标志
return flag;
}
void delay(unsigned int ms)
{
unsigned int i,j;
for(i=ms;i>0;i--)
for(j=100;j>0;j--);
}
unsigned char Key_Scan(void)
{