LCD帶溫度顯示萬年歷_第1頁
LCD帶溫度顯示萬年歷_第2頁
LCD帶溫度顯示萬年歷_第3頁
LCD帶溫度顯示萬年歷_第4頁
LCD帶溫度顯示萬年歷_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、LCD帶溫度顯示萬年歷(2009-4-5 15:02:00)【收藏】 【評論】 【打印】 【關(guān)閉】 標(biāo)簽:1 #include #include #define uchar unsigned char#define uint unsigned int#define LCD_Data P2#define Busy 0x80 /用于檢測LCD狀態(tài)字中的Busy標(biāo)識sbit DQ = P13;sbit lcdrs = P35; /數(shù)據(jù)命令選擇端 (H/L)sbit lcdrw = P36; /讀寫選擇端 (H/L)sbit lcde = P37; /使能信號sbit SCL2=P10; /SCL2定

2、義為P1口的第3位腳,連接DS1302SCL和ADC0831SCL腳sbit SDA2=P11; /SDA2定義為P1口的第4位腳,連接DS1302SCL和ADC0831SDA腳sbit RST = P12; / DS1302片選腳sbit key0 = P00; /定義三個按鍵sbit key1 = P01; /加按鍵sbit key2 = P02; /減按鍵sbit H1 = P03;sbit L=P04;bit presence ;uchar code cdis2 = . C ;uchar buzyc,funtion_flag=0,temp_num;char shi,fen,miao,n

3、ian,yue,ri,xin;uchar beep;unsigned char data temp_data2 = 0x00,0x00 ;unsigned char data display5 = 0x00,0x00,0x00,0x00,0x00 ;unsigned char code ditab16 = 0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04, 0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09 ;unsigned char code mytab8 = 0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0

4、x00 ;unsigned char l_tmpdate8=0x00,59,12,19,2,8,2;/顯示初值unsigned char l_tmpdisplay8=0x40,0x40,0x40,0x40,0x40,0x40,0x40,0;/待顯示的數(shù)code unsigned char write_rtc_address7=0x80,0x82,0x84,0x86,0x88,0x8c,0x8a; /1302寫入地址code unsigned char read_rtc_address7=0x81,0x83,0x85,0x87,0x89,0x8d,0x8b;/1302讀出地址code unsig

5、ned char table= 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x00;/共陰數(shù)碼管 0-9 - 熄滅表uchar date1=MON;uchar date2=TUE;uchar date3=WED;uchar date4=THU;uchar date5=FRI;uchar date6=SAT;uchar date7=SUN; #define delayNOP() ; _nop_() ;_nop_() ;_nop_() ;_nop_() ; ; /*字節(jié)寫入子函數(shù): 將要寫入的數(shù)字先右移一位,是最低位溢出,然后在s

6、cl2的一個上升沿數(shù)據(jù)被寫入DS1302,注意 數(shù)據(jù)的寫入時從低位到高位*/void Write_Ds1302_Byte(unsigned char date) uchar j,temp; temp=date; for(j=0;j1; /將數(shù)據(jù)右移一位使最低位溢出 SDA2=CY; /將待寫入數(shù)據(jù)最低位寫入數(shù)據(jù)緩沖 SCL2=0; SCL2=1; /在scl的上升延寫入數(shù)據(jù) /*字節(jié)讀出子函數(shù): 分8次讀出數(shù)據(jù),將讀出的數(shù)據(jù)存入temp:在時鐘信號的下降沿讀出一個字節(jié)的數(shù)據(jù) 數(shù)據(jù)讀出也是從低位到高位的*/uchar Read_Ds1302_Byte() uchar j,temp; for(j=

7、0;j1; /將temp右移一位使最高位變?yōu)榇胃呶?,最高為? SDA2=1; SCL2=1; SCL2=0; /在scl2的一個下降沿數(shù)據(jù)讀出 if(SDA2=1)/如果讀出的數(shù)據(jù)為1,即在temp的最高位寫入1,為0的時候則寫入0 temp=temp|0x80; temp=temp|0x00; return temp;/*數(shù)據(jù)寫入DS1302函數(shù): 在指定地址寫入指定數(shù)據(jù),在rst為低scl2為低的情況下將rst置高,即允許數(shù)據(jù)寫入 先寫入地址 然后寫入數(shù)據(jù) 最后將rst拉低結(jié)束數(shù)據(jù)寫入*/void Write_Ds1302( unsigned char address,unsigned

8、char dat ) RST=0; _nop_();_nop_();_nop_();_nop_();_nop_(); SCL2=0; _nop_();_nop_();_nop_();_nop_();_nop_(); RST=1; _nop_();_nop_();_nop_();_nop_();_nop_(); /開啟,寫入數(shù)據(jù)地址,這是本串口協(xié)議的開始,加入延時防止干擾 Write_Ds1302_Byte(address); /發(fā)送地址 Write_Ds1302_Byte(dat); /發(fā)送數(shù)據(jù) RST=0; /恢復(fù)/*從指定地址讀取數(shù)據(jù)子函數(shù): 在rst為低scl2為低的情況下將rst置高,

9、即允許數(shù)據(jù)被讀出(注意加入延時可以防止干擾) 先寫入地址 在分八次吧數(shù)據(jù)一位位讀出,在scl2的一個下降沿數(shù)據(jù)被讀出,數(shù)據(jù)位從0開始*/unsigned char Read_Ds1302 ( unsigned char address ) unsigned char temp1,temp2; RST=0; _nop_();_nop_();_nop_();_nop_();_nop_(); SCL2=0; _nop_();_nop_();_nop_();_nop_();_nop_(); RST=1; _nop_();_nop_();_nop_();_nop_();_nop_(); /開啟,寫入數(shù)據(jù)

10、地址,這是本串口協(xié)議的開始,加入延時防止干擾 Write_Ds1302_Byte(address); temp1=Read_Ds1302_Byte(); temp2=temp14; temp2=temp2*10; temp1=temp1&0x0f; temp1=temp1+temp2;/讀出數(shù)據(jù)的時候要將BCD碼轉(zhuǎn)換成十進(jìn)制 RST=0; return (temp1); /返回/*設(shè)定時間初值: 在寫入之前應(yīng)當(dāng)把要寫入的數(shù)據(jù)轉(zhuǎn)換成BCD碼的形式 每寫入一次數(shù)據(jù)記得都要打開1302的寫入允許 */void Set_RTC(void) /設(shè)定 日歷 unsigned char i,*p,tmp;

11、for(i=0;i8;i+) /數(shù)據(jù)的轉(zhuǎn)換,DS1302以4位存儲一個10進(jìn)制數(shù),因此需要將數(shù)據(jù)進(jìn)行轉(zhuǎn)換 tmp=l_tmpdatei/10; l_tmpdatei=l_tmpdatei%10; l_tmpdatei=l_tmpdatei+tmp*16; Write_Ds1302(0x8E,0X00); /打開DS1302的寫入允許 p=write_rtc_address; /傳地址 for(i=0;i8;i+) /7次寫入 年月日時分秒星期 Write_Ds1302(*p,l_tmpdatei); p+; Write_Ds1302(0x8E,0x80); /關(guān)閉DS1302的寫入允許/*讀取

12、時鐘日歷: 循環(huán)幾次分別讀出數(shù)據(jù),存放在一個數(shù)組中,注意此時讀出的數(shù)據(jù)為BCD碼形式 每讀一次數(shù)值讓1302空運(yùn)行一次 可有效的防治干擾*/void Read_RTC(void) /讀取 日歷 unsigned char i,*p; p=read_rtc_address; /地址傳遞 for(i=0;i8;i+) /分7次讀取 年月日時分秒星期 l_tmpdisplayi=Read_Ds1302(*p);/讀取數(shù)據(jù)存放于數(shù)組變量中 p+; _nop_();_nop_();_nop_();_nop_();_nop_(); /5個空指令 Read_Ds1302(0x00); /讓1302空運(yùn)行一次

13、是最好的防干擾方法 _nop_();_nop_();_nop_();_nop_();_nop_(); /*液晶顯示函數(shù)部分*/void delay(int ms) unsigned char y ; while(ms-) for(y = 0 ; y250 ; y+) _nop_() ; _nop_() ; _nop_() ; _nop_() ; /讀狀態(tài),忙檢測處理函數(shù)unsigned char ReadStatusLCD(void) LCD_Data = 0xFF; lcdrs = 0; lcdrw = 1; lcde = 0; lcde = 0; lcde = 1; while (LCD_

14、Data & Busy)=Busy); /檢測忙信號 return(LCD_Data); /寫命令子函數(shù)void write_com(uchar com,buzyc) if(buzyc) ReadStatusLCD();/忙檢測 lcdrw=0; lcdrs=0; P2=com; delay(3); lcde=1; delay(6); lcde=0; /寫數(shù)據(jù)子函數(shù)void write_date(uchar date) ReadStatusLCD();/忙檢測 lcdrw=0; lcdrs=1; P2=date; delay(3); lcde=1; delay(6); lcde=0; /初始化

15、函數(shù)void init() H1=0; /定義按鍵 lcde=0; delay(15); write_com(0x38,0); delay(5); write_com(0x38,0); delay(5); write_com(0x38,0); write_com(0x38,1); /設(shè)置led為162顯示,57點(diǎn)陣,8位數(shù)據(jù)口 write_com(0x0c,1); /開顯示,不顯示光標(biāo),光標(biāo)閃爍 write_com(0x06,1); /光標(biāo)自動移動,整屏不動 write_com(0x01,1); /顯示清屏,數(shù)據(jù)指針和地址指針全部清零/ write_com(0x80,1); /設(shè)置數(shù)據(jù)指針到屏

16、幕的最開始端/*自定義字符寫入CGRAM */*/void writetab() unsigned char i ; write_com(0x40) ; /寫CGRAM for (i = 0 ; i 8 ; i+) write_date(mytab i ) ; /*液晶顯示掃描程序*/void display_scan(uchar add,uchar date) uchar sw,gw; sw=date/10; gw=date%10; write_com(0x80+add,1); write_date(0x30+sw); write_date(0x30+gw); void week_displ

17、ay() uchar j; write_com(0x80+0x40+11,1); switch(xin) case 1: for(j=0;j3;j+) write_date(date1j); break; case 2: for(j=0;j3;j+) write_date(date2j); break; case 3: for(j=0;j3;j+) write_date(date3j); break; case 4: for(j=0;j3;j+) write_date(date4j); break; case 5: for(j=0;j3;j+) write_date(date5j); brea

18、k; case 6: for(j=0;j3;j+) write_date(date6j); break; case 7: for(j=0;j 0 ; i-) DQ = 0 ; / 給脈沖信號 dat = 1 ; DQ = 1 ; / 給脈沖信號 if(DQ) dat |= 0x80 ; Delay1(4) ; return (dat) ;/* 寫一個字節(jié) */*/ WriteOneChar(uchar dat) unsigned char i = 0 ; for (i = 8 ; i 0 ; i-) DQ = 0 ; DQ = dat&0x01 ; Delay1(5) ; DQ = 1 ; d

19、at=1 ; /* 讀取溫度 */*/ Read_Temperature(void) Init_DS18B20() ; WriteOneChar(0xCC) ; / 跳過讀序號列號的操作 WriteOneChar(0x44) ; / 啟動溫度轉(zhuǎn)換 Init_DS18B20() ; WriteOneChar(0xCC) ; /跳過讀序號列號的操作 WriteOneChar(0xBE) ; /讀取溫度寄存器 temp_data0 = ReadOneChar() ; /溫度低8位 temp_data1 = ReadOneChar() ; /溫度高8位 /* 設(shè)定顯示位置 */*/void lcd_p

20、os(uchar pos) write_com (pos | 0x80) ; /數(shù)據(jù)指針=80+地址變量/*自定義字符寫入CGRAM */* 數(shù)據(jù)轉(zhuǎn)換與溫度顯示 */*/ Disp_Temperature() display4=temp_data0&0x0f ; display0=ditabdisplay4+0x30 ; /查表得小數(shù)位的值 display4=(temp_data0&0xf0)4)|(temp_data1&0x0f)4) ; display3=display4/100+0x30 ; display1=display4%100 ; display2=display1/10+0x3

21、0 ; display1=display1%10+0x30 ; if(display3=0x30) /高位為0,不顯示 display3=0x20 ; if(display2=0x30) /次高位為0,不顯示 display2=0x20 ; lcd_pos(0x00) ; write_date(display3) ; /百位數(shù)顯示 lcd_pos(0x01) ; write_date(display2) ; /十位數(shù)顯示 lcd_pos(0x02) ; write_date(display1) ; /個位數(shù)顯示 lcd_pos(0x04) ; write_date(display0) ; /小數(shù)位數(shù)顯示 /* 主函數(shù)部分*/void main() uchar m ; uchar j=0; init();/* Set_RTC();*/ while(1) time_set(); Read_RTC(); if(funtion_flag=0) write_com(0x80+0x0a,1); wri

溫馨提示

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

最新文檔

評論

0/150

提交評論