#include<reg52.h>
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
sbit led4=P2^3;
void display(unsigned int,unsigned int,unsigned int,unsigned int);
void delayms(unsigned int x);
unsigned int qian,bai,shi,ge,num,num1;
unsigned char code table[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e,0x00
};//共阳级字符表
void main()
{
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;//开总中断
ET0=1;//开定时器0中断
TR0=1;//启动定时器0
while(1)
{
display(qian,bai,shi,ge);
}
}
void delayms(unsigned int x)//延时程序
{
unsigned i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void display(unsigned int qian,unsigned int bai,unsigned int shi,unsigned int ge)
{
led4=1;
led3=1;
led2=1;
led1=0;
P0=table[qian];
delayms(3);
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
led4=1;
led3=1;
led2=0;
led1=1;
P0=table[bai];
delayms(3);
led4=1;
led3=0;
led2=1;
led1=1;
P0=table[shi];
delayms(3);
led4=0;
led3=1;
led2=1;
led1=1;
P0=table[ge];
delayms(3);
}//显示子程序
void T_time() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num1++;//定时计数器加1
if(num1==20)
{
num1=0;//定时计数器加20次即1S,清零
num++;//1S后加1
if(num==1000)//显示到1000的子程序
num=0;
qian=num/1000;
bai=num%1000/100;
shi=num%1000%100/10;
ge=num%10;
}
以上两程序都要将数字分离。这里注意一点,因为显示的是4位数,所以定义变量的时候不能用unsigned char,要用unsigned int。因为unsigned char表示的范围是0~255。
循环到255的时候就会清零,溢出,不会继续显示下去。
用户197418 2009-3-31 18:12
用户197418 2009-3-31 18:08
用户199415 2009-3-29 22:33