1302芯片小时调整的怪问题?

2020-01-16 18:35发布

<ignore_js_op> <img src="https://www.xiaopingtou.net/data/attach/2001/5u5fgme5y3xijc6wf75zr59qnfowjcge.jpg">&nbsp;</ignore_js_op><p><br> <br> 程序用数码管显示是没问题的,调整分的时候小时会变成0,然后又恢复正常了,分钟调整是没问题的<br><br> <br> #include <regx52.h><br> #define WRITE_SECOND 0x80<br> #define WRITE_MINUTE 0x82<br> #define WRITE_HOUR 0x84<br> #define READ_SECOND 0x81<br> #define READ_MINUTE 0x83<br> #define READ_HOUR 0x85<br> #define WRITE_PROTECT 0x8E<br> sbit ACC_7=ACC^7;//位寻址寄存器定义<br> sbit SCLK=P2^0; // DS1302时钟信号&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;7脚<br> sbit DIO=P2^1;&nbsp;&nbsp;// DS1302数据信号&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;6脚<br> sbit CE=P2^2;&nbsp; &nbsp;// DS1302片选&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;5脚<br> unsigned char Hour_10_bit,Hour_0_bit,Min_10_bit,Min_0_bit,Sec_10_bit,Sec_0_bit,g_bSystemTime2Ms,Hour,Min,Sec;<br> void Write1302 ( unsigned char addr,dat ) //地址、数据发送子程序&nbsp; &nbsp; <br> {<br> &nbsp; &nbsp;&nbsp; &nbsp; unsigned char i,temp;<br> &nbsp; &nbsp;&nbsp; &nbsp; CE=0;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; //CE引脚为低,数据传送中止<br> &nbsp; &nbsp;&nbsp; &nbsp; SCLK=0;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//清零时钟总线<br> &nbsp; &nbsp;&nbsp; &nbsp; CE = 1;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//CE引脚为高,逻辑控制有效//发送地址<br> &nbsp; &nbsp;&nbsp; &nbsp; for ( i=8; i&gt;0; i-- ) //循环8次移位<br> &nbsp; &nbsp;&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp;&nbsp;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;SCLK=0;//上升沿准备<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;temp=addr;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;DIO=(bit)(temp&amp;0x01);//每次传输低字节<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;addr&gt;&gt;= 1;//右移一位<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;SCLK=1;//形成上升沿<br> &nbsp; &nbsp;&nbsp; &nbsp; }<br> &nbsp; &nbsp;&nbsp; &nbsp; <br> &nbsp; &nbsp;&nbsp; &nbsp; for ( i=8; i&gt;0; i-- ) <br> &nbsp; &nbsp;&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp;&nbsp;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;SCLK=0;//上升沿准备<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;temp=dat;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;DIO=(bit)(temp&amp;0x01);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;dat&gt;&gt;=1;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;SCLK=1;//形成上升沿<br> &nbsp; &nbsp;&nbsp; &nbsp; }<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;CE=0;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <br> }<br> unsigned char Read1302 (unsigned char addr )//数据读取子程序<br> {<br> &nbsp; &nbsp;&nbsp; &nbsp; unsigned char i,temp,dat1,dat2;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<br> &nbsp; &nbsp;&nbsp; &nbsp; CE=1; <br> &nbsp; &nbsp;&nbsp; &nbsp; <br> &nbsp; &nbsp;&nbsp; &nbsp; for ( i=8; i&gt;0; i-- ) //发送地址&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; //循环8次移位<br> &nbsp; &nbsp;&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp;&nbsp;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;SCLK=0;//上升沿准备<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;temp=addr;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;DIO=(bit)(temp&amp;0x01);//每次传输低字节<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;addr&gt;&gt;=1;//右移一位<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;SCLK=1; //形成上升沿<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br> &nbsp; &nbsp;&nbsp; &nbsp;<br> &nbsp; &nbsp;&nbsp; &nbsp; for ( i=8;i&gt;0;i--) <br> &nbsp; &nbsp;&nbsp; &nbsp; {<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ACC_7=DIO;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;SCLK=1;//下降沿准备<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ACC&gt;&gt;=1;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;SCLK=0;//形成下降沿<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}&nbsp; &nbsp;&nbsp;&nbsp;<br> &nbsp; &nbsp;&nbsp; &nbsp; CE=0;<br> &nbsp; &nbsp;&nbsp; &nbsp; dat1=ACC;<br> &nbsp; &nbsp;&nbsp; &nbsp; dat2=dat1/16;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//数据进制转换<br> &nbsp; &nbsp;&nbsp; &nbsp; dat1=dat1%16;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//十六进制转十进制<br> &nbsp; &nbsp;&nbsp; &nbsp; dat1=dat1+dat2*10;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp;&nbsp; &nbsp; return (dat1);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br> void Initial_DS1302( )//初始化DS1302&nbsp; &nbsp; <br> { <br> &nbsp; &nbsp;&nbsp; &nbsp; Write1302(WRITE_PROTECT,0X00);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; //关写保护<br> &nbsp; &nbsp;&nbsp; &nbsp; Write1302(WRITE_SECOND,0x40);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; //秒位初始化<br> &nbsp; &nbsp;&nbsp; &nbsp; Write1302(WRITE_MINUTE,0x59);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//分钟初始化<br> &nbsp; &nbsp;&nbsp; &nbsp; Write1302(WRITE_HOUR,0x02);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; //小时初始化<br> &nbsp; &nbsp;&nbsp; &nbsp; Write1302(WRITE_PROTECT,0x80);&nbsp; &nbsp;&nbsp; &nbsp;//开写保护<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br> void Read_1302( )<br> {<br> &nbsp; &nbsp;&nbsp; &nbsp; if(g_bSystemTime2Ms&gt;30)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;{&nbsp; &nbsp;&nbsp; &nbsp;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;TR0=0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; g_bSystemTime2Ms=0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;TR0=1;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Hour=Read1302(READ_HOUR);//读取时&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Min=Read1302(READ_MINUTE);//读取分<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Sec=Read1302(READ_SECOND);//读取秒 <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Hour_10_bit=Hour/10;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Hour_0_bit=Hour%10;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Min_10_bit=Min/10;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Min_0_bit=Min%10;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Sec_10_bit=Sec/10;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Sec_0_bit=Sec%10;<br> &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;} </regx52.h></p><p><br></p><p></p>