|
程序如下:
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar count;
sbit pwm=P3^0;
sbit inc=P3^7;
sbit dec=P3^6;
uchar angle;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
TMOD=0x01;
IE=0x82;
TH0=0xfe;
TL0=0x33;
TR0=1;
}
void timer0() interrupt 1
{
TH0=0xfe;
TL0=0x33;
if(count<angle)
pwm=1;
else
pwm=0;
count++;
count=count%40;
}
void keyscan()
{
if(inc==0)
{
delayms(10);
if(inc==0)
{
angle++;
count=0;
if(angle==6)
angle=5;
while(!inc);
}
}
if(dec==0)
{
delayms(10);
if(dec==0)
{
angle--;
count=0;
if(angle==0)
angle=1;
while(!dec);
}
}
}
void display()
{
uchar bai,shi,ge;
switch(angle)
{
case 1:
bai=0;
shi=0;
ge=0;
break;
case 2:
bai=0;
shi=4;
ge=5;
break;
case 3:
bai=0;
shi=9;
ge=0;
break;
case 4:
bai=1;
shi=3;
ge=5;
break;
case 5:
bai=1;
shi=8;
ge=0;
break;
}
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayms(5);
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayms(5);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delayms(5);
}
void main()
{
angle=1;
count=0;
init();
while(1)
{
keyscan();
display();
}
}
|
|