//簡易秒表,T1定時器,方式1,9999秒定時
#include<reg51.h>
#define uchar unsigned char
#define TH1_DATA (65536-50000)/256
#define TL1_DATA (65536-50000)%256
#define count_1s 20
int time_value=0;
#define SET_PORT P0
sbit DIS_TENS=P1^1; //顯示數(shù)碼秒值十位的公共端
sbit DIS_UNIT=P1^0; //顯示數(shù)碼秒值個位的公共端
sbit DIS_HUND=P1^2; //顯示數(shù)碼秒值百位的公共端
sbit DIS_THOU=P1^3; //顯示數(shù)碼秒值千位的公共端
sbit key=P3^2; //定義按鍵接口P3^2
bit flag_1s; //定義1秒時間到標(biāo)志
void delay(uchar);
uchar dis_number[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //0~9的字形碼
void main(void)
{
uchar time_thou=0,time_hund=0,time_tens=0,time_unit=0;
TMOD=0x10;
TH1=TH1_DATA;
TL1=TL1_DATA;
ET1=1;
EA=1;
TR1=1; //啟動定時器
IT0=1; //外部中斷0
EX0=1;
EA=1;
while(1)
{
if(flag_1s == 1) //如果1s到了
{
flag_1s=0; //標(biāo)記重新清0
time_value++; //1S次數(shù)加1
if(time_value >= 10000) //如果到了10000秒
{
time_value=0; //1s的次數(shù)歸0
}
time_thou=time_value / 1000; //計算當(dāng)前秒表計時值的千位
time_hund=(time_value % 1000) / 100; //計算當(dāng)前秒表計時值的百位
time_tens=(time_value % 100) / 10; //計算當(dāng)前秒表計時值的十位
time_unit=time_value % 10; //計算當(dāng)前秒表計時值的個位
}
//顯示千位數(shù)字
SET_PORT=dis_number[time_thou]; //送段碼
DIS_THOU=0; //送位碼
delay(4); //延時
DIS_THOU=1; //關(guān)閉當(dāng)前顯示位
//顯示百位數(shù)字
SET_PORT=dis_number[time_hund];
DIS_HUND=0;
delay(4);
DIS_HUND=1;
//顯示十位數(shù)字
SET_PORT=dis_number[time_tens];
DIS_TENS=0;
delay(4);
DIS_TENS=1;
//顯示個位數(shù)字
SET_PORT=dis_number[time_unit];
DIS_UNIT=0;
delay(4);
DIS_UNIT=1;
}
}
void int_t3() interrupt 3 using 1
{
static uchar count_50ms=0;
TH1=TH1_DATA;
TL1=TL1_DATA;
count_50ms++;
if(count_50ms >= count_1s)
{
count_50ms=0;
flag_1s=1;
}
}
void int_t1() interrupt 1 using 1 //當(dāng)按鍵按下時
{
TR1=~TR1; //定時器的狀態(tài)取反
}
void delay(uchar x)
{
uchar i,j;
for(i=x;i>0;i--)
{
for(j=250;j>0;j--)
{
;
}
}
}
QQ截圖20141121162208.jpg (486.43 KB, 下載次數(shù): 118)
下載附件
2014-11-21 16:23 上傳
|