读秒寄存器 0x81 写0x80
读分寄存器 0x83 写0x82
读小时寄存器 0x85 写0x84
读日寄存器 0x87 写0x86
读月寄存器 0x 写0x88
读星期寄存器 0x8b 写0x8a
读年寄存器 0x8d 写0x8c
写保护寄存器 0x8e 写0x00为无保护 可向任何寄存器中写入
写0x80为有保护 不可向任何寄存器中写入 只能读取
读写保护寄存器0x8f
注意:在读取的数据中 年月日分、秒寄存器:(笼统地)高四位位为十进制中的10位,低四位为十进制中的个位
时寄存器:最高位7为1(12进制)位6固为0 位5为1(PM)为0(AM)
最高位7为0(24进制)位6固为0 位5和位4为十进制中的10位,
低四位为十进制中的个位。
每读一个寄存器的数据以后,都写一个类似于复位的语句write1302(0x00,0x00)
这样读下一个寄存器才不会受影响。
*/
#include #define rst573 P2&=0x1f #define ledY4C P2|=0x80 #define beeY5C P2|=0xa0 #define smgY6C P2|=0xc0 #define smgY7C P2|=0xe0 sbit P36=P3^6; sbit P06=P0^6; sbit P04=P0^4; sbit c1302=P1^7;//sck SPI协议 上升沿读入 下降沿输出 sbit d1302=P2^3;//I/O 低位先进 低位先出 sbit s1302=P1^3;//片选 高有效 低复位 sbit rs=P2^0;//高数据 低命令 sbit w=P2^1; //高读 低写 sbit en=P1^2;//高使能 低释放 unsigned char table[]={0x81,0x83,0x85,0x87,0x,0x8b,0x8d}; unsigned char row[]=\"一二三四五六日\"; unsigned char row1[]=\"20 -- -- \"; unsigned char row2[]=\" 时 分 秒\"; unsigned char row3[]=\"星期 \"; void delaynms(unsigned int ms) { unsigned int x,y; for(x=ms;x;x--) for(y=110;y;y--); } void init107() { P36=0; rst573; //close led P0=0xff; ledY4C; rst573; //close bee P06=0; P04=0; beeY5C; rst573; //close smg P0=0x00; smgY6C; rst573; } void write1302(unsigned char addr,unsigned char dat) { unsigned char i; s1302=0; c1302=0; s1302=1; for(i=8;i;i--) { addr=addr>>1; d1302=CY; c1302=0; c1302=1; } for(i=8;i;i--) { dat=dat>>1; d1302=CY; c1302=0; c1302=1; } s1302=0; } unsigned char read1302(unsigned char addr) { unsigned i,temp; s1302=0; c1302=0; s1302=1; for(i=8;i;i--) { addr=addr>>1; d1302=CY; c1302=0; c1302=1; } for(i=8;i;i--) { c1302=1; c1302=0; if(d1302==1) { temp>>=1; temp|=0x80; } else temp>>=1; } s1302=0; return temp; } void init_ds1302() { write1302(0x8e,0x00);//无保护 write1302(0x80,0x40);//秒 40 write1302(0x82,0x16);//分 16 write1302(0x84,0x13);//时 13 write1302(0x86,0x02);//日 02 write1302(0x88,0x10);//月 10 write1302(0x8a,0x02);//周 日 write1302(0x8c,0x11);//年(20)11 write1302(0x8e,0x80);//有保护 只可读 } void com128(unsigned char com) { unsigned char temp; do { rs=0; w=1; en=1; delaynms(1); temp=P0; en=0; }while(temp&0x80); rs=0; w=0; P0=com; en=1; delaynms(1); en=0; delaynms(1); } void dat128(unsigned char dat) { unsigned char temp; do { rs=0; w=1; en=1; delaynms(1); temp=P0; en=0; }while(temp&0x80); rs=1; w=0; P0=dat; en=1; delaynms(1); en=0; delaynms(1); } void init128() { com128(0x30); com128(0x02); com128(0x04); com128(0x0c); com128(0x01); } void main() { unsigned char buf[8],i,*p,temp; init107(); init_ds1302(); init128(); com128(0x80); for(p=row1;*p;p++) { dat128(*p); } com128(0x90); for(p=row2;*p;p++) { dat128(*p); } com128(0x88); for(p=row3;*p;p++) { dat128(*p); } while(1) { for(i=7;i;i--) { buf[7-i]=read1302(table[7-i]); write1302(0x00,0x00); } //buf[0]秒,buf[1]分,buf[2]时,buf[3]日,buf[4]月,buf[5]周,buf[6]年 com128(0x81);//年 temp=buf[6]/16; dat128(temp+'0'); temp=buf[6]%16; dat128(temp+'0'); com128(0x83);//月 temp=buf[4]/16; dat128(temp+'0'); temp=buf[4]%16; dat128(temp+'0'); com128(0x85);//日 temp=buf[3]/16; dat128(temp+'0'); temp=buf[3]%16; dat128(temp+'0'); com128(0x90);//时 buf[2]&=0x3f; temp=buf[2]/16; dat128(temp+'0'); temp=buf[2]%16; dat128(temp+'0'); com128(0x92);//分 temp=buf[1]/16; dat128(temp+'0'); temp=buf[1]%16; dat128(temp+'0'); com128(0x94);//秒 temp=buf[0]/16; dat128(temp+'0'); temp=buf[0]%16; dat128(temp+'0'); com128(0x8a);//周 temp=buf[5]; if(temp%2==0) temp=temp+1; dat128(row[temp-1]); dat128(row[temp]); delaynms(800); } } 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- sceh.cn 版权所有 湘ICP备2023017654号-4
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务