[STM32U5] 【NUCLEO-U5A5ZJ-Q测评】读取HS3003温湿度传感器数据

[复制链接]
 楼主| EPTmachine 发表于 2023-12-3 23:07 | 显示全部楼层 |阅读模式
<
读取HS3003温度传感器的数据
支持I2C接口的传感器种类有很多,本文对使用STM32U5的I2C读取HS3003温湿度传感器的数据进行介绍。
1.硬件配置
HS3003传感器模块的原理图如图所示。
63b51baf5faabc4f033855098c272e93
开发板与传感器器模块的接线图如图所示。
ce720649d8dd568e4f81c62fb9c2504f
2.软件编写
首先,在STM32 CubeIDE中创建开发板的工程,选择根据开发板创建工程,
f43d27b7095a4114a9a14d89e5dd7796
在CubeMX界面中会自动初始化开发板上的部分外设,比如串口、调试口、LED等,本文通过I2C1控制传感器,使用的PB8和PB9,选择对应管脚的复用功能,并在I2C的配置界面选择相应的I2C通讯参数。
a0a15bdea454cc6d128e0fc0b8e13374
完成上述操作后,保存配置并生成相应的外设初始化代码。
通过查看数据手册可知,传感器的从机地址为0x44。
e1f682add5862f35e6a4545d813a17a8
由于STM32 HAL接口的实现问题,在传入从机地址时,需要将第8位的读写控制位作为从机地址作为函数的参数传入。相关的宏定义如下
HS3003默认状态位睡眠模式。在睡眠模式下,在进行测量前,传感器等待从主机发送过来的指令。当传感器接收到测量请求指令后,数字核进行数模转换,否则,传感器处于关机状态。
主机通过发送7位从机地址和第八位写标记来启动测量请求。
478d6586c27335bd2bb2b14f2b1c495d
这里的表述表示测量请求时,I2C发出的数据长度为零,只包含从机地址和写标志位。按照上述要求发出控制指令后,传感器会进行一次ADC数据转换,转换结果会存放在数据寄存器中。
daa5cb77c70fe60ff09471948040bde7
使用数据抓取指令,可以从传感器获取转换后的湿度和温度数据。读取数据操作的各字节数据的含义如下图所示,其中除了温湿度数据外,在传感返回的第一字节的高两位表示数据是否被读取。
82464e69f384afba891febdae4925d8b
传感器中的指定几个寄存器可以用于配置传感器的湿度分辨率和温度分辨率,在修改其中的配置参数前,需要控制传感器进入编程模式。设置其进入编程模式的指令如下。
d1b38f37181c46ced00ebe04ed472992
修改测量分辨率的操作,根据数据手册中的说明,进入编程模式后,读取指定寄存器中数据,然后根据需要修改相应的数据位,将数据写入寄存器中。
9df24e7deefb51e461dc2f50192a2127
读取到的ADC数据值根据下图中的公式进行转换,得到可读的温湿度数据。
9268d79d0cbdf1a92e9f7d860a6be264
基于上述从数据手册得到的信息,可以编写以下几个函数。
首先是基本的读取和写入指定寄存器
  1. uint32_t Humiture_HS3003_readRegister32(uint8_t reg_address) {

  2.     uint32_t value;
  3.     uint8_t data[4];

  4.     /* Read data from I2C slave */
  5.         if (HAL_I2C_Master_Receive(&hi2c1, (uint16_t)HS300X_ADR_R, (uint8_t *)data, 0x04,10000) != HAL_OK)
  6.         {
  7.         /* Error_Handler() function is called when error occurs. */
  8.         Error_Handler();
  9.         }

  10.         /*##- Wait for the end of the transfer #################################*/
  11.         /*  Before starting a new communication transfer, you need to check the current
  12.           state of the peripheral; if it�s busy you need to wait for the end of current
  13.           transfer before starting a new one.
  14.           For simplicity reasons, this example is just waiting till the end of the
  15.           transfer, but application may perform other tasks while transfer operation
  16.           is ongoing. */
  17.         while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
  18.         {
  19.         }

  20.     value=data[0];
  21.     value<<=8;
  22.     value|=data[1];
  23.     value<<=8;
  24.     value|=data[2];
  25.     value<<=8;
  26.     value|=data[3];
  27.     return value;
  28. }
  1. void Humiture_HS3003_writeRegister( uint8_t reg_address, uint8_t *buffer, uint8_t len)
  2. {
  3.         if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)HS300X_ADR_W, buffer, len,10000) != HAL_OK)
  4.         {
  5.         /* Error_Handler() function is called when error occurs. */
  6.         Error_Handler();
  7.         }

  8.         /*##- Wait for the end of the transfer #################################*/
  9.         /*  Before starting a new communication transfer, you need to check the current
  10.           state of the peripheral; if it�s busy you need to wait for the end of current
  11.           transfer before starting a new one.
  12.           For simplicity reasons, this example is just waiting till the end of the
  13.           transfer, but application may perform other tasks while transfer operation
  14.           is ongoing. */
  15.         while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
  16.         {
  17.         }

  18. }
接下来是初始化传感器和读取温湿度传感器数据的函数。
  1. void Humiture_HS3003_init(void)
  2. {
  3.     Humiture_HS3003_writeRegister(HS300X_ADR,NULL,0);
  4. }


  5. //读取原始的数据
  6. uint32_t Humiture_HS3003_ReadData_Raw(uint8_t resolution )
  7. {
  8.     uint32_t data=0;
  9.     Humiture_HS3003_init();//唤醒
  10.     //8bit->1.2ms
  11.     //10bit->2.72ms
  12.     //12bit->9.10ms
  13.     //14bit->33.90ms
  14.     if(resolution==8)
  15.             HAL_Delay(1200);
  16.     else if(resolution==10)
  17.             HAL_Delay(2720);
  18.     else if(resolution==12)
  19.             HAL_Delay(9100);
  20.     else
  21.             HAL_Delay(34);
  22.     data=Humiture_HS3003_readRegister32(HS300X_ADR);
  23.     return data;
  24. }
在主函数中,对读取到的传感器数据按照上图中计算公式进行转换,得到相应的可读温湿度数据。
  1.   /* USER CODE BEGIN 2 */
  2.   Humiture_HS3003_init();
  3.   /* USER CODE END 2 */

  4.   /* Infinite loop */
  5.   /* USER CODE BEGIN WHILE */
  6.   while (1)
  7.   {
  8.           HAL_Delay(1000);
  9.       Data=Humiture_HS3003_ReadData_Raw(14);
  10.       Temp = (float)( (Data & 0xFFFF) >> 2);//只要低十六位
  11.       Temp = Temp*HS300X_TEMP_MULTY*165-40;//HS300X_TEMP_MULTY->0.00006163516(1/(2^14-1) )

  12.       Humidity = (float)( (Data >> 16) &0x3FFF );//只要高十六位,且最高2位不要
  13.       Humidity = Humidity*HS300X_HUMD_MULTY*100;//HS300X_HUMD_MULTY->0.00006163516(1/(2^14-1) )
  14.       printf("temp is %f,humidity is %f\r\n",Temp,Humidity);
  15.     /* USER CODE END WHILE */

  16.     /* USER CODE BEGIN 3 */
  17.   }
输出的温湿度数据如下。
0a5f4d87254ad2db52b6e48e9f855c8e
3.总结
传感器的数据读取在低功耗应用很广泛,这里和大家分享HS3003的使用方法。

U5A5ZJ_I2C_HS3003.zip

1.43 MB, 下载次数: 5

REN_HS300x-Datasheet_DST_20210809.pdf

751.51 KB, 下载次数: 1

HS3003温湿度_Schematic1_2022-12-10.pdf

45.91 KB, 下载次数: 1

cr315 发表于 2023-12-12 09:32 | 显示全部楼层
在配置I2C接口时有什么需要特别设置的吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

487

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部