前几天刚做了一个
#include <reg52.h>
typedef unsigned char uchar;
typedef unsigned int uint;
sbit shineng = P1^2;
sbit i0 = P1^0;
sbit i1 = P1^1;
sbit stop = P3^4;
sbit fan = P3^5;
sbit jia = P3^7;
sbit jian = P3^6;
sbit we=P2^7;
sbit du=P2^6;
static uchar c;
uchar th0=(65535-50000)/256;
uchar tl0=(65535-50000)%256;
code uchar disptab[]={0x3f,0x6,0x5b,0x4f,0x66,
0x6d,0x7d,0x27,0x7f,0x6f,0x77,0x7c,0x39,0x5e,
0x79,0x71,0x0};
code uchar dispbit[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar dispbuf[6];
uchar speed = 0;
delayms(uint k);
PWM() ;
key();
delay1(uchar x);
Inittimer0();
void main(void)
{
delayms(10);
Inittimer0();
while(1)
{
key();
PWM();
if(speed<100)
{
dispbuf[5]=speed/20;
}
else if(speed>=100)
{
dispbuf[5]=5;
}
}
}
delayms(uint k)
{
uint a,s;
for(a=k;a>0;a--)
for(s=110;s>0;s--);
}
PWM()
{
shineng=1;
delayms(speed);
shineng=0;
delayms(100-speed);
}
key()
{
if(stop==0)
{
delayms(1);
if(stop==0)
{
while(stop==0);
i0 = 0;
i1 = 0;
}
}
if(fan==0)
{
delayms(1);
if(fan==0)
{
while(fan==0);
i0=~i1;
i0=~i0;
i1=~i1;
}
}
if(jia==0) //??
{
delayms(1);
if(jia==0)
{
while(jia==0);
speed=speed+20;
if(speed >= 100)
speed=100;
}
}
if(jian==0) //??
{
delayms(1);
if(jian==0)
{
while(jian==0);
if(speed != 0)
speed=speed-20;
else if(speed<=0)
speed=0;
}
}
}
delay1(uchar x)
{
uint y;
for(;x>0;x--)
for(y=100;y>0;y--);
}
timer0() interrupt 1
{
static uchar count=0;
uchar tmp;
P0|=0x3f;
we=1;
tmp=dispbit[count];
P0=P0&tmp;
we=0;
P0=disptab[16];
du=1;
tmp=dispbuf[count];
tmp=disptab[tmp];
P0=tmp;
du=0;
count++;
if(count==6)
{
count=0;
}
TH0=th0;
TL0=tl0;
}
Inittimer0()
{
TMOD=0x02;
TH0=th0;
TL0=tl0;
EA=1;
ET0=1;
TR0=1;
}
|