Rtcx.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "Rtcx.h"
  2. #include "string.h"
  3. #include "gpio.h"
  4. #include "stdio.h"
  5. #include "ac780x.h"
  6. DateTime g_dateTime;
  7. //DateTime tmpdateTime;
  8. #define ISL1208_IIC_ADDR (0x6F)
  9. #define ISL1208_SEC_Reg 0x00
  10. #define ISL1208_MIN_Reg 0x01
  11. #define ISL1208_HOUR_Reg 0x02
  12. #define ISL1208_DATE_Reg 0x03
  13. #define ISL1208_MONTH_Reg 0x04
  14. #define ISL1208_YEAR_Reg 0x05
  15. #define ISL1208_WEEK_Reg 0x06
  16. #define ISL1208_CFG_Reg 0x07
  17. #define FLAG_Reg (0x12)
  18. #define FLAG_VAL (0xAA)
  19. static uint8_t bin2bcd(uint8_t bin)
  20. {
  21. return (((bin/10) << 4) + bin%10);
  22. }
  23. static uint8_t bcd2bin(uint8_t bcd)
  24. {
  25. return ((bcd/16)*10 + bcd%16);
  26. }
  27. /**
  28. * @prototype I2C_ParaInit(void)
  29. *
  30. * @param[in] void
  31. * @return void
  32. *
  33. * @brief 初始化I2C.
  34. */
  35. void I2C_ParaInit(void)
  36. {
  37. I2C_ConfigType i2cConfig;
  38. /*初始化引脚功能为I2C.*/
  39. //GPIO_SetFunc(I2C0_SCL_PORT, I2C0_SCL_PIN, GPIO_FUN3);
  40. //GPIO_SetFunc(I2C0_SDA_PORT, I2C0_SDA_PIN, GPIO_FUN3);
  41. /*清零结构体变量.*/
  42. memset(&i2cConfig, 0x00, sizeof(i2cConfig));
  43. /*无论是主机还是从机模式都需要配置的参数.*/
  44. i2cConfig.mode = I2C_MASTER;/*设置主从机模式.*/
  45. i2cConfig.extAddrEn = DISABLE;/*10bit扩展地址禁能.*/
  46. i2cConfig.dmaRxEn = DISABLE;/*设置DMA接收数据.*/
  47. i2cConfig.dmaTxEn = DISABLE;/*设置DMA发送数据.*/
  48. i2cConfig.interruptEn = DISABLE;/*I2C中断,BND/SAMF/ARBLOST.*/
  49. i2cConfig.nackInterruptEn = DISABLE;/*NACK中断.*/
  50. i2cConfig.ssInterruptEn = DISABLE;/*总线start或stop中断.*/
  51. i2cConfig.i2cEn = ENABLE;/*使能模块.*/
  52. i2cConfig.callBack = NULL;/*中断回调函数.*/
  53. /*主机模式需要配置的参数,配置成从机模式可忽略.*/
  54. i2cConfig.masterConfig.sampleCnt = 9;/*设置波特率为100Kbps,bandrate=(24M/(10*12*2))=100Kbps.*/
  55. i2cConfig.masterConfig.stepCnt = 11;
  56. i2cConfig.masterConfig.ARBEn = ENABLE;/*设置主机仲裁功能.*/
  57. i2cConfig.masterConfig.SYNCEn = ENABLE;/*设置主机SCL同步功能.*/
  58. /*从机模式需要配置的参数,配置成主机模式可忽略.*/
  59. //i2cConfig.slaveConfig.slaveAddr = AT24C02_DEV_ADDR;/*从机地址.*/
  60. //i2cConfig.slaveConfig.slaveRangeAddr = 0;/*从机范围地址.*/
  61. //i2cConfig.slaveConfig.glitchFilterCnt= 0;/*毛刺过滤.*/
  62. //i2cConfig.slaveConfig.stretchEn = DISABLE;/*从机SCL延伸功能.*/
  63. //i2cConfig.slaveConfig.rangeAddrEn = DISABLE;/*禁能范围地址.*/
  64. //i2cConfig.slaveConfig.monitorEn = DISABLE;/*禁能从机监测功能.*/
  65. //i2cConfig.slaveConfig.generalCallEn = DISABLE;/*从机SCL广播地址.*/
  66. //i2cConfig.slaveConfig.wakeupEn = DISABLE;/*唤醒功能,仅从机时有效.*/
  67. //i2cConfig.slaveConfig.rxOFInterruptEn = DISABLE;/*接收缓存溢出中断.*/
  68. //i2cConfig.slaveConfig.txUFInterruptEn = DISABLE;/*发送缓存溢出中断.*/
  69. I2C_Init(I2C1, &i2cConfig);
  70. }
  71. static uint8_t isl1208_readbyte(uint8_t addr)
  72. {
  73. uint8_t data;
  74. I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, &addr, 1, DISABLE);//发送读取地址
  75. I2C_MasterReceivePoll(I2C1, ISL1208_IIC_ADDR, &data, 1);//读取数据
  76. return data;
  77. }
  78. static void isl1208_writebyte(uint8_t addr, uint8_t data)
  79. {
  80. uint8_t buffer[2];
  81. buffer[0] = addr;
  82. buffer[1] = data;
  83. I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, buffer, 2, ENABLE);//发送读取地址
  84. }
  85. void RTCx_SetTime(DateTime* pDateTime)
  86. {
  87. uint8_t addr = 0x00;
  88. uint8_t buffer[8];
  89. pDateTime->year-=2000; //年默认2000年开始
  90. if(pDateTime->year > 100) pDateTime->year = 0;
  91. pDateTime->week -= 1;
  92. //cfg = ht1382_readbyte(HT1382_CFG_Reg);
  93. buffer[0] = addr;
  94. buffer[1] = bin2bcd(pDateTime->second);
  95. buffer[2] = bin2bcd(pDateTime->minute);
  96. buffer[3] = bin2bcd(pDateTime->hour);
  97. buffer[3] |= 0x80; //时 24小时模式
  98. buffer[4] = bin2bcd(pDateTime->day);
  99. buffer[5] = bin2bcd(pDateTime->month);
  100. buffer[6] = bin2bcd(pDateTime->year);
  101. buffer[7] = bin2bcd(pDateTime->week);
  102. isl1208_writebyte(ISL1208_CFG_Reg,0x90); //写控制寄存器、 开启写使能、暂停时钟晶振、状态自动复位
  103. I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, buffer, 8, ENABLE);//发送读取地址
  104. printf("RTC_SetTime end \r\n");
  105. }
  106. static void RTCx_GetTime(void)
  107. {
  108. uint8_t addr = 0x00;
  109. uint8_t data[7];
  110. I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, &addr, 1, DISABLE);//发送读取地址
  111. I2C_MasterReceivePoll(I2C1, ISL1208_IIC_ADDR, data, 7);//读取数据
  112. data[2] = 0x7F&data[2];
  113. //BCD码转十进制
  114. g_dateTime.second = bcd2bin(data[0]);
  115. g_dateTime.minute = bcd2bin(data[1]);
  116. g_dateTime.hour = bcd2bin(data[2]);
  117. g_dateTime.day = bcd2bin(data[3]);
  118. g_dateTime.month = bcd2bin(data[4]);
  119. g_dateTime.year = bcd2bin(data[5])+2000;
  120. g_dateTime.week = bcd2bin(data[6])+1;
  121. }
  122. void RTCx_UpdateTime(void)
  123. {
  124. RTCx_GetTime();
  125. }
  126. void RTCx_PrintDateTime(void)
  127. {
  128. RTCx_GetTime();
  129. printf("DateTime:[%d][%d][%d] [%d][%d][%d] week[%d] \r\n",g_dateTime.year, g_dateTime.month, g_dateTime.day, g_dateTime.hour, g_dateTime.minute, g_dateTime.second, g_dateTime.week);
  130. }
  131. /*
  132. *********************************************************************************************************
  133. * 函 数 名: void DS1307_Init_Time(void)
  134. * 功能说明: 第一次上电时,需要初始化时间,初始化一次后就不必重复初始化
  135. * 形 参:无
  136. * 返 回 值: 无
  137. *********************************************************************************************************
  138. */
  139. void RTCx_Init(void)
  140. {
  141. I2C_ParaInit();
  142. if(FLAG_VAL != isl1208_readbyte(FLAG_Reg))//如果未初始化,执行初始化时间
  143. {
  144. g_dateTime.year = 2024;
  145. g_dateTime.month = 7;
  146. g_dateTime.day = 12;
  147. g_dateTime.hour = 11;
  148. g_dateTime.minute = 56;
  149. g_dateTime.second = 20;
  150. g_dateTime.week = 5;
  151. RTCx_SetTime(&g_dateTime);
  152. isl1208_writebyte(FLAG_Reg, FLAG_VAL);
  153. } else{
  154. RTCx_PrintDateTime();
  155. }
  156. }
  157. void RTCx_DeInit(void)
  158. {
  159. }