99S單鍵秒表,正計時,單鍵實現啟動、暫停、歸零。帶Proteus仿真
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (13.5 KB, 下載次數: 73)
下載附件
2018-9-13 16:34 上傳
0.png (6.87 KB, 下載次數: 77)
下載附件
2018-9-13 16:34 上傳
單片機源程序如下:
- /* 名稱:10s 的秒表 說明:首次按鍵計時開始,再次按鍵暫停,第三次按鍵清零。*/
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit K1=P3^7;
- uchar i,Second_Counts,Key_num;
- bit Key_State;
- uchar
- DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void DelayMS(uint ms) //延時
- {
- uchar t;
- while(ms--) for(t=0;t<120;t++);
- }
- void Key_Event() //處理按鍵事件
- {
- if(Key_State==0)
- {
- Key_num=(Key_num+1)%3;
- switch(Key_num)
- {
- case 1: EA=1;ET0=1;TR0=1;break;
- case 2: EA=0;ET0=0;TR0=0;break;
- case 0: P0=0x3f;P2=0x3f;i=0;Second_Counts=3;
- }
- }
- }
- void main() //主程序
- {
- P0=0x3f; //顯示00
- P2=0x3f;
- i=0;
- Second_Counts=0;
- Key_num=0; //按鍵次數(取值0,1,2,3)
- Key_State=1; //按鍵狀態
- TMOD=0x01; //定時器0 方式1
- TH0=(65536-50000)/256; //定時器0:15ms
- TL0=(65536-50000)%256;
- while(1)
- {
- if(Key_State!=K1)
- {
- DelayMS(10);
- Key_State=K1;
- Key_Event();
- }
- }
- }
- void DSY_Refresh() interrupt 1 //T0 中斷函數
- {
- TH0=(65536-50000)/256; //恢復定時器0 初值
- TL0=(65536-50000)%256;
- if(++i==20) //50ms*2=0.1s 轉換狀態
- {
- i=0;
- Second_Counts++;
- P0=DSY_CODE[Second_Counts/10];
- P2=DSY_CODE[Second_Counts%10];
- if(Second_Counts==100) Second_Counts=0; //滿100(10s)后顯示00
- }
- }
復制代碼
所有資料51hei提供下載:
99單鍵秒表.rar
(22.37 KB, 下載次數: 28)
2018-9-13 08:23 上傳
點擊文件名下載附件
99單鍵秒表C源碼 下載積分: 黑幣 -5
|