基于DS1302寫的12864LCD時鐘程序_第1頁
基于DS1302寫的12864LCD時鐘程序_第2頁
基于DS1302寫的12864LCD時鐘程序_第3頁
基于DS1302寫的12864LCD時鐘程序_第4頁
基于DS1302寫的12864LCD時鐘程序_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、word以下是我寫的12864LCD并行口時鐘顯示程序,時鐘用按鍵可調(diào)/*12864.h頭文件*/#ifndef _12864_h#define _12864_h/*包含頭文件*/#include<reg51.h>#include"string.h"#include"DS1302.h"/*定義I/0口*/#define LCD12864_DATA P0sbit LCD12864_E = P27;sbit LCD12864_RW = P25;sbit LCD12864_RS = P26;sbit LCD12864_RET= P20;sbit L

2、CD12864_PSB =P22; /*聲明全局函數(shù)*/void Delay1ms(unsigned int); /聲明延時函數(shù)void BusyChk_12864(void); /聲明忙檢測函數(shù)void WrCom(unsigned char); /聲明寫入命令函數(shù)/unsigned char ReDat(void); /聲明讀數(shù)據(jù)函數(shù)void WrDat(unsigned char); /聲明寫數(shù)據(jù)函數(shù)void LcdInit(void); /12864初始化函數(shù)void SetAddress(unsigned char ,unsigned char); /設(shè)置地址轉(zhuǎn)換void Displ

3、ayString(unsigned char ,unsigned char,unsigned char *p);/行顯示字符void DisplayTime();void DisplayDay();void DisplayWeek();#endif/*12864.c*/#include"12864.h"/* 函 數(shù) 名 : Delay1ms* 函數(shù)功能 : 延時函數(shù),延時1ms* 輸 入 : c* 輸 出 e : 無* 說 名 : 該函數(shù)是在12MHZ晶振下,12分頻單片機的延時。*/void Delay1ms(unsigned int i) /誤差 0us unsigne

4、d char a;for (i; i>0; i-) for (a=199;a>0;a-) ; /*函數(shù)名字:void BusyChk()函數(shù)功能:12864LCD忙檢測* 輸 入 : 無* 輸 出 : 無*/void BusyChk(void) unsigned int timeout=0; LCD12864_E=0; /E=0 使能關(guān)閉 LCD12864_RS=0; /0 指令;1 數(shù)據(jù) LCD12864_RW=1; /1 讀數(shù)據(jù);0寫數(shù)據(jù) Delay1ms(50); /延時100ms LCD12864_E=1; /1 開使能,讀數(shù)據(jù) while(LCD12864_DATA &a

5、mp; 0x80) && +timeout);/判斷 1表示內(nèi)部在工作; 0表示正常 LCD12864_E=0; /E=0 使能關(guān)閉/*函數(shù)名字:void WrDat(usnigned char data)函數(shù)功能:寫入數(shù)據(jù)* 輸 入 : data* 輸 出 : 無*/void WrDat(unsigned char bTemp)BusyChk();/忙檢測LCD12864_E=0; /E=0 使能關(guān)閉 LCD12864_RS=1; /0 指令;1 數(shù)據(jù) LCD12864_RW=0; /1 讀數(shù)據(jù);0寫數(shù)據(jù)LCD12864_E=1; /E=1 使能翻開LCD12864_DATA

6、=bTemp;/寫入數(shù)據(jù)Delay1ms(10); /延時100msLCD12864_E=0; /E=0 使能關(guān)閉/*函數(shù)名字:void WrCom(usnigned char com)函數(shù)功能:寫入命令* 輸 入 : com* 輸 出 : 無*/void WrCom(unsigned char com) BusyChk(); LCD12864_E=0; /E=0 使能關(guān)閉 LCD12864_RS=0; /0 指令;1 數(shù)據(jù) LCD12864_RW=0; /1 讀數(shù)據(jù);0寫數(shù)據(jù)LCD12864_E=1; /E=0 使能翻開LCD12864_DATA=com;/寫入數(shù)據(jù)Delay1ms(10);

7、 /延時100msLCD12864_E=0; /E=0 使能關(guān)閉/*函數(shù)名字:unsigned char ReData(void)函數(shù)功能:讀數(shù)據(jù)* 輸 入 : 無* 輸 出 : data*/unsigned char ReData(void)/ unsigned char bTempt;/BusyChk();/LCD12864_DATA=0xFF;/ LCD12864_E=0; /E=0 使能關(guān)閉/ LCD12864_RS=1; /0 指令;1 數(shù)據(jù)/ LCD12864_RW=1; /1 讀數(shù)據(jù);0寫數(shù)據(jù)/LCD12864_E=1; /E=0 使能翻開/bTempt=LCD12864_DAT

8、A;/讀出數(shù)據(jù)/Delay1ms(100); /延時100ms/LCD12864_E=0; /E=0 使能關(guān)閉/return bTempt;/*函數(shù)名字:void LcdInit(void)函數(shù)功能:初始化* 輸 入 : 無* 輸 出 : 無*/void LcdInit() LCD12864_RS=0; /0 指令;1 數(shù)據(jù) LCD12864_RW=0; /1 讀數(shù)據(jù);0寫數(shù)據(jù) LCD12864_E=1; /E=1 使能翻開 LCD12864_PSB=1;/1為并行模式,0為串行模式 LCD12864_RET=1; Delay1ms(50); LCD12864_RET=0;/低電平復(fù)位 Del

9、ay1ms(300); LCD12864_RET=1;/恢復(fù)高電平 WrCom(0x30); /根本指令用0x30,擴展指令用0x34 Delay1ms(20); WrCom(0x30); WrCom(0x0C); /顯示開,光標關(guān)/ WrCom(0x14); /光標位置右移一個位置 WrCom(0x01); /清屏 WrCom(0x06); /進入模式 指針加1,屏幕不移動 /*函數(shù)名字:void SetAddress(unsigned char x,unsigned char y)函數(shù)功能:初始化* 輸 入 : 行、數(shù)據(jù)* 輸 出 : 無*/void SetAddress(unsigned

10、 char x,unsigned char y) unsigned char bAddress;switch(y)case 1: bAddress=0x80+x;break;case 2: bAddress=0x90+x;break;case 3: bAddress=0x88+x;break;case 4: bAddress=0x98+x;break;default: break;WrCom(bAddress);/*函數(shù)名字:void DisplayString(unsigned char x,unsigned char y, unsigned)函數(shù)功能:初始化* 輸 入 : 行、數(shù)據(jù)* 輸

11、出 : 無*/void DisplayString(unsigned char x,unsigned char y,unsigned char *add) unsigned char i;i=strlen(add);SetAddress(x,y);while(i-) WrDat(*(add+);/*函數(shù)名字:void DisplayTime()函數(shù)功能:顯示時間* 輸 入 : 行、數(shù)據(jù)* 輸 出 : 無*/void DisplayTime() WrCom(0x88); WrDat('0'+(TIME2/10); WrDat('0'+(TIME2%10); Dis

12、playString(1,3,":");WrDat('0'+(TIME1/10);WrDat('0'+(TIME1%10);WrDat(':');WrDat('0'+(TIME0/10);WrDat('0'+(TIME0%10);/*函數(shù)名字:void DisplayDay()函數(shù)功能:顯示時間* 輸 入 : 行、數(shù)據(jù)* 輸 出 : 無*/void DisplayDay() WrCom(0x80);WrDat('0'+2);WrDat('0'+0);WrDat(&

13、#39;0'+(TIME6/10);WrDat('0'+(TIME6%10);DisplayString(2,1,"年");WrDat('0'+(TIME4/10);WrDat('0'+(TIME4%10);DisplayString(4,1,"月");WrDat('0'+(TIME3/10);WrDat('0'+(TIME3%10);DisplayString(6,1,"日");/*函數(shù)名字:void DisplayWeek()函數(shù)功能:顯示時間

14、* 輸 入 : 行、數(shù)據(jù)* 輸 出 : 無*/void DisplayWeek() switch(TIME5) case 1: DisplayString(0,2,"星期:Mo");break; case 2: DisplayString(0,2,"星期:Tu");break; case 3: DisplayString(0,2,"星期:We");break; case 4: DisplayString(0,2,"星期:Th");break; case 5: DisplayString(0,2,"星期:F

15、r");break; case 6: DisplayString(0,2,"星期:Sa");break; case 7: DisplayString(0,2,"星期:Su");break; /*DS1302.h*/#ifndef _DS1302_h#define _DS1302_h/*包含頭文件*/#include<reg51.h>#include<intrins.h>/*定義I/O口*/sbit DSIO=P34;sbit RST= P35;sbit SCLK=P36;/*聲明全局變量*/extern unsigned

16、 char TIME7;/-聲明全局函數(shù)-/void Ds1302Write(unsigned char addr, unsigned char dat);unsigned char Ds1302Read(unsigned char addr);void Ds1302Init();void Ds1302ReadTime();unsigned char changeBCD_Variable(unsigned char );unsigned char changeVariable_BCD(unsigned char );#endif/*DS1302.c*/#include"DS1302.

17、h" /-DS1302寫入和讀取時分秒的地址命令-/-秒分時日月周年 最低位讀寫位;-/unsigned char code READ_RTC_ADDR7 = 0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d; unsigned char code WRITE_RTC_ADDR7 = 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c;/* 函 數(shù) 名 : Ds1302Write* 函數(shù)功能 : 向DS1302命令地址+數(shù)據(jù)* 輸 入 : addr,dat* 輸 出 : 無*/void Ds1302Write(unsign

18、ed char addr, unsigned char dat)unsigned char n;RST = 0;_nop_();SCLK = 0;/先將SCLK置低電平。_nop_();RST = 1; /然后將RST(CE)置高電平。_nop_();for (n=0; n<8; n+)/開始傳送八位地址命令DSIO = addr & 0x01;/數(shù)據(jù)從低位開始傳送addr >>= 1;SCLK = 1;/數(shù)據(jù)在上升沿時,DS1302讀取數(shù)據(jù)_nop_();SCLK = 0;_nop_();for (n=0; n<8; n+)/寫入8位數(shù)據(jù)DSIO = dat

19、& 0x01;dat >>= 1;SCLK = 1;/數(shù)據(jù)在上升沿時,DS1302讀取數(shù)據(jù)_nop_();SCLK = 0;_nop_(); RST = 0;/傳送數(shù)據(jù)結(jié)束_nop_();/* 函 數(shù) 名 : Ds1302Read* 函數(shù)功能 : 讀取一個地址的數(shù)據(jù)* 輸 入 : addr* 輸 出 : dat*/unsigned char Ds1302Read(unsigned char addr)unsigned char n,dat,dat1;RST = 0;_nop_();SCLK = 0;/先將SCLK置低電平。_nop_();RST = 1;/然后將RST(CE

20、)置高電平。_nop_();for(n=0; n<8; n+)/開始傳送八位地址命令DSIO = addr & 0x01;/數(shù)據(jù)從低位開始傳送addr >>= 1;SCLK = 1;/數(shù)據(jù)在上升沿時,DS1302讀取數(shù)據(jù)_nop_();SCLK = 0;_nop_();_nop_();for(n=0; n<8; n+)/讀取8位數(shù)據(jù)dat1 = DSIO;/從最低位開始接收dat = (dat>>1) | (dat1<<7);SCLK = 1;_nop_();SCLK = 0;/ _nop_();RST = 0;_nop_();/以下為D

21、S1302復(fù)位的穩(wěn)定時間,必須的。SCLK = 1;_nop_();DSIO = 0;_nop_();DSIO = 1;_nop_();return dat;/* 函 數(shù) 名 : Ds1302Init* 函數(shù)功能 : 初始化DS1302.* 輸 入 : 無* 輸 出 : 無*/void Ds1302Init()unsigned char n;Ds1302Write(0x8E,0X00); /關(guān)閉寫保護功能for (n=0; n<7; n+)/寫入7個字節(jié)的時鐘信號:分秒時日月周年Ds1302Write(WRITE_RTC_ADDRn,TIMEn);Ds1302Write(0x8E,0x8

22、0); /翻開寫保護功能/* 函 數(shù) 名 : Ds1302ReadTime* 函數(shù)功能 : ReadTime* 輸 入 : 無* 輸 出 : 無*/void Ds1302ReadTime()unsigned char n; for (n=0; n<7; n+)/寫入7個字節(jié)的時鐘信號:分秒時日月周年TIMEn=Ds1302Read(READ_RTC_ADDRn);TIMEn=changeBCD_Variable(TIMEn);/*/*BCD碼轉(zhuǎn)換成十進制*/unsigned char changeBCD_Variable(unsigned char data1) unsigned cha

23、r i=0,j=0; i=data1&0xf0; i>>=4; j=data1&0x0f; data1=i*10+j; return data1;/*十進制轉(zhuǎn)換成BCD*/unsigned char changeVariable_BCD(unsigned char data2)unsigned char i=0,j=0;i=data2/10; i<<=4; j=data2%10; data2=i+j;return data2;/*key.h*/#ifndef _Key_h#define _Key_h#include<reg51.h>#incl

24、ude"12864.h"/*定義I/O口*/#define GPIO_KEY P1/*聲明全局變量*/extern unsigned char keyvalue;/*聲明全局函數(shù)*/void KeyDown();unsigned char keycurrent(unsigned char ,int);void toolmode();#endif/*key.c*/#include"Key.h"/* 函 數(shù) 名 : keyvalue* 函數(shù)功能 : 矩陣按鍵值* 輸 入 : 無* 輸 出 : keyvalue*/const char code ValueLi

25、st= /下限 上限 0, 59, /秒 1, 59, /分 1, 12, /時 1, 31, /日 1, 12, /月 1, 7, /周 1, 99 / 年 ;/*矩陣按鍵掃描*/void KeyDown(void)GPIO_KEY=0x0f;if(GPIO_KEY!=0x0f)Delay1ms(3); if(GPIO_KEY!=0x0f) switch(GPIO_KEY) case 0x07: keyvalue=1; break; case 0x0b: keyvalue=2; break; case 0x0d: keyvalue=3; break; case 0x0e: keyvalue=

26、4; break; GPIO_KEY=0xf0; switch(GPIO_KEY) case 0x70: keyvalue=keyvalue;break; case 0xb0: keyvalue+=4; break; case 0xd0: keyvalue+=8; break; case 0xe0: keyvalue+=12;break; while(GPIO_KEY!=0xf0) Delay1ms(3); unsigned char keycurrent(unsigned char value,int i) if(value=2) keyvalue=0; TIMEi+; if(TIMEi&g

27、t;ValueList2*i+1) TIMEi=ValueList2*i; if(value=3) keyvalue=0; TIMEi-; if(TIMEi<=ValueList2*i) TIMEi=ValueList2*i+1; return TIMEi;/*按鍵處理,調(diào)節(jié)時間*/void toolmode(void) unsigned char n; keyvalue=0; WrCom(0x01); /清屏 WrCom(0x02); /復(fù)位 Ds1302ReadTime();/讀ds1302中數(shù)值 / TIME0=0x00; while(1) /年設(shè)置 WrCom(0x80); Wr

28、Dat('2'); WrDat('0'); WrDat('0'+TIME6/10); WrDat('0'+(TIME6%10); DisplayString(2,1,"年"); KeyDown(); /按鍵掃描 keycurrent(keyvalue,6); if(keyvalue=1) keyvalue=0; break; if(keyvalue=4) /進入月設(shè)置 keyvalue=0; break; while(1) /月份設(shè)置 WrCom(0x80+3); WrDat('0'+(TIME

29、4/10); WrDat('0'+(TIME4%10); DisplayString(4,1,"月"); KeyDown(); keycurrent(keyvalue, 4); if(keyvalue=1) keyvalue=0; goto EXIT_toolmode; if(keyvalue=4)/進入日期設(shè)置 keyvalue=0; break; while(1)/日期設(shè)置 WrCom(0x80+5); WrDat('0'+(TIME3/10); WrDat('0'+(TIME3%10); DisplayString(6,

30、1,"日"); KeyDown(); keycurrent(keyvalue, 3); if(keyvalue=1) keyvalue=0; goto EXIT_toolmode; if(keyvalue=4) /進入周設(shè)置 keyvalue=0; break; while(1) /周設(shè)置 DisplayWeek(); KeyDown(); keycurrent(keyvalue,5); if(keyvalue=1) keyvalue=0; goto EXIT_toolmode; if(keyvalue=4)/進入時間設(shè)計 keyvalue=0; break; while(1)/時間設(shè)置 WrCom(0x88); WrDat('0'+(TIME2/1

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論