找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1460|回复: 1
打印 上一主题 下一主题
收起左侧

单片机驱动实时时钟芯片S35390A读取时间有误 求分析 附程序

[复制链接]
回帖奖励 100 黑币 回复本帖可获得 10 黑币奖励! 每人限 1 次
跳转到指定楼层
楼主
S35390A通过IIC读写,读取的时间与写入的时间不一致,读出来的数据只有年是对的,不知道问题出在哪里,请大佬帮忙分析

写入时间为:S35390A[6]={0x16,0x0C,0x17,0x0E,0x24,0x00}; //22-12-23 14:36:00

读出时间为:

以下是单片机程序部分:
  1. #include <stdio.h>
  2. #include <reg52.h>
  3. #include <35390.h>
  4. //#include <S-35390A.h>

  5. void InitUART(void)//使用定时器1作为串口波特率发生器
  6. {
  7. TH1 = 0xFD; //晶振11.0592mhz 波特率设为9600
  8. TL1 = TH1;
  9. TMOD |= 0x20; //定时器1方式2
  10. SCON = 0x50; //串口接收使能
  11. ES = 1; //串口中断使能
  12. TR1 = 1; //定时器1使能
  13. TI = 1; //发送中断标记位,必须设置
  14. }
  15. //毫秒延时函数
  16. void delay_ms(unsigned int t)
  17. {
  18. unsigned char a,b;
  19. while(t--)
  20. {
  21. for(b=102;b>0;b--)
  22. for(a=3;a>0;a--);
  23. }
  24. }

  25. void main()
  26. {
  27. InitUART(); //初始化串口
  28. EA = 1; //开总中断
  29. Write_S35390A();//初始化时间
  30. while(1)
  31. {
  32. Read_S35390A();//读取时间

  33. printf("S35390A[0]=%02BX\r\nS35390A[1]=%02BX\r\nS35390A[2]=%02BX\r\n",S35390A[0],S35390A[1],S35390A[2]);
  34. printf("S35390A[3]=%02BX\r\nS35390A[4]=%02BX\r\nS35390A[5]=%02BX\r\n",S35390A[3],S35390A[4],S35390A[5]);
  35. }
  36. }

  37. 35390.h

  38. #define Uchar unsigned char
  39. #define Uint unsigned int
  40. unsigned char S35390A[6]={0x16,0x0C,0x17,0x0E,0x24,0x00};// 22-12-23 14:36:00

  41. sbit IIC_SCL = P2^3;
  42. sbit IIC_SDA = P2^2;

  43. void delayms(Uint n)
  44. {
  45. Uint i,j;
  46. for(i=n;i>0;i--)
  47. {
  48. for(j=750;j>0;j--)
  49. {
  50. }
  51. }
  52. }

  53. /*****************************************
  54. I2c Wait
  55. Wait for some time to get proper I2C timing
  56. ******************************************/
  57. void IICWait(void)
  58. {
  59. unsigned int i=10;//i=5;
  60. while(i--);
  61. }


  62. /*****************************************
  63. i2c start
  64. condition SDA 1-0 while SCL=1
  65. ******************************************/
  66. void IICStart(void)
  67. {
  68. IIC_SDA=1;
  69. IIC_SCL=1;
  70. IICWait();
  71. IIC_SDA=0;
  72. IICWait();
  73. IIC_SCL=0;
  74. IICWait();
  75. }


  76. /*****************************************
  77. I2c sotp
  78. condition SDA=0-1 while SCL=1
  79. ******************************************/
  80. void IICStop(void)
  81. {
  82. IIC_SDA=0;
  83. IICWait();
  84. IIC_SCL=1;
  85. IICWait();
  86. IIC_SDA=1;
  87. IICWait();
  88. }


  89. /*****************************************
  90. 35390 Init
  91. Initialize I2C interface
  92. Release I2c BUS
  93. ****************************************

  94. void S35390_Init(void)
  95. {
  96. IIC_SDA=1;
  97. IIC_SCL=1;
  98. IIC_Wait();
  99. }

  100. /*****************************************
  101. I2c SentByte
  102. master transfer data to slave and return acknowledge bit
  103. don't include<intrins.h>
  104. ******************************************/
  105. bit IICSentByte(unsigned char bytedata)
  106. {
  107. unsigned char i;
  108. bit ack;

  109. for(i=0;i<8;i++)
  110. {
  111. if(bytedata & 0x80)
  112. IIC_SDA=1;
  113. else
  114. IIC_SDA=0;
  115. bytedata<<=1;
  116. IICWait();

  117. IIC_SCL=1;
  118. IICWait();
  119. IIC_SCL=0;
  120. IICWait();
  121. }
  122. IIC_SDA=1; //释放数据总线,等待应答
  123. IICWait();
  124. IIC_SCL=1; //等待数据线的ACK(时钟信号一般上升沿有效)
  125. IICWait();
  126. ack=IIC_SDA;
  127. IIC_SCL=0;
  128. IICWait();
  129. return ack;
  130. }


  131. /*****************************************
  132. I2c ReceiveByte
  133. slave trransfer data to master
  134. ******************************************/
  135. unsigned char IICReceiveByte(void)
  136. {
  137. unsigned char i;
  138. unsigned char bytedata=0;


  139. IIC_SCL=0;
  140. IICWait();
  141. for(i=0;i<8;i++)
  142. {
  143. IIC_SCL=1;
  144. IICWait();
  145. bytedata=bytedata<<1;
  146. if(IIC_SDA==1)
  147. {
  148. bytedata = bytedata|0x01;
  149. }

  150. IICWait();
  151. IIC_SCL=0;
  152. IICWait();
  153. }
  154. return bytedata;
  155. }


  156. /*****************************************
  157. I2c SendAcknowledge
  158. Master send acknowledge bit to slave
  159. acknowledge="0",non-acknowledge="1"
  160. ******************************************/
  161. void SendAck(bit ack)
  162. {
  163. IIC_SDA=ack;
  164. IIC_SCL=1;
  165. IICWait();
  166. IIC_SCL=0;
  167. }

  168. unsigned char Exchange_highlow(unsigned char car)
  169. {
  170. unsigned i,c=0;
  171. for(i=0;i<8;i++)
  172. {c=c*2+(car-2*(car>>1));car=car>>1;}
  173. return c;
  174. }


  175. void Write_S35390A(void)
  176. {
  177. unsigned char car;
  178. IICStart(); //Send start signal
  179. IICSentByte(0x60); //Send identifer IIC address,状态寄存器1
  180. IICSentByte(0xe0); //reset 0xe0
  181. IICStop();
  182. delayms(10); // Delay a period of time to write

  183. IICStart(); //Send start signal
  184. IICSentByte(0x61); //Send identifer IIC address,状态寄存器1

  185. IICStop();
  186. car=IICReceiveByte();
  187. if(car)
  188. {
  189. car^=0xff;
  190. }

  191. IICStart(); //Send start signal
  192. IICSentByte(0x68); //Send identifer IIC address,INT1
  193. IICSentByte(0x08); //16Hz
  194. IICStop();

  195. IICStart(); //Send start signal
  196. IICSentByte(0x62); //Send identifer IIC address,状态寄存器2
  197. IICSentByte(0x80); //INT1频率输出
  198. IICStop();

  199. IICStart(); //Send start signal
  200. IICSentByte(0x6C); //Send identifer IIC address,时钟较正
  201. IICSentByte(0x86); //较正值
  202. IICStop();

  203. // Send repeated start signal
  204. S35390A[0]=Exchange_highlow(S35390A[0]);
  205. S35390A[1]=Exchange_highlow(S35390A[1]);
  206. S35390A[2]=Exchange_highlow(S35390A[2]);

  207. S35390A[3]=Exchange_highlow(S35390A[3]);
  208. S35390A[4]=Exchange_highlow(S35390A[4]);

  209. IICStart(); // Send start signal
  210. IICSentByte(0x64); // Send identifer IIC address
  211. IICSentByte(S35390A[0]); //year
  212. IICSentByte(S35390A[1]); //month
  213. IICSentByte(S35390A[2]); //day
  214. IICSentByte(0); //week;
  215. IICSentByte(S35390A[3]); //hour
  216. IICSentByte(S35390A[4]); //min
  217. IICSentByte(0); //second
  218. IICStop();
  219. }


  220. void Read_S35390A(void)
  221. {
  222. unsigned char car;
  223. IICStart(); // Send start signal
  224. IICSentByte(0x65); // Send identifer IIC address
  225. // Send repeated start signal
  226. car=IICReceiveByte();S35390A[0]=Exchange_highlow(car); //year
  227. car=IICReceiveByte();S35390A[1]=Exchange_highlow(car); //mon
  228. car=IICReceiveByte();S35390A[2]=Exchange_highlow(car); //day
  229. car=IICReceiveByte(); //week;
  230. car=IICReceiveByte();S35390A[3]=Exchange_highlow(car); //hour
  231. *(&S35390A[3])=*(&S35390A[3])&0x3f; //???
  232. car=IICReceiveByte();S35390A[4]=Exchange_highlow(car); //min
  233. car=IICReceiveByte();S35390A[5]=Exchange_highlow(car); //second
  234. IICStop();
  235. }

复制代码

S35390A_datasheet.pdf

708.92 KB, 下载次数: 1

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:584814 发表于 2021-12-24 08:55 | 只看该作者
网上扫了一下有这样的描述看了和通常用的RTC读算法不一样供参考
百度"S35390驱动程序"即可看到
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表