根據(jù)客戶的要求利用單片機制作一個小的工控鍵盤,將下面對應(yīng)的鍵值發(fā)送到電腦顯示,利用的協(xié)議就是PS2,單片機型號為stc89c52rc,晶振為12M;
#include
#include "PS2.H"
本文引用地址:
http://www.sanyacts.com.cn/article/201611/320263.htmBYTE 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; //設(shè)定標志位
Key_line=0xe0; //將P0口低5位全部設(shè)置位0
temp3=Key_list; //讀取P2口的狀態(tài),若果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)
{
unsigned char temp2=0,temp3=0; //temp2用來保存行鍵盤數(shù)據(jù),temp3保存列鍵盤數(shù)據(jù)
unsigned char temp=0,flag=0; //函數(shù)返回值temp
unsigned char i=0,key=0; //i位循環(huán)控制變量,給行送數(shù)據(jù),key保存檢測鍵盤按下的標志位
if((key=Key_Press())!=0) //判斷是否有鍵按下
{