基于STC89C52單片機(jī)的數(shù)字時(shí)鐘設(shè)計(jì)(附電路圖及源程序)_第1頁(yè)
基于STC89C52單片機(jī)的數(shù)字時(shí)鐘設(shè)計(jì)(附電路圖及源程序)_第2頁(yè)
基于STC89C52單片機(jī)的數(shù)字時(shí)鐘設(shè)計(jì)(附電路圖及源程序)_第3頁(yè)
基于STC89C52單片機(jī)的數(shù)字時(shí)鐘設(shè)計(jì)(附電路圖及源程序)_第4頁(yè)
基于STC89C52單片機(jī)的數(shù)字時(shí)鐘設(shè)計(jì)(附電路圖及源程序)_第5頁(yè)
已閱讀5頁(yè),還剩67頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

PAGE43-第九屆長(zhǎng)通杯電子設(shè)計(jì)大賽論文基于STC89C52單片機(jī)的數(shù)字時(shí)鐘設(shè)計(jì)組長(zhǎng):組員:聯(lián)系方式:目錄1、系統(tǒng)方案 -3-1.1、方案比較與選擇 -3-1.1.1、單片機(jī)選擇與論證 -3-1.1.2、穩(wěn)壓電源選擇與論證 -3-1.1.3、時(shí)鐘芯片選擇與論證 -3-1.1.4、感溫器件選擇與論證 -4-1.1.5、顯示器件選擇與論證 -4-1.1.6、鍵盤形式選擇與論證 -4-2、電路與程序設(shè)計(jì) -4-2.1、電路的設(shè)計(jì) -4-2.1.1、總體框圖設(shè)計(jì) -4-2.1.2、單片機(jī)最小系統(tǒng)電路 -5-2.1.3、穩(wěn)壓電源電路 -5-2.1.4、時(shí)鐘芯片電路 -6-2.1.5、按鍵電路 -7-2.1.6、蜂鳴器電路 -7-2.1.7、感溫電路 -7-2.1.8、顯示電路 -7-2.2、程序設(shè)計(jì) -8-2.2.1、DS1302程序流程圖 -8-2.2.2、液晶顯示子程序流程圖 -9-附錄 -10-附錄1:電路原理圖 -10-附錄2:源程序 -11-1、系統(tǒng)方案1.1、方案比較與選擇1.1.1、單片機(jī)選擇與論證方案一:SST單片機(jī)。SST單片機(jī)省去了開發(fā)用的編程器和仿真器。省去了外部EEPROM、電壓監(jiān)控芯片、上電復(fù)位電路和5個(gè)UART的擴(kuò)展芯片。它自帶在線下載和在線仿真功能,帶有5路PCA模塊(主成分分析),PCA功能比普通單片機(jī)有更強(qiáng)的計(jì)時(shí)性。利用PCA的脈沖寬度調(diào)制(PWM)模式可產(chǎn)生一個(gè)8位PWM。方案二:STC89C52是一種低功耗、高性能CMOS8位微控制器,具有8K在系統(tǒng)可編程Flash存儲(chǔ)器。在單芯片上,擁有靈巧的8位CPU和在系統(tǒng)可編程Flash,使得STC89C52為眾多嵌入式控制應(yīng)用系統(tǒng)提供高靈活、超有效的解決方案。具有以下標(biāo)準(zhǔn)功能:8k字節(jié)Flash,512字節(jié)RAM,32位I/O口線,看門狗定時(shí)器,內(nèi)置4KBEEPROM,MAX810復(fù)位電路,三個(gè)16位定時(shí)器/計(jì)數(shù)器,一個(gè)6向量2級(jí)中斷結(jié)構(gòu),全雙工串行口?;谝陨媳容^,本設(shè)計(jì)選用方案二。1.1.2、穩(wěn)壓電源選擇與論證方案一:采用集成多路輸出電源模塊。集成多路輸出電源模塊具有制作簡(jiǎn)單,使用方便等特點(diǎn),但臨時(shí)短時(shí)間內(nèi)要購(gòu)買比較困難,而且價(jià)格較貴,電源組合方式固定不易選擇。方案二:采用LM7805穩(wěn)壓電路。LM7805是我們最常用到的穩(wěn)壓芯片,它的使用方便,用很簡(jiǎn)單的電路即可以輸入一個(gè)直流穩(wěn)壓電源,而且它的輸出電壓恰好為5V,是51系列單片機(jī)運(yùn)行所需的電壓??紤]到設(shè)計(jì)成本,本設(shè)計(jì)選用方案二。1.1.3、時(shí)鐘芯片選擇與論證方案一:是一款低功耗,具有56字節(jié)非失性RAM的全BCD碼時(shí)鐘日歷實(shí)時(shí)時(shí)鐘芯片,地址和數(shù)據(jù)通過(guò)兩線雙向的串行總線的傳輸,芯片可以提供秒,分,小時(shí)等信息,每一個(gè)月的天數(shù)能自動(dòng)調(diào)整。并且有閏年補(bǔ)償功能。AM/PM標(biāo)志位決定時(shí)鐘工作于24小時(shí)或12小時(shí)模式,芯片有一個(gè)內(nèi)置的電源感應(yīng)電路,具有掉電檢測(cè)和電池切換功能。方案二:DS1302是美國(guó)DALLAS公司推出的一種高性能、低功耗、帶RAM的實(shí)時(shí)時(shí)鐘電路,它可以對(duì)年、月、日、周日、時(shí)、分、秒進(jìn)行計(jì)時(shí),具有閏年補(bǔ)償功能,工作電壓為2.5V~5.5V。采用三線接口與CPU進(jìn)行同步通信,并可采用突發(fā)方式一次傳送多個(gè)字節(jié)的時(shí)鐘信號(hào)或RAM數(shù)據(jù)。DS1302內(nèi)部有一個(gè)31×8的用于臨時(shí)性存放數(shù)據(jù)的RAM寄存器。DS1302是DS1202的升級(jí)產(chǎn)品,與DS1202兼容,但增加了主電源/后備電源雙電源引腳,同時(shí)提供了對(duì)后備電源進(jìn)行涓細(xì)電流充電的能力??紤]到時(shí)鐘應(yīng)該具有掉電保護(hù)功能,因此本設(shè)計(jì)選用方案二。1.1.4、感溫器件選擇與論證采用DS18B20數(shù)字溫度傳感器。DS18B20數(shù)字溫度傳感器接線方便,封裝成后可應(yīng)用于多種場(chǎng)合,如管道式,螺紋式,磁鐵吸附式,不銹鋼封裝式,型號(hào)多種多樣,有LTM8877,LTM8874等等。封裝后的DS18B20耐磨耐碰,體積小,使用方便,封裝形式多樣,適用于各種狹小空間設(shè)備數(shù)字測(cè)溫和控制領(lǐng)域。故本設(shè)計(jì)采用DS18B20數(shù)字溫度傳感器作為感溫模塊。1.1.5、顯示器件選擇與論證方案一:數(shù)碼管。數(shù)碼管顯示能在低電壓、小電流條件下驅(qū)動(dòng)發(fā)光,能與CMOS、ITL電路兼容。發(fā)光響應(yīng)時(shí)間極短(<0.1μs),高頻特性好,單色性好,亮度高。體積小,重量輕,抗沖擊性能好。壽命長(zhǎng),使用壽命在10萬(wàn)小時(shí)以上,甚至可達(dá)100萬(wàn)小時(shí)。但顯示功能有限,只能顯示數(shù)字和個(gè)別字母。方案二:液晶顯示器。液晶顯示器每一個(gè)點(diǎn)在收到信號(hào)后就一直保持那種色彩和亮度,恒定發(fā)光,顯示質(zhì)量高。其接口都是數(shù)字式的,和單片機(jī)系統(tǒng)的接口更加簡(jiǎn)單可靠,操作更加方便。功耗主要消耗在其內(nèi)部的電極和驅(qū)動(dòng)IC上,因而耗電量比其它顯示器要少得多。基于對(duì)顯示器功能的穩(wěn)定性和齊全性考慮,我們采用方案二。1.1.6、鍵盤形式選擇與論證方案一:獨(dú)立式按鍵。獨(dú)立按鍵的輸入線較多,結(jié)構(gòu)復(fù)雜,一般適用于按鍵較少,操作速度較高的場(chǎng)合。方案二:行列式鍵盤。行列式鍵盤由行和列線交叉組成,一般適用于按鍵較多的場(chǎng)合。使用4×4矩形鍵盤,這樣鍵盤模塊僅使用8根線與處理器相連,節(jié)省了系統(tǒng)資源。由于我們功能比較少,矩形鍵盤占用電路板位置較大,因此選擇方案一。2、電路與程序設(shè)計(jì)2.1、電路的設(shè)計(jì)2.1.1、總體框圖設(shè)計(jì)系統(tǒng)總體框圖如圖1所示。圖1系統(tǒng)總體框圖2.1.2、單片機(jī)最小系統(tǒng)電路宏晶公司的STC82C52是51內(nèi)核的單片機(jī)。完全兼容51系列的單片機(jī),具有豐富的I/O口資源。并且支持在系統(tǒng)下載,不用燒寫器就可以給單片機(jī)下載程序,給調(diào)試帶來(lái)了很大的方便。因此控制模塊是以STC89C52單片機(jī)為核心的一塊最小系統(tǒng)板,如下圖2所示。圖2控制模塊原理圖2.1.3、穩(wěn)壓電源電路電源模塊采用LM7805穩(wěn)壓電路,如下圖3所示。圖3穩(wěn)壓電路原理圖2.1.4、時(shí)鐘芯片電路DS1302時(shí)鐘芯片是美國(guó)DALLAS公司推出的具有涓細(xì)電流充電能力的低功耗實(shí)時(shí)芯片,它可以對(duì)年、月、日、周、日、時(shí)、分、秒進(jìn)行計(jì)時(shí),且具有閏年補(bǔ)償?shù)榷喾N功能,其引腳圖如下圖4所示。圖4DS1302時(shí)鐘芯片2.1.5、按鍵電路此電路實(shí)現(xiàn)四種不同功能的轉(zhuǎn)換。按鍵一實(shí)現(xiàn)菜單功能,按鍵二實(shí)現(xiàn)加一功能,按鍵三實(shí)現(xiàn)減一功能,按鍵四實(shí)現(xiàn)始確定功能。電路圖如圖5。圖5按鍵電路2.1.6、蜂鳴器電路當(dāng)整點(diǎn)及鬧鐘到點(diǎn)時(shí),蜂鳴器響。電路圖如圖6。圖6蜂鳴器連接電路2.1.7、感溫電路DS18B20數(shù)字溫度傳感器接線方便,封裝成后可應(yīng)用于多種場(chǎng)合。如下圖7所示:圖7感溫原理圖2.1.8、顯示電路顯示模塊采用的是LCD1602液晶顯示,硬件原理實(shí)現(xiàn)如圖8所示。圖8顯示模塊原理圖2.2、程序設(shè)計(jì)2.2.1、DS1302程序流程圖圖9DS1302流程圖2.2.2、液晶顯示子程序流程圖開始開始初始化LCD顯示內(nèi)容寫入緩存LCD控制器啟動(dòng)延時(shí)置DISP為高電平液晶屏正常顯示置DISP為低電平延時(shí)LCD控制器關(guān)閉液晶屏無(wú)顯示圖11液晶顯示流程圖附錄附錄1:電路原理圖附錄2:源程序#include<at89x52.h> #include<intrins.h> typedefunsignedcharuchar;typedefunsignedintuint;#defineLCD1602_DATAP0//1602液晶數(shù)據(jù)口sbitLCD1602_RS=P1^1;sbitLCD1602_RW=P1^2;sbitLCD1602_EN=P1^3;//1602液晶使能控制sbitDS1302_CLK=P1^4;//實(shí)時(shí)時(shí)鐘時(shí)鐘線引腳sbitDS1302_IO=P1^5;//實(shí)時(shí)時(shí)鐘數(shù)據(jù)線引腳sbitDS1302_RST=P1^6;//實(shí)時(shí)時(shí)鐘復(fù)位線引腳sbitDQ=P1^0;//18B20接口sbitBeepIO=P2^3;//蜂鳴器sbitesc=P2^1;//取消播放音樂sbitmode=P3^4;//模式鍵sbitinc=P3^5;//增加鍵sbitdec=P3^6;//減少鍵sbitok=P3^7;//OK鍵sbitlight=P3^2;//背光控制sbitACC0=ACC^0;//累加器位定義sbitACC7=ACC^7;bitfirst_flag=1,//一級(jí)主界面顯示標(biāo)志second_flag,//二級(jí)菜單顯示標(biāo)志third_flag,//三級(jí)菜單顯示標(biāo)志playmusic,//播放音樂的標(biāo)志light_flag1=1,//背光第一次計(jì)時(shí)標(biāo)志light_flag=1;//二三級(jí)界面開背光標(biāo)志uinttemp1,temp2,temp2t,temp3,temp4,ser;ucharcount1,sec1,min1,hour1;//定時(shí)器2使用的變量停表用ucharcount,sec,min;//定時(shí)器1使用的變量貪睡用uinttemp;//溫度暫存//////以下為播放音樂的設(shè)置#defineSYSTEM_OSC12000000#defineSOUND_SPACE4/5uintcodeFreTab[12]={262,277,294,311,330,349,369,392,415,440,466,494};//原始頻率表ucharcodeSignTab[7]={0,2,4,5,7,9,11};//1~7在頻率表中的位置ucharcodeLengthTab[7]={1,2,4,8,16,32,64}; ucharSound_Temp_TH0,Sound_Temp_TL0;//音符定時(shí)器初值暫存ucharSound_Temp_TH1,Sound_Temp_TL1;//音長(zhǎng)定時(shí)器初值暫存bitstop;charmenu_num;//菜單號(hào)charnum;//按鍵加減調(diào)整暫存數(shù)charposition1,position2,position3,position4,position5,position6,position7;//菜單1-7的光標(biāo)位置標(biāo)志charcodemenu[][16]={"1.TimeSetting","2.AlarmSetting","3.AlarmMusic","4.Snooze","5.BackLight","6.StopWatch","7.LowPowerMode","8.Exit"};charcodeon_off_flag[][4]={"OFF","ON"};charcodealarm_times[][7]={"Once","Repeat"};charcodeweek[][4]={"MON","TUE","WED","THU","FRI","SAT","SUN"};ucharcodeself_table[]={0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,//年00x0F,0x09,0x0F,0x09,0x0F,0x09,0x13,0x00,//月10x1f,0x11,0x11,0x1f,0x11,0x11,0x1f,0x00,//日20x01,0x0c,0x17,0x14,0x17,0x0c,0x01,0x00,//鬧鐘標(biāo)志 30x17,0x04,0x04,0x07,0x00,0x00,0x00,0x00,//攝氏度標(biāo)志 40x1f,0x15,0x0a,0x04,0x0a,0x15,0x1f,0x00}; //貪睡標(biāo)志5/***********************延時(shí)函數(shù)(單位ms)**************************/voiddelay(uintz){uintx;uchary;for(x=z;x>0;x--)for(y=112;y>0;y--);}/******************LCD1602驅(qū)動(dòng)程序******************//******忙檢測(cè)*********/voidLCD1602_check_busy(){LCD1602_DATA=0xff;LCD1602_RS=0; LCD1602_RW=1; LCD1602_EN=1; while(LCD1602_DATA&0x80); LCD1602_EN=0; }/********************寫指令********************/voidLCD1602_write_cmd(ucharcmd){LCD1602_check_busy(); LCD1602_RS=0; LCD1602_RW=0; LCD1602_DATA=cmd; LCD1602_EN=1;_nop_();LCD1602_EN=0;}/*******************寫數(shù)據(jù)**********************/voidLCD1602_write_data(uchardat){LCD1602_check_busy(); LCD1602_RS=1; LCD1602_RW=0; LCD1602_DATA=dat; LCD1602_EN=1;_nop_(); LCD1602_EN=0;}/*************************1602初始化*************************/voidLCD1602_init(){/****************寫入自定義字符***************************/uchari;LCD1602_write_cmd(0x40);//CGRAM起始地址for(i=0;i<48;i++)LCD1602_write_data(self_table[i]);//寫入6個(gè)自定義字符LCD1602_write_cmd(0x38);//8位數(shù)據(jù),雙列顯示,5*7字形LCD1602_write_cmd(0x0c);//開顯示,關(guān)光標(biāo),光標(biāo)不閃爍LCD1602_write_cmd(0x01);//清屏}/**************************設(shè)置顯示坐標(biāo)***************************/voidLCD1602_set_postion(ucharx,uchary){if(!y)LCD1602_write_cmd(0x80+x);elseif(y)LCD1602_write_cmd(0xc0+x);}/***********************指定位置寫字符************************/voidLCD1602_write_char(ucharx,uchary,ucharchardata){LCD1602_set_postion(x,y);LCD1602_write_data(chardata);}/***********************指定位置寫字符串************************/voidLCD1602_write_string(ucharx,uchary,uchar*string){LCD1602_set_postion(x,y);while((*string)!='\0'){ LCD1602_write_data(*string);string++;}}/******************DS1302驅(qū)動(dòng)程序*******************/voidDS1302InputByte(unsignedchard)//實(shí)時(shí)時(shí)鐘寫入一字節(jié)(內(nèi)部函數(shù)){unsignedchari;ACC=d;for(i=8;i>0;i--){DS1302_IO=ACC0;//相當(dāng)于匯編中的RRCDS1302_CLK=1;DS1302_CLK=0;ACC=ACC>>1;}}unsignedcharDS1302OutputByte(void)//實(shí)時(shí)時(shí)鐘讀取一字節(jié)(內(nèi)部函數(shù)){unsignedchari;for(i=8;i>0;i--){ACC=ACC>>1;//相當(dāng)于匯編中的RRCACC7=DS1302_IO;DS1302_CLK=1;DS1302_CLK=0;}return(ACC);}voidWrite1302(unsignedcharucAddr,unsignedcharucDa)//ucAddr:DS1302地址,ucData:要寫的數(shù)據(jù){DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1;DS1302InputByte(ucAddr);//地址,命令DS1302InputByte(ucDa);//寫1Byte數(shù)據(jù)DS1302_CLK=1;DS1302_RST=0;}unsignedcharRead1302(unsignedcharucAddr)//讀取DS1302某地址的數(shù)據(jù){unsignedcharucData;DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1;DS1302InputByte(ucAddr|0x01);//地址,命令ucData=DS1302OutputByte();//讀1Byte數(shù)據(jù)DS1302_CLK=1;DS1302_RST=0;return(ucData);}voidDS1302_init(){if(Read1302(0xc1)!=0xf0) {Write1302(0x8e,0x00);//允許寫操作Write1302(0xc0,0xf0);//寫入初始化標(biāo)志,系統(tǒng)上電后檢測(cè)此標(biāo)志,即此子函數(shù)只會(huì)在第一次初始化一次。/******時(shí)間初始值******/Write1302(0x8c,0x10);//年Write1302(0x8a,0x03);//星期Write1302(0x88,0x05);//月Write1302(0x86,0x26);//日Write1302(0x84,0x22);//時(shí)Write1302(0x82,0x59);//分Write1302(0x80,0x55);//秒 Write1302(0xd2,0);//歌曲選擇初始Write1302(0xd4,5);//貪睡初始延時(shí)時(shí)間5分鐘Write1302(0xd6,0);//貪睡初始值關(guān)閉/******鬧鐘初始值******/Write1302(0xc2,0);//十時(shí)Write1302(0xc4,0);//時(shí)Write1302(0xc6,0);//鬧鐘標(biāo)志W(wǎng)rite1302(0xc8,0);//鬧鐘次數(shù)標(biāo)志////第二個(gè)鬧鐘Write1302(0xca,0);//十時(shí)Write1302(0xcc,0);//時(shí)Write1302(0xce,0);//鬧鐘標(biāo)志W(wǎng)rite1302(0xd0,0);//鬧鐘次數(shù)標(biāo)志W(wǎng)rite1302(0xd8,0);//背光控制開關(guān)Write1302(0xda,0x10);//背光延時(shí)時(shí)間Write1302(0x90,0xa4);//涓流充電 Write1302(0x8e,0x80);//禁止寫操作 }}/******************DS18B20驅(qū)動(dòng)程序*******************/voiddelay_18b20(unsignedinti)//延時(shí)函數(shù){while(i--);}//18b20初始化函數(shù)voidInit_DS18B20(void){unsignedcharx=0;DQ=1;//DQ復(fù)位delay_18b20(8);//稍做延時(shí)DQ=0;//單片機(jī)將DQ拉低delay_18b20(80);//精確延時(shí)大于480usDQ=1;//拉高總線delay_18b20(10);x=DQ;//稍做延時(shí)后如果x=0則初始化成功x=1則初始化失敗delay_18b20(5);}//讀一個(gè)字節(jié)unsignedcharReadOneChar(void){unsignedchari=0;unsignedchardat=0;for(i=8;i>0;i--){DQ=0;//給脈沖信號(hào)dat>>=1;DQ=1;//給脈沖信號(hào)if(DQ)dat|=0x80;delay_18b20(5);}return(dat);}//寫一個(gè)字節(jié)voidWriteOneChar(unsignedchardat){unsignedchari=0;for(i=8;i>0;i--){DQ=0;DQ=dat&0x01;delay_18b20(5);DQ=1;dat>>=1;}delay_18b20(5);}//讀取溫度unsignedintReadTemperature(void){unsignedinta=0;unsignedintb=0;unsignedintt=0;floattt;Init_DS18B20();WriteOneChar(0xCC);//跳過(guò)讀序號(hào)列號(hào)的操作WriteOneChar(0x44);//啟動(dòng)溫度轉(zhuǎn)換delay_18b20(200);Init_DS18B20();WriteOneChar(0xCC);//跳過(guò)讀序號(hào)列號(hào)的操作WriteOneChar(0xBE);//讀取溫度寄存器等(共可讀9個(gè)寄存器)前兩個(gè)就是溫度a=ReadOneChar();b=ReadOneChar();t=b;t<<=8;t=t|a;tt=t*0.0625;t=tt*10+0.5;return(t);}/************一級(jí)主界面顯示函數(shù)************/voidfirst_interface(){LCD1602_write_string(0,0,"20");LCD1602_write_data(Read1302(0x8d)/16+0x30);//十年LCD1602_write_data(Read1302(0x8d)%16+0x30);//年LCD1602_write_data(0);//顯示自定義字符“年”LCD1602_write_data(Read1302(0x89)/16+0x30);//十月LCD1602_write_data(Read1302(0x89)%16+0x30);//月LCD1602_write_data(1);//顯示自定義字符“月”LCD1602_write_data(Read1302(0x87)/16+0x30);//十日LCD1602_write_data(Read1302(0x87)%16+0x30);//日LCD1602_write_data(2);//顯示自定義字符“日”if(Read1302(0xd7))LCD1602_write_char(9,1,5);//顯示自定義字符貪睡標(biāo)志elseLCD1602_write_char(9,1,'');LCD1602_write_string(12,0,week[Read1302(0x8b)-1]);//顯示星期if(Read1302(0xc7)||Read1302(0xcf))LCD1602_write_char(10,1,3);//顯示自定義字符鬧鐘標(biāo)志elseLCD1602_write_char(10,1,'');LCD1602_write_char(0,1,Read1302(0x85)/16+0x30);//十時(shí)LCD1602_write_data(Read1302(0x85)%16+0x30);//時(shí)LCD1602_write_data(':');LCD1602_write_char(3,1,Read1302(0x83)/16+0x30);//十分LCD1602_write_data(Read1302(0x83)%16+0x30);//分LCD1602_write_data(':');LCD1602_write_char(6,1,Read1302(0x81)/16+0x30);//十秒LCD1602_write_data(Read1302(0x81)%16+0x30);//秒temp=ReadTemperature();temp=temp;temp1=temp/100;temp2t=temp%100;temp2=temp2t/10;temp3=temp2t%10;temp4=temp3%100/10;LCD1602_write_char(11,1,temp1+0x30);//溫度顯示LCD1602_write_data(temp2+0x30);LCD1602_write_data(0x2E);LCD1602_write_data(temp3+0x30);LCD1602_write_data(4);}/****************定時(shí)器2初始化函數(shù)***********停表用*************/voidinit_timer2(){T2CON=0;T2MOD=0;RCAP2H=(65536-10000)/256;//10msRCAP2L=(65536-10000)%256;TH2=(65536-10000)/256;//10msTL2=(65536-10000)%256; EA=1;ET2=1;TR2=1;}/**************定時(shí)器1初始化函數(shù)*************貪睡用**************/voidinit_timer1(){TMOD=0x10;TH1=(65536-10000)/256;//10msTL1=(65536-10000)%256; EA=1;ET1=1;TR1=1;}/************二級(jí)菜單顯示函數(shù)************/voidsecond_interface(){LCD1602_write_char(0,(menu_num*3)%2,0x7E);//三角形指示符號(hào)LCD1602_write_char(0,!((menu_num*3)%2),'');//清除殘留顯示三角形if(!((menu_num*3)%2))//判斷是否要往液晶寫入顯示,menu_num每跳變2時(shí)寫一次{LCD1602_write_string(1,0,menu[menu_num]);//1602液晶第一行顯示LCD1602_write_string(1,1,menu[menu_num+1]);//1602液晶第二行顯示}elseif((menu_num*3)%2)//防止從三級(jí)界面回來(lái)時(shí)二級(jí)界面不顯示了{(lán)LCD1602_write_string(1,0,menu[menu_num-1]);//1602液晶第一行顯示LCD1602_write_string(1,1,menu[menu_num]);//1602液晶第二行顯示} }/************三級(jí)菜單顯示函數(shù)************/voidthird_interface(){switch(menu_num){case0:{if(!position1)//時(shí)間設(shè)置界面,僅進(jìn)入三級(jí)菜單界面時(shí)刻顯示一下{ LCD1602_write_string(0,0,"20");LCD1602_write_data(Read1302(0x8d)/16+0x30);//十年LCD1602_write_data(Read1302(0x8d)%16+0x30); //年LCD1602_write_data(0);//顯示自定義字符“年”LCD1602_write_data(Read1302(0x89)/16+0x30); //十月LCD1602_write_data(Read1302(0x89)%16+0x30);//月LCD1602_write_data(1);//顯示自定義字符“月”LCD1602_write_data(Read1302(0x87)/16+0x30); //十日LCD1602_write_data(Read1302(0x87)%16+0x30);//日LCD1602_write_data(2);//顯示自定義字符“日” LCD1602_write_string(12,0,week[Read1302(0x8b)-1]);//顯示星期 LCD1602_write_char(0,1,Read1302(0x85)/16+0x30);//十時(shí)LCD1602_write_data(Read1302(0x85)%16+0x30); //時(shí)LCD1602_write_data(':');LCD1602_write_char(3,1,Read1302(0x83)/16+0x30);//十分LCD1602_write_data(Read1302(0x83)%16+0x30); //分LCD1602_write_data(':');LCD1602_write_char(6,1,Read1302(0x81)/16+0x30);//十秒LCD1602_write_data(Read1302(0x81)%16+0x30);//秒LCD1602_write_cmd(0x80+3);//第一次進(jìn)入三級(jí)菜單時(shí)將光標(biāo)放的位置position1=1; }}break;case1:{if(!position2) //鬧鐘設(shè)置界面 {LCD1602_write_char(0,0,Read1302(0xc3)/16+0x30);//十時(shí)LCD1602_write_data(Read1302(0xc3)%16+0x30); //時(shí)LCD1602_write_data(':');LCD1602_write_char(3,0,Read1302(0xc5)/16+0x30);//十分LCD1602_write_data(Read1302(0xc5)%16+0x30); //分LCD1602_write_string(6,0,on_off_flag[Read1302(0xc7)]);//鬧鐘標(biāo)志LCD1602_write_string(10,0,alarm_times[Read1302(0xc9)]);//鬧鐘次數(shù)標(biāo)志////////第二個(gè)鬧鐘LCD1602_write_char(0,1,Read1302(0xcb)/16+0x30);//十時(shí)LCD1602_write_data(Read1302(0xcb)%16+0x30); //時(shí)LCD1602_write_data(':');LCD1602_write_char(3,1,Read1302(0xcd)/16+0x30);//十分LCD1602_write_data(Read1302(0xcd)%16+0x30); //分LCD1602_write_string(6,1,on_off_flag[Read1302(0xcf)]);//鬧鐘標(biāo)志LCD1602_write_string(10,1,alarm_times[Read1302(0xd1)]);//鬧鐘次數(shù)標(biāo)志LCD1602_write_cmd(0x80+1);//第一次進(jìn)入三級(jí)菜單時(shí)將光標(biāo)放的位置position2=1; }}break;case2:{//鬧鈴音樂選擇if(!position3) {LCD1602_write_string(1,0,"AlarmMusicIs");if(Read1302(0xd3))LCD1602_write_string(3,1,"LoveTransfer");elseLCD1602_write_string(3,1,"FairyTale");LCD1602_write_cmd(0xc0+2);position3=1; }}break;case3:{if(!position4) {LCD1602_write_string(0,0,"Snooze:");LCD1602_write_string(13,0,on_off_flag[Read1302(0xd7)]);LCD1602_write_string(0,1,"Delayminutes");LCD1602_write_char(6,1,Read1302(0xd5)/16+0x30);LCD1602_write_data(Read1302(0xd5)%16+0x30);LCD1602_write_cmd(0x80+13);//首次進(jìn)入光標(biāo)保持的位置position4=1; }}break;case4:{//背光控制if(!position5) {LCD1602_write_string(0,0,"LightControl:");LCD1602_write_string(13,0,on_off_flag[Read1302(0xd9)]);LCD1602_write_string(0,1,"KeepSeconds");LCD1602_write_char(5,1,Read1302(0xdb)/16+0x30);LCD1602_write_char(6,1,Read1302(0xdb)%16+0x30);LCD1602_write_cmd(0x80+13);//首次進(jìn)入光標(biāo)保持的位置position5=1; }}break;case5:{if(!position6) //停表 {LCD1602_write_string(3,0,"hmsms");LCD1602_write_char(4,1,':');LCD1602_write_char(7,1,':');LCD1602_write_char(10,1,'.');position6=1; }LCD1602_write_char(2,1,hour1/10+0x30);LCD1602_write_data(hour1%10+0x30);LCD1602_write_char(5,1,min1/10+0x30);LCD1602_write_data(min1%10+0x30);LCD1602_write_char(8,1,sec1/10+0x30);LCD1602_write_data(sec1%10+0x30);LCD1602_write_char(11,1,count1/10+0x30);LCD1602_write_data(count1%10+0x30);}break;case6:{light=1;//關(guān)背光 PCON=2;//進(jìn)入掉電模式}break; }}/************播放音樂函數(shù)************/voidInitialSound(){playmusic=1;//播放音樂標(biāo)志置位BeepIO=0; Sound_Temp_TH1=(65535-(1/1200)*SYSTEM_OSC)/256;//計(jì)算TL1應(yīng)裝入的初值(10ms的初裝值)Sound_Temp_TL1=(65535-(1/1200)*SYSTEM_OSC)%256;//計(jì)算TH1應(yīng)裝入的初值TH1=Sound_Temp_TH1;TL1=Sound_Temp_TL1;TMOD|=0x11;ET0=1;ET1=0;TR0=0;TR1=0;EA=1;}voidBeepTimer0()interrupt1//音符發(fā)生中斷{if(esc==0){delay(50);if(esc==0)stop=1;while(!esc);}BeepIO=!BeepIO;TH0=Sound_Temp_TH0;TL0=Sound_Temp_TL0;}voidPlay(unsignedchar*Sound,unsignedcharSignature,unsignedOctachord,unsignedintSpeed){unsignedintNewFreTab[12];//新的頻率表unsignedchari,j;unsignedintPoint,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength;unsignedcharTone,Length,SL,SH,SM,SLen,XG,FD;for(i=0;i<12;i++)//根據(jù)調(diào)號(hào)及升降八度來(lái)生成新的頻率表{j=i+Signature;if(j>11) {j=j-12;NewFreTab[i]=FreTab[j]*2; }elseNewFreTab[i]=FreTab[j];if(Octachord==1)NewFreTab[i]>>=2;elseif(Octachord==3)NewFreTab[i]<<=2;} SoundLength=0;while(Sound[SoundLength]!=0x00)//計(jì)算歌曲長(zhǎng)度{SoundLength+=2;}Point=0;Tone=Sound[Point]; Length=Sound[Point+1];//讀出第一個(gè)音符和它時(shí)時(shí)值LDiv0=12000/Speed;//算出1分音符的長(zhǎng)度(幾個(gè)10ms) LDiv4=LDiv0/4;//算出4分音符的長(zhǎng)度LDiv4=LDiv4-LDiv4*SOUND_SPACE;//普通音最長(zhǎng)間隔標(biāo)準(zhǔn)TR0=0;TR1=1;while(Point<SoundLength){SL=Tone%10; //計(jì)算出音符SM=Tone/10%10; //計(jì)算出高低音SH=Tone/100; //計(jì)算出是否升半CurrentFre=NewFreTab[SignTab[SL-1]+SH];//查出對(duì)應(yīng)音符的頻率 if(SL!=0) {if(SM==1)CurrentFre>>=2; //低音if(SM==3)CurrentFre<<=2; //高音Temp_T=65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//計(jì)算計(jì)數(shù)器初值Sound_Temp_TH0=Temp_T/256;Sound_Temp_TL0=Temp_T%256;TH0=Sound_Temp_TH0;TL0=Sound_Temp_TL0+12;//加12是對(duì)中斷延時(shí)的補(bǔ)償 }SLen=LengthTab[Length%10];//算出是幾分音符XG=Length/10%10;//算出音符類型(0普通1連音2頓音)FD=Length/100;LDiv=LDiv0/SLen;//算出連音音符演奏的長(zhǎng)度(多少個(gè)10ms)if(FD==1)LDiv=LDiv+LDiv/2;if(XG!=1) if(XG==0)//算出普通音符的演奏長(zhǎng)度if(SLen<=4) LDiv1=LDiv-LDiv4; elseLDiv1=LDiv*SOUND_SPACE; elseLDiv1=LDiv/2;//算出頓音的演奏長(zhǎng)度elseLDiv1=LDiv;if(SL==0)LDiv1=0;LDiv2=LDiv-LDiv1;//算出不發(fā)音的長(zhǎng)度if(SL!=0) {TR0=1;for(i=LDiv1;i>0;i--)//發(fā)規(guī)定長(zhǎng)度的音 {while(TF1==0);TH1=Sound_Temp_TH1;TL1=Sound_Temp_TL1; TF1=0;} }if(LDiv2!=0) {TR0=0;BeepIO=0;for(i=LDiv2;i>0;i--)//音符間的間隔 {while(TF1==0);TH1=Sound_Temp_TH1;TL1=Sound_Temp_TL1; TF1=0; } }if(stop)Point=SoundLength;Point+=2;Tone=Sound[Point];Length=Sound[Point+1];}stop=0;BeepIO=1;ET0=0;ET1=0;TR0=0;TR1=0;EA=0;playmusic=0;}ucharcodelove[]={0x1A,0x03,0x15,0x03,0x17,0x03,0x15,0x03,0x16,0x03,0x15,0x03,0x11,0x03,0x17,0x02,0x16,0x02,0x10,0x01,0x15,0x03,0x17,0x03,0x19,0x03,0x19,0x03,0x1A,0x02,0x19,0x03,0x18,0x03,0x17,0xC9,0x1B,0x03,0x1A,0x03,0x17,0x66,0x15,0x02,0x10,0x03,0x17,0x03,0x16,0x03,0x18,0x01,0x19,0x03,0x18,0x03,0x17,0x02,0x16,0x03,0x15,0x03,0x17,0x02,0x16,0x02,0x10,0xC9,0x1A,0x03,0x15,0x03,0x17,0x03,0x15,0x03,0x16,0x02,0x15,0x02,0x11,0x03,0x17,0x02,0x16,0x02,0x10,0x01,0x15,0x03,0x17,0x03,0x19,0x03,0x19,0x03,0x1A,0x02,0x19,0x03,0x18,0x03,0x17,0xC9,0x1B,0x03,0x1A,0x03,0x17,0x66,0x15,0x02,0x10,0x03,0x17,0x03,0x16,0x03,0x18,0x01,0x19,0x03,0x18,0x03,0x17,0x02,0x16,0x03,0x15,0x03,0x17,0x02,0x16,0x02,0x10,0xC9,0x00,0x00};ucharcodefairy_tale[]={0x0F,0x03,0x10,0x03,0x15,0x03,0x16,0x03,0x15,0x03,//愛情呼叫轉(zhuǎn)移0x1F,0x67,0x1A,0x0E,0x1A,0x03,0x1F,0x03,0x1A,0x66,0x19,0x0D,0x19,0x03,0x15,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x03,0x1B,0x03,0x1A,0x03,0x19,0x03,0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x17,0x03,0x19,0x03,0x1A,0x03,0x19,0x0D,0x19,0x03,0x19,0x03,0x1A,0x03,0x19,0x03,0x1A,0x67,0x1A,0x0E,0x1A,0x03,0x1A,0x03,0x1A,0x67,0x19,0x0E,0x19,0x03,0x1A,0x03,0x19,0x03,0x17,0x0D,0x17,0x16,0x15,0x03,0x16,0x03,0x17,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x15,0x0D,0x15,0x03,0x15,0x03,0x16,0x03,0x17,0x03,0x1A,0x03,0x17,0x03,0x16,0x03,0x15,0x03,0x1F,0x15,0x1F,0x03,0x1A,0x03,0x1F,0x03,0x1A,0x03,0x19,0x0D,0x19,0x03,0x19,0x03,0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x17,0x03,0x16,0x0D,0x16,0x03,0x15,0x03,0x10,0x03,0x15,0x03,0x15,0x03,0x16,0x03,0x15,0x00,0x15,0x0B,0x15,0x03,0x1F,0x03,0x1A,0x03,0x1F,0x03,0x20,0x03,0x1F,0x0D,0x1F,0x03,0x1F,0x03,0x20,0x03,0x1F,0x03,0x1A,0x03,0x1F,0x03,0x21,0x03,0x20,0x0D,0x20,0x03,0x1F,0x03,0x1A,0x03,0x1F,0x03,0x20,0x02,0x15,0x00,0x00,0x00};/************按鍵加減調(diào)整函數(shù)************/voidinc_set(charflag,charflag_num,ucharaddress,charlast,charfirst,charx,chary,ucharkeep){if(flag==flag_num)if(inc==0){delay(50);if(inc==0){ num=Read1302(address); num=(num/16)*10+num%16; num++;if(num==last)num=first;num=(num/10)*16+num%10;Write1302(0x8e,0x00);//允許寫入Write1302(address-1,num);//寫入Write1302(0x8e,0x80);//禁止寫操作 }while(!inc);//檢測(cè)按鍵松開LCD1602_write_char(x,y,Read1302(address)/16+0x30);//十 LCD1602_write_data(Read1302(address)%16+0x30);//個(gè)LCD1602_write_cmd(keep+x+1);//使光標(biāo)持在改動(dòng)的位置 }}voiddec_set(charflag,charflag_num,ucharaddress,charlast,charfirst,charx,chary,ucharkeep){if(flag==flag_num)if(dec==0){delay(50);if(dec==0){ num=Read1302(address); num=(num/16)*10+num%16; nu

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論