• ISL1208时钟芯片 Linux下 i2c 设置报警时钟。


    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. #include
    18. #include
    19. #include
    20. #include
    21. #include
    22. #include
    23. #include
    24. #include
    25. #include
    26. #include
    27. #define I2C_BUS "/dev/i2c-1"
    28. #define ISL2108_ADDR 0x6F
    29. static int i2c_write_bytes(int fd, uint8_t slave_addr, uint8_t reg_addr, uint8_t *values, uint8_t len);
    30. typedef struct time_datas
    31. {
    32. int _weeks;
    33. int _weeks_enable; //
    34. int _months;
    35. int _months_enable;
    36. int _days;
    37. int _days_enable;
    38. int _hours;
    39. int _hours_enable; //
    40. int _minutes;
    41. int _minutes_enable; //
    42. int _seconds;
    43. int _seconds_enable;
    44. uint8_t _time_data[10];
    45. } time_datas;
    46. // 指定的某一位数置1
    47. #define SET_BIT(x, bit) (x |= (1 << bit)) /* 置位第bit位 */
    48. // 指定的某一位数置0
    49. #define SET_PIN(PIN, N) (PIN &= ~(1 << N)) /* 置位第bit位 */
    50. int IntToBCD(int i) // 十进制转BCD
    51. {
    52. return (((i / 10) << 4) + ((i % 10) & 0x0f));
    53. }
    54. int BCDToInt(int bcd) // BCD转十进制
    55. {
    56. return (0xff & (bcd >> 4)) * 10 + (0xf & bcd);
    57. }
    58. time_datas gettimedate(time_datas _time_datas)
    59. {
    60. time_datas framev;
    61. framev._time_data[0] = 0x00;
    62. framev._time_data[1] = 0x00;
    63. framev._time_data[2] = 0x00;
    64. framev._time_data[3] = 0x00;
    65. framev._time_data[4] = 0x00;
    66. framev._time_data[5] = 0x00;
    67. framev._time_data[6] = 0x00;
    68. framev._time_data[7] = 0x00;
    69. framev._time_data[8] = 0x00;
    70. framev._time_data[9] = 0x00;
    71. framev._weeks = IntToBCD(_time_datas._weeks);
    72. printf("报警时钟星期[%d],_enable:=[%d]\n", _time_datas._weeks, _time_datas._weeks_enable);
    73. if (_time_datas._weeks_enable == 1)
    74. {
    75. SET_BIT(framev._weeks, 6);
    76. }
    77. if (_time_datas._weeks_enable == 0)
    78. {
    79. SET_PIN(framev._weeks, 6);
    80. }
    81. printf("十六进制输出:\n"); // 以十六进制形式输出
    82. printf("报警时钟星期=[%#X]\n", framev._weeks); // X大写,则输出的前缀和字母都大写
    83. //
    84. framev._hours = IntToBCD(_time_datas._hours);
    85. printf("报警时钟小时[%d],_enable:=[%d]\n", _time_datas._hours, _time_datas._hours_enable);
    86. if (_time_datas._hours_enable == 1)
    87. {
    88. SET_BIT(framev._hours, 7);
    89. }
    90. if (_time_datas._hours_enable == 0)
    91. {
    92. SET_PIN(framev._hours, 7);
    93. }
    94. printf("十六进制输出:\n"); // 以十六进制形式输出
    95. printf("报警时钟小时=[%#X]\n", framev._hours); // X大写,则输出的前缀和字母都大写
    96. ///
    97. //
    98. framev._minutes = IntToBCD(_time_datas._minutes);
    99. printf("报警时钟分[%d],_enable:=[%d]\n", _time_datas._minutes, _time_datas._minutes_enable);
    100. if (_time_datas._minutes_enable == 1)
    101. {
    102. SET_BIT(framev._minutes, 7);
    103. }
    104. if (_time_datas._minutes_enable == 0)
    105. {
    106. SET_PIN(framev._minutes, 7);
    107. }
    108. printf("十六进制输出:\n"); // 以十六进制形式输出
    109. printf("报警时钟分=[%#X]\n", framev._minutes); // X大写,则输出的前缀和字母都大写
    110. ///
    111. framev._time_data[1] = framev._minutes;
    112. framev._time_data[2] = framev._hours;
    113. framev._time_data[5] = framev._weeks;
    114. printf("Data SET: \n");
    115. for (int i = 0; i < 11; i++)
    116. printf("%.2X ", framev._time_data[i]);
    117. printf("\n");
    118. return framev;
    119. }
    120. static int i2c_write_bytes(int fd, uint8_t slave_addr, uint8_t reg_addr, uint8_t *values, uint8_t len)
    121. {
    122. uint8_t *outbuf = NULL;
    123. struct i2c_rdwr_ioctl_data packets;
    124. struct i2c_msg messages[1];
    125. outbuf = (uint8_t *)malloc(len + 1);
    126. if (!outbuf)
    127. {
    128. printf("Error: No memory for buffer\n");
    129. return -1;
    130. }
    131. outbuf[0] = reg_addr; // i2c设备要操作的reg地址
    132. memcpy(outbuf + 1, values, len);
    133. messages[0].addr = slave_addr; // i2c设备地址
    134. messages[0].flags = 0; // write flag
    135. messages[0].len = len + 1;
    136. messages[0].buf = outbuf; // 向reg写入的值
    137. /* Transfer the i2c packets to the kernel and verify it worked */
    138. packets.msgs = messages;
    139. packets.nmsgs = 1;
    140. if (ioctl(fd, I2C_RDWR, &packets) < 0)
    141. {
    142. printf("Error: Unable to send data");
    143. free(outbuf);
    144. return -1;
    145. }
    146. free(outbuf);
    147. return 0;
    148. }
    149. int Auto_Time_Set(time_datas _time_datas)
    150. {
    151. int i2c_fd;
    152. unsigned long len;
    153. uint8_t buffer[1024];
    154. // 打开I2C总线
    155. i2c_fd = open(I2C_BUS, O_RDWR);
    156. if (i2c_fd < 0)
    157. {
    158. perror("Failed to open I2C bus");
    159. return -1;
    160. }
    161. time_datas get_time_datas;
    162. get_time_datas = gettimedate(_time_datas);
    163. // 读取ISL2108的数据
    164. len = 10;
    165. buffer[0] = 0x00;
    166. buffer[1] = 0x00;
    167. // Alarm
    168. i2c_write_bytes(i2c_fd, ISL2108_ADDR, 0x0c, get_time_datas._time_data, len);
    169. // 关闭I2C总线
    170. close(i2c_fd);
    171. return 0;
    172. }
    173. int main()
    174. {
    175. time_datas time_datas_V;
    176. time_datas_V._hours_enable = 1;
    177. time_datas_V._hours = 9; // 小时
    178. time_datas_V._weeks = 1; // 周
    179. time_datas_V._weeks_enable = 1;
    180. time_datas_V._minutes = 03; // 分钟
    181. time_datas_V._minutes_enable = 1;
    182. Auto_Time_Set(time_datas_V);
    183. return 0;
    184. }

  • 相关阅读:
    工作不是生活的全部,请默念10遍
    a+=b、a-=b、a*=b和a=a+b、a=a-b以及a=a*b的区别(易混淆)
    [ 渗透测试面试篇 ] 渗透测试面试题大集合(详解)(二)XSS注入相关面试题
    不允许你还不会OSS文件操作,看完一篇就够了
    数据链路层-点对点PPP(point-to-point protocal)
    C++ Reference: Standard C++ Library reference: C Library: cctype: isgraph
    Dubbo线程池
    Elasticsearch核心技术与实战-05-elasticsearch的安装与简单配置-Windows
    基于单片机的停车场车位管理系统设计
    中睿天下&Coremail | 2023年Q3企业邮箱安全态势观察报告
  • 原文地址:https://blog.csdn.net/2004v2004/article/details/134511364