这是一个让共阴极数码管中的3位显示秒计时的,精度是1%。
#include<reg52.h>
//16进制数的显示代码
unsigned char code tablenum[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//带小数点的16进制数的显示代码
unsigned char code tablepnt[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1};
//位选开关,由于硬件高位在最右边所以是从高位至低位排列的
unsigned char code tablewei[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
unsigned char numplace[8]; //分别存储8位数码管的编码
void delay(unsigned int); //延时函数声明
void display(); //8位数码管动态显示函数声明
sbit switchnum=P2^7; //段选开关
sbit switchwei=P2^6; //位选开关
unsigned int a=0; //时间计数变量,用来计1%秒
main()
{
TMOD|=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
display();
while(1);
}
void display() //8位数码管动态显示函数
{
unsigned int i;
for(i=0;i<8;i++)
{
P0=tablewei[i];
switchwei=1;
switchwei=0;
P0=numplace[i];
switchnum=1;
delay(1);
P0=0x00;
switchnum=0;
}
}
void delay(unsigned int i) //延时函数
{
unsigned int a,b;
for(a=0;a<i;a++)
for(b=0;b<123;b++);
}
void miaojia(void) interrupt 1 //中断函数
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
a++;
if(a==1000)
a=0;
numplace[0]=tablenum[a%10];
numplace[1]=tablenum[a/10%10];
numplace[2]=tablepnt[a/100%10];
display();
}
#include<reg52.h>
//16进制数的显示代码
unsigned char code tablenum[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//带小数点的16进制数的显示代码
unsigned char code tablepnt[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1};
//位选开关,由于硬件高位在最右边所以是从高位至低位排列的
unsigned char code tablewei[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
unsigned char numplace[8]; //分别存储8位数码管的编码
void delay(unsigned int); //延时函数声明
void display(); //8位数码管动态显示函数声明
sbit switchnum=P2^7; //段选开关
sbit switchwei=P2^6; //位选开关
unsigned int a=0; //时间计数变量,用来计1%秒
main()
{
TMOD|=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
display();
while(1);
}
void display() //8位数码管动态显示函数
{
unsigned int i;
for(i=0;i<8;i++)
{
P0=tablewei[i];
switchwei=1;
switchwei=0;
P0=numplace[i];
switchnum=1;
delay(1);
P0=0x00;
switchnum=0;
}
}
void delay(unsigned int i) //延时函数
{
unsigned int a,b;
for(a=0;a<i;a++)
for(b=0;b<123;b++);
}
void miaojia(void) interrupt 1 //中断函数
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
a++;
if(a==1000)
a=0;
numplace[0]=tablenum[a%10];
numplace[1]=tablenum[a/10%10];
numplace[2]=tablepnt[a/100%10];
display();
}