#include< reg52.h>
#include< intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar code disp[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7f,0x39,0x3f,0x79,0x71};
//數(shù)碼管顯示十六進(jìn)制,從0到f
uchar code select[]={7,6,5,4,3,2,1,0};
//位選數(shù)組,自左至右
uchar tt0,tt1;
uint temp1=0xfe;
uint temp=432;
void alllight();
void delay(uint z);
void init() //初始化函數(shù)
{
TMOD=0x11; //定時器0工作在方式一
TH0=(65536-50000)/256; //裝入初值
TL0=(65536-50000)%256;
TH1=(65536-50000)/256; //裝入初值
TL1=(65536-50000)%256;
EA=1; //開總中斷
ET0=1; //開定時器0中斷
ET1=1;
TR0=1;
TR1=1; //計時開始
}
void Timer0() interrupt 1 //定時器0中斷子程序
{
TH0=(65536-50000)/256; //再次裝入初值
TL0=(65536-50000)%256;
tt0++;
}
void Timer1() interrupt 3 //定時器1中斷子程序
{
TH0=(65536-50000)/256; //再次裝入初值
TL0=(65536-50000)%256;
tt1++;
}
void display(uint temp) //顯示子程序
{
uint bai,shi,ge;
bai=temp/100;
shi=temp%100/10;
ge=temp%100%10;
if(bai!=0) //此處的判斷是為了消除重影,不然當(dāng)沒有到100時,百位上總會有一個0在隱隱約約的顯示
{
P0=disp[bai]; //顯示百位
P2=select[2];
delay(1); //延時0.1s左右
}
P0=disp[shi]; //顯示十位
P2=select[1];
delay(1);
P0=disp[ge]; //顯示個位
P2=select[0];
delay(1);
}
void delay(uint z) //延時子函數(shù),z為傳遞的參數(shù),用來決定延時時間的長短,若z為1,延時時間大概為0.001s
{
uint x,y;
for(x=z;x>0;x--)
for(y=10;y>0;y--);
}
void main()
{
init();
while(1)
{
if(tt1!=2) //數(shù)碼管顯示程序段
{
P0=disp[7]; //顯示高三位的765
P2=select[5];
delay(1);
P0=disp[6];
P2=select[4];
delay(1);
P0=disp[5];
P2=select[3];
delay(1);
display(temp);
}
else if(tt1==2)
{
tt1=0;
if(temp==398)
{
TR1=0;
}
else
{
temp--;
}
}
if(tt0==10) //流水燈程序段
{
tt0=0;
{
if(temp==398)
alllight();
}
temp1=_crol_(temp1,1);
P1=temp1;
}
}
}
void alllight() //數(shù)碼管停止后的動作
{
while(tt0<=60)
{
temp1=0xff;
if(tt0%10==0)
{
P1=~temp1;
}
P0=disp[7]; //顯示高三位的765
P2=select[5];
delay(1);
P0=disp[6];
P2=select[4];
delay(1);
P0=disp[5];
P2=select[3];
delay(1);
display(temp);
}
while(1)
{
P1=0xff;
P0=0x76; //顯示字母H
P2=3;
delay(1);
P0=0x79; //顯示字母E
P2=4;
delay(1);
P0=0x38; //顯示字母L
P2=5;
delay(1);
P0=0x38; //顯示字母L
P2=6;
delay(1);
P0=0x3f; //顯示字母O
P2=7;
delay(1);
}
}
|