📄 scan_keyboard_driver.c
字号:
#include "AT89S52.H"
#include "CONFIG.H"
#include <intrins.h>
/******************************
250US定时 *
******************************/
#define RELOAD_H 0XFF
#define RELOAD_L 0X19
bit bdata Key_Delay;
bit bdata Key_Read;
uchar data TIME_COUNT;
uchar data SUM;
//#define _BUZZER_
#ifdef _BUZZER_
sbit SPK=P2^7;//喇叭接口
void Delayms(uchar ms)
{ // 按键音延时子程序
uchar i;
while(ms--)
{
for(i = 0; i< 50; i++)
{
_nop_();
}
}
}
void Error_Beep()
{
uchar data i;
for(i=0;i<200;i++)
{
SPK=~SPK;
delayms(5);
}
}
void Push_Beep()
{
uchar data i;
for(i=0;i<200;i++)
{
SPK=~SPK;
delayms(1);
}
}
#endif
#ifdef _TIMER1_
/****************************************************
定时器1,方式1,定时250US *
****************************************************/
void Key_Init()
{
IE|=0x88; //EA=1;ET1=1;
TMOD=0x11;
TH1=RELOAD_H;
TL1=RELOAD_L;
TR1=1;
}
void time1() interrupt 3 using 1
{
uchar data x,y;
TH1=RELOAD_H;
TL1|=RELOAD_L;
P1=0x0f;
if(P1==0x0f)
{
Key_Delay=0;
Key_Read=0;
TIME_COUNT=0;
}
else if(Key_Delay==0)
{
TIME_COUNT++;
if(TIME_COUNT==100)
Key_Delay=1;
}
else if(Key_Read==0)
{
if(P1_0==0) x=0;
else if(P1_1==0) x=4;
else if(P1_2==0) x=8;
else if(P1_3==0) x=12;
P1=0xf0;
if(P1_4==0) y=0;
else if(P1_5==0) y=1;
else if(P1_6==0) y=2;
else if(P1_7==0) y=3;
SUM=(x+y);
Key_Read=1;
}
}
#else
/****************************************************
定时器2,16位自动装载模式,定时250US *
****************************************************/
void Key_Init()
{
IE|=0xA0; //EA=1;ET2=1;
RCAP2H=RELOAD_H;
RCAP2L=RELOAD_L;
TH2=RELOAD_H;
TL2=RELOAD_L;
TR2=1;
}
void time2() interrupt 5 using 1
{
uchar data x,y;
TF2=0;
P1=0x0f;
if(P1==0x0f)
{
Key_Delay=0;
Key_Read=0;
TIME_COUNT=0;
}
else if(Key_Delay==0)
{
TIME_COUNT++;
if(TIME_COUNT==100)
Key_Delay=1;
}
else if(Key_Read==0)
{
if(P1_0==0) x=0;
else if(P1_1==0) x=4;
else if(P1_2==0) x=8;
else if(P1_3==0) x=12;
P1=0xf0;
if(P1_4==0) y=0;
else if(P1_5==0) y=1;
else if(P1_6==0) y=2;
else if(P1_7==0) y=3;
SUM=(x+y);
Key_Read=1;
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -