基于單片機(jī)的電子時(shí)鐘和顯示屏的設(shè)計(jì)_第1頁(yè)
基于單片機(jī)的電子時(shí)鐘和顯示屏的設(shè)計(jì)_第2頁(yè)
基于單片機(jī)的電子時(shí)鐘和顯示屏的設(shè)計(jì)_第3頁(yè)
基于單片機(jī)的電子時(shí)鐘和顯示屏的設(shè)計(jì)_第4頁(yè)
基于單片機(jī)的電子時(shí)鐘和顯示屏的設(shè)計(jì)_第5頁(yè)
已閱讀5頁(yè),還剩34頁(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)介

1、目錄1 方案設(shè)計(jì)1.1單片機(jī)電子時(shí)鐘的背景11.2電子時(shí)鐘的意義11.3 電子時(shí)鐘的工作原理及其具體實(shí)現(xiàn)11.4 設(shè)計(jì)任務(wù)22 電路方案設(shè)計(jì)32.1 模塊一電路圖32.2 模塊二電路圖32.3 模塊三電路圖42.4 總電路圖43程序設(shè)計(jì)53.1 主程序流程圖53.2模塊一ds1302流程圖63.3模塊二12864lcd流程圖83.4 模塊三ds18b20流程圖73.5 源程序74 仿真與調(diào)試7總結(jié)語(yǔ)7參考文獻(xiàn)7 1 方案設(shè)計(jì)1.1單片機(jī)電子時(shí)鐘的背景隨著電子技術(shù)和計(jì)算機(jī)技術(shù)的迅速發(fā)展,單片機(jī)已經(jīng)在工業(yè)過(guò)程控制、智能儀表、計(jì)算機(jī)集成控制、人工智能、通信設(shè)備的智能化以及家電的智能化等方面得到廣泛的

2、應(yīng)用。以單片機(jī)和微控制器為核心仍然是當(dāng)今實(shí)現(xiàn)智能化技術(shù)的主流。隨著單片機(jī)技術(shù)廣泛地應(yīng)用與人們生活的各個(gè)方面,一些人們經(jīng)常使用的產(chǎn)品大多數(shù)是基于單片機(jī)研發(fā)的,如電子時(shí)鐘。鐘表從原來(lái)的沙漏計(jì)時(shí)和機(jī)械打點(diǎn)塔鐘等演變到如今的電池驅(qū)動(dòng)鐘、交流鐘、指針式石英電子鐘表和數(shù)字式石英電子鐘表,并且鐘表的準(zhǔn)確度越來(lái)越高了。隨著人們的時(shí)間觀念逐漸地增強(qiáng),時(shí)鐘在我們?nèi)祟惖氖澜缋锇缪葜粋€(gè)非常重要的角色,幾乎出現(xiàn)在我們生活的各個(gè)角落里,并且人們幾乎離不開它了。如今的時(shí)鐘的功能更加全面,與機(jī)械時(shí)鐘相比具有更高的正確性和直觀性,且無(wú)機(jī)械裝置,具有更長(zhǎng)的壽命,因此廣泛地應(yīng)用于人類正常生活中,得到人們的關(guān)注和研發(fā)。1.2電子時(shí)

3、鐘的意義電子時(shí)鐘采用數(shù)字電路實(shí)現(xiàn)對(duì)時(shí)、分、秒和數(shù)字顯示的計(jì)時(shí)裝置,廣泛使用于個(gè)人家庭、車站、機(jī)場(chǎng)等公共場(chǎng)所,成為人們?nèi)粘I钪胁豢扇鄙俚谋匦杵?,給人們的生活、學(xué)習(xí)、工作、娛樂(lè)帶來(lái)了極大的方便。由于數(shù)字集成電路的發(fā)展和石英晶體振蕩器的廣泛應(yīng)用,使得電子時(shí)鐘的精度、穩(wěn)定性大幅度提高。現(xiàn)在的鐘表的功能越來(lái)越強(qiáng),具有定時(shí)自動(dòng)報(bào)警、按時(shí)自動(dòng)打鈴、時(shí)間程序自動(dòng)控制、定時(shí)廣播、自動(dòng)啟閉路燈、定時(shí)開關(guān)烘箱、通斷動(dòng)力設(shè)備、甚至各種定時(shí)電氣的自動(dòng)啟用等,因此,研究數(shù)字時(shí)鐘及其擴(kuò)展,有著重大意義。1.3 電子時(shí)鐘的工作原理及其具體實(shí)現(xiàn)電子時(shí)鐘的工作原理是采用單片機(jī)最小系統(tǒng),用ds1302時(shí)鐘芯片和12864lcd液

4、晶顯示屏芯片以及ds18b20溫度傳感器分別進(jìn)行寄存相關(guān)信息、顯示年月日時(shí)分秒和采取溫度,用四個(gè)鍵分別實(shí)現(xiàn)選擇調(diào)整對(duì)象為、增加調(diào)節(jié)對(duì)象的數(shù)值、減少調(diào)節(jié)對(duì)象的數(shù)值和調(diào)整確定,采用24小時(shí)制、定時(shí)器0中斷和外部中斷0,從而實(shí)現(xiàn)可調(diào)式中文電子日歷、溫度及整點(diǎn)報(bào)時(shí)功能。采用定時(shí)器0中斷,定時(shí)50ms,其中斷服務(wù)程序的功能是將時(shí)間和日期顯示到lcd上。每1s時(shí)間,刷新液晶顯示屏上的秒鐘數(shù)值;每60s時(shí)間,刷新顯示屏的分鐘數(shù)值;每60min時(shí)間,刷新顯示屏的小時(shí)數(shù)值;每24h時(shí)間,刷新顯示屏的日期;星期是通過(guò)年月日進(jìn)行調(diào)整;溫度是通過(guò)溫度傳感器芯片ds18b20進(jìn)行調(diào)整;時(shí)鐘每次到整點(diǎn)時(shí),通過(guò)蜂鳴器進(jìn)行整

5、點(diǎn)報(bào)時(shí)。采用外部中斷0實(shí)現(xiàn)時(shí)鐘的調(diào)整,將四個(gè)鍵通過(guò)“與門”連接單片機(jī)p32端口,從而通過(guò)鍵產(chǎn)生外部中斷信號(hào)。當(dāng)要調(diào)整日期時(shí)間的時(shí)候,首先要按k1鍵來(lái)選擇調(diào)整對(duì)象,否則按其他鍵不會(huì)進(jìn)行時(shí)間調(diào)整,然后可以按k2和k3鍵對(duì)日期時(shí)間進(jìn)行加減調(diào)整,最后按k4鍵可以將改動(dòng)的日期時(shí)間重新顯示到lcd上。并且每次按下鍵后,都會(huì)通過(guò)蜂鳴器發(fā)出按鍵聲音。1.4 設(shè)計(jì)任務(wù)(1)可調(diào)式中文電子日歷的硬件和軟件設(shè)計(jì);(2)畫出程序流程圖;(3)編寫代碼;(4)程序分析與調(diào)試;2 電路方案設(shè)計(jì)2.1 模塊一電路圖該模塊主要通過(guò)ds1302時(shí)鐘芯片實(shí)現(xiàn)計(jì)算秒、分、時(shí)、星期、日、月、年的信息,且具有閏年補(bǔ)償?shù)墓δ?,其在pro

6、teus中的電路圖如圖2.1.1所示。圖2.1.1 記錄年月日秒分時(shí)星期的電路圖2.2 模塊二電路圖該模塊主要通過(guò)12864lcd實(shí)現(xiàn)中文電子日歷功能,其在proteus中的電路圖如圖2.2.1所示圖2.2.1 顯示時(shí)間電路圖2.3 模塊三電路圖該模塊主要通過(guò)ds18b20溫度傳感芯片、四個(gè)鍵以及蜂鳴器實(shí)現(xiàn)溫度采集,時(shí)間調(diào)節(jié)和鬧鐘等功能。圖2.3.1 時(shí)間調(diào)整電路圖2.4 總電路圖如圖2.4.1所示。圖2.4.1 總體硬件電路圖3程序設(shè)計(jì)3.1 主程序流程圖圖3.1.1 主函數(shù)流程圖3.2模塊一ds1302流程圖圖3.2.1獲取當(dāng)前時(shí)間的流程圖圖3.2.2 年月日時(shí)分秒+/-流程圖圖3.2.3

7、定時(shí)器0的中斷服務(wù)程序圖3.2.4外部中斷0服務(wù)程序3.3模塊二12864lcd流程圖圖3.3.1 lcd初始化流程圖圖3.3.2 通用顯示函數(shù)流程圖3.4 模塊三ds18b20流程圖圖3.4 1 讀取溫度值3.5 源程序#include<reg51.h>#include<intrins.h>#include<string.h>#define uchar unsigned char#define uint unsigned intsbit sda=p10;/*ds1302數(shù)據(jù)線*/sbit clk=p11;/*ds1302時(shí)鐘線*/sbit rst=p12;

8、/*ds1302復(fù)位線*/sbit k1=p34; /*選擇鍵*/sbit k2=p35; /*加鍵*/sbit k3=p36; /*減鍵*/sbit k4=p37; /*確定鍵*/sbit spk=p26;/*子函數(shù)定義*/*顯示年函數(shù)*/extern void lcd_initialize();/*液晶初始化函數(shù)*/*讀取溫度值*/extern void read_temperature();extern void display_temperature(); /*顯示時(shí)間函數(shù)*/extern void display_a_char_8x16(uchar p,uchar l,ucha*m

9、)reentrant;/*顯示年月日,星期,時(shí)分秒的固定漢字函數(shù)*/extern void display_a_word(uchar p,uchar l,uchar *m)reentrant;void beep();/*啟動(dòng)蜂鳴器函數(shù)*/*在調(diào)整日期時(shí)間時(shí),用該位決定是否反白顯示*/extern bit reverse_display;extern uchar display_digit=0,0,0,0;/*待顯示的各溫度數(shù)位*/extern bit ds18b20_is_ok=1;/*傳感器正常標(biāo)志*/*全局變量和數(shù)組定義*/uchar tcount=0;/*一年中每個(gè)月的天數(shù),2月的天數(shù)由

10、年份決定*/uchar monthsdays=0,31,0,31,30,31,30,31,31,30,31,30,31;/*所讀取的日期時(shí)間*/uchar datetime7;/*當(dāng)前調(diào)整的時(shí)間對(duì)象:秒,分,時(shí),日,月,星期,年(0,1,2,3,4,5,6)變量*/*其中星期由年月日調(diào)整自動(dòng)完成*/char adjust_index=-1;uchar h_offset=10,v_page_offset=0;/*水平與垂直偏移變量*/*年月日,星期,時(shí)分秒漢字點(diǎn)陣16乘16*/uchar code date_time_words= 0x40,0x20,0x10,0x0c,0xe3,0x22,0x

11、22,0x22,0xfe,0x22,0x22,0x22,0x22,0x02,0x00,0x00, 0x04,0x04,0x04,0x04,0x07,0x04,0x04,0x04,0xff,0x04,0x04,0x04,0x04,0x04,0x04,0x00, /*年*/ 0x00,0x00,0x00,0x00,0x00,0xff,0x11,0x11,0x11,0x11,0x11,0xff,0x00,0x00,0x00,0x00, 0x00,0x40,0x20,0x10,0x0c,0x03,0x01,0x01,0x01,0x21,0x41,0x3f,0x00,0x00,0x00,0x00, /*

12、月*/ 0x00,0x00,0x00,0xfe,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0xfe,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x3f,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x3f,0x00,0x00,0x00,0x00, /*日*/ 0x00,0x00,0x00,0xbe,0x2a,0x2a,0x2a,0xea,0x2a,0x2a,0x2a,0x2a,0x3e,0x00,0x00,0x00, 0x00,0x48,0x46,0x41,0x49,0x49,0x49,0x7f,0x49,0x

13、49,0x49,0x49,0x49,0x41,0x40,0x00, /*星*/ 0x00,0x04,0xff,0x54,0x54,0x54,0xff,0x04,0x00,0xfe,0x22,0x22,0x22,0xfe,0x00,0x00, 0x42,0x22,0x1b,0x02,0x02,0x0a,0x33,0x62,0x18,0x07,0x02,0x22,0x42,0x3f,0x00,0x00, /*期*/ 0x00,0xfc,0x44,0x44,0x44,0xfc,0x10,0x90,0x10,0x10,0x10,0xff,0x10,0x10,0x10,0x00, 0x00,0x07,0

14、x04,0x04,0x04,0x07,0x00,0x00,0x03,0x40,0x80,0x7f,0x00,0x00,0x00,0x00, /*時(shí)*/ 0x80,0x40,0x20,0x98,0x87,0x82,0x80,0x80,0x83,0x84,0x98,0x30,0x60,0xc0,0x40,0x00, 0x00,0x80,0x40,0x20,0x10,0x0f,0x00,0x00,0x20,0x40,0x3f,0x00,0x00,0x00,0x00,0x00, /*分*/ 0x12,0x12,0xd2,0xfe,0x91,0x11,0xc0,0x38,0x10,0x00,0xff,0

15、x00,0x08,0x10,0x60,0x00, 0x04,0x03,0x00,0xff,0x00,0x83,0x80,0x40,0x40,0x20,0x23,0x10,0x08,0x04,0x03,0x00, /*秒*/ 0x10,0x21,0x86,0x70,0x00,0x7e,0x4a,0x4a,0x4a,0x4a,0x4a,0x7e,0x00,0x00,0x00,0x00, 0x02,0xfe,0x01,0x40,0x7f,0x41,0x41,0x7f,0x41,0x41,0x7f,0x41,0x41,0x7f,0x40,0x00, /*溫*/ 0x00,0x00,0xfc,0x04,

16、0x24,0x24,0xfc,0xa5,0xa6,0xa4,0xfc,0x24,0x24,0x24,0x04,0x00, 0x80,0x60,0x1f,0x80,0x80,0x42,0x46,0x2a,0x12,0x12,0x2a,0x26,0x42,0xc0,0x40,0x00, /*度*/;/*星期幾的漢字點(diǎn)陣16乘16*/uchar code weekday_words= 0x00,0x00,0x00,0xfe,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0xfe,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x3f,0x10,0x1

17、0,0x10,0x10,0x10,0x10,0x10,0x3f,0x00,0x00,0x00,0x00, /*日*/ 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xc0,0x80,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*一*/ 0x00,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x06,0x04,0x00,0x0

18、0,0x00, 0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x18,0x10,0x00, /*二*/ 0x00,0x04,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x04,0x00,0x00, 0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00, /*三*/ 0x00,0xfe,0x02,0x02,0x02,0xfe,0x02,0x

19、02,0xfe,0x02,0x02,0x02,0x02,0xfe,0x00,0x00, 0x00,0x7f,0x28,0x24,0x23,0x20,0x20,0x20,0x21,0x22,0x22,0x22,0x22,0x7f,0x00,0x00, /*四*/ 0x00,0x02,0x82,0x82,0x82,0x82,0xfe,0x82,0x82,0x82,0xc2,0x82,0x02,0x00,0x00,0x00, 0x20,0x20,0x20,0x20,0x20,0x3f,0x20,0x20,0x20,0x20,0x3f,0x20,0x20,0x30,0x20,0x00, /*五*/ 0

20、x10,0x10,0x10,0x10,0x10,0x91,0x12,0x1e,0x94,0x10,0x10,0x10,0x10,0x10,0x10,0x00, 0x00,0x40,0x20,0x10,0x0c,0x03,0x01,0x00,0x00,0x01,0x02,0x0c,0x78,0x30,0x00,0x00, /*六*/;/*半角數(shù)字點(diǎn)陣8乘16*/uchar code digits= 0x00,0xe0,0x10,0x08,0x08,0x10,0xe0,0x00,0x00,0x0f,0x10,0x20,0x20,0x10,0x0f,0x00,/*0*/ 0x00,0x10,0x10

21、,0xf8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3f,0x20,0x20,0x00,0x00,/*1*/ 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,/*2*/ 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0e,0x00,/*3*/ 0x00,0x00,0xc0,0x20,0x10,0xf8,0x00,0x00,0x00,0x07,0x04,0

22、x24,0x24,0x3f,0x24,0x00,/*4*/ 0x00,0xf8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0e,0x00,/*5*/ 0x00,0xe0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0f,0x11,0x20,0x20,0x11,0x0e,0x00,/*6*/ 0x00,0x38,0x08,0x08,0xc8,0x38,0x08,0x00,0x00,0x00,0x00,0x3f,0x00,0x00,0x00,0x00,/*7*/ 0x00,0x70,0x

23、88,0x08,0x08,0x88,0x70,0x00,0x00,0x1c,0x22,0x21,0x21,0x22,0x1c,0x00,/*8*/ 0x00,0xe0,0x10,0x08,0x08,0x10,0xe0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0f,0x00,/*9*/;/*生日快樂(lè)歌的音符頻率表,不同的頻率由不同的延時(shí)值來(lái)決定*/uchar code song_tone=212,212,190,212,159,169,212,212,190,212,142,159, 212,212,106,126,159,169,190,119,119,126

24、,159,142,159,0;/*生日快樂(lè)歌節(jié)拍表,節(jié)拍表決定了每個(gè)音符的演奏長(zhǎng)短*/uchar code song_long=9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0;void delayms(uint x) /*調(diào)用延時(shí)子函數(shù)*/uchar i;while(x-) for(i=0;i<120;i+);void playmusic()/*音樂(lè)播放函數(shù)*/uint i=0,j,k;while(song_longi!=0|song_tonei!=0)/*播放各個(gè)音符,song_long為拍*/f

25、or(j=0;j<song_longi*20;j+)spk=spk;/*song_tone延時(shí)表決定了每個(gè)音符的頻率*/for(k=0;k<song_tonei/3;k+);delayms(100);/*下一個(gè)音符的索引*/i+;void alarm(uchar t) /*溫度報(bào)警函數(shù)*/uchar i,j;for(i=0;i<200;i+)spk=spk;/*由參數(shù)t形成不同的頻率*/for(j=0;j<t;j+);/*向ds1302寫入一字節(jié)*/void write_a_byte_to_ds1302(uchar x)uchar i;for(i=0;i<8;i+

26、)sda=x&1; clk=1; clk=0; x>>=1;/*從ds1302讀取一字節(jié)*/uchar get_a_byte_from_ds1302()uchar i,b,t;for(i=0;i<8;i+)b>>=1;t=sda;b|=t<<7;clk=1;clk=0;/*bcd碼轉(zhuǎn)換*/return(b/16*10+b%16);/*從ds1302指定位置讀數(shù)據(jù)*/uchar read_data(uchar addr)uchar dat;rst=0;clk=0;rst=1;write_a_byte_to_ds1302(addr);dat=get

27、_a_byte_from_ds1302();clk=1;rst=0;return dat;/*向ds1302某地址寫入數(shù)據(jù)*/void write_ds1302(uchar addr,uchar dat)clk=0; rst=1;write_a_byte_to_ds1302(addr);write_a_byte_to_ds1302(dat);clk=1;rst=0;/*設(shè)置時(shí)間*/void set_ds1302()uchar i;write_ds1302(0x8e,0x00);/*秒分時(shí)日月年依次寫入*/for(i=0;i<7;i+)/*秒,分,時(shí),日,月,星期,年的起始地址分別為0x8

28、0,0x82,0x84,0x86,0x88,0x8a,0x8c*/ write_ds1302(0x80+2*i,(datetimei/10<<4)|(datetimei%10);write_ds1302(0x8e,0x80);/*讀取當(dāng)前時(shí)間*/void gettime()uchar i;for(i=0;i<7;i+)datetimei=read_data(0x81+2*i); uchar isleapyear(uint y)/*判斷是否為閏年*/return (y%4=0&&y%100!=0)|(y%400=0);/*刷新星期*/void refreshwe

29、ekday()uint i,d,w=5;/*已知1999.12.31是周五*/for(i=2000;i<2000+datetime6;i+)d=isleapyear(i)?366:365;w=(w+d)%7;d=0;for(i=1;i<datetime4;i+) d+=monthsdaysi;d+=datetime3;/*保存星期,06表示星期日,星期一,二,三,四,五,六,為了與ds1302的星期格式匹配,返回值需要加1*/datetime5=(w+d)%7+1;/*蜂鳴器函數(shù)*/void beep(uchar t)uchar i;for(i=0;i<100;i+)spk=

30、spk;delayms(t);spk=0;void datetime_adjust(char x)/*年月日時(shí)分+或-*/switch(adjust_index)case 6: /*年00-99*/if(x=1&&datetime6<99) datetime6+;if(x=-1&&datetime6>0) datetime6-;/*獲取2月天數(shù)*/ monthsdays2=isleapyear(2000+datetime6)?29:28;/*如果年份變化后當(dāng)前月份的天數(shù)大于上限則設(shè)為上限*/if(datetime3>monthsdaysdate

31、time4)datetime3=monthsdaysdatetime4;refreshweekday();/*刷新星期*/break;case 4: /*月01至12*/if(x=1&&datetime4<12) datetime4+;if(x=-1&&datetime4>1) datetime4-;/*獲取2月天數(shù)*/ monthsdays2=isleapyear(2000+datetime6)?29:28;/*如果年份變化后當(dāng)前月份的天數(shù)大于上限則設(shè)為上限*/if(datetime3>monthsdaysdatetime4) datetim

32、e3=monthsdaysdatetime4;refreshweekday();/*刷新星期*/break;case 3:/*日00至28/29/30/31;調(diào)節(jié)之前首先根據(jù)年份得出該年中2月的天數(shù)*/ monthsdays2=isleapyear(2000+datetime6)?29:28;/*再根據(jù)當(dāng)前月份決定調(diào)節(jié)日期的上限*/if(x=1&&datetime3<monthsdaysdatetime4) datetime3+;if(x=-1&&datetime3>0) datetime3-;refreshweekday();/*刷新星期*/bre

33、ak;case 2: /*小時(shí)*/if(x=1&&datetime2<23) datetime2+;datetime2-;break;case 1: /*分鐘*/if(x=1&&datetime1<59) datetime1+;if(x=-1&&datetime1>0) datetime1-;break;case 0: /*秒*/if(x=1&&datetime0<59) datetime0+;break;/*定時(shí)器0每秒刷新lcd顯示*/void t0_int() interrupt 1th0=(6553

34、6-50000)/256;tl0=(65536-50000)%256;if(+tcount!=2) return;tcount=0;/*年(后兩位)*/reverse_display=adjust_index=6; display_a_char_8x16(v_page_offset,70+h_offset,digits+datetime6/10*16); display_a_char_8x16(v_page_offset,78+h_offset,digits+datetime6%10*16);/*星期*/reverse_display=adjust_index=5; display_a_wor

35、d(v_page_offset+3,32+h_offset,weekday_words+(datetime5%10-1)*32);/*月*/reverse_display=adjust_index=4; display_a_char_8x16(v_page_offset,102+h_offset,digits+datetime4/10*16); display_a_char_8x16(v_page_offset,110+h_offset,digits+datetime4%10*16);/*日*/reverse_display=adjust_index=3; display_a_char_8x1

36、6(v_page_offset,5+h_offset,digits+datetime3/10*16); display_a_char_8x16(v_page_offset,13+h_offset,digits+datetime3%10*16);/*時(shí)*/reverse_display=adjust_index=2; display_a_char_8x16(v_page_offset+6,56+h_offset,digits+datetime2/10*16); display_a_char_8x16(v_page_offset+6,64+h_offset,digits+datetime2%10*

37、16);/*分鐘*/reverse_display=adjust_index=1; display_a_char_8x16(v_page_offset+6,88+h_offset,digits+datetime1/10*16); display_a_char_8x16(v_page_offset+6,96+h_offset,digits+datetime1%10*16);/*秒*/reverse_display=adjust_index=0; display_a_char_8x16(v_page_offset+6,-10+h_offset,digits+datetime0/10*16); di

38、splay_a_char_8x16(v_page_offset+6,-2+h_offset,digits+datetime0%10*16);/*鍵盤中斷(int0)*/void ex_int0() interrupt 0if(k1=0) /*選擇調(diào)整對(duì)象*/ beep(1);if(adjust_index=-1|adjust_index=0) adjust_index=7;adjust_index-;if(adjust_index=5) adjust_index=4; /*跳過(guò)對(duì)星期的調(diào)整*/elseif(k2=0) /*加*/ beep(1);datetime_adjust(1);elsei

39、f(k3=0) /*減*/ beep(1); datetime_adjust(-1);elseif(k4=0) /*確定*/beep(1);set_ds1302(); /*將調(diào)整后的時(shí)間寫入ds1302*/adjust_index=-1; /*操作索引重設(shè)為-1,時(shí)間繼續(xù)正常顯示*/*主函數(shù)*/void main()lcd_initialize();/*液晶初始化*/read_temperature();delayms(50);delayms(50);/*顯示年的固定前兩位*/ display_a_char_8x16(v_page_offset,55+h_offset,digits+2*16)

40、; display_a_char_8x16(v_page_offset,62+h_offset,digits);/*顯示固定漢字:年月日,星期,時(shí)分秒,溫度:*/ display_a_word(v_page_offset,86+h_offset,date_time_words+0*32); display_a_word(v_page_offset,-10+h_offset,date_time_words+1*32); display_a_word(v_page_offset,21+h_offset,date_time_words+2*32); display_a_word(v_page_off

41、set+3,0+h_offset,date_time_words+3*32); display_a_word(v_page_offset+3,16+h_offset,date_time_words+4*32); display_a_word(v_page_offset+3,56+h_offset,date_time_words+8*32); display_a_word(v_page_offset+3,72+h_offset,date_time_words+9*32); display_a_word(v_page_offset+6,72+h_offset,date_time_words+5*3

42、2); display_a_word(v_page_offset+6,104+h_offset,date_time_words+6*32); display_a_word(v_page_offset+6,6+h_offset,date_time_words+7*32);/*允許外部中斷,定時(shí)器0中斷*/ie=0x83;ip=0x01;it0=0x01;tmod=0x01;th0=0x3c;tl0=0xb0;tr0=1;while(1)/*如果未執(zhí)行調(diào)整時(shí)間操作,則就繼續(xù)顯示當(dāng)前時(shí)間*/if(adjust_index=-1) gettime();read_temperature();if(ds1

43、8b20_is_ok) display_temperature();delayms(100);/*溫度*/if(display_digit3!=0) display_a_char_8x16(v_page_offset+3,80+h_offset,digits+display_digit3*16);if(display_digit3=0&&display_digit2!=0) display_a_char_8x16(v_page_offset+3,88+h_offset,digits+display_digit2*16); display_a_char_8x16(v_page_o

44、ffset+3,96+h_offset,digits+display_digit1*16); if(display_digit3!=0)|(display_digit2=6)&&(display_digit1=5)alarm(90); alarm(120);if(datetime0=0&&datetime1=0)playmusic(); delayms(5001);/*lcd_12864.c*/*名稱:12864lcd顯示驅(qū)動(dòng)程序(不帶字庫(kù))*/#include<reg51.h>#include<intrins.h>#define uch

45、ar unsigned char#define uint unsigned int#define lcd_db_port p0 /*液晶db0db7*/#define lcd_start_row 0xc0 /*起始行*/#define lcd_page 0xb8 /*頁(yè)指令*/#define lcd_col 0x40 /*列指令*/*液晶引腳定義*/sbit di=p20;sbit rw=p21;sbit e=p22;sbit cs1=p23;sbit cs2=p24;sbit rs=p25;/*是否反相顯示(白底黑字/黑底白字)*/bit reverse_display=0;/*檢查lcd是

46、否忙*/bit lcd_check_busy()rw=1;_nop_();di=0;e=1;_nop_();e=0;return (bit)(p0&0x80);/*向lcd發(fā)送命令*/void lcd_write_command(uchar c)while(lcd_check_busy();lcd_db_port=0x00;rw=0;_nop_();di=0;lcd_db_port=c;e=1;_nop_();e=0;/*向lcd發(fā)送數(shù)據(jù)*/void lcd_write_data(uchar d)while(lcd_check_busy();lcd_db_port=0x00;rw=0;

47、_nop_();di=1;/*根據(jù)reverse_display決定是否反相顯示*/if(!reverse_display)lcd_db_port=d;else lcd_db_port=d;e=1;_nop_();e=0;/*初始化lcd*/void lcd_initialize()cs1=1; cs2=1;lcd_write_command(0x38); /*8位形式,2行字符*/lcd_write_command(0x0f); /*開顯示*/lcd_write_command(0x01); /*清屏*/lcd_write_command(0x06); /*畫面不動(dòng)光標(biāo)右移*/lcd_wri

48、te_command(lcd_start_row); /*設(shè)置起始行*/*通用顯示函數(shù)*/*從第p頁(yè)第l列開始顯示w字節(jié),數(shù)據(jù)在r所指向的緩沖*/*每字節(jié)8位是垂直顯示的,高位在下,低位在上*/*每個(gè)8*128的矩形區(qū)域?yàn)橐豁?yè)(每頁(yè)分左半頁(yè)與右半頁(yè))*/*整個(gè)lcd又由64*64的左半屏和64*64的右半屏構(gòu)成*/void common_show(uchar p,uchar l,uchar w,uchar *r) reentrantuchar i;if(l<64)/*顯示在左半屏或右半屏*/cs1=1; cs2=0;lcd_write_command(lcd_page+p);lcd_wr

49、ite_command(lcd_col+l);if(l+w<64)/*全部顯示在左半屏*/for(i=0;i<w;i+) lcd_write_data(ri);else /*如果越界則跨越左右半屏顯示*/for(i=0;i<64-l;i+) lcd_write_data(ri); /*左半屏顯示*/cs1=0; cs2=1; /*右半屏顯示*/lcd_write_command(lcd_page+p);lcd_write_command(lcd_col);for(i=64-l;i<w;i+) lcd_write_data(ri);else /*全部顯示在左右半屏*/cs

50、1=0;cs2=1;lcd_write_command(lcd_page+p);lcd_write_command(lcd_col+l-64);for(i=0;i<w;i+) lcd_write_data(ri);/*顯示一個(gè)8*16點(diǎn)陣字符*/void display_a_char_8x16(uchar p,uchar l,uchar *m) reentrantcommon_show(p,l,8,m); /*顯示漢字上半部分*/ common_show(p+1,l,8,m+8);/*顯示一個(gè)16*16點(diǎn)陣漢字*/void display_a_word(uchar p,uchar l,uchar *m)reentrantcommon_show(p,l,16,m);/*顯示漢字上部分*/common_show(p+1,l,16,m+16);/*顯示漢字下部分*/*名稱:ds18b20溫度傳感器*/#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int#define delaynop() _nop_();_nop_();_nop_();_nop_();sbit dq=p33;uchar currentt=0;/*當(dāng)前溫度

溫馨提示

  • 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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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)論