当前位置:   article > 正文

S-35390A计时芯片介绍及开发方案

S-35390A计时芯片介绍及开发方案

计时芯片

S-35390A芯片是计时芯片,一般用来计算时间。低功耗,宽电压,受温度影响小,适用于很多电路。它有一个问题,不阻止用户设置不存在的时间,设置进去之后计时或者闹钟定时会出错。

规格书阅读

首先我们先读懂这个芯片是怎么用的。

引脚表

基本上一看这个引脚表就知道大概,1号和5号引脚是中断输出引脚,2号和3号引脚之间应该接个晶振,6号和7号引脚通过IIC跟主控芯片通讯,其余的都是电源和地。

模块框图

大概就能知道里面的框架是啥样的,里面最主要就是实时时间的寄存器,通过跟中断1和中断2寄存器对比决定两路中断的输出。具体时间通过中间的总线借助IIC串口通讯模块发出去。另外就是有几个检测电源电压的寄存器、状态寄存器、时钟校正寄存器和一个给用户可以当做E方用的自由寄存器。

通讯数据配置

跟普通的IIC一样,主控芯片先发个设备号0110,跟着3位的命令,1位的读写方向,等到计时芯片ACK之后,继续发送剩下的数据。

命令表格

这里有所有的命令,读写状态寄存器1和2、读写实时数据1和2、读写中断寄存器1和2、存取时钟校正寄存器和自由寄存器。

标1的是只写。标2的是用户自由寄存器,对应上面框图的用户自由寄存器。标3和4的是只读。标5的是测试位,使用的时候要保证为0。标6的怎么写都不影响。

状态寄存器1

POC检测电源电流,初始化的时候是0,有电流的时候是1,如果上电之后为0,就要重启整个芯片。

BLD检测电源电压,正常为0,读到是1的时候要重启整个芯片了。

INT1和INT2是中断事件发生标志位。

SC0和SC1都是用户自由定义的。

12/24如果是0就是12小时制,如果是1就是24小时制。

RESET平时为0,写个1进去就重启芯片。

状态寄存器2

TEST测试位设置为0就行,当设置为1的时候,INT1会输出1Hz的方波。

INT2AE、 INT2ME、 INT2FE共同决定了中断输出2引脚的输出模式:无中断、输出用户设定的频率、每分钟边沿中断、50%占空比的每分钟周期中断、闹钟模式。

INT1AE、 INT1ME、 INT1FE共同决定了中断输出1引脚的输出模式:无中断、输出用户设定的频率、每分钟边沿中断、50%占空比的每分钟周期中断、闹钟模式、每分钟周期中断、输出32.768kHz。

实时数据1和2

以写入实时数据寄存器1为例,流程如下。

里面的年是按照西历来计算的,只能写0-99,使用BCD码,低位在前。

譬如2053年,就写入53,二进制Y1-Y80就是1100 1010.

AM/PM如果是24小时制就无所谓写什么,如果是12小时制0是am,1就是pm.

后面的实时数据寄存器2也是一样。

中断寄存器1和2

根据状态寄存器2的设置,选择中断输出引脚是闹钟模式或者输出用户设定的频率

INT1AE = 1, INT1ME = 0, INT1FE = 0,就是闹钟模式。
INT1ME = 0, INT1FE = 1,就是输出用户设定的频率。
其实就是对应状态寄存器那个表格,INT2也是一样。

闹钟模式

时间到了就触发中断,里面的AM/PM要跟实时时钟里面一致。

B0的三个标志位表示设置的周、时、分是否有效,譬如下面这个设置闹钟为每天晚上7点,周的标志位就需要为0.

输出用户设定的频率

频率是每个位累加的,SC是用户自由寄存器。

校准寄存器

可以提前和延迟一定的时间。

用户自由寄存器

写一些用户想存储的信息,当做E方使用

使用注意事项

上电后POC为0,或者POC为1但是INT没有输出1 Hz方波,芯片内部数据可能处于不确定状态,需要重新上电。
上电之后由于检测电路正在工作,在上电后至少0.5秒后才进行数据传输。

接口编写

基本定义

命令

  1. #define S35390_TIME_ADDRESS 0x32
  2. #define S35390_STATUS1_ADDRESS 0x30
  3. #define S35390_STATUS2_ADDRESS 0x31
  4. #define S35390_INT1_REG_ADDRESS 0x34
  5. #define S35390_INT2_REG_ADDRESS 0x35

当前时间结构体和中断闹钟结构体

  1. typedef struct
  2. {
  3. unsigned int year; //year
  4. unsigned char month; //month
  5. unsigned char day; //day
  6. unsigned char hour; //hour
  7. unsigned char minute; //minute
  8. unsigned char second; //second
  9. unsigned char week; //week
  10. }rtc_time;
  11. typedef struct
  12. {
  13. unsigned char minute; //minute
  14. unsigned char hour; //hour
  15. unsigned char week; //week
  16. unsigned char min_en; //minute enable
  17. unsigned char hour_en; //hour enable
  18. unsigned char week_en; //min enable
  19. }rtc_alarm;

接口

RTC_S35390_Init

初始化函数里面只是要配置下主控芯片的IIC外设,设置下上电默认时间。

S35390_ReadRegs

读取多字节数据

  1. bool S35390_ReadRegs(uint8_t addr, uint8_t length,uint8_t *values)
  2. {
  3. status_t ret=STATUS_BUSY;
  4. uint8_t Try = 3;
  5. if(xSemaphore_i2c != NULL )
  6. {
  7. if( xSemaphoreTake( xSemaphore_i2c, ( TickType_t ) 50 ) == pdTRUE )//获取信号量
  8. {
  9. while((Try--)&&(ret != STATUS_SUCCESS))//最多尝试3次
  10. {
  11. lpi2cMasterState.slaveAddress = addr;//读取地址
  12. /* Request data from the bus slave */
  13. ret = I2C_DRV_MasterReceiveDataBlock(INST_LPI2C1,values,length,true,10);
  14. }
  15. xSemaphoreGive( xSemaphore_i2c);//释放信号量
  16. }
  17. }
  18. if(ret != STATUS_SUCCESS)//读出失败
  19. {
  20. I2C_Reset();//重新初始化IIC模块
  21. }
  22. return ret;
  23. }

S35390_WriteRegs

读取多字节数据

  1. static bool S35390_WriteRegs(uint8_t addr, uint8_t length, uint8_t *values)
  2. {
  3. status_t ret=STATUS_BUSY;
  4. uint8_t Try = 3;
  5. uint8_t masterbuffer[32]={0};
  6. memcpy(&masterbuffer[0],values,length);
  7. if( xSemaphore_i2c != NULL )
  8. {
  9. if( xSemaphoreTake( xSemaphore_i2c, ( TickType_t ) 50 ) == pdTRUE )//获取信号量
  10. {
  11. while((Try--)&&(ret != STATUS_SUCCESS))//最多尝试3次
  12. {
  13. lpi2cMasterState.slaveAddress = addr;//写入地址
  14. ret=I2C_DRV_MasterSendDataBlock(INST_LPI2C1, masterbuffer, length, true,100UL);
  15. }
  16. xSemaphoreGive( xSemaphore_i2c);//释放信号量
  17. }
  18. }
  19. if(ret != STATUS_SUCCESS)//写入失败
  20. {
  21. I2C_Reset();//重新初始化IIC模块
  22. }
  23. return ret;
  24. }

S35390_GetYear

别被这个名字骗了,其实就是将获取的年数据BCD码转成十进制数据。

  1. static uint8_t S35390_GetYear(uint8_t regYearData )
  2. {
  3. uint8_t timeYearData = 0x00;
  4. if ( regYearData & 1 )
  5. {
  6. timeYearData += 80;
  7. }
  8. regYearData >>= 1;
  9. if ( regYearData & 1 )
  10. {
  11. timeYearData += 40;
  12. }
  13. regYearData >>= 1;
  14. if ( regYearData & 1 )
  15. {
  16. timeYearData += 20;
  17. }
  18. regYearData >>= 1;
  19. if ( regYearData & 1 )
  20. {
  21. timeYearData += 10;
  22. }
  23. regYearData >>= 1;
  24. if ( regYearData & 1 )
  25. {
  26. timeYearData += 8;
  27. }
  28. regYearData >>= 1;
  29. if ( regYearData & 1 )
  30. {
  31. timeYearData += 4;
  32. }
  33. regYearData >>= 1;
  34. if ( regYearData & 1 )
  35. {
  36. timeYearData += 2;
  37. }
  38. regYearData >>= 1;
  39. if ( regYearData & 1 )
  40. {
  41. timeYearData += 1;
  42. }
  43. if ( timeYearData > 99 )//最大限制
  44. {
  45. timeYearData = 99;
  46. }
  47. else
  48. {
  49. ; /* do nothing*/
  50. }
  51. return timeYearData;
  52. }

S35390_GetMonth

获取月数据BCD码转成十进制数据。

  1. static uint8_t S35390_GetMonth(uint8_t regMonthData )
  2. {
  3. uint8_t timeMonthData = 0x00;
  4. regMonthData >>= 3;
  5. if ( regMonthData & 1 )
  6. {
  7. timeMonthData += 10;
  8. }
  9. regMonthData >>= 1;
  10. if ( regMonthData & 1 )
  11. {
  12. timeMonthData += 8;
  13. }
  14. regMonthData >>= 1;
  15. if ( regMonthData & 1 )
  16. {
  17. timeMonthData += 4;
  18. }
  19. regMonthData >>= 1;
  20. if ( regMonthData & 1 )
  21. {
  22. timeMonthData += 2;
  23. }
  24. regMonthData >>= 1;
  25. if ( regMonthData & 1 )
  26. {
  27. timeMonthData += 1;
  28. }
  29. if ( timeMonthData == 0x00 )//最小限制
  30. {
  31. timeMonthData = 0x01;
  32. }
  33. else if ( timeMonthData > 12 )//最大限制
  34. {
  35. timeMonthData = 12;
  36. }
  37. else
  38. {
  39. ; /* do nothing*/
  40. }
  41. return timeMonthData;
  42. }

S35390_GetDay

获取日数据BCD码转成十进制数据。

  1. static uint8_t S35390_GetDay(uint8_t regDayData )
  2. {
  3. uint8_t timeDayData = 0x00;
  4. regDayData >>= 2;
  5. if ( regDayData & 1 )
  6. {
  7. timeDayData += 20;
  8. }
  9. regDayData >>= 1;
  10. if ( regDayData & 1 )
  11. {
  12. timeDayData += 10;
  13. }
  14. regDayData >>= 1;
  15. if ( regDayData & 1 )
  16. {
  17. timeDayData += 8;
  18. }
  19. regDayData >>= 1;
  20. if ( regDayData & 1 )
  21. {
  22. timeDayData += 4;
  23. }
  24. regDayData >>= 1;
  25. if ( regDayData & 1 )
  26. {
  27. timeDayData += 2;
  28. }
  29. regDayData >>= 1;
  30. if ( regDayData & 1 )
  31. {
  32. timeDayData += 1;
  33. }
  34. if ( timeDayData == 0 )//最小限制
  35. {
  36. timeDayData = 1;
  37. }
  38. else if ( timeDayData > 31 )//最大限制
  39. {
  40. timeDayData = 31;
  41. }
  42. else
  43. {
  44. ; /* do nothing*/
  45. }
  46. return timeDayData;
  47. }

S35390_GetWeek

获取周数据BCD码转成十进制数据。

  1. static uint8_t S35390_GetWeek(uint8_t regWeekData )
  2. {
  3. uint8_t timeWeekData = 0x00;
  4. regWeekData >>= 5;
  5. if ( regWeekData & 1 )
  6. {
  7. timeWeekData += 4;
  8. }
  9. regWeekData >>= 1;
  10. if ( regWeekData & 1 )
  11. {
  12. timeWeekData += 2;
  13. }
  14. regWeekData >>= 1;
  15. if ( regWeekData & 1 )
  16. {
  17. timeWeekData += 1;
  18. }
  19. return timeWeekData;
  20. }

S35390_GetHour

获取时数据BCD码转成十进制数据。

  1. static uint8_t S35390_GetHour(uint8_t regHourData )
  2. {
  3. uint8_t timeHourData = 0x00;
  4. regHourData >>= 2;
  5. if ( regHourData & 1 )
  6. {
  7. timeHourData += 20;
  8. }
  9. regHourData >>= 1;
  10. if ( regHourData & 1 )
  11. {
  12. timeHourData += 10;
  13. }
  14. regHourData >>= 1;
  15. if ( regHourData & 1 )
  16. {
  17. timeHourData += 8;
  18. }
  19. regHourData >>= 1;
  20. if ( regHourData & 1 )
  21. {
  22. timeHourData += 4;
  23. }
  24. regHourData >>= 1;
  25. if ( regHourData & 1 )
  26. {
  27. timeHourData += 2;
  28. }
  29. regHourData >>= 1;
  30. if ( regHourData & 1 )
  31. {
  32. timeHourData += 1;
  33. }
  34. if ( timeHourData > 23 )//最大限制
  35. {
  36. timeHourData = 23;
  37. }
  38. else
  39. {
  40. ; /* do nothing*/
  41. }
  42. return timeHourData;
  43. }

S35390_GetMinute

获取分数据BCD码转成十进制数据。

  1. static uint8_t S35390_GetMinute(uint8_t regMinuteData )
  2. {
  3. uint8_t timeMinuteData = 0x00;
  4. regMinuteData >>= 1;
  5. if ( regMinuteData & 1 )
  6. {
  7. timeMinuteData += 40;
  8. }
  9. regMinuteData >>= 1;
  10. if ( regMinuteData & 1 )
  11. {
  12. timeMinuteData += 20;
  13. }
  14. regMinuteData >>= 1;
  15. if ( regMinuteData & 1 )
  16. {
  17. timeMinuteData += 10;
  18. }
  19. regMinuteData >>= 1;
  20. if ( regMinuteData & 1 )
  21. {
  22. timeMinuteData += 8;
  23. }
  24. regMinuteData >>= 1;
  25. if ( regMinuteData & 1 )
  26. {
  27. timeMinuteData += 4;
  28. }
  29. regMinuteData >>= 1;
  30. if ( regMinuteData & 1 )
  31. {
  32. timeMinuteData += 2;
  33. }
  34. regMinuteData >>= 1;
  35. if ( regMinuteData & 1 )
  36. {
  37. timeMinuteData += 1;
  38. }
  39. if ( timeMinuteData > 59 )//最大限制
  40. {
  41. timeMinuteData = 59;
  42. }
  43. else
  44. {
  45. ; /* do nothing*/
  46. }
  47. return timeMinuteData;
  48. }

S35390_SetYear

别被它名字骗了,这个接口只是将年数据转换成BCD码,然后返回回去。

  1. static uint8_t S35390_SetYear(uint16_t timeYearData )
  2. {
  3. uint8_t regYearData = 0x00;
  4. if (timeYearData >= 2000)//允许入参格式为2000+的
  5. {
  6. timeYearData -= 2000;
  7. }
  8. else
  9. {
  10. ; /* do nothing*/
  11. }
  12. if (timeYearData > 99)//限制入参大小
  13. {
  14. timeYearData = 0;
  15. }
  16. else
  17. {
  18. ; /* do nothing*/
  19. }
  20. if ((timeYearData/80) == 1)
  21. {
  22. regYearData |= 0x01;
  23. timeYearData -= 80;
  24. }
  25. if ((timeYearData/40) == 1)
  26. {
  27. regYearData |= 0x02;
  28. timeYearData -= 40;
  29. }
  30. if ((timeYearData/20) == 1)
  31. {
  32. regYearData |= 0x04;
  33. timeYearData -= 20;
  34. }
  35. if ((timeYearData/10) == 1)
  36. {
  37. regYearData |= 0x08;
  38. timeYearData -= 10;
  39. }
  40. if ((timeYearData/8) == 1)
  41. {
  42. regYearData |= 0x10;
  43. timeYearData -= 8;
  44. }
  45. if ((timeYearData/4) == 1)
  46. {
  47. regYearData |= 0x20;
  48. timeYearData -= 4;
  49. }
  50. if ((timeYearData/2) == 1)
  51. {
  52. regYearData |= 0x40;
  53. timeYearData -= 2;
  54. }
  55. if (timeYearData == 1)
  56. {
  57. regYearData |= 0x80;
  58. }
  59. return regYearData;
  60. }

S35390_SetMonth

将月数据转换成BCD码,然后返回回去。

  1. static uint8_t S35390_SetMonth(uint8_t timeMonthData )
  2. {
  3. uint8_t regMonthData = 0x00;
  4. if (timeMonthData == 0x00)//限制最小
  5. {
  6. timeMonthData = 0x01;
  7. }
  8. else if (timeMonthData > 12)//限制最大
  9. {
  10. timeMonthData = 1;
  11. }
  12. else
  13. {
  14. ; /* do nothing*/
  15. }
  16. if ((timeMonthData/10) == 1)
  17. {
  18. regMonthData |= 0x08;
  19. timeMonthData -= 10;
  20. }
  21. if ((timeMonthData/8) == 1)
  22. {
  23. regMonthData |= 0x10;
  24. timeMonthData -= 8;
  25. }
  26. if ((timeMonthData/4) == 1)
  27. {
  28. regMonthData |= 0x20;
  29. timeMonthData -= 4;
  30. }
  31. if ((timeMonthData/2) == 1)
  32. {
  33. regMonthData |= 0x40;
  34. timeMonthData -= 2;
  35. }
  36. if (timeMonthData == 1)
  37. {
  38. regMonthData |= 0x80;
  39. }
  40. return regMonthData;
  41. }

S35390_SetDay

将日数据转换成BCD码,然后返回回去。

  1. static uint8_t S35390_SetDay(uint8_t timeDayData )
  2. {
  3. uint8_t regDayData = 0x00;
  4. if (timeDayData == 0x00)//限制最小
  5. {
  6. timeDayData = 0x01;
  7. }
  8. else if (timeDayData > 31)//限制最大
  9. {
  10. timeDayData = 1;
  11. }
  12. else
  13. {
  14. ; /* do nothing*/
  15. }
  16. if ((timeDayData/20) == 1)
  17. {
  18. regDayData |= 0x04;
  19. timeDayData -= 20;
  20. }
  21. if ((timeDayData/10) == 1)
  22. {
  23. regDayData |= 0x08;
  24. timeDayData -= 10;
  25. }
  26. if ((timeDayData/8) == 1)
  27. {
  28. regDayData |= 0x10;
  29. timeDayData -= 8;
  30. }
  31. if ((timeDayData/4) == 1)
  32. {
  33. regDayData |= 0x20;
  34. timeDayData -= 4;
  35. }
  36. if ((timeDayData/2) == 1)
  37. {
  38. regDayData |= 0x40;
  39. timeDayData -= 2;
  40. }
  41. if (timeDayData == 1)
  42. {
  43. regDayData |= 0x80;
  44. }
  45. return regDayData;
  46. }

S35390_SetWeek

将周数据转换成BCD码,然后返回回去。

  1. static uint8_t S35390_SetWeek(uint8_t timeWeekData )
  2. {
  3. uint8_t regWeekData = 0x00;
  4. if (timeWeekData > 6)//限制最大
  5. {
  6. timeWeekData = 0;
  7. }
  8. else
  9. {
  10. ; /* do nothing*/
  11. }
  12. if ((timeWeekData/4) == 1)
  13. {
  14. regWeekData |= 0x20;
  15. timeWeekData -= 4;
  16. }
  17. if ((timeWeekData/2) == 1)
  18. {
  19. regWeekData |= 0x40;
  20. timeWeekData -= 2;
  21. }
  22. if (timeWeekData == 1)
  23. {
  24. regWeekData |= 0x80;
  25. }
  26. return regWeekData;
  27. }

S35390_SetHour

将时数据转换成BCD码,然后返回回去。

  1. static uint8_t S35390_SetHour(uint8_t timeHourData)
  2. {
  3. uint8_t regHourData = 0x00;
  4. if (timeHourData > 23)//限制最大
  5. {
  6. timeHourData = 0;
  7. }
  8. else
  9. {
  10. ; /* do nothing*/
  11. }
  12. if ((timeHourData/20) == 1)
  13. {
  14. regHourData |= 0x04;
  15. timeHourData -= 20;
  16. }
  17. if ((timeHourData/10) == 1)
  18. {
  19. regHourData |= 0x08;
  20. timeHourData -= 10;
  21. }
  22. if ((timeHourData/8) == 1)
  23. {
  24. regHourData |= 0x10;
  25. timeHourData -= 8;
  26. }
  27. if ((timeHourData/4) == 1)
  28. {
  29. regHourData |= 0x20;
  30. timeHourData -= 4;
  31. }
  32. if ((timeHourData/2) == 1)
  33. {
  34. regHourData |= 0x40;
  35. timeHourData -= 2;
  36. }
  37. if (timeHourData == 1)
  38. {
  39. regHourData |= 0x80;
  40. }
  41. return regHourData;
  42. }

S35390_SetMinute

将分数据转换成BCD码,然后返回回去。

  1. static uint8_t S35390_SetMinute(uint8_t timeMinuteData )
  2. {
  3. uint8_t regMinuteData = 0x00;
  4. if (timeMinuteData > 59)//限制最大
  5. {
  6. timeMinuteData = 0;
  7. }
  8. else
  9. {
  10. ; /* do nothing*/
  11. }
  12. if ((timeMinuteData/40) == 1)
  13. {
  14. regMinuteData |= 0x02;
  15. timeMinuteData -= 40;
  16. }
  17. if ((timeMinuteData/20) == 1)
  18. {
  19. regMinuteData |= 0x04;
  20. timeMinuteData -= 20;
  21. }
  22. if ((timeMinuteData/10) == 1)
  23. {
  24. regMinuteData |= 0x08;
  25. timeMinuteData -= 10;
  26. }
  27. if ((timeMinuteData/8) == 1)
  28. {
  29. regMinuteData |= 0x10;
  30. timeMinuteData -= 8;
  31. }
  32. if ((timeMinuteData/4) == 1)
  33. {
  34. regMinuteData |= 0x20;
  35. timeMinuteData -= 4;
  36. }
  37. if ((timeMinuteData/2) == 1)
  38. {
  39. regMinuteData |= 0x40;
  40. timeMinuteData -= 2;
  41. }
  42. if (timeMinuteData == 1)
  43. {
  44. regMinuteData |= 0x80;
  45. }
  46. return regMinuteData;
  47. }

S35390_GetStatusReg1

获取状态寄存器1、状态寄存器2也是如此。

  1. uint8_t S35390_GetStatusReg1(uint8_t *statusReg)
  2. {
  3. return S35390_ReadRegs(S35390_STATUS1_ADDRESS,1,statusReg);
  4. }

S35390_SetStatusReg1

设置状态寄存器1、状态寄存器2也是如此。

  1. uint8_t S35390_SetStatusReg1(uint8_t *statusReg)
  2. {
  3. return S35390_WriteRegs(S35390_STATUS1_ADDRESS,1, statusReg);
  4. }

S35390_SetAlarm1

设置中断闹钟1、中断闹钟2也是如此,注意需要先在状态寄存器里面设置为闹钟模式。

  1. int S35390_SetAlarm1(rtc_alarm *alarmTime)
  2. {
  3. status_t ret=STATUS_BUSY;
  4. uint8_t reg_data[3] = {0};
  5. uint8_t flg_am_pm = 0; //0:am,1:pm
  6. reg_data[0] = S35390_SetWeek(alarmTime->week) | alarmTime->week_en;
  7. if(alarmTime->hour >= 12)
  8. {
  9. flg_am_pm = 1;
  10. }
  11. reg_data[1] = S35390_SetHour(alarmTime->hour) | alarmTime->hour_en | (flg_am_pm << 1);
  12. reg_data[2] = S35390_SetMinute(alarmTime->minute) | alarmTime->min_en;
  13. ret = S35390_WriteRegs(S35390_INT1_REG_ADDRESS,3,reg_data);
  14. return ret;
  15. }

S35390_GetAlarm1

获取闹钟1设置

  1. int S35390_GetAlarm1(rtc_alarm *alarmTime)
  2. {
  3. status_t ret=STATUS_BUSY;
  4. uint8_t reg_data[3] = {0};
  5. ret = S35390_ReadRegs(S35390_INT1_REG_ADDRESS,3, reg_data);
  6. if(ret != STATUS_SUCCESS)
  7. {
  8. LOG_ERR( "Get alarm set time error!\r\n");
  9. }
  10. else
  11. {
  12. alarmTime->week = S35390_GetWeek(reg_data[0]);
  13. alarmTime->week_en = reg_data[0] & 0x01;
  14. alarmTime->hour = S35390_GetHour(reg_data[1]);
  15. alarmTime->hour_en = reg_data[1] & 0x01;
  16. alarmTime->minute = S35390_GetMinute(reg_data[2]);
  17. alarmTime->min_en = reg_data[2] & 0x01;
  18. }
  19. return ret;
  20. }

S35390_EnableAlarm1

使能闹钟模式,其实就是将状态寄存器1改成闹钟模式。

  1. int S35390_EnableAlarm1(void)
  2. {
  3. status_t ret=STATUS_BUSY;
  4. uint8_t reg_data = 0;
  5. ret = S35390_ReadRegs(S35390_STATUS2_ADDRESS,1, &reg_data);
  6. if(STATUS_SUCCESS == ret)
  7. {
  8. //INT1AE = 1;INT1ME = 0;INT1FE = 0;
  9. reg_data &= ~(0x01 << 4);
  10. reg_data |= (0x01 << 5);
  11. reg_data &= ~(0x03 << 6);
  12. ret = S35390_WriteRegs(S35390_STATUS2_ADDRESS,1, &reg_data);
  13. }
  14. return ret;
  15. }

S35390_DisableAlarm

失能闹钟模式,其实就是改变状态寄存器1。

  1. int S35390_DisableAlarm(void)
  2. {
  3. status_t ret=STATUS_BUSY;
  4. uint8_t reg_data = 0;
  5. ret = S35390_ReadRegs(S35390_STATUS1_ADDRESS,1, &reg_data);
  6. if(STATUS_SUCCESS == ret)
  7. {
  8. //INT1AE = 0;INT1ME = 0;INT1FE = 0;
  9. reg_data &= ~(0x01 << 4);
  10. reg_data &= ~(0x01 << 5);
  11. reg_data &= ~(0x03 << 6);
  12. ret = S35390_WriteRegs(S35390_STATUS1_ADDRESS,1, &reg_data);
  13. }
  14. return ret;
  15. }

S35390_GetTime

获取当前时间

  1. #define S35390_BTC_SEC 0x06
  2. #define S35390_BTC_MIN 0x05
  3. #define S35390_BTC_HOUR 0x04
  4. #define S35390_BTC_WEEK 0x03
  5. #define S35390_BTC_DAY 0x02
  6. #define S35390_BTC_MONTH 0x01
  7. #define S35390_BTC_YEAR 0x00
  8. uint8_t S35390_GetTime(rtc_time *time)
  9. {
  10. uint8_t reg_date[7]={0};
  11. uint8_t err = 0;
  12. err = S35390_ReadRegs(S35390_TIME_ADDRESS,7,reg_date);//获取当前时间到reg_date
  13. time->second = S35390_GetMinute(reg_date[S35390_BTC_SEC]); //转换成十进制数据
  14. time->minute = S35390_GetMinute(reg_date[S35390_BTC_MIN]);
  15. time->hour = S35390_GetHour(reg_date[S35390_BTC_HOUR]);
  16. time->week = S35390_GetWeek(reg_date[S35390_BTC_WEEK]);
  17. time->day = S35390_GetDay(reg_date[S35390_BTC_DAY]);
  18. time->month = S35390_GetMonth(reg_date[S35390_BTC_MONTH]);
  19. time->year = S35390_GetYear(reg_date[S35390_BTC_YEAR])+2000;
  20. LOG_INFO("%-30s%s%d,%d,%d,%d,%d,%d,%d\r\n","GetRTC","=",time->year,time->month,time->day,time->hour,time->minute,time->second,time->week);
  21. return err;
  22. }

S35390_EnableAlarm

其实就是将状态寄存器设置为闹钟模式

  1. int S35390_EnableAlarm(void)
  2. {
  3. status_t ret=STATUS_BUSY;
  4. uint8_t reg_data = 0;
  5. ret = S35390_ReadRegs(S35390_STATUS2_ADDRESS,1, &reg_data);
  6. if(STATUS_SUCCESS == ret)
  7. {
  8. //INT1AE = 1;INT1ME = 0;INT1FE = 0;
  9. reg_data &= ~(0x01 << 4);
  10. reg_data |= (0x01 << 5);
  11. reg_data &= ~(0x03 << 6);
  12. ret = S35390_WriteRegs(S35390_STATUS2_ADDRESS,1, &reg_data);
  13. }
  14. return ret;
  15. }

S35390_SetTime

设置当前时间

  1. uint8_t S35390_SetTime(rtc_time *time)
  2. {
  3. uint8_t reg_date[7] = {0};
  4. uint8_t ret = 0xff;
  5. uint8_t week_tmp = 0;
  6. reg_date[S35390_BTC_SEC] = S35390_SetMinute(time->second); //转换成BCD码
  7. reg_date[S35390_BTC_MIN] = S35390_SetMinute(time->minute);
  8. reg_date[S35390_BTC_HOUR] = S35390_SetHour(time->hour);
  9. week_tmp = CalculateWeekForKimLarsenCalculationFormula(time->year,time->month,time->day);//计算出周数据
  10. if(time->week != week_tmp)
  11. {
  12. LOG_ERR("week set error!\r\n");
  13. }
  14. reg_date[S35390_BTC_WEEK] = S35390_SetWeek(week_tmp);
  15. reg_date[S35390_BTC_DAY] = S35390_SetDay(time->day);
  16. reg_date[S35390_BTC_MONTH] = S35390_SetMonth(time->month);
  17. reg_date[S35390_BTC_YEAR] = S35390_SetYear(time->year);
  18. ret = S35390_WriteRegs(S35390_TIME_ADDRESS,7, reg_date);
  19. return ret;
  20. }
  21. static uint8_t CalculateWeekForKimLarsenCalculationFormula(uint16_t year, uint8_t month, uint8_t date)
  22. {
  23. uint8_t week;
  24. if ((month == 1) || (month == 2)) {
  25. month += 12;
  26. year--;
  27. }
  28. week = (date + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400 + 1) % 7;
  29. return week;
  30. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/144564
推荐阅读
相关标签
  

闽ICP备14008679号