|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2015-11-20 16:39:09
|
显示全部楼层
修改了一下,这样结构比较好!
#include<reg51.h>
unsigned char code LedChar[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
};
unsigned char LedBuff[4] = {0xff,0xff,0xff,0xff};
sbit gewei = P1^3;
sbit shiwei = P1^2;
sbit baiwei = P1^1;
unsigned char i = 0;
unsigned char iset = 0;
signed int disp = 0;
signed int disp0 = 15;
signed int disp1 = 21;
signed int disp2 = 5;
void delayms(unsigned int k)
{
unsigned int i,j;
for(i=k;i>0;i--)
for(j=110;j>0;j--);
}
unsigned char keyscan(void)
{
unsigned char keynum=0;
unsigned char temp=0;
P3=0xff; //P3口置oxff,准备读取输入值
if(P3!=0xff) //判断是否有键按下
{
delayms(10); //延时10ms消抖动
P3=0xff; //P3口置oxff,准备2次读取输入值
if(P3!=0xff) //2次判断是否有键按下
{
temp=P3; //读取P3口值至temp中
while(P3!=0xff); //按键释放检测语句
switch(temp) //进入开关多分支语句判断
{
case 0xfb:
keynum=1; //SET键
break;
case 0xf7:
keynum=2; //LEFT键
break;
case 0xef:
keynum=3; //UP键
break;
case 0xdf:
keynum=4; //DOWN键
break;
case 0xbf:
keynum=5; //模拟SET键长按
break;
case 0x7f:
keynum=6; //模拟LEFT键长按
break;
default:
break;
}
}
}
return keynum;
}
void main()
{
unsigned long sec = 0;
unsigned char key;
EA = 1;
TMOD = 0x01;
TH0 = 0xfc;
TL0 = 0x67;
ET0 = 1;
TR0 = 1;
while(1)
{
key = keyscan();
if(key == 1) //SET键
{
iset++;
if(iset == 3)
iset=0;
}
else if(key == 3) //UP键
{
if(iset == 1)
{
disp0++;
}
else if(iset == 2)
{
disp1++;
}
}
else if(key == 4) //DOWN键
{
if(iset == 1)
{
disp0--;
}
else if(iset == 2)
{
disp1--;
}
}
if(iset == 0)
{
disp=disp2;
}
else if(iset == 1)
{
disp=disp0;
}
else if(iset == 2)
{
disp=disp1;
}
LedBuff[0] = LedChar[disp % 10];
LedBuff[1] = LedChar[disp / 10 % 10];
LedBuff[2] = LedChar[disp / 100 % 10];
}
}
void InterruptTimer0() interrupt 1
{
TH0 = 0xfc;
TL0 = 0x67;
P0 = 0xff;
if(i == 0)
{
baiwei = 1;
shiwei = 1;
gewei = 0;
i++;
P0 = LedBuff[0];
}
else if(i == 1)
{
baiwei = 1;
shiwei = 0;
gewei = 1;
i++;
P0 = LedBuff[1];
}
else if(i == 2)
{
baiwei = 0;
shiwei = 1;
gewei = 1;
i = 0;
P0 = LedBuff[2];
}
} |
|