PS2单片机代码
简单实例代码如下:#include<REG52.h>
sbit LS138A=P2^2;
sbit LS138B=P2^3;
sbit LS138C=P2^4;
sbit BEEP = P1^5;
sbit Key_Data = P1^6 ; //定义键盘引脚
sbit Key_CLK = P3^2; //使用外部中断
void Decode(unsigned char ScanCode);//解码子程序
void delay(unsigned int i); //函数声名
void display();
void beep();
// 此表为 LED 的字模 // 0 1 2 3 4 5 6 7 8 9 A b c d E - L P U Hidden_ (20)
unsigned char code Disp_Tab[] = { ~0xC0,~0xF9,~0xA4,~0xB0,~0x99,~0x92,~0x82,~0xF8,~0x80,~0x90,~0x88,~0x83,~0xC6,~0xA1,~0x86,~0xbf,~0xc7,~0x8c,~0xc1, ~0xff,~0xf7 };
bit BF;
unsigned char KeyV;
unsigned char LedNumVal;
int KEY_val,IntNum;
void main()
{
IT1 = 0; //设外部中断1为低电平触发
EA = 1; //外部中断开
EX0 = 1; //开中断
while(1)
{
if (BF)
{
if (KeyV != 240)beep();
Decode(KeyV); //解码
}
else
EA = 1; //开中断
display();
}
}
void Keyboard_out(void) interrupt 0
{
if ((IntNum > 0) && (IntNum < 9))
{
KeyV = KeyV >> 1; //因键盘数据是低>>高,结合上一句所以右移一位
if (Key_Data)
KeyV = KeyV | 0x80; //当键盘数据线为1时到最高位
}
IntNum++;
while (!Key_CLK); //等待PS/2CLK拉高
if (IntNum > 10)
{
IntNum = 0; //当中断11次后表示一帧数据收完,清变量准备下一次接收
BF = 1; //标识有字符输入完了
EA = 0; //关中断等显示完后再开中断
}
}
void Decode(unsigned char ScanCode) //注意:如SHIFT+G为12H 34H F0H 34H F0H 12H,也就是说shift的通码+G的通码+shift的断码+G的断码
{
switch (ScanCode)
{
case 0x70 : // 当收到0xF0,Key_UP置1表示断码开始
KEY_val = 0;
break;
case 0x69 :
KEY_val = 1;
break;
case 0x72 :
KEY_val = 2;
break;
case 0x7A :
KEY_val = 3;
break;
case 0x6B :
KEY_val = 4;
break;
case 0x73 :
KEY_val = 5;
break;
case 0x74 :
KEY_val = 6;
break;
case 0x6c :
KEY_val = 7;
break;
case 0x75 :
KEY_val = 8;
break;
case 0x7d :
KEY_val = 9;
break;
case 0x71 :
// dot = 0X7F;
break;
}
BF = 0; //标识字符处理完了
}
void display( )
{
unsigned char i;
unsigned int LedOut;
LedNumVal = KEY_val ;
LedOut=Disp_Tab;
LedOut=Disp_Tab;
LedOut=Disp_Tab;
LedOut=Disp_Tab;
LedOut=Disp_Tab; //千位
LedOut=Disp_Tab;//百位带小数点
LedOut=Disp_Tab; //十位
LedOut=Disp_Tab; //个位
for( i=0; i<8; i++)
{P0 = LedOut;
switch(i)
{
case 0:LS138A=0; LS138B=0; LS138C=0; break;
case 1:LS138A=1; LS138B=0; LS138C=0; break;
case 2:LS138A=0; LS138B=1; LS138C=0; break;
case 3:LS138A=1; LS138B=1; LS138C=0; break;
case 4:LS138A=0; LS138B=0; LS138C=1; break;
case 5:LS138A=1; LS138B=0; LS138C=1; break;
case 6:LS138A=0; LS138B=1; LS138C=1; break;
case 7:LS138A=1; LS138B=1; LS138C=1; break;
}
delay(50);
}
}
void delay(unsigned int i)
{
char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--);
}
void beep()
{
unsigned char i;
for (i=0;i<255;i++)
{
delay(5);
BEEP=!BEEP; //BEEP取反
}
BEEP=1; //关闭蜂鸣器
}
PS2单片机代码 :) PS2单片机代码 PS2单片机代码
页:
[1]