|
- #include "reg51.h"
- #include "intrins.h"
- #define GPIO_DIG P2 //數(shù)據(jù)端
- #define uchar unsigned char //重命名關(guān)鍵字
- #define uint unsigned int //重命名關(guān)鍵字
- sbit K1=P3^0; //開始
- sbit K2=P3^1; //暫停
- sbit K3=P3^2; //復(fù)位
- sbit L1=P1^4; //數(shù)碼管1位選
- sbit L2=P1^5; //數(shù)碼管2位選
- sbit L3=P1^6; //數(shù)碼管3位選
- sbit L4=P1^7; //數(shù)碼管4位選
- sbit beep=P1^0;
- unsigned char code DIG_CODE[10] = {
- 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f};
- //0、1、2、3、4、5、6、7、8、9的顯示碼
- unsigned char code DIG_CODE2[10] = {
- 0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,
- 0xff,0xef};
- //0、1、2、3、4、5、6、7、8、9帶小數(shù)點(diǎn)的顯示碼
- unsigned char DisplayData[4];
- //用來存放要顯示的8位數(shù)的值
- uchar i,m; //定義變量
- uchar min=0;
- long int k=0;
- uint Second_Counts; //定義變量
- void DigDisplay() //數(shù)碼管顯示子函數(shù)
- {
- unsignedint j; //定義變量
- L1=0; //位選
- L2=1;
- L3=1;
- L4=1;
- GPIO_DIG= DisplayData[0]; //發(fā)送段碼
- j= 1000; //掃描間隔時(shí)間設(shè)定
- while(j--);
- L1=1; //消隱
- L2=1;
- L3=1;
- L4=1;
- L1=1;
- L2=0; //位選
- L3=1;
- L4=1;
- GPIO_DIG= DisplayData[1]; //發(fā)送段碼
- j= 1000; //掃描間隔時(shí)間設(shè)定
- while(j--);
- L1=1; //消隱
- L2=1;
- L3=1;
- L4=1;
- L1=1;
- L2=1;
- L3=0; //位選
- L4=1;
- GPIO_DIG= DisplayData[2]; //發(fā)送段碼
- j= 1000; //掃描間隔時(shí)間設(shè)定
- while(j--);
- L1=1; //消隱
- L2=1;
- L3=1;
- L4=1;
- L1=1;
- L2=1;
- L3=1;
- L4=0; //位選
- GPIO_DIG= DisplayData[3]; //發(fā)送段碼
- j= 1000; //掃描間隔時(shí)間設(shè)定
- while(j--);
- L1=1; //消隱
- L2=1;
- L3=1;
- L4=1;
- }
- void main()
- {
- // Second_Counts=570;
- // min=9;
- GPIO_DIG= 0x00; //初始化數(shù)據(jù)口
- TMOD=0x01; //定時(shí)器0方式1
- TH0=(65536-50000)/256; //定時(shí)器0:50ms
- TL0=(65536-50000)%256;
- TH1=(65536-2000)/256;
- TL1=(65536-2000)%256;
- EA=1;
- // ET1=1;
- // TR1=1;
- while (1)
- {
- if(K1==0)
- {
- ET0=1;
- TR0=1; //開啟定時(shí)器
- }
- if(K2==0)
- {
- ET0=0;
- TR0=0; //關(guān)閉定時(shí)器
- }
- if(K3==0) //復(fù)位按鍵按下時(shí)
- {
- ET0=0;
- TR0=0; //關(guān)閉定時(shí)器
- DisplayData[0]= DIG_CODE2[0]; //清零顯示
- DisplayData[1]= DIG_CODE[0]; //清零顯示
- DisplayData[2]= DIG_CODE2[0]; //清零顯示
- DisplayData[3]= DIG_CODE[0]; //清零顯示
- i=0; //清零計(jì)數(shù)
- Second_Counts=0; //清零秒
- }
- if((Second_Counts>0)&&(m==0))
- {
- ET1=1;
- TR1=1;
- }
- if((min==9)&&(Second_Counts==599))
- {
- ET0=0;
- TR0=0; //關(guān)閉定時(shí)器
- ET1=1;
- TR1=1;
- }
- DigDisplay(); //執(zhí)行數(shù)碼管顯示函數(shù)
- }
- }
- //T0中斷函數(shù)
- void DSY_Refresh() interrupt 1
- {
- TH0=(65536-50000)/256; //恢復(fù)定時(shí)器0初值
- TL0=(65536-50000)%256;
- i++;
- m++;
- if(m>=20)
- {
- m=0;
- }
- if(i==2) //50ms*2=0.1s轉(zhuǎn)換狀態(tài)
- {
- i=0;
- Second_Counts++;
- if(Second_Counts>=600)
- {
- Second_Counts=0;
- min++;
- }
- DisplayData[0]= DIG_CODE2[min%10]; //顯示百位
- DisplayData[1]= DIG_CODE[Second_Counts/100%10]; //顯示十位
- DisplayData[2]= DIG_CODE2[Second_Counts/10%10]; //顯示個(gè)位
- DisplayData[3]= DIG_CODE[Second_Counts%10]; //顯示小數(shù)點(diǎn)后一位
- // if(Second_Counts==10000)Second_Counts=0; //當(dāng)秒計(jì)數(shù)到1000秒又變?yōu)?
- }
- }
- void DSY_Refresh2() interrupt 3
- {
- TH1=(65536-2000)/256;
- TL1=(65536-2000)%256;
- k++;
- beep=!beep;
- if(k>=300)
- {
- ET1=0;
- TR1=0;
- k=0;
- }
- }
復(fù)制代碼
|
|