?亮度分为三档? ?没按下一次按键? 档位会升高? 数码管显示相应的档位
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^7;
sbit dula=P2^6;
sbit led1=P1^0;
sbit key1=P3^4;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
//uchar code tablew[]=
//{0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
void delay(uint z);
void display(uchar s);
void keysm();
uchar keynum,count,comprate;
void init()
{
TMOD=0x01;
TH0=(65536-1)/256;
TL0=(65536-1)%256;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
while(1)
{
init();
display(keynum);
keysm();
switch(keynum)
{
case 0:
comprate=0;
break;
case 1:
comprate=5;
break;
case 2:
comprate=50;
break;
case 3:
comprate=100;
break;
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar s)
{
dula=1;
P0=table[s];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(5);
}
void keysm()
{
P3=0xff;
while(1)
{
if(key1==0)
{
delay(5);
if(key1==0)
{
keynum++;
if(keynum==4)
{
keynum=0;
}
while(!key1);
break;
}
}
}
}
void time_0() interrupt 1
{
TH0=(65536-1)/256;
TL0=(65536-1)%256;
count++;
if(count==100)
{
count=0;
}
if(count<comprate)
{
led1=0;
}
else
{
led1=1;
}
}
|