|
#include<reg51.h> //單片機(jī)頭文件
#define uint unsigned int
#define uchar unsigned char
#define CF P0 //宏定義
uint sec=0; //數(shù)碼管顯示同步定義數(shù)值sec 后面還會講
sbit K1=P3^2; //K1復(fù)位 就是將運(yùn)行的程序結(jié)束 然后重新開始運(yùn)行
sbit K2=P3^1; //暫停
sbit K3=P3^0; //開始
sbit LED1=P2^0;
sbit LED2=P2^1;
sbit LED3=P2^2; //位選定義
sbit LED4=P2^3;
uchar disbuff[]={0,0,0,0}; //數(shù)碼管顯示位數(shù)的數(shù)值數(shù)組
uchar const num[10]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f}; //0-9的顯示碼
void delay(int s)
{
uint i,j;
for(i=0;i<s;i++) //延時(shí)函數(shù)
for(j=114;j>0;j--);
}
void DigDisplay() //顯示函數(shù)
{
LED1=1;LED2=1;LED3=1;LED4=0; //最右邊一位開啟 也就是個(gè)位
CF = disbuff[0];
delay(5);
CF=0x00;//消隱
LED1=1;LED2=1;LED3=0;LED4=1; //十位
CF=disbuff[1]; //因?yàn)槿巳庋鄣目梢娮兓钚?00ms 這個(gè)數(shù)碼管此刻的變化為微秒級 所以不會看到有不顯示的情況
delay(5);
CF=0x00; //消隱
LED1=1;LED2=0;LED3=1;LED4=1; //百位
CF=disbuff[2];
delay(5);
CF=0x00; //消隱
LED1=0;LED2=1;LED3=1;LED4=1; //千位
CF=disbuff[3];
delay(5);
CF=0x00; //消隱
}
void Count() //計(jì)算函數(shù)
{
disbuff[0]=num[sec%10]; //個(gè)位數(shù)字
disbuff[1]=num[sec/10%10]; //十位數(shù)字
disbuff[2]=num[sec/100%10]; //百位數(shù)字
disbuff[3]=num[sec/1000%10]; //千位數(shù)字
}
void KEY() //按鍵函數(shù)
{
if(K1==0); //啟動(dòng)
delay(10);
if(K1==0)
{TR0=1;}
while(!K1) DigDisplay();Count(); //按鍵釋放
if(K2==0)
{
delay(5);
if(K2==0)
{
TR0=0; //關(guān)閉定時(shí)器
}
} while(!K2)DigDisplay();Count();
if(K3==0)
{
delay(10);
if(K3==0)
{
sec=0;
TH0=0x3c;//設(shè)定T0初始值(高八位)
TL0=0xb0;
}
}
}
void main()
{
TMOD=0x11;//設(shè)定定時(shí)氣的工作方式
TH0=0x3c;//設(shè)定初始值(高八位)
TL0=0xb0;//設(shè)定T0初始值(低五位) 設(shè)定時(shí)間為50ms
ET0=1; //內(nèi)部中斷
EA=1; //打開總中斷
TR0=1; //啟動(dòng)定時(shí)器0
while(1) // while循環(huán)
{
Count(); //計(jì)算
DigDisplay(); //延時(shí)
KEY();
}
}
void time1() interrupt 1
{
uchar m;
TH0=0x3c;
TL0=0xb0; //重新賦初值
m++; //50ms m++
if(m==20)
{ // 每50ms執(zhí)行20次也就是一秒
m=0;
sec++; //每一秒數(shù)碼管的顯示同步數(shù)值加一
if(sec==10000) //因?yàn)槲覀冿@示只能到9999 所以達(dá)到最大顯示需要清零
{
sec=0;
}
}
}
|
-
捕獲.PNG
(68.14 KB, 下載次數(shù): 96)
下載附件
2021-12-28 17:28 上傳
|