//此秒表有時分秒和毫秒位,最多可以記小時,有暫停和繼續計時功能,獨立鍵盤上key1為暫停和繼續鍵,key3為復位和開始計時鍵//由于ms中斷時間很短,所以如果中斷和顯示延遲關系處理不好,秒表走時不準,應注意
本文引用地址:http://www.104case.com/article/201611/322834.htm
#include
#defineucharunsignedchar
#defineuintunsignedint
uchar code table[]={0x 3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//時分秒的個位顯示后帶小數點
uchar ms,s,m,h,count,count1;
sbit k1=P3^0;
sbit k3=P3^2;
voiddelay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
voiddisplays(uchar temp)//數碼管動態顯示秒位函數
{
uchar shi,ge,i;
i=0;
shi=temp/10;
ge=temp;
P0=0xef;
P1=table[shi];
delay(1);//必須要有延遲,動態掃描,為了不影響整個秒表八位數的掃描速率提高顯示效果,延遲又不要太高,ms比較合適
P0=0xdf;
P1=table1[ge];
i=0;
delay(1);
}
voiddisplayms(uchar temp)//數碼管動態顯示毫秒位函數
{
uchar shi,ge,i;
i=0;
shi=temp/10;
ge=temp;
P0=0xbf;
P1=table[shi];
delay(1);
P0=0x7f;
P1=table[ge];
i=0;
delay(1);
}
voiddisplaym(uchar temp)//數碼管動態顯示分位函數
{
uchar shi,ge,i;
i=0;
shi=temp/10;
ge=temp;
P0=0xfb;
P1=table[shi];
delay(1);
P0=0xf7;
P1=table1[ge];
i=0;
delay(1);
}
voiddisplayh(uchar temp)//數碼管動態顯示小時位函數
{
uchar shi,ge,i;
i=0;
shi=temp/10;
ge=temp;
P0=0xfe;
P1=table[shi];
delay(1);
P0=0xfd;
P1=table1[ge];
i=0;
delay(1);
}
voidkeyscan()//鍵盤掃描函數
{
if(k1==0)
{
delay(5);
if(k1==0)//檢測k1確實被按下防抖動
{
count++;
while(!k1);//檢測松手
delay(1);//檢測確實松手
while(!k1);
if(count==1)
TR0=0;//暫停定時器
if(count==2)
{
TR0=1;//定時器繼續計時
count=0;
}
}
}
if(k3==0)
{
delay(5);
if(k3==0)
{
count1++;
while(!k3);
delay(1);
while(!k3);
if(count1==1)//復位秒表
{
TR0=0;
ms=0;
s=0;
m=0;
h=0;
}
if(count1==2)//重新開始計時
{
TR0=1;
count1=0;
}
}
}
}
voidmain()
{
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
TH0=(65536-10000)/256;//設定定時器初值
TL0=(65536-10000)%6;//12M晶振時ms數為
while(1)
{
keyscan();
displays(s);//數碼管動態掃描秒位顯示
displayms(ms);//數碼管動態掃描毫秒位顯示
displaym(m);//數碼管動態掃描秒分顯示
displayh(h);//數碼管動態掃描秒小時顯示
}
}
voidtimer0() interrupt 1//中斷服務程序
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%6;
ms++;
if(ms==100)//定時器中斷次為s
{//把這部分放在中斷中,能減少程序執行時間對中斷時間的影響
ms=0;
s++;
if(s==60)
{
s=0;
m++;
}
if(m==60)
{
m=0;
h++;
}
if(h==24)
{
h=0;
}
}
}
評論