用了一天時(shí)間,終于修改完了這個(gè)程序,請(qǐng)大神看看那些地方需要優(yōu)化的。
#include <reg52.h> //52系列頭文件
#define uchar unsigned char //宏定義
#define uint unsigned int //宏定義
sbit key=P1^4; //聲明P1口的第5位是按鍵引腳
sbit tt=P2^6; //聲明P2口按鍵十位數(shù)是數(shù)碼管位選引腳
sbit st=P2^7; //聲明P2口按鍵個(gè)位數(shù)是數(shù)碼管位選引腳
sbit thdis=P2^0; //
sbit ttdis=P2^1; //
sbit tsdis=P2^2; //
sbit oput1=P1^0; //聲明P1.0為輸出引腳
uchar code table[]={ //編碼定義
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90};
void delayms(uint); //聲明子函數(shù)
uchar num0,num1,hendreds,tens,single; //聲明顯示變量的數(shù)據(jù)類型
uint time0; //聲明計(jì)時(shí)變量的數(shù)據(jù)類型
/**************************************************
初始化函數(shù)
***************************************************/
void init()
{
oput1=0;
TMOD=0x01; //設(shè)置定時(shí)器0為工作方式1(0000 0001)
TH0=(65536-45827)/256; //裝初值50ms一次中斷
TL0=(65536-45827)%256; //
IE=0x82; //開(kāi)總中斷,開(kāi)定時(shí)器0中斷
}
/**************************************************
按鍵顯示函數(shù)
***************************************************/
void display(uchar numdis) //
{
tens=numdis/10; //
single=numdis%10; //
tt=0; //十位數(shù)顯示
P0=table[tens]; //
delayms(5);
tt=1;
P0=0xff;
st=0; //個(gè)位數(shù)顯示
P0=table[single]; //
delayms(5);
st=1;
P0=0xfff;
}
/**************************************************
倒數(shù)顯示函數(shù)
***************************************************/
void tdisplay(uint tdis)
{
hendreds=tdis/100;
tens=tdis%100/10;
single=tdis%10;
thdis=0;
P0=table[hendreds];
delayms(5);
thdis=1;
P0=0xff;
ttdis=0;
P0=table[tens];
delayms(5);
ttdis=1;
P0=0xff;
tsdis=0;
P0=table[single];
delayms(5);
tsdis=1;
P0=0xff;
}
/**************************************************
鍵盤掃描函數(shù)
***************************************************/
void keyscan()
{
if(key==0)
{
delayms(10);
if(key==0)
{
num1++; //顯示變量num自增1
time0=num1*60; //輸出變量時(shí)間
if(num1>5) //如果顯示到5就清0
num1=0;
while(!key);
}
}
}
/**************************************************
P1.0輸出的時(shí)間
***************************************************/
void oput()
{
if((num1>0)&&(time0>0))
{
oput1=1;
TR0=1;
}
else
{
oput1=0;
TR0=0;
num1=0;
time0=0;
}
}
/**************************************************
延時(shí)函數(shù)
***************************************************/
void delayms(uint xms)
{
uchar i,j;
for (i=xms;i>0;i--) //i=xms 即延時(shí)約xms毫秒
for(j=110;j>0;j--);
}
/**************************************************
主函數(shù)
***************************************************/
void main() //
{
init();
while(1) //大循環(huán)
{
keyscan();
display(num1);
tdisplay(time0);
oput();
}
}
/**************************************************
中斷函數(shù)
***************************************************/
void T0_time() interrupt 1 //
{
TH0=(65536-45872)/256; //
TL0=(65536-45872)%256;
num0++;
if(num0==20) //如果到時(shí)了20次,說(shuō)明一秒時(shí)間到
{
num0=0;
time0--;
}
}
|