DS1302时钟芯片C语言程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includereg52.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintsbitlcd_rs=P2^0;sbitlcd_e=P2^1;sbitds1302_rst=P2^5;sbitds1302_sclk=P2^6;sbitds1302_io=P2^7;uchartime_date[]={0x50,0x59,0x23,0x00,0x00,0x00,0x00};voiddelay(ucharz){ucharm,n;for(m=z;m0;m--)for(n=110;n0;n--);}voidds1302writebyte(uchardat)//写一个字节{uchari;for(i=0;i8;i++)//开始传输8个字节数据{ds1302_sclk=0;//拉低,为上升做准备ds1302_io=dat&0x01;//取最低位ds1302_sclk=1;//拉低ds1302_sclk=0;//拉高dat=1;//数据右移一位,准备传输下一位数据}}uchards1302readbyte()//读一个字节{uchari,dat;for(i=0;i8;i++)//开始读取8个字节数据{dat=1;//要返回的数据左移一位ds1302_sclk=0;//拉低if(ds1302_io==1)//当IO为高时,证明该位数据位1dat=dat|0x80;//要传输数据的当前值置为1。若不是,为0ds1302_sclk=1;//拉高ds1302_sclk=0;//拉低}returndat;//返回读出的数据}voidds1302write(ucharadd,uchardat)//写入数据{ds1302_rst=0;ds1302_sclk=0;ds1302_rst=1;//传输开始ds1302writebyte(add);//传输命令字,写入时间的地址ds1302writebyte(dat);//写入要修改的时间ds1302_sclk=1;//拉高ds1302_rst=0;//读取结束,结束数据的传输}uchards1302read(ucharadd)//读取某地址的数据{uchardat;ds1302_rst=0;ds1302_sclk=0;ds1302_rst=1;//传输开始ds1302writebyte(add);//传输命令字,要读出时间的地址dat=ds1302readbyte();//读取要得到的时间ds1302_sclk=1;ds1302_rst=0;returndat;//返回得到的时间}voidds1302set()//设置初始时间{uchari;ds1302write(0x8e,0x00);//关写保护for(i=0;i7;i++){ds1302write(0x80+i+i,time_date[i]);//将初始时间数据写入1302对应的寄存器里delay(5);}ds1302write(0x8e,0x80);//开写保护}voidds1302get()//读取当前时间{uchari;for(i=0;i7;i++){time_date[i]=ds1302read(0x81+i+i);//将当前时间数据从1302对应的寄存器里读出来delay(5);}}voidds1302init()//初始化1302{ds1302_rst=0;ds1302_sclk=0;ds1302write(0x8e,0x00);ds1302write(0x90,0x00);}voidlcdwrite_com(ucharcom)//写命令{lcd_rs=0;P0=com;delay(5);lcd_e=1;delay(5);lcd_e=0;}voidlcdwrite_data(uchardate)//写数据{lcd_rs=1;P0=date;delay(5);lcd_e=1;delay(5);lcd_e=0;}voidlcdinit()//初始化1602{lcd_e=0;lcdwrite_com(0x38);//设置16×2显示,5×7点阵,8位数据接口lcdwrite_com(0x0c);//设置开显示,不显示光标lcdwrite_com(0x06);//写一个字符后地址指针加1lcdwrite_com(0x01);//显示清0,数据指针清0}main(){lcdinit();//初始化1602ds1302init();//初始化1302ds1302set();//设置初始时间while(1){ds1302get();//读取当前时间lcdwrite_com(0x80);lcdwrite_data('0'+((time_date[6]&0xf0)4));//显示年lcdwrite_data('0'+(time_date[6]&0x0f));lcdwrite_data('/');lcdwrite_data('0'+((time_date[4]&0xf0)4));//显示月lcdwrite_data('0'+(time_date[4]&0x0f));lcdwrite_data('/');lcdwrite_data('0'+((time_date[3]&0xf0)4));//显示日lcdwrite_data('0'+(time_date[3]&0x0f));lcdwrite_data('');lcdwrite_data('0'+((time_date[5]&0xf0)4));//显示星期lcdwrite_data('0'+(time_date[5]&0x0f));lcdwrite_com(0x80+0x40);lcdwrite_data('0'+((time_date[2]&0xf0)4));//显示时lcdwrite_data('0'+(time_date[2]&0x0f));lcdwrite_data(':');lcdwrite_data('0'+((time_date[1]&0xf0)4));//显示分lcdwrite_data('0'+(time_date[1]&0x0f));lcdwrite_data(':');lcdwrite_data('0'+((time_date[0]4)&0x0f));//显示秒lcdwrite_data('0'+((time_date[0]&0x0f)));}}

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功