標(biāo)題:
單片機(jī)數(shù)碼管99秒秒表源代碼
[打印本頁]
作者:
wangboyun
時間:
2017-10-16 21:53
標(biāo)題:
單片機(jī)數(shù)碼管99秒秒表源代碼
/********************************************************************************
*程序名: 數(shù)碼管99秒秒表 *
*編寫人: 馬飛龍 *
*編寫時間: 1.24 *
*硬件支持: stc89c52 11.0592MHz *
*接口說明: P0口接數(shù)碼管段選,P2.0個位 P2.1十位 *
*修改日志: *
1)修改通電后不操作就開始計時的錯誤; *
2)給數(shù)碼管顯示加消影; *
********************************************************************************/
#include <reg52.h>
#define uchar unsigned char //宏定義 用uchar代替 unsigned char
#define uint unsigned int
/*引腳定義*/
sbit START=P1^2; //開始、停止鍵 低電平有效
sbit RST=P1^3; //復(fù)位鍵
/*定義全局變量*/
uint time; //時間變量
/*數(shù)碼管字模,對應(yīng) 0-9*/
uchar code table[]={ // code 放在ROM
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
/*延時函數(shù)*/
void Delay(uint ms) //毫秒級延時函數(shù)
{
uint i, j;
for(i =ms; i>0; i--)
for(j =110; j>0; j--);
}
/*中斷*/
void time0() interrupt 1
{
uchar tt;
TH0 = 0x4c; //晶振11.0592Mhz
TL0 = 0x00; //若用12Mhz晶振 則改為 TH0=0x3c;TL0=0xb0;
tt++;
if(tt == 20)
{
time++;
if(time==99) //到99后在從00開始
time = 0;
tt = 0;
}
}
/*顯示函數(shù)*/
void Display(uchar sum)
{
uchar shi, ge;
P0 = 0x00; //消影
P2 = 0x00;
shi = table[sum/10];
P0 = shi;
P2 = 0xfd;
Delay(5);
P0 = 0x00; //消影
P2 = 0x00;
ge = table[sum%10];
P0 = ge;
P2 = 0xfe;
Delay(5);
}
/*按鍵掃描*/
void keyscan()
{
if(START==0) //開始、停止
{
Delay(10); //消抖
if(START==0)
{
TR0 =!TR0;
while(!START) Display(time);
}
}
if(RST==0) //復(fù)位
{
Delay(10); //消抖
if(RST==0)
{
time=0;
TR0=0;
while(!RST)Display(time);
}
}
}
//主函數(shù)
void main()
{
EA = 1; //中斷開關(guān)
ET0 = 1;
TMOD = 0x01;
TH0 = 0x4c; //晶振11.0592Mhz
TL0 = 0x00; //若用12Mhz晶振 則改為 TH0=0x3c;Tl0=0xb0;
TR0 = 0;
while(1)
{
keyscan();
Display(time);
}
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1