📄 trykeyboard1.c
字号:
#include<reg52.h>
#define unit unsigned int
#define uchar unsigned char
void delay();
void BeepCount (uchar x);
uchar KeyDown(void);
uchar KeyUp(void);
uchar KeyNum(void);
#define BEEP_ON() (P1_3=0)
#define BEEP_OFF() (P1_3=1)
sbit P1_3=P1^3;
sbit row1=P2^7;
sbit row2=P2^6;
sbit row3=P2^5;
sbit row4=P2^4;
sbit col1=P2^3;
sbit col2=P2^2;
sbit col3=P2^1;
sbit col4=P2^0;
//void BeepCount(uchar count) ;
void main (void)
{
BEEP_OFF(); // 蜂鸣器不蜂鸣
while(1) // 死循环
{
BeepCount(KeyNum()); //function clarify 函数格式声明
}
}
void BeepCount(uchar count)
{
for(;count>0;count--)
{
BEEP_ON(); delay();
BEEP_OFF();delay();
}
}
uchar KeyNum(void)
{
uchar KeyTemp;
KeyTemp=0;
if(KeyDown()==1)
{
col1=0; //colmn one
col2=1;
col3=1;
col4=1;
if(row1==0) KeyTemp=1;
if(row2==0) KeyTemp=5;
if(row3==0) KeyTemp=9;
if(row4==0) KeyTemp=13;
col1=1; //colmn two
col2=0;
col3=1;
col4=1;
if(row1==0) KeyTemp=2;
if(row2==0) KeyTemp=6;
if(row3==0) KeyTemp=10;
if(row4==0) KeyTemp=14;
col1=1; //colmn three
col2=1;
col3=0;
col4=1;
if(row1==0) KeyTemp=3;
if(row2==0) KeyTemp=7;
if(row3==0) KeyTemp=11;
if(row4==0) KeyTemp=15;
col1=1;
col2=1;
col3=1;
col4=0;
if(row1==0) KeyTemp=4;
if(row2==0) KeyTemp=8;
if(row3==0) KeyTemp=12;
if(row4==0) KeyTemp=16;
while(KeyUp()!=1);
return KeyTemp;
}
else
return 0;
}
uchar KeyDown(void) //键盘按下函数
{
col1=0;
col2=0;
col3=0;
col4=0;
if(row1==0||row2==0||row3==0||row4==0)
{
delay();
if(row1==0||row2==0||row3==0||row4==0)
return 1;
else
return 0;
}
else
return 0;
}
uchar KeyUp(void) //键盘弹起函数
{
col1=0;
col2=0;
col3=0;
col4=0;
if(row1==1&&row2==1&&row3==1&&row4==1)
{
delay();
if(row1==1&&row2==1&&row3==1&&row4==1)
return 1;
else
return 0;
}
else
return 0;
}
void delay () //delay function
{
unit i;
// while(--count!=0)
//{
for(i=0;i<125;i++);
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -