/**************************************************************************
动态数码管显示
***************************************************************************/
#include <REGX52.H>
//函数申明
void disp(unsigned char *x,db);
void delay(unsigned int c);
void timerint () ;
//D1--D4:四位数码管COM端,在本例中为共阴极;DP:小数位。
sbit D1=P2^0;
sbit D2=P2^1;
sbit D3=P2^2;
sbit D4=P2^3;
sbit DP=P1^7;
//DT:数码管八位数据总线
#define DT P1
//0--9 位码,0X00:清显示。
unsigned char code dis[13]={
0X3F,/*0*/
0X06,/*1*/
0X5B,/*2*/
0X4F,/*3*/
0X66,/*4*/
0X6D,/*5*/
0X7D,/*6*/
0X07,/*7*/
0X7F,/*8*/
0X6F,/*9*/
0X40,/*-*/
0XBF,/*0.*/
0X00,/*NULL*/
};
//sign:处理负数
signed int sign,Num=-20;
int count=0;
void main(void)
{
unsigned char DB,X[4]="0000"; /*四位要显示的数码:压缩的BCD码表示*/
DB=4; /*小数点位置:0-无小数点,1-末位,4-最高位*/
EA = 1;
ET0 = 1;
TR0 = 1;
TMOD = 0x01; /*50ms定时*/
TH0 = 0x3C;
TL0 = 0xB0;
while(1)
{
DB=P3&3; /*按键控制小数点位置,大家可以自行修改设定*/
if(Num>9999)Num=-50;
if(Num<0)
{
*X=10;
sign=-Num;
*(X+1)=(sign/100)%10;
*(X+2)=(sign/10)%10;
*(X+3)=sign%10;
}
else
{
*X=(Num/1000)%10;
*(X+1)=(Num/100)%10;
*(X+2)=(Num/10)%10;
*(X+3)=Num%10;
}
if(*X==0&&DB<4)*X=12; /*高位多余的零消除*/
if(*(X+1)==0&&DB<3)*(X+1)=12;
if(*(X+2)==0&&DB<2)*(X+1)=12;
disp(X,DB);
}
}
void delay(unsigned int c)
{
unsigned int i,j;
for(i=0;i<c;i++)
for(j=0;j<100;j++);
}
void disp(unsigned char *x,db) /*本子程序可以做为动态数码管显示的通用程序*/
{
DT=dis[*x];
D1=1;
if(db==4)DP=1;
delay(10);
D1=0;
DT=dis[*(x+1)];
D2=1;
if(db==3)DP=1;
delay(10);
D2=0;
DT=dis[*(x+2)];
D3=1;
if(db==2)DP=1;
delay(10);
D3=0;
DT=dis[*(x+3)];
D4=1;
if(db==1)DP=1;
delay(10);
D4=0;
}
void timerint () interrupt 1
{
count++;
if(count==10){ count=0; Num++; }
TF0 = 0; /*50ms定时*/
TH1=0X3C;
TL1=0XB0;
TR0 = 1;
}
#include <REGX52.H>
void disp(unsigned char *x,db);
void delay(unsigned int c);
void timerint () ;
sbit D1=P2^0;
sbit D2=P2^1;
sbit D3=P2^2;
sbit D4=P2^3;
sbit DP=P1^7;
#define DT P1
unsigned char code dis[13]={
0X3F,/*0*/
0X06,/*1*/
0X5B,/*2*/
0X4F,/*3*/
0X66,/*4*/
0X6D,/*5*/
0X7D,/*6*/
0X07,/*7*/
0X7F,/*8*/
0X6F,/*9*/
0X40,/*-*/
0XBF,/*0.*/
0X00,/*NULL*/
};
signed int sign,Num=-20;
int count=0;
void main(void)
{
unsigned char DB,X[4]="0000";
DB=4;
EA = 1;
ET0 = 1;
TR0 = 1;
TMOD = 0x01;
TH0 = 0x03C;
TL0 = 0x0B0;
while(1)
{
DB=P3&3;
if(Num>9999)Num=-50;
if(Num<0)
{
*X=10;
sign=-Num;
*(X+1)=(sign/100)%10;
*(X+2)=(sign/10)%10;
*(X+3)=sign%10;
}
else
{
*X=(Num/1000)%10;
*(X+1)=(Num/100)%10;
*(X+2)=(Num/10)%10;
*(X+3)=Num%10;
}
if(*X==0&&DB<4)*X=12;
if(*(X+1)==0&&DB<3)*(X+1)=12;
if(*(X+2)==0&&DB<2)*(X+1)=12;
disp(X,DB);
}
}
void delay(unsigned int c)
{
unsigned int i,j;
for(i=0;i<c;i++)
for(j=0;j<100;j++);
}
void disp(unsigned char *x,db)
{
DT=dis[*x];
D1=1;
if(db==4)DP=1;
delay(10);
D1=0;
DT=dis[*(x+1)];
D2=1;
if(db==3)DP=1;
delay(10);
D2=0;
DT=dis[*(x+2)];
D3=1;
if(db==2)DP=1;
delay(10);
D3=0;
DT=dis[*(x+3)];
D4=1;
if(db==1)DP=1;
delay(10);
D4=0;
}
void timerint () interrupt 1
{
count++;
if(count==5){ count=0; Num++; }
TF0 = 0;
TH1=(65536-64000)/256;
TL1=(65536-64000)%256;
TR0 = 1;
}