i2c_aht10_sample.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-15 misonyo first implementation.
  9. */
  10. /*
  11. * 程序清单:这是一个 I2C 设备使用例程
  12. * 例程导出了 i2c_aht10_sample 命令到控制终端
  13. * 命令调用格式:i2c_aht10_sample i2c1
  14. * 命令解释:命令第二个参数是要使用的I2C总线设备名称,为空则使用默认的I2C总线设备
  15. * 程序功能:通过 I2C 设备读取温湿度传感器 aht10 的温湿度数据并打印
  16. */
  17. #include <rtthread.h>
  18. #include <rtdevice.h>
  19. #define AHT10_I2C_BUS_NAME "i2c2" /* 传感器连接的I2C总线设备名称 */
  20. #define AHT10_ADDR 0x38 /* 从机地址 */
  21. #define AHT10_CALIBRATION_CMD 0xE1 /* 校准命令 */
  22. #define AHT10_NORMAL_CMD 0xA8 /* 一般命令 */
  23. #define AHT10_GET_DATA 0xAC /* 获取数据命令 */
  24. static struct rt_i2c_bus_device *i2c_bus = RT_NULL; /* I2C总线设备句柄 */
  25. static rt_bool_t initialized = RT_FALSE; /* 传感器初始化状态 */
  26. /* 写传感器寄存器 */
  27. static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
  28. {
  29. rt_uint8_t buf[3];
  30. struct rt_i2c_msg msgs;
  31. buf[0] = reg; //cmd
  32. buf[1] = data[0];
  33. buf[2] = data[1];
  34. msgs.addr = AHT10_ADDR;
  35. msgs.flags = RT_I2C_WR;
  36. msgs.buf = buf;
  37. msgs.len = 3;
  38. /* 调用I2C设备接口传输数据 */
  39. if (rt_i2c_transfer(bus, &msgs, 1) == 1)
  40. {
  41. return RT_EOK;
  42. }
  43. else
  44. {
  45. return -RT_ERROR;
  46. }
  47. }
  48. /* 读传感器寄存器数据 */
  49. static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf)
  50. {
  51. struct rt_i2c_msg msgs;
  52. msgs.addr = AHT10_ADDR;
  53. msgs.flags = RT_I2C_RD;
  54. msgs.buf = buf;
  55. msgs.len = len;
  56. /* 调用I2C设备接口传输数据 */
  57. if (rt_i2c_transfer(bus, &msgs, 1) == 1)
  58. {
  59. return RT_EOK;
  60. }
  61. else
  62. {
  63. return -RT_ERROR;
  64. }
  65. }
  66. static void read_temp_humi(float *cur_temp, float *cur_humi)
  67. {
  68. rt_uint8_t temp[6];
  69. write_reg(i2c_bus, AHT10_GET_DATA, 0); /* 发送命令 */
  70. rt_thread_mdelay(400);
  71. read_regs(i2c_bus, 6, temp); /* 获取传感器数据 */
  72. /* 湿度数据转换 */
  73. *cur_humi = (temp[1] << 12 | temp[2] << 4 | (temp[3] & 0xf0) >> 4) * 100.0 / (1 << 20);
  74. /* 温度数据转换 */
  75. *cur_temp = ((temp[3] & 0xf) << 16 | temp[4] << 8 | temp[5]) * 200.0 / (1 << 20) - 50;
  76. }
  77. static void aht10_init(const char *name)
  78. {
  79. rt_uint8_t temp[2] = {0, 0};
  80. /* 查找I2C总线设备,获取I2C总线设备句柄 */
  81. i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
  82. if (i2c_bus == RT_NULL)
  83. {
  84. rt_kprintf("can't find %s device!\n", name);
  85. }
  86. else
  87. {
  88. write_reg(i2c_bus, AHT10_NORMAL_CMD, temp);
  89. rt_thread_mdelay(400);
  90. temp[0] = 0x08;
  91. temp[1] = 0x00;
  92. write_reg(i2c_bus, AHT10_CALIBRATION_CMD, temp);
  93. rt_thread_mdelay(400);
  94. initialized = RT_TRUE;
  95. }
  96. }
  97. static void i2c_aht10_sample(int argc, char *argv[])
  98. {
  99. float humidity, temperature;
  100. char name[RT_NAME_MAX];
  101. humidity = 0.0;
  102. temperature = 0.0;
  103. if (argc == 2)
  104. {
  105. rt_strncpy(name, argv[1], RT_NAME_MAX);
  106. }
  107. else
  108. {
  109. rt_strncpy(name, AHT10_I2C_BUS_NAME, RT_NAME_MAX);
  110. }
  111. if (!initialized)
  112. {
  113. /* 传感器初始化 */
  114. aht10_init(name);
  115. }
  116. if (initialized)
  117. {
  118. /* 读取温湿度数据 */
  119. read_temp_humi(&temperature, &humidity);
  120. rt_kprintf("read aht10 sensor humidity : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10);
  121. rt_kprintf("read aht10 sensor temperature: %d.%d \n", (int)temperature, (int)(temperature * 10) % 10);
  122. }
  123. else
  124. {
  125. rt_kprintf("initialize sensor failed!\n");
  126. }
  127. }
  128. /* 导出到 msh 命令列表中 */
  129. MSH_CMD_EXPORT(i2c_aht10_sample, i2c aht10 sample);