#include "reg52.h"
typedef unsigned char u8;
typedef unsigned int u16;
#define p2 P2 //把大写改成小写,打字累
#define p1 P1 //按Caps Lock键也是要花力气的
sbit jia =P2^3; //频率增加键
sbit jian =P2^2; //频率递减键
u16 pinlv=875; //频率,请看拼音
bit jishi; //延时用
char chufa; //按键触发值
int zijia; //消抖计时
bit zisuo; //按键自锁,短按避免重复触发
bit changanbiaozhi; //长按标志
#define duanan 10 //100毫秒为短按
#define changan 100 //1秒以上为长按
#define lianxuchufa 5 //连续触发时间为50毫秒一次
unsigned char code shuju[11]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
void yanshi(int a)
{
while(a)
{
if(jishi)
{
jishi=0;
a--;
}
}
}
void xianshi(void)
{
p2&=0x0f; //先关位选
if(pinlv/1000) //如果频率超过100兆赫兹就显示
{
p1=shuju[pinlv/1000%10];
p2|=0x10;
}
yanshi(10);
p2&=0x0f;
p1=shuju[pinlv/100%10];
p2|=0x20;
yanshi(10);
p2&=0x0f;
p1=shuju[pinlv/10%10]+0x80;
p2|=0x40;
yanshi(10);
p2&=0x0f;
p1=shuju[pinlv%10];
p2|=0x80;
yanshi(10);
}
void anjian(void)
{
if(jia&&jian) //如果没按键按下
{
zijia=0; //清除自加计时
zisuo=0; //置零按键自锁
changanbiaozhi=0; //清除长按标志
}
else if(zisuo==0) //如果有按键按下
{
zijia++; //消抖计时自加
if(zijia>duanan) //如果加到设定时间
{
zijia=0; //置零自加计时
if(!jia)
chufa=1; //触发1号键
if(!jian)
chufa=2; //触发2号键
zisuo=1; //按键自锁
}
}
else if(changanbiaozhi) //如果连续触发为1
{
zijia++; //消抖计时自加
if(zijia>lianxuchufa) //如果消抖时间达到连续触发时间
{
zijia=0;
if(!jia)
chufa=1; //触发1号键
if(!jian)
chufa=2; //触发2号键
}
}
else
{
zijia++;
if(zijia>changan) //如果自加超过设定的长按时间
zijia=0,
changanbiaozhi=1; //开启长按标志
}
}
void main(void)
{
TMOD=0x01;
TH0=0xfc;
TL0=0x18;
TR0=1;
ET0=1;
EA=1;
while(1)
{
if(chufa==1)
{
chufa=0;
pinlv++;
if(pinlv>1080)
pinlv=875;
}
else if(chufa==2)
{
chufa=0;
pinlv--;
if(pinlv<875)
pinlv=1080;
}
xianshi();
}
}
void dingshiqi(void) interrupt 1
{
static char a;
TH0=0xfc;
TL0=0x18;
jishi=1;
a++;
if(a>10)
a=0,
anjian();
}