WL55JC_AS923/Core/Src/sht3x.c

321 lines
9.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file sts_aq_sht3x.c *
* @author Yunhorn (r) Technology Limited Application Team *
* @brief Yunhorn (r) SmarToilets (r) Product configuration file. *
******************************************************************************
* @attention
*
* Copyright (c) 2025 Yunhorn Technology Limited.
* Copyright (c) 2025 Shenzhen Yunhorn Technology Co., Ltd.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
#include "sensirion_common.h"
#include "sensirion_i2c_hal.h"
#include "sht3x_i2c.h"
#include <stdio.h> // printf
#define sensirion_hal_sleep_us sensirion_i2c_hal_sleep_usec
int uut_sht3x_main(void) {
int16_t error = NO_ERROR;
sensirion_i2c_hal_init();
sht3x_init(SHT30_I2C_ADDR_44);
sht3x_stop_measurement();
sensirion_hal_sleep_us(1000);
sht3x_soft_reset();
sensirion_hal_sleep_us(100000);
uint16_t a_status_register = 0u;
error = sht3x_read_status_register(&a_status_register);
if (error != NO_ERROR) {
printf("error executing read_status_register(): %d\n", error);
return error;
}
printf("a_status_register: %02x\n", a_status_register);
int32_t a_temperature = 0;
int32_t a_humidity = 0;
uint16_t repetition = 0;
for (repetition = 0; repetition < 50; repetition++) {
error = sht3x_measure_single_shot(REPEATABILITY_MEDIUM, false,
&a_temperature, &a_humidity);
if (error != NO_ERROR) {
printf("error executing measure_single_shot(): %d\n", error);
continue;
}
printf("a_temperature [milli degC]: %li ", a_temperature);
printf("a_humidity [milli RH]: %li\n", a_humidity);
}
return NO_ERROR;
}
/*
* THIS FILE IS AUTOMATICALLY GENERATED
*
* Generator: sensirion-driver-generator 0.33.0
* Product: sht3x
* Model-Version: 2.0.0
*/
/*
* Copyright (c) 2023, Sensirion AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#if 0
#include "sensirion_common.h"
#include "sensirion_i2c_hal.h"
#include "sht3x_i2c.h"
#include <stdio.h> // printf
#define sensirion_hal_sleep_us sensirion_i2c_hal_sleep_usec
int main(void) {
int16_t error = NO_ERROR;
sensirion_i2c_hal_init();
sht3x_init(SHT30_I2C_ADDR_44);
sht3x_stop_measurement();
sensirion_hal_sleep_us(1000);
sht3x_soft_reset();
sensirion_hal_sleep_us(100000);
uint16_t a_status_register = 0u;
error = sht3x_read_status_register(&a_status_register);
if (error != NO_ERROR) {
printf("error executing read_status_register(): %i\n", error);
return error;
}
printf("a_status_register: %02x\n", a_status_register);
error = sht3x_start_periodic_measurement(REPEATABILITY_MEDIUM,
MPS_ONE_PER_SECOND);
if (error != NO_ERROR) {
printf("error executing start_periodic_measurement(): %i\n", error);
return error;
}
int32_t a_temperature = 0.0;
int32_t a_humidity = 0.0;
uint16_t repetition = 0;
for (repetition = 0; repetition < 50; repetition++) {
error = sht3x_blocking_read_measurement(&a_temperature, &a_humidity);
if (error != NO_ERROR) {
printf("error executing blocking_read_measurement(): %i\n", error);
continue;
}
printf("a_temperature [milli degC]: %li ", a_temperature);
printf("a_humidity [milli RH]: %li\n", a_humidity);
}
error = sht3x_stop_measurement();
if (error != NO_ERROR) {
return error;
}
return NO_ERROR;
}
#endif
#if 0
#include "sht3x.h"
#include "usart.h"
#include "stdio.h"
#include "i2c.h"
#include "stm32f0xx_hal_i2c.h"
#include "system.h"
#define SHT3x_ADDR_WRITE 0x44<<1 //10001000
#define SHT3x_ADDR_READ (0x44<<1)+1 //10001011
/* 描述向SHT30发送一条16bit指令
* 参数cmdSHT30指令在SHT30_MODE中枚举定义
* 返回值发送成功返回0发送失败返回1 */
static uint8_t SHT3x_Send_Cmd(SHT3X_CMD cmd)
{
uint8_t cmd_buffer[2];
cmd_buffer[0] = cmd >> 8;
cmd_buffer[1] = cmd;
return HAL_I2C_Master_Transmit(&hi2c1, SHT3x_ADDR_WRITE, (uint8_t* )cmd_buffer, 2, 0xFFFF);
}
/**
* @brief 复位SHT30
* @param none
* @retval none
*/
void SHT3x_Reset(void)
{
SHT3x_Send_Cmd(SOFT_RESET_CMD);
HAL_Delay(20);
}
/* 描述SHT3x初始化函数并将其设置为周期测量模式
* 参数:无
* 返回值初始化成功返回0初始化失败返回1 */
uint8_t SHT3x_Init(void)
{
uint8_t ret;
ret = SHT3x_Send_Cmd(MEDIUM_2_CMD);
return ret;
}
/* 描述数据CRC校验
* 参数message需要校验的数据
* 参数initial_valuecrc初始值
* 返回值计算得到的CRC码 */
#define CRC8_POLYNOMIAL 0x31
uint8_t CheckCrc8(uint8_t* const message, uint8_t initial_value)
{
uint8_t remainder; //余数
uint8_t i = 0, j = 0; //循环变量
/* 初始化 */
remainder = initial_value;
for(j = 0; j < 2;j++)
{
remainder ^= message[j];
/* 从最高位开始依次计算 */
for (i = 0; i < 8; i++)
{
if (remainder & 0x80)
remainder = (remainder << 1)^CRC8_POLYNOMIAL;
else
remainder = (remainder << 1);
}
}
/* 返回计算的CRC码 */
return remainder;
}
/* 描述:温湿度数据获取函数,周期读取,注意,需要提前设置周期模式
* 参数Tem_val存储温度数据的指针, 温度单位为°C
* 参数Hum_val存储湿度数据的指针, 温度单位为%
* 返回值0-读取成功1-读取失败
********************************************************************/
uint8_t SHT3x_Get_Humiture_periodic(double *Tem_val,double *Hum_val)
{
uint8_t ret=0;
uint8_t buff[6]={0};
uint16_t tem,hum;
double Temperature=0;
double Humidity=0;
ret=SHT3x_Send_Cmd(READOUT_FOR_PERIODIC_MODE);
//Delay_us(4);
LL_mDelay(50);
ret=HAL_I2C_Master_Receive(&hi2c1, SHT3x_ADDR_READ, buff, 6, 0xFFFF);
/* 校验温度数据和湿度数据是否接收正确 */
if(CheckCrc8(buff, 0xFF) != buff[2] || CheckCrc8(&buff[3], 0xFF) != buff[5])
{
printf("CRC_ERROR,ret = 0x%x\r\n",ret);
return 1;
}
/* 转换温度数据 */
tem = (((uint16_t)buff[0]<<8) | buff[1]);//温度数据拼接
Temperature= (175.0*(double)tem/65535.0-45.0) ; // T = -45 + 175 * tem / (2^16-1)
/* 转换湿度数据 */
hum = (((uint16_t)buff[3]<<8) | buff[4]);//湿度数据拼接
Humidity= (100.0*(double)hum/65535.0); // RH = hum*100 / (2^16-1)
/* 过滤错误数据 */
if((Temperature>=-20)&&(Temperature<=125)&&(Humidity>=0)&&(Humidity<=100))
{
*Tem_val = Temperature;
*Hum_val = Humidity;
return 0;
}
else
return 1;
}
/* 描述:温湿度数据获取函数,单次获取
* 参数Tem_val存储温度数据的指针, 温度单位为°C
* 参数Hum_val存储湿度数据的指针, 温度单位为%
* 返回值0-读取成功1-读取失败
********************************************************************/
uint8_t SHT3x_Get_Humiture_single(double *Tem_val,double *Hum_val)
{
uint8_t ret=0;
uint8_t buff[6];
uint16_t tem,hum;
double Temperature=0;
double Humidity=0;
SHT3x_Send_Cmd(HIGH_ENABLED_CMD);
HAL_Delay(50);
ret=HAL_I2C_Master_Receive(&hi2c1, SHT3x_ADDR_READ, buff, 6, 0xFFFF);
/* 校验温度数据和湿度数据是否接收正确 */
if(CheckCrc8(buff, 0xFF) != buff[2] || CheckCrc8(&buff[3], 0xFF) != buff[5])
{
printf("CRC_ERROR,ret = 0x%x\r\n",ret);
return 1;
}
/* 转换温度数据 */
tem = (((uint16_t)buff[0]<<8) | buff[1]);//温度数据拼接
Temperature= (175.0*(double)tem/65535.0-45.0) ; // T = -45 + 175 * tem / (2^16-1)
/* 转换湿度数据 */
hum = (((uint16_t)buff[3]<<8) | buff[4]);//湿度数据拼接
Humidity= (100.0*(double)hum/65535.0); // RH = hum*100 / (2^16-1)
/* 过滤错误数据 */
if((Temperature>=-20)&&(Temperature<=125)&&(Humidity>=0)&&(Humidity<=100))
{
*Tem_val = Temperature;
*Hum_val = Humidity;
return 0;
}
else
return 1;
}
#endif