1、ds1302时间读取一、/*DS1302*#include#define uchar unsigned char#define uint unsigned int#define LED P0#define LED_OFF 0xffsbit ds1302_clk=P22;sbit ds1302_io =P21;sbit ds1302_rst=P20;uchar time7=0x30,0x24,0x15,0x08,0x12,0x01,0x08;void settime_ds1302();void gettime_ds1302();void writetime_ds1302(uchar,uchar)
2、; uchar readtime_ds1302(uchar);uchar readbyte_ds1302();void writebyte_ds1302(uchar);void numdis(uchar,uchar,uchar);void time_display();main() settime_ds1302(); /设置时间 while(1) gettime_ds1302(); /读取时间 time_display(); /显示时间 /*/设置预置时间到DS1302当中/*void settime_ds1302() uchar i; writetime_ds1302(0x8e,0x00);
3、 /write enable for(i=0;i7;i+) /set time-second,munite,hour,day,month,week,year writetime_ds1302(0x80+i*2,timei); writetime_ds1302(0x8e,0x80); /write protect/*/从DS1302中一次读出所有的数据并存在数组time7当中/*void gettime_ds1302() uchar i; for(i=0;i7;i+) /get time-second,munite,hour,day,month,week,year timei=readtime_
4、ds1302(0x81+i*2); /*/往DS1302的地址ds1302_add处写入ds1302_cmd数据或命令/*void writetime_ds1302(uchar ds1302_add,uchar ds1302_cmd)/ ds1302_rst=0;/ ds1302_clk=0; ds1302_rst=1; writebyte_ds1302(ds1302_add); writebyte_ds1302(ds1302_cmd);/ ds1302_clk=1; ds1302_rst=0; /*/从DS1302的地址DS1302_ADD处读出时间值/*uchar readtime_ds1
5、302(uchar ds1302_add) uchar temp_data; / ds1302_rst=0;/ ds1302_clk=0; ds1302_rst=1; writebyte_ds1302(ds1302_add); temp_data=readbyte_ds1302();/ ds1302_clk=1; ds1302_rst=0; return temp_data; /*/从DS1302中读出一字节并返回/*uchar readbyte_ds1302() uchar i; uchar temp_data; for(i=0; i1)|0x80; else temp_data=1; */
6、 /*以上被注释部分的代码也可以用,实际测试过* if(ds1302_io) temp_data|=(1i); /如果读到高电平,则置位相应位 else temp_data&=(10;i-) ds1302_io=temp_data&0x01; temp_data=1; ds1302_clk=1; ds1302_clk=0; /*/将读到的时间显示在六个数码管上,该子程序只显示分秒时。/*void time_display() uchar i; for(i=0;i4),i*2+1,20); /*/数码管显示子程序,将一个数num显示到某一位weixuan数码管/*void numdis(ucha
7、r num,uchar weixuan,uchar brightness ) while(brightness-)LED=num*0x10+weixuan*0x01; LED=LED_OFF;MS1602的读写时序*void Write(uchar tempdata) DB0_DB7=tempdata;RS=0;RW=0;E=0;nop;E=1;nop;CheckBusy();void WriteDDR(uchar tempdata) DB0_DB7=tempdata;RS=1;nop;RW=0;E=0;nop;E=1;nop;CheckBusy();void CheckBusy()DB0_D
8、B7=0xff;RW=1;RS=0;E=0;nop;E=1;do nop; TEMP=DB0_DB7;while(TEMP&0x80)=0x80);RW=0;二、#include #include #define uchar unsigned char#define uint unsigned intsbit SCK=P36; sbit SDA=P34; sbit RST=P35;/ DS1302复位sbit set=P30;/定义按键端口sbit s1=P31;sbit s2=P32;sbit s3=P33;/*液晶口定义*/sbit RS=P24;/RS为数据/指令选择1/0sbit RW
9、=P25;/读写sbit E=P26;/E为使能信号bit flag;/是否读取时间的标志位uchar setn=0;/复位键、增加键、减少键按下次数uchar code time0=DATE: - - ;uchar code time1=TIME: : : ;uchar l_tmpdate7=0,55,16,22,8,6,9;/秒分时日月周年/ 可随时更改uchar code write_add7=0x80,0x82,0x84,0x86,0x88,0x8a,0x8c; /秒分时日月周年 写的寄存器地址uchar code read_add7=0x81,0x83,0x85,0x87,0x89,
10、0x8b,0x8d; /秒分时日月周年 读的寄存器地址uchar code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40; /共阴数码管 0-9加-熄灭表void delay(uint z) while(-z);void write_com(uchar com)/液晶写命令函数 RS=0;/写命令使能 RW=0;/写 delay(5000);/*在高脉冲下命令输入*/ E=0; P0=com; delay(500); E=1; delay(500); E=0;void write_data(uchar dat)/液晶写
11、数据函数 RS=1;/写数据使能 RW=0; delay(5000);/*在高脉冲下数据输入*/ E=0; P0=dat; delay(500); E=1; delay(500); E=0; void init()/液晶初始化 uchar num; E=0; write_com(0x38);/显示模式设置 delay(500); write_com(0x38); delay(500); write_com(0x38); write_com(0x08);/显示关闭 delay(500); write_com(0x01);/显示清屏 write_com(0x06);/光标移动设置整屏不移动 wri
12、te_com(0x0c);/屏幕开、光标不闪(0)不显示(0) delay(500); write_com(0x80); for(num=0;num16;num+) write_data(time0num); delay(5); write_com(0x80+0x40); for(num=0;num13;num+) write_data(time1num); delay(5); void Write_Ds1302_Byte(uchar temp) /写一个字节 uchar i; for (i=0;i=1; /右移一位 SCK=1; /*/void Write_Ds1302(uchar add,
13、uchar dat )/向寄存器写数据 RST=0; _nop_(); SCK=0; _nop_(); RST=1;/启动 _nop_(); Write_Ds1302_Byte(add); /发送地址 Write_Ds1302_Byte(dat); /发送数据 RST=0; /恢复uchar Read_Ds1302_Byte()/读一个字节 uchar i,temp=0x00; for (i=0;i=1; /右移一位 SCK=1; return(temp); uchar Read_Ds1302(uchar address )/从寄存器读数据 uchar date; RST=0; _nop_()
14、; SCK=0; _nop_(); RST=1; _nop_(); Write_Ds1302_Byte(address); date=Read_Ds1302_Byte(); RST=0; _nop_(); /以下为DS1302复位的稳定时间 RST=0; SCK=0; _nop_(); SCK=1; _nop_(); SDA=0; _nop_(); SDA=1; _nop_(); return (date);/*/void Read_RTC(void) /读取日历 uchar i,*p; p=read_add; /读的地址传递 for(i=0;i7;i+) /分7次读取 秒分时日月周年 l_t
15、mpdatei=Read_Ds1302(*p); p+; /*/void Set_RTC(void) /设定日历 uchar i,*p,tmp; for(i=0;i7;i+) /BCD处理,将十六进制转换成十进制 tmp=l_tmpdatei/10; l_tmpdatei=l_tmpdatei%10; l_tmpdatei=l_tmpdatei+tmp*16; Write_Ds1302(0x8E,0X00);/关闭写保护 p=write_add;/传要写的寄存器地址 for(i=0;i=60) l_tmpdate0=0; tp=l_tmpdate0/10; l_tmpdate0=l_tmpda
16、te0%10; l_tmpdate0=l_tmpdate0+tp*16; Write_Ds1302(0x8E,0X00);/关闭写保护 Write_Ds1302(write_add0,l_tmpdate0); Write_Ds1302(0x8E,0x80);/打开写保护 write_com(0x80+0x40+11); tem0=l_tmpdate0/16;/数据的转换 tem1=l_tmpdate0&0x0f; write_data(0x30+tem0); write_data(0x30+tem1); write_com(0x80+0x40+12); Flag=1; if(setn=2) l
17、_tmpdate1=change(l_tmpdate1); l_tmpdate1+; if(l_tmpdate1=60) l_tmpdate1=0; tp=l_tmpdate1/10; l_tmpdate1=l_tmpdate1%10; l_tmpdate1=l_tmpdate1+tp*16; Write_Ds1302(0x8E,0X00);/关闭写保护 Write_Ds1302(write_add1,l_tmpdate1); Write_Ds1302(0x8E,0x80);/打开写保护 write_com(0x80+0x40+8); tem0=l_tmpdate1/16;/数据的转换 tem1=l_tmpdate1&0x0f; write_data(0x30+tem0); write_data(0x30+tem
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1