本帖最后由 jinlehuang 于 2011-3-11 20:59 编辑
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define dudata
#define wedata
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};
uint num;
uchar t;
uchar hms(uint,char)
void display(uint);
uchar fenjie(uchar,uchar);
void delay(uint);
void init()
{
t=0;
num=0;
EA=1;
ET0=1;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
}
void main()
{
init();
while(1)
{
display(num);
}
}
uchar fenjie(uchar a,uchar b)
{
switch(b)
{
case 1: return a%10;
case 2: return a/10;
}
}
void delay(uint z)
{
uint x,y;
for(x=0;x<z;x++)
for(y=0;y<100;y++);
}
void display(uint num)
{
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=table[fenjie(hms(num,'h'),2)];
dula=0;
delay(5);
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
dula=1;
P0=table[fenjie(hms(num,'h'),1)];
dula=0;
delay(5);
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
dula=1;
P0=table[fenjie(hms(num,'m'),2)];
dula=0;
delay(5);
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
dula=1;
P0=table[fenjie(hms(num,'m'),1)];
dula=0;
delay(5);
P0=0xff;
wela=1;
P0=0xef;
wela=0;
dula=1;
P0=table[fenjie(hms(num,'s'),2)];
dula=0;
delay(5);
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
dula=1;
P0=table[fenjie(hms(num,'s'),1)];
dula=0;
delay(5);
P0=0xff;
}
uchar hms(uint num,char tim)
{
switch(tim)
{
case 'h': return num/3600;
case 'm': return num%3600/60;
case 's': return num%60;
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t++;
} |