|
求助!我要做個(gè)10s的秒表。有兩個(gè)按鍵,一個(gè)按鍵是電源,一個(gè)是開關(guān)。
當(dāng)按下開關(guān)時(shí),前提電源開關(guān)也按下了,數(shù)碼管開始走動,再按下開關(guān)會暫停定時(shí)器,再按清零(已實(shí)現(xiàn))。
關(guān)鍵是電源另一個(gè)功能。當(dāng)再按下電源開關(guān)時(shí),數(shù)碼管熄滅,定時(shí)器停止。(未實(shí)現(xiàn))
有沒有大神能夠告訴我怎么實(shí)現(xiàn)這個(gè)功能,關(guān)系到我掛不掛科的事情 ......
QQ截圖20171201113034.png (68.01 KB, 下載次數(shù): 47)
下載附件
2017-12-1 11:35 上傳
單片機(jī)代碼如下:
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code Dsy_Code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- sbit Key_Start = P2^2;
- sbit Key_Power=P2^3;
- sbit dp = P0^7; //小數(shù)點(diǎn)
- uchar num=0; //定時(shí)器的計(jì)數(shù)
- uchar count=0;//數(shù)碼管的計(jì)數(shù)
- uchar Key_Num=0;//按鍵次數(shù)的計(jì)數(shù)
- uchar Power_Num=0;
- bit Key_State=1,Power_State=1;
- void DelayMS(uint x){
- uchar i;
- while(x--)
- for(i=0;i<120;i++);
- }
- void Dsy(){ //顯示函數(shù)
- P2=0xfd;
- P0=Dsy_Code[num%10];
- DelayMS(2);
- P2=0xfe;
- P0=Dsy_Code[num/10];
- dp=1;
- DelayMS(2);
- }
- void Timer()interrupt 1{ //定時(shí)器
- TH0=(65536-50000)/256; //50ms
- TL0=(65536-50000)%256;
- count++;
- if(count==2){
- count=0;
- num++;
- if(num==100)
- num=0;
- }
- }
- void Key_Handle(){//按鍵處理
- if(Key_State==0){
- Key_Num=(Key_Num+1)%3;
- switch(Key_Num){
- case 1:IE=0x82;TR0=1;break;
- case 2:IE=0x00;TR0=0;break;
- case 0:IE=0x00;TR0=0;num=0;break;
- }
- }
- }
- void main(){
- TMOD=0x01;
- TH0=(65536-50000)/256; //10ms
- TL0=(65536-50000)%256;
- if(Power_State!=Key_Power){
- DelayMS(10);
- if(Power_State!=Key_Power){
- Power_Num++;
- if(Power_Num%2==1){
- while(1){
- if(Key_State!=Key_Start){
- DelayMS(10);
- Key_State=Key_Start;
- Key_Handle();
- }
- Dsy();
- }
- }
- }
- }
- }
復(fù)制代碼
|
|