DS時(shí)鐘DSB用LCD顯示系統(tǒng)很簡(jiǎn)單_第1頁(yè)
DS時(shí)鐘DSB用LCD顯示系統(tǒng)很簡(jiǎn)單_第2頁(yè)
DS時(shí)鐘DSB用LCD顯示系統(tǒng)很簡(jiǎn)單_第3頁(yè)
DS時(shí)鐘DSB用LCD顯示系統(tǒng)很簡(jiǎn)單_第4頁(yè)
DS時(shí)鐘DSB用LCD顯示系統(tǒng)很簡(jiǎn)單_第5頁(yè)
已閱讀5頁(yè),還剩17頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、DS1302時(shí)鐘+DS18B20用LCD1602顯示(系統(tǒng)很簡(jiǎn)單)看仔細(xì)點(diǎn)啊 貌似很多這樣的圖但可不一樣喔 最小系統(tǒng)是用AT89s52搭建的!記得回去自己用protues和Keil仿真一下,這樣有利于進(jìn)一步了解!故未將仿真好的代碼上傳,如果真的需要的話(huà)可以和我聯(lián)系!程序代碼:#include #include /#include LCD1602.h/#include DS1302.h#define uint unsigned int#define uchar unsigned charsbit DS1302_CLK = P17; /實(shí)時(shí)時(shí)鐘時(shí)鐘線(xiàn)引腳sbit DS1302_IO = P16;

2、/實(shí)時(shí)時(shí)鐘數(shù)據(jù)線(xiàn)引腳sbit DS1302_RST = P15; /實(shí)時(shí)時(shí)鐘復(fù)位線(xiàn)引腳sbit wireless_1 = P30;sbit wireless_2 = P31;sbit wireless_3 = P32;sbit wireless_4 = P33;sbit ACC0 = ACC0;sbit ACC7 = ACC7;char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; /秒,分,時(shí)到日,月,年位閃的計(jì)數(shù)sbit Set = P20; /模式切換鍵sbit Up = P21; /加法按鈕sb

3、it Down = P22; /減法按鈕sbit out = P23; /立刻跳出調(diào)整模式按鈕sbit DQ = P10; /溫度傳送數(shù)據(jù)IO口char done,count,temp,flag,up_flag,down_flag;uchar temp_value; /溫度值uchar TempBuffer5,week_value2;void show_time(); /液晶顯示程序/*1602液晶顯示部分子程序*/Port Definitions*sbit LcdRs= P25;sbit LcdRw= P26;sbit LcdEn = P27;sfr DBPort = 0x80;/P0=0x

4、80,P1=0x90,P2=0xA0,P3=0xB0.數(shù)據(jù)端口/內(nèi)部等待函數(shù)*unsigned char LCD_Wait(void)LcdRs=0;LcdRw=1;_nop_();LcdEn=1;_nop_();LcdEn=0;return DBPort;/向LCD寫(xiě)入命令或數(shù)據(jù)*#define LCD_COMMAND0 / Command#define LCD_DATA1 / Data#define LCD_CLEAR_SCREEN0x01 / 清屏#define LCD_HOMING 0x02 / 光標(biāo)返回原點(diǎn)void LCD_Write(bit style, unsigned char

5、 input)LcdEn=0;LcdRs=style;LcdRw=0;_nop_();DBPort=input;_nop_();/注意順序LcdEn=1;_nop_();/注意順序LcdEn=0;_nop_();LCD_Wait();/設(shè)置顯示模式*#define LCD_SHOW0x04 /顯示開(kāi)#define LCD_HIDE0x00 /顯示關(guān)#define LCD_CURSOR0x02 /顯示光標(biāo)#define LCD_NO_CURSOR0x00 /無(wú)光標(biāo)#define LCD_FLASH0x01 /光標(biāo)閃動(dòng)#define LCD_NO_FLASH0x00 /光標(biāo)不閃動(dòng)void LCD_

6、SetDisplay(unsigned char DisplayMode)LCD_Write(LCD_COMMAND, 0x08|DisplayMode);/設(shè)置輸入模式*#define LCD_AC_UP0x02#define LCD_AC_DOWN0x00 / default#define LCD_MOVE0x01 / 畫(huà)面可平移#define LCD_NO_MOVE0x00 /defaultvoid LCD_SetInput(unsigned char InputMode)LCD_Write(LCD_COMMAND, 0x04|InputMode);/初始化LCD*void LCD_In

7、itial()LcdEn=0;LCD_Write(LCD_COMMAND,0x38); /8位數(shù)據(jù)端口,2行顯示,5*7點(diǎn)陣LCD_Write(LCD_COMMAND,0x38);LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); /開(kāi)啟顯示, 無(wú)光標(biāo)LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); /清屏LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); /AC遞增, 畫(huà)面不動(dòng)/液晶字符輸入的位置*void GotoXY(unsigned char x, unsigned char y)if(y=0)LCD_W

8、rite(LCD_COMMAND,0x80|x);if(y=1)LCD_Write(LCD_COMMAND,0x80|(x-0x40);/將字符輸出到液晶顯示void Print(unsigned char *str)while(*str!=0)LCD_Write(LCD_DATA,*str);str+;/*DS1302時(shí)鐘部分子程序*/typedef struct _SYSTEMTIME_unsigned char Second;unsigned char Minute;unsigned char Hour;unsigned char Week;unsigned char Day;unsig

9、ned char Month;unsigned char Year;unsigned char DateString11;unsigned char TimeString9;SYSTEMTIME;/定義的時(shí)間類(lèi)型SYSTEMTIME CurrentTime;#define AM(X)X#define PM(X)(X+12) / 轉(zhuǎn)成24小時(shí)制#define DS1302_SECOND0x80 /時(shí)鐘芯片的寄存器位置,存放時(shí)間#define DS1302_MINUTE0x82#define DS1302_HOUR0x84#define DS1302_WEEK0x8A#define DS1302_

10、DAY0x86#define DS1302_MONTH0x88#define DS1302_YEAR0x8Cvoid DS1302InputByte(unsigned char d) /實(shí)時(shí)時(shí)鐘寫(xiě)入一字節(jié)(內(nèi)部函數(shù)) unsigned char i; ACC = d; for(i=8; i0; i-) DS1302_IO = ACC0; /相當(dāng)于匯編中的 RRC DS1302_CLK = 1; DS1302_CLK = 0; ACC = ACC 1; unsigned char DS1302OutputByte(void) /實(shí)時(shí)時(shí)鐘讀取一字節(jié)(內(nèi)部函數(shù)) unsigned char i; f

11、or(i=8; i0; i-) ACC = ACC 1; /相當(dāng)于匯編中的 RRC ACC7 = DS1302_IO; DS1302_CLK = 1; DS1302_CLK = 0; return(ACC);void Write1302(unsigned char ucAddr, unsigned char ucDa)/ucAddr: DS1302地址, ucData: 要寫(xiě)的數(shù)據(jù) DS1302_RST = 0; DS1302_CLK = 0; DS1302_RST = 1; DS1302InputByte(ucAddr); / 地址,命令 DS1302InputByte(ucDa); / 寫(xiě)

12、1Byte數(shù)據(jù) DS1302_CLK = 1; DS1302_RST = 0;unsigned char Read1302(unsigned char ucAddr)/讀取DS1302某地址的數(shù)據(jù) unsigned char ucData; 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);void D

13、S1302_GetTime(SYSTEMTIME *Time) /獲取時(shí)鐘芯片的時(shí)鐘數(shù)據(jù)到自定義的結(jié)構(gòu)型數(shù)組unsigned char ReadValue;ReadValue = Read1302(DS1302_SECOND);Time-Second = (ReadValue&0x70)4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_MINUTE);Time-Minute = (ReadValue&0x70)4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_HOUR);Time-H

14、our = (ReadValue&0x70)4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_DAY);Time-Day = (ReadValue&0x70)4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_WEEK);Time-Week = (ReadValue&0x70)4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_MONTH);Time-Month = (ReadValue&0x70)4)*10 + (ReadValu

15、e&0x0F);ReadValue = Read1302(DS1302_YEAR);Time-Year = (ReadValue&0x70)4)*10 + (ReadValue&0x0F);void DateToStr(SYSTEMTIME *Time) /將時(shí)間年,月,日,星期數(shù)據(jù)轉(zhuǎn)換成液晶顯示字符串,放到數(shù)組里DateString if(hide_year2) /這里的if,else語(yǔ)句都是判斷位閃爍,2就不顯示,輸出字符串為 2007/07/22 Time-DateString0 = 2; Time-DateString1 = 0; Time-DateString2 = Time-Yea

16、r/10 + 0; Time-DateString3 = Time-Year%10 + 0; else Time-DateString0 = ; Time-DateString1 = ; Time-DateString2 = ; Time-DateString3 = ;Time-DateString4 = /;if(hide_monthDateString5 = Time-Month/10 + 0; Time-DateString6 = Time-Month%10 + 0; else Time-DateString5 = ; Time-DateString6 = ; Time-DateStri

17、ng7 = /;if(hide_dayDateString8 = Time-Day/10 + 0; Time-DateString9 = Time-Day%10 + 0; else Time-DateString8 = ; Time-DateString9 = ; if(hide_weekWeek%10 + 0; /星期的數(shù)據(jù)另外放到 week_value數(shù)組里,跟年,月,日的分開(kāi)存放,因?yàn)榈纫幌乱谧詈箫@示 else week_value0 = ; week_value1 = 0;Time-DateString10 = 0; /字符串末尾加 0 ,判斷結(jié)束字符void TimeToStr(S

18、YSTEMTIME *Time) /將時(shí),分,秒數(shù)據(jù)轉(zhuǎn)換成液晶顯示字符放到數(shù)組 TimeString; if(hide_hourTimeString0 = Time-Hour/10 + 0; Time-TimeString1 = Time-Hour%10 + 0; else Time-TimeString0 = ; Time-TimeString1 = ;Time-TimeString2 = :; if(hide_minTimeString3 = Time-Minute/10 + 0; Time-TimeString4 = Time-Minute%10 + 0; else Time-TimeS

19、tring3 = ; Time-TimeString4 = ; Time-TimeString5 = :; if(hide_secTimeString6 = Time-Second/10 + 0; Time-TimeString7 = Time-Second%10 + 0; else Time-TimeString6 = ; Time-TimeString7 = ; Time-DateString8 = 0;void Initial_DS1302(void) /時(shí)鐘芯片初始化unsigned char Second=Read1302(DS1302_SECOND);if(Second&0x80)

20、 /判斷時(shí)鐘芯片是否關(guān)閉 Write1302(0x8e,0x00); /寫(xiě)入允許Write1302(0x8c,0x07); /以下寫(xiě)入初始化時(shí)間 日期:07/07/25.星期: 3. 時(shí)間: 23:59:55Write1302(0x88,0x07);Write1302(0x86,0x25);Write1302(0x8a,0x07);Write1302(0x84,0x23);Write1302(0x82,0x59);Write1302(0x80,0x55);Write1302(0x8e,0x80); /禁止寫(xiě)入/*ds18b20子程序*/*ds18b20延遲子函數(shù)(晶振12MHz )*/void

21、 delay_18B20(unsigned int i)while(i-);/*ds18b20初始化函數(shù)*/void Init_DS18B20(void) unsigned char x=0; DQ = 1; /DQ復(fù)位 delay_18B20(8); /稍做延時(shí) DQ = 0; /單片機(jī)將DQ拉低 delay_18B20(80); /精確延時(shí) 大于 480us DQ = 1; /拉高總線(xiàn) delay_18B20(14); x=DQ; /稍做延時(shí)后 如果x=0則初始化成功 x=1則初始化失敗 delay_18B20(20);/*ds18b20讀一個(gè)字節(jié)*/unsigned char ReadO

22、neChar(void)uchar i=0;uchar dat = 0;for (i=8;i0;i-) DQ = 0; / 給脈沖信號(hào) dat=1; DQ = 1; / 給脈沖信號(hào) if(DQ) dat|=0x80; delay_18B20(4); return(dat);/*ds18b20寫(xiě)一個(gè)字節(jié)*/void WriteOneChar(uchar dat)unsigned char i=0;for (i=8; i0; i-) DQ = 0;DQ = dat&0x01; delay_18B20(5);DQ = 1; dat=1;/*讀取ds18b20當(dāng)前溫度*/void ReadTemp(v

23、oid)unsigned char a=0;unsigned char b=0;unsigned char t=0;Init_DS18B20();WriteOneChar(0xCC); / 跳過(guò)讀序號(hào)列號(hào)的操作WriteOneChar(0x44); / 啟動(dòng)溫度轉(zhuǎn)換delay_18B20(100); / this message is wery importantInit_DS18B20();WriteOneChar(0xCC); /跳過(guò)讀序號(hào)列號(hào)的操作WriteOneChar(0xBE); /讀取溫度寄存器等(共可讀9個(gè)寄存器) 前兩個(gè)就是溫度delay_18B20(100);a=ReadO

24、neChar(); /讀取溫度值低位b=ReadOneChar(); /讀取溫度值高位temp_value=b4;void temp_to_str() /溫度數(shù)據(jù)轉(zhuǎn)換成液晶字符顯示 TempBuffer0=temp_value/10+0; /十位 TempBuffer1=temp_value%10+0; /個(gè)位 TempBuffer2=0xdf; /溫度符號(hào) TempBuffer3=C; TempBuffer4=0;void Delay1ms(unsigned int count)unsigned int i,j;for(i=0;icount;i+)for(j=0;j0;delay-) for

25、(i=0;i0x59) /超過(guò)59秒,清零 temp=0; break; case 2: temp=Read1302(DS1302_MINUTE); /讀取分?jǐn)?shù) temp=temp+1; /分?jǐn)?shù)加1 up_flag=1; if(temp0x59) /超過(guò)59分,清零 temp=0; break; case 3: temp=Read1302(DS1302_HOUR); /讀取小時(shí)數(shù) temp=temp+1; /小時(shí)數(shù)加1 up_flag=1; if(temp0x23) /超過(guò)23小時(shí),清零 temp=0; break; case 4: temp=Read1302(DS1302_WEEK); /讀

26、取星期數(shù) temp=temp+1; /星期數(shù)加1 up_flag=1; if(temp0x7) temp=1; break; case 5: temp=Read1302(DS1302_DAY); /讀取日數(shù) temp=temp+1; /日數(shù)加1 up_flag=1; if(temp0x31) temp=1; break; case 6: temp=Read1302(DS1302_MONTH); /讀取月數(shù) temp=temp+1; /月數(shù)加1 up_flag=1; if(temp0x12) temp=1; break; case 7: temp=Read1302(DS1302_YEAR); /

27、讀取年數(shù) temp=temp+1; /年數(shù)加1 up_flag=1; if(temp0x85) temp=0; break; default:break; while(Up=0); while(wireless_2=1); /void Downkey()/降序按鍵Down=1; if(Down=0|wireless_3=1) mdelay(8); switch(count) case 1: temp=Read1302(DS1302_SECOND); /讀取秒數(shù) temp=temp-1; /秒數(shù)減1 down_flag=1; /數(shù)據(jù)調(diào)整后更新標(biāo)志 if(temp=0x7f) /小于0秒,返回59

28、秒 temp=0x59; break; case 2: temp=Read1302(DS1302_MINUTE); /讀取分?jǐn)?shù) temp=temp-1; /分?jǐn)?shù)減1 down_flag=1; if(temp=-1) temp=0x59; /小于0秒,返回59秒 break; case 3: temp=Read1302(DS1302_HOUR); /讀取小時(shí)數(shù) temp=temp-1; /小時(shí)數(shù)減1 down_flag=1; if(temp=-1) temp=0x23; break; case 4: temp=Read1302(DS1302_WEEK); /讀取星期數(shù) temp=temp-1;

29、/星期數(shù)減1 down_flag=1; if(temp=0) temp=0x7; break; case 5: temp=Read1302(DS1302_DAY); /讀取日數(shù) temp=temp-1; /日數(shù)減1 down_flag=1; if(temp=0) temp=31; break; case 6: temp=Read1302(DS1302_MONTH); /讀取月數(shù) temp=temp-1; /月數(shù)減1 down_flag=1; if(temp=0) temp=12; break; case 7: temp=Read1302(DS1302_YEAR); /讀取年數(shù) temp=tem

30、p-1; /年數(shù)減1 down_flag=1; if(temp=-1) temp=0x85; break; default:break; while(Down=0); while(wireless_3=1); void Setkey()/模式選擇按鍵Set=1;if(Set=0|wireless_4=1) mdelay(8); count=count+1; /Setkey按一次,count就加1 done=1; /進(jìn)入調(diào)整模式 while(Set=0); while(wireless_4=1); void keydone()/按鍵功能執(zhí)行 uchar Second; if(flag=0) /關(guān)

31、閉時(shí)鐘,停止計(jì)時(shí) Write1302(0x8e,0x00); /寫(xiě)入允許 temp=Read1302(0x80); Write1302(0x80,temp|0x80); Write1302(0x8e,0x80); /禁止寫(xiě)入 flag=1; Setkey(); /掃描模式切換按鍵 switch(count) case 1:do /count=1,調(diào)整秒 outkey(); /掃描跳出按鈕 Upkey(); /掃描加按鈕 Downkey(); /掃描減按鈕 if(up_flag=1|down_flag=1) /數(shù)據(jù)更新,重新寫(xiě)入新的數(shù)據(jù) Write1302(0x8e,0x00); /寫(xiě)入允許 W

32、rite1302(0x80,temp|0x80); /寫(xiě)入新的秒數(shù) Write1302(0x8e,0x80); /禁止寫(xiě)入 up_flag=0; down_flag=0; hide_sec+; /位閃計(jì)數(shù) if(hide_sec3) hide_sec=0; show_time(); /液晶顯示數(shù)據(jù) while(count=2);break; case 2:do /count=2,調(diào)整分 hide_sec=0; outkey(); Upkey(); Downkey(); if(temp0x60) temp=0; if(up_flag=1|down_flag=1) Write1302(0x8e,0

33、x00); /寫(xiě)入允許 Write1302(0x82,temp); /寫(xiě)入新的分?jǐn)?shù) Write1302(0x8e,0x80); /禁止寫(xiě)入 up_flag=0; down_flag=0; hide_min+; if(hide_min3) hide_min=0; show_time(); while(count=3);break; case 3:do /count=3,調(diào)整小時(shí) hide_min=0; outkey(); Upkey(); Downkey(); if(up_flag=1|down_flag=1) Write1302(0x8e,0x00); /寫(xiě)入允許 Write1302(0x84

34、,temp); /寫(xiě)入新的小時(shí)數(shù) Write1302(0x8e,0x80); /禁止寫(xiě)入 up_flag=0; down_flag=0; hide_hour+; if(hide_hour3) hide_hour=0; show_time(); while(count=4);break; case 4:do /count=4,調(diào)整星期 hide_hour=0; outkey(); Upkey(); Downkey(); if(up_flag=1|down_flag=1) Write1302(0x8e,0x00); /寫(xiě)入允許 Write1302(0x8a,temp); /寫(xiě)入新的星期數(shù) Writ

35、e1302(0x8e,0x80); /禁止寫(xiě)入 up_flag=0; down_flag=0; hide_week+; if(hide_week3) hide_week=0; show_time(); while(count=5);break; case 5:do /count=5,調(diào)整日 hide_week=0; outkey(); Upkey(); Downkey(); if(up_flag=1|down_flag=1) Write1302(0x8e,0x00); /寫(xiě)入允許 Write1302(0x86,temp); /寫(xiě)入新的日數(shù) Write1302(0x8e,0x80); /禁止寫(xiě)入 up_flag=0; down_flag=0; hide_day+; if(hide_day3) hide_day=0;

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論