SPI總線的DS1302實時鐘控制_第1頁
SPI總線的DS1302實時鐘控制_第2頁
SPI總線的DS1302實時鐘控制_第3頁
SPI總線的DS1302實時鐘控制_第4頁
SPI總線的DS1302實時鐘控制_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、實驗四 spi總線的ds1302實時鐘控制一、實驗?zāi)康?、了解電子電路proteus軟件的在單片機方面的仿真應(yīng)用2、掌握具有spi總線ds1302、max7219芯片的應(yīng)用及編程二、實驗要求在proteus環(huán)境下,利用ds1302實時鐘芯片實現(xiàn)時間、日期的顯示,并且具有調(diào)節(jié)時間、日期的功能,顯示用共陰極led顯示。用串行芯片max7219驅(qū)動led顯示.實現(xiàn)此功能的電路原理圖如下所示:1、顯示“星期”的共陽極二極管接到單片機的p0口,p0口數(shù)據(jù)線接上拉電阻;2、顯示“時間”或“日期”,是采用max7219控制共陰極led來實現(xiàn)3、ds1302的io腳接單片機的p1.1,時鐘信號sclk接單片機

2、p1.0,復(fù)位信號/rst接單片機p1.2。ds1302的晶振在這里可以不接,電源、地線都省略。4、max7219是用來驅(qū)動“時間”或“日期”的led顯示的。max7219的din信號腳接單片機的p2.0,/load接單片機的p2.1,clk接單片機的p2.2;5、時間/日期的調(diào)整:按下調(diào)整鍵,再分別按相應(yīng)的“星期”、“年”、“月”、“日”,“時”、“分”、“秒“鍵,即可以調(diào)整時間/日期;6、在正常顯示時,按下“時間/日期“切換鍵,即可以在顯示時間或日期來回切換。三、proteus環(huán)境下的硬件設(shè)計1、打開proteus isis,在proteus isis編輯窗口中單擊列表上的“p”按鈕,添加

3、下列元器件:元件名稱所屬類at89c51microprocessor icscrystal 晶振misecllaneouscap 電容capacitorres 電阻resistords1302microprocessor ics7seg-com-anode共陽極optoeletronicsbutton按鍵switches&relaysmax7129microprocessor icsrespack-7排阻resistor7seg-mpx-ca-blue 共陰極optoeletronics表1在proteus編輯窗口添加完元器件,連線。如附圖1。四、軟件設(shè)計 在keil環(huán)境下編輯、編譯程

4、序,生成*.hex文件后,裝入單片機即可。五、實驗程序源程序:#include<reg51.h>/包含特殊功能寄存器庫#include <ds1302.h> #include <7219.h> /該漢字庫要求在你要顯示的位置裝入你要的漢字模(16*16)。void main()init7219(); /初始化7219 set1302(time);/設(shè)置初始時間 get1302(curtime);/讀回ds1302中的數(shù)據(jù) p0=code1curtime5-1;/顯示星期 display1(num);/默認(rèn)顯示時間 delay(255);/延時 delay(2

5、55); while(1) get1302(curtime);/取數(shù)據(jù) if(tick=0)/判斷是否更改 t=t; while(tick=0) while (t=1) if(week=0)/更改星期 w+; while (week=0);get1302(curtime);curtime5=curtime5+w; if(curtime5=8) curtime5=1; w=0;set1302(curtime); else if(year=0)/更改年份 while (year=0); get1302(curtime);curtime6=add5(curtime6);set1302(curtime

6、); else if(month=0)/更改月份 while (month=0); get1302(curtime);curtime4=add4(curtime4);set1302(curtime); else if(day=0)/更改號數(shù) while (day=0); get1302(curtime);curtime3=add3(curtime3);set1302(curtime); else if(hour=0)/更改時 while (hour=0); get1302(curtime);curtime2=add2(curtime2);set1302(curtime); else if(mi

7、n=0)/更改分 while (min=0); get1302(curtime);curtime1=add1(curtime1);set1302(curtime); else if(sec=0)/更改秒 while (sec=0); get1302(curtime);curtime0=0;set1302(curtime); if(chan=0)/是否有顯示切換 n=n; while(chan=0) get1302(curtime); if(n=1) display2(num); else display1(num); p0=code1curtime5-1; if(tick=0) t=t; wh

8、ile(tick=0) if(chan=0) /是否有顯示切換 n=n; while(chan=0) if(n=1) display2(num); else display1(num);頭文件“ds1302”#ifndef _ds1302_h_#define _ds1302_h_/*/#include <7219.h> void inputbyte(unsigned char da)/把數(shù)據(jù)da寫入ds1302 的io口,先送低位unsigned char i; acc=da; for(i=8;i>0;i-)/開始傳輸8個字節(jié)的數(shù)據(jù) io=acc0; /取最低位,注意ds13

9、02的數(shù)據(jù)和地址都是從最低位開始傳輸?shù)?sclk=1;/時鐘線拉高,制造上升沿,sda的數(shù)據(jù)被傳輸 sclk=0;/時鐘線拉低,為下一個上升沿做準(zhǔn)備 acc=acc>>1; unsigned char outputbyte(void)/讀出ds1302的數(shù)據(jù)并保存在da中 unsigned char i; for(i=8;i>0;i-) acc=acc>>1; acc7=io; sclk=1; sclk=0; return(acc); void write1302(unsigned char addr, unsigned char da)/寫入ds1302地址和數(shù)

10、據(jù)rst=0; /初始ce線置為0 sclk=0; /初始時鐘線置為0 rst=1; /初始ce置為1,傳輸開始 inputbyte(addr); /地址,命令 傳輸命令字,要寫入的時間/日期地址 inputbyte(da); /*寫1byte數(shù)據(jù) /寫入要修改的時間/日期 sclk =1;/時鐘線拉高 rst=0;/讀取結(jié)束,ce置為0,結(jié)束數(shù)據(jù)的傳輸unsigned char read1302(unsigned char addr)/根據(jù)寄存器地址讀出ds1302的相應(yīng)數(shù)據(jù)unsigned char dd; rst=0;/初始ce線置為0 sclk=0;/初始時鐘線置為0 rst=1;/初

11、始ce置為1,傳輸開始 inputbyte(addr); /地址,命令 傳輸命令字,要讀取的時間/日歷地址 dd=outputbyte(); /讀1byte數(shù)據(jù)讀取要得到的時間/日期 sclk=1; /時鐘線拉高 rst =0; /讀取結(jié)束,ce置為0,結(jié)束數(shù)據(jù)的傳輸 return(dd);/得到的時間/日期void set1302(unsigned char *psda)/設(shè)置時間 unsigned char i; unsigned char addr=0x80; write1302(0x8e,0x00); /控制命令,wp=0,寫操作 for(i =7;i>0;i-) write13

12、02(addr,*psda); /格式為:秒 分 時 日 月 星期 年 psda+; addr+=2; write1302(0x8e,0x80); /控制命令,wp=1,寫保護void get1302(unsigned char *psda) unsigned char i; unsigned char addr=0x81; for(i=0;i<7;i+) *(psda+i)=read1302(addr);/格式為: 秒 分 時 日 月 星期 年 addr+=2; #endif頭文件“7219”#ifndef _7219_h_#define _7219_h_/*/#define deco

13、de_mode 0x09#define intesity 0x0a#define scan_limit 0x0b#define shut_down 0x0c#define deisplay_test 0x0funsigned char time=0x00,0x00,0x08,0x28,0x05,0x06,0x11;/初始值,秒分時日月星期年unsigned char curtime7,num8;unsigned char code1=0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8;/星期,共陽極字段unsigned char code2=0x7e,0x30,0x6d,0x

14、79,0x33,0x5b,0x5f,0x70,0x7f,0x7b;sbit rst=p12;sbit io =p11;sbit sclk=p10;sbit din=p20;sbit load=p21;sbit clk=p22;sbit week=p30;sbit year=p31;sbit month=p32;sbit day=p33;sbit hour=p34;sbit min=p35;sbit sec=p36;sbit tick=p37;sbit chan=p17;sbit acc0=acc0;sbit acc7=acc7;bit n=0;bitt=0;unsigned char w=0;

15、void write_7219(unsigned char data)/max7219寫數(shù)據(jù)函數(shù)unsigned char i;acc=data; for(i=8;i>=1;i-) clk=0; din=acc7; acc=acc<<1; clk=1; void write7219(unsigned char addr,unsigned char dat)load=0; write_7219(addr); write_7219(dat); load=1; void init7219(void) write7219(decode_mode,0x00);/譯碼寄存器,寫入00h,

16、對0至7位均不譯碼 write7219(intesity,0x04);/亮度調(diào)節(jié)寄存器,寫入04h,實現(xiàn)一般的亮度顯示 write7219(scan_limit,0x07);/掃描界限寄存器,寫入07h,實現(xiàn)0到7位數(shù)碼管的顯示 write7219(shut_down,0x01);/停機寄存器,寫入01h,實現(xiàn)正常工作 write7219(deisplay_test,0x00);/顯示測試寄存器,寫入00h,實現(xiàn)正常工作void delay(unsigned char m)/延時程序unsigned char x,y; for(x=m;x>0;x-) for(y=255;y>0;y

17、-);unsigned char add1(unsigned char data)/分、秒調(diào)整子程序unsigned char i,j; i=data&0x0f; j=data>>4; i+; if(i=0x0a) i=0x00;j+; if(j=0x06)&&(i=0x00) /超過59就變成0 i低位,j高位 i=0;j=0; data=(j<<4)+i; return(data);unsigned char add2(unsigned char data)/小時調(diào)整子程序unsigned char i,j; i=data%16; j=dat

18、a>>4; i+; if(i=10) i=0;j+; if(j=2)&&(i=4)/超過23就變成0,i低位,j高位 i=0;j=0; data=(j<<4)+i; return(data);unsigned char add3(unsigned char data)/號數(shù)調(diào)整子程序unsigned char i,j; i=data%16; j=data>>4; i+; if(i=10) i=0;j+;if(curtime6/16*10+curtime6%16)%4=0)&&(curtime4=0x02)&&(

19、j=0x03)&&(i=0x00) i=1;j=0;else if(curtime6/16*10+curtime6%16)%4!=0)&&(curtime4=0x02)&&(j=0x02)&&(i=0x09) i=1;j=0; else if(curtime4=0x01)|(curtime4=0x03)|(curtime4=0x05)|(curtime4=0x07)|(curtime4=0x08)|(curtime4=0x10)|(curtime4=0x12)&&(j=0x03)&&(i=0x02)

20、i=1;j=0;else if(curtime4=0x04)|(curtime4=0x06)|(curtime4=0x09)|(curtime4=0x11)&&(j=0x03)&&(i=0x01) i=1;j=0; data=(j<<4)+i; return(data); unsigned char add4(unsigned char data)/月份調(diào)整子程序unsigned char i,j; i=data%16; j=data>>4; i+; if(i=0x0a) i=0;j+; if(j=0x01)&&(i=0x

21、03)/超過12就變成0,i低位,j高位 i=1;j=0; data=(j<<4)+i; return(data);unsigned char add5(unsigned char data)/年份調(diào)整子程序unsigned char i,j; i=data%16; j=data/16; i+; if(i=0x0a) i=0;j+; if(j=0x0a)&&(i=0x00) i=0;j=0; data=(j<<=4)+i; return(data);void display1(unsigned char *num)/顯示時間unsigned char i,j,m=0x08; for(i=0;i<3;i+) *num=code2curtimei&0x0f; write7219(m,*num); j=curtimei>>4; nu

溫馨提示

  • 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)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論