新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 一個(gè)很好的單片機(jī)按鍵處理子程序

一個(gè)很好的單片機(jī)按鍵處理子程序

作者: 時(shí)間:2016-11-30 來(lái)源:網(wǎng)絡(luò) 收藏
這是一個(gè)用于實(shí)際產(chǎn)品的stc單片機(jī)按鍵處理程序頭文件和原理圖可從原帖下載:http://www.51hei.com/bbs/dpj-19294-1.html
#include "STC12C5620AD.H"#include "Key_Scan.H"#include "Key_Handle.H"#include "DS1302.H"#include "Radio.H"#include "Channel_Handle.H"#include "Volume_Handle.H"/********************** 聲明外部變量**************************/extern uchar Volume;			//Volume_Handle.cextern uint PLL;  				//From Radio.cextern bit Flag_40ms;	  		//From Main.cextern uchar Work_Mode;   		//From Main.cextern uchar data Sec;			//From DS1302.cextern uchar data Min;		    //From DS1302.cextern uchar data Hour;			//From DS1302.cextern uchar data Date;			//From DS1302.cextern uchar data Month;		//From DS1302.cextern uchar data Day;		    //From DS1302.cextern uchar data Year;			//From DS1302.cextern uchar Display_Channel[];			//From Main.c/********************** 定義變量**************************/uchar Radio_Index;					 	//預(yù)置電臺(tái)切換計(jì)數(shù),0-15,8用于記錄最后一次的電臺(tái)uchar Key_Flag=0x00;   			 		//按鍵計(jì)時(shí),有按鍵時(shí)清零,無(wú)按鍵10S左右使Woke_Mode=0;/********************** 按鍵處理**************************/void Key_Handle(){uchar Time_Write_Temp;			  		 		//調(diào)整用的緩存uchar Key_Value;Radio_Index=EEPROM_Get_Index();	 				//獲取頻道序號(hào)Display_Channel[3]=(Radio_Index/10)|0x30;			 Display_Channel[4]=(Radio_Index%10)|0x30;		//顯示頻道序號(hào)if(Flag_40ms){Key_Value=Key_Scan();			   		//40ms進(jìn)行一次掃描Flag_40ms=0;Key_Flag++;if(Key_Flag>=250){Key_Flag=250;LCD_BLK=0;Work_Mode=0;					  //無(wú)按鍵10s后,自動(dòng)回到模式0}elseLCD_BLK=1;}switch (Key_Value){case 0x0e:		                  //按下MENUswitch (Work_Mode){case 0:Work_Mode=1;	  //進(jìn)入電臺(tái)搜索break;case 1:Work_Mode=2;	  //進(jìn)入調(diào)節(jié)年份break;case 2:Work_Mode=3;	  //進(jìn)入調(diào)節(jié)月份break;case 3:Work_Mode=4;	  //進(jìn)入調(diào)節(jié)日break;case 4:Work_Mode=5;	  //進(jìn)入調(diào)節(jié)星期break;case 5:Work_Mode=6;	  //進(jìn)入調(diào)節(jié)時(shí)break;case 6:Work_Mode=7;	  //進(jìn)入調(diào)節(jié)分break;case 7:DS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x80,0x00);      //秒置0DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)Work_Mode=0;	  //回到正常顯示break;default:break;}Key_Flag=0;  //清除按鍵計(jì)時(shí)break;case 0x0d:		 //按下|<>4)*10+(Year&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==0){Time_Write_Temp=99;}else{Time_Write_Temp--;}Year=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x8c,Year);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 3:				 //月減少 Time_Write_Temp=(Month>>4)*10+(Month&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==1){Time_Write_Temp=12;}else{Time_Write_Temp--;}Month=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x88,Month);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 4:				 //日減少 Time_Write_Temp=(Date>>4)*10+(Date&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==1){Time_Write_Temp=31;}else{Time_Write_Temp--;}Date=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x86,Date);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 5:				 //星期減少 Time_Write_Temp=(Day>>4)*10+(Day&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==1){Time_Write_Temp=7;}else{Time_Write_Temp--;}Day=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x8a,Day);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 6:				 //時(shí)減少 Time_Write_Temp=(Hour>>4)*10+(Hour&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==0){Time_Write_Temp=23;}else{Time_Write_Temp--;}Hour=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x84,Hour);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 7:				 //分減少 Time_Write_Temp=(Min>>4)*10+(Min&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==0){Time_Write_Temp=59;}else{Time_Write_Temp--;}Min=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x82,Min);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 8:             //音量調(diào)節(jié)Volume+=5;		   		     	//音量減小5dBif(Volume>=35)Volume=35;Volume_Write(Volume);			//寫(xiě)入聲音參數(shù)break;default:break;}Key_Flag=0;  //清除按鍵計(jì)時(shí)break;case 0x0b:		 //按下>>|switch (Work_Mode){case 0:				 //正常狀態(tài)Radio_Index++;	    //下一個(gè)預(yù)置電臺(tái)if(Radio_Index==16)Radio_Index=1;EEPROM_Write_Index(Radio_Index);  	  //更新新電臺(tái)序號(hào)PLL=EEPROM_Get_PLL(Radio_Index);	  //更新新電臺(tái)PLLTEA5767_Update_PLL();			  //寫(xiě)入PLLbreak;case 1:TEA5767_User_Search(1);	  //向上手動(dòng)搜索break;case 2:				 //年增加 Time_Write_Temp=(Year>>4)*10+(Year&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==99){Time_Write_Temp=0;}else{Time_Write_Temp++;}Year=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x8c,Year);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 3:				 //月增加 Time_Write_Temp=(Month>>4)*10+(Month&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==12){Time_Write_Temp=1;}else{Time_Write_Temp++;}Month=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x88,Month);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 4:				 //日增加 Time_Write_Temp=(Date>>4)*10+(Date&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==31){Time_Write_Temp=1;}else{Time_Write_Temp++;}Date=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x86,Date);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 5:				 //星期增加 Time_Write_Temp=(Day>>4)*10+(Day&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==7){Time_Write_Temp=1;}else{Time_Write_Temp++;}Day=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x8a,Day);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 6:				 //時(shí)增加 Time_Write_Temp=(Hour>>4)*10+(Hour&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==23){Time_Write_Temp=0;}else{Time_Write_Temp++;}Hour=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x84,Hour);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 7:				 //分增加 Time_Write_Temp=(Min>>4)*10+(Min&0x0f);	 //裝換成十進(jìn)制數(shù)if(Time_Write_Temp==59){Time_Write_Temp=0;}else{Time_Write_Temp++;}Min=(Time_Write_Temp/10)<<4|(Time_Write_Temp%10);	 //裝換成BCDDS1302_Write_Data(0x8e,0x00);      // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x82,Min);           //寫(xiě)入DS1302_Write_Data(0x8e,0x80);      // 開(kāi)啟寫(xiě)保護(hù)break;case 8:             //音量調(diào)節(jié)if(Volume<=0)Volume=0;				elseVolume-=5;				    //音量增加5dBVolume_Write(Volume);			//寫(xiě)入聲音參數(shù)break;default:break;}Key_Flag=0;  //清除按鍵計(jì)時(shí)break;case 0x07:		 //按下SET{	switch (Work_Mode){case 0:		  //正常狀態(tài)Work_Mode=8;			//轉(zhuǎn)到音量調(diào)節(jié)狀態(tài)break;case 1:		  //電臺(tái)搜索EEPROM_Save_PLL(Radio_Index,PLL);    //存入新收到的電臺(tái)Work_Mode=0;break;case 8:		  //音量調(diào)節(jié)狀態(tài)Work_Mode=0;			//轉(zhuǎn)到正常狀態(tài)break;default:DS1302_Write_Data(0x8e,0x00);        // 禁止寫(xiě)保護(hù)DS1302_Write_Data(0x80,0x00); 	     //置秒為00,并啟動(dòng)時(shí)鐘DS1302_Write_Data(0x8e,0x80);        // 開(kāi)啟寫(xiě)保護(hù)Work_Mode=0;break;}								}Key_Flag=0;  //清除按鍵計(jì)時(shí)break;default:break;}		}


評(píng)論


技術(shù)專區(qū)

關(guān)閉