• RT-Thread的I2C接口实现SHTC1温湿度传感器数据读取


    目录

    一、基准材料及依据

     二、相关配置

     三、编码实现

             3.1 方法一

            3.2 方法二,调用现成组件

    四、编辑及测试


    一、基准材料及依据

    本人测试的是STM32L496VGx开发板(ali-developer-kit),其I2C接口如下,

     其SHTC1的文档资料:

    1)SHTC1地址

    2)软重置

    3)通讯序列

    4)值计算公式

     

     二、相关配置

    【1】采用的是RT-Thread Studio 2.2.5的工具,按开发板创建好工程后,直接打开.ioc通过STM32CubeMx配置I2C接口,本文采用的默认启用I2C2。

    完成配置进行代码输出

    【2】在RT-Thread Settings组件页面中打开I2C支持,或者menuconfig配置

     在rtconfig.h或board.h调整I2C定义,本文在rtconfig.h直接定义,PIN口和在STM32CubeMx配置对应。

     三、编码实现

             3.1 方法一

    i2c_shtc1_sample.c

    1. /*
    2. * 程序清单:这是一个 I2C 设备使用例程
    3. * 例程导出了 i2c_shtc1_sample 命令到控制终端
    4. * 命令调用格式:i2c_shtc1_sample i2c1
    5. * 命令解释:命令第二个参数是要使用的I2C总线设备名称,为空则使用默认的I2C总线设备
    6. * 程序功能:通过 I2C 设备读取温湿度传感器 shtc1 的温湿度数据并打印
    7. */
    8. #include
    9. #include
    10. #define shtc1_I2C_BUS_NAME "i2c2" /* 传感器连接的I2C总线设备名称 */
    11. #define shtc1_ADDR (0xE0 >> 1) /* 从机地址 7 位地址模式*/
    12. static struct rt_i2c_bus_device *i2c_bus = RT_NULL; /* I2C总线设备句柄 */
    13. static rt_bool_t initialized = RT_FALSE; /* 传感器初始化状态 */
    14. //字段16进制打印输出
    15. void out_temp(rt_uint8_t *temp, rt_uint16_t len)
    16. {
    17. for(rt_uint8_t i=0; i
    18. {
    19. rt_kprintf("%02X ", temp[i]);
    20. }
    21. rt_kprintf("\n");
    22. }
    23. //软重置
    24. void reSet()
    25. {
    26. rt_uint8_t temp[2] = {0X80, 0X5D};
    27. rt_i2c_master_send(i2c_bus,shtc1_ADDR,RT_I2C_WR,temp,2);
    28. }
    29. /*SHTC1提供了定义测量期间的传感器行为以及测量结果的传输序列的可能性。
    30. *每 个测量命令都会同时触发温度测量和湿度测量。
    31. *bool enabled时钟是否启用,bool humi是否在前*/
    32. void clock_set(rt_bool_t enabled,rt_bool_t humi)
    33. {
    34. rt_uint8_t temp[2] = {0, 0};
    35. if(enabled){
    36. /*时钟拉伸 已启用*/
    37. if(humi){
    38. /*湿度、温度*/
    39. temp[0] = 0x5C;
    40. temp[1] = 0x24;
    41. }else{
    42. /*温度、湿度*/
    43. temp[0] = 0x7C;
    44. temp[1] = 0xA2;
    45. }
    46. }else{
    47. /*时钟拉伸 丧失能力的*/
    48. if(humi){
    49. /*湿度、温度*/
    50. temp[0] = 0x58;
    51. temp[1] = 0xE0;
    52. }else{
    53. /*温度、湿度*/
    54. temp[0] = 0x78;
    55. temp[1] = 0x66;
    56. }
    57. }
    58. out_temp(temp,2);
    59. rt_i2c_master_send(i2c_bus,shtc1_ADDR,RT_I2C_WR,temp,2);
    60. }
    61. void read_temp_humi(float *cur_humi,float *cur_temp)
    62. {
    63. rt_uint8_t temp[6];
    64. rt_i2c_master_recv(i2c_bus,shtc1_ADDR
    65. ,RT_I2C_RD
    66. ,temp,6); /* 获取传感器数据 */
    67. out_temp(temp,6); /* 十六进制输出显示 */
    68. rt_uint32_t val01 = (temp[0] << 8 | temp[1]);
    69. rt_uint32_t val02 = (temp[3] << 8 | temp[4]);
    70. rt_kprintf("val01:%d,val02:%d\n",val01,val02);
    71. /* 温度数据转换 */
    72. *cur_temp = val01 * 175.0 / (1 << 16) - 45;
    73. /* 湿度数据转换 */
    74. *cur_humi = val02 * 100.0 / (1 << 16);
    75. }
    76. void shtc1_init(const char *name)
    77. {
    78. /* 查找I2C总线设备,获取I2C总线设备句柄 */
    79. i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
    80. if (i2c_bus == RT_NULL)
    81. {
    82. rt_kprintf("can't find %s device!\n", name);
    83. }
    84. else
    85. {
    86. reSet();
    87. rt_thread_mdelay(1000);
    88. /*温度在前*/
    89. clock_set(1,0);
    90. rt_thread_mdelay(100);
    91. initialized = RT_TRUE;
    92. }
    93. }
    94. static void i2c_shtc1_sample(int argc, char *argv[])
    95. {
    96. float humidity, temperature;
    97. char name[RT_NAME_MAX];
    98. humidity = 0.0;
    99. temperature = 0.0;
    100. if (argc == 2)
    101. {
    102. rt_strncpy(name, argv[1], RT_NAME_MAX);
    103. }
    104. else
    105. {
    106. rt_strncpy(name, shtc1_I2C_BUS_NAME, RT_NAME_MAX);
    107. }
    108. if (!initialized)
    109. {
    110. /* 传感器初始化 */
    111. shtc1_init(name);
    112. }
    113. int count = 0;
    114. while (initialized&&(count++<1))
    115. {
    116. /* 读取温湿度数据 */
    117. read_temp_humi(&humidity,&temperature);
    118. rt_kprintf("\nread shtc1 sensor humidity : %d.%d\n", (int)humidity, (int)(humidity * 100) % 100);
    119. if( temperature >= 0 )
    120. {
    121. rt_kprintf("read shtc1 sensor temperature: %d.%d\n", (int)temperature, (int)(temperature * 100) % 100);
    122. }
    123. else
    124. {
    125. rt_kprintf("read shtc1 sensor temperature: %d.%d\n", (int)temperature, (int)(-temperature * 100) % 100);
    126. }
    127. rt_thread_mdelay(1000);
    128. }
    129. }
    130. /* 导出到 msh 命令列表中 */
    131. MSH_CMD_EXPORT(i2c_shtc1_sample, i2c shtc1 sample);

    编译通过后,烧录进板块,测试:

            3.2 方法二,调用现成组件

    在RT-Thread Settings配置中添加软件包,搜索SHTC1添加软件包

    并按其说明在menuconfig开启配置,注意这种配置会改变rtconfig.h,可能会把上面对于I2C的声明即PIN定义给剔除了,如果烧录是提示设备找不到,可以查看是否出现这种情况。

     参考软件包使用说明,本文的main.c代码如下,没有采用软件包建议的INIT_APP_EXPORT方式是怕I2C没初始化到:

    1. #include
    2. #include
    3. #include "sensor_sr_shtc1.h"
    4. int shtc1_port(void)
    5. {
    6. struct rt_sensor_config cfg;
    7. cfg.intf.dev_name = SHTC1_I2CBUS_NAME;
    8. cfg.intf.user_data = (void *)SHTC1_ADDR_DEFAULT;
    9. rt_hw_shtc1_init("shtc1", &cfg);
    10. return 0;
    11. }
    12. int main(void)
    13. {
    14. shtc1_port();
    15. int count = 1;
    16. while (count++)
    17. {
    18. rt_thread_mdelay(1000);
    19. }
    20. return RT_EOK;
    21. }

    四、编辑及测试

            烧录进板,测试

     两种方法得到的数值几乎近似,传感器所处地南方、湿热,哈哈

  • 相关阅读:
    TCPIP网络编程 学习笔记_1 --网络编程入门
    emq系统调优支持更大并发
    串行、并行、并发
    Sovit3D智慧园区:数字孪生园区大屏一体化管理平台
    m基于光纤光栅传感网接入GPON的光纤通信系统matlab性能仿真,包括解码,解封装,分接,码率恢复,解帧,拆包,译码
    南大通用GBase8s 常用SQL语句(263)
    第九章(2):长短期记忆网络(Long short-term memory, LSTM)与pytorch示例(简单字符级语言模型训练器)
    Java - 位运算的基本原理和用途
    【SpringMVC】| SpringMVC拦截器
    《最新出炉》系列初窥篇-Python+Playwright自动化测试-37-如何截图-上篇
  • 原文地址:https://blog.csdn.net/py8105/article/details/126427261