本文主要是介绍HI3861开发(四)驱动sht30获取温湿度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- sht30介绍
- 编写代码
- 烧录测试
- 总结
sht30介绍
SHT30 是一种数字温湿度传感器,由瑞士的Sensirion公司生产。它能够精确地测量环境的温度和相对湿度,并提供数字输出。
以下是 SHT30 的主要特点和功能:
-
高精度测量:SHT30 采用先进的传感器技术,能够以高精度测量环境的温度和相对湿度。温度测量精度为±0.3°C,湿度测量精度为±2%。
-
快速响应时间:SHT30 具有快速的响应时间,能够在短时间内提供准确的测量结果。
-
低功耗:SHT30 的设计经过优化,具有低功耗特性,适用于需要长期运行的应用场景。
-
数字输出:SHT30 通过 I2C 接口与主控设备进行通信,输出数字信号,方便数据的处理和分析。
-
宽工作电压范围:SHT30 支持广泛的工作电压范围,从2.4V到5.5V,适用于多种电源供应情况。
-
可编程性:SHT30 具有可编程的参数和设置选项,可以根据应用需求进行配置,如测量周期、传感器分辨率等。
-
高可靠性:SHT30 采用先进的传感器技术和质量控制,具有高可靠性和稳定性,适用于长期使用和要求精确测量的应用场景。
SHT30 在许多领域都有广泛的应用,包括气象观测、室内环境监测、工业自动化、智能家居等。它的高精度测量和可靠性使其成为许多应用中信赖的温湿度传感器之一。
编写代码
应用入口demo.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>#include "ohos_init.h"
#include "cmsis_os2.h"
#include "sht30.h"#define TASK_STACK_SIZE 1024 * 8
#define TASK_PRIO 25Data_TypeDef HT_Data;static void sht30_Task(void)
{hw_Init();while (1){Read_Data();printf("\r\n******************************Humidity is %.2f\r\n", HT_Data.Humidity);printf("\r\n******************************Temperature is %.2f\r\n", HT_Data.Temperature);usleep(1000000);}
}static void appEntry(void)
{osThreadAttr_t attr;attr.name = "Example_Task";attr.attr_bits = 0U;attr.cb_mem = NULL;attr.cb_size = 0U;attr.stack_mem = NULL;attr.stack_size = TASK_STACK_SIZE;attr.priority = TASK_PRIO;if (osThreadNew((osThreadFunc_t)sht30_Task, NULL, &attr) == NULL){printf("Falied to create Example_Task!\n");}
}APP_FEATURE_INIT(appEntry);
sht30.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include "cmsis_os2.h"
#include "sht30.h"
#include "wifiiot_errno.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_i2c.h"
#include "wifiiot_i2c_ex.h"static void IO_Init(void)
{GpioInit();IoSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_GPIO);//设置GPIO_2的复用功能为普通GPIOGpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_GPIO_DIR_OUT);//设置GPIO_2为输出模式IoSetFunc(WIFI_IOT_IO_NAME_GPIO_14, WIFI_IOT_IO_FUNC_GPIO_14_GPIO);//设置GPIO_2的复用功能为普通GPIOGpioSetDir(WIFI_IOT_IO_NAME_GPIO_14, WIFI_IOT_GPIO_DIR_OUT);//设置GPIO_2为输出模式IoSetFunc(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA); // GPIO_0复用为I2C1_SDAIoSetFunc(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL); // GPIO_1复用为I2C1_SCLI2cInit(WIFI_IOT_I2C_IDX_1, 400000); /* baudrate: 400kbps */I2cSetBaudrate(WIFI_IOT_I2C_IDX_1, 400000);}void SHT30_reset(void)
{WifiIotI2cData sht30_i2c_data = { 0 };uint8_t send_data[2] = { 0x30,0xA2 };sht30_i2c_data.sendBuf = send_data;sht30_i2c_data.sendLen = 2;I2cWrite(WIFI_IOT_I2C_IDX_1,(SHT30_Addr<<1)|0x00,&sht30_i2c_data);
}void Init_SHT30(void)
{WifiIotI2cData sht30_i2c_data = { 0 };uint8_t send_data[2] = { 0x22,0x36 };sht30_i2c_data.sendBuf = send_data;sht30_i2c_data.sendLen = 2;I2cWrite(WIFI_IOT_I2C_IDX_1,(SHT30_Addr<<1)|0x00,&sht30_i2c_data);
}static uint8_t SHT3x_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum)
{uint8_t crc = 0xFF;uint8_t bit = 0;uint8_t byteCtr ;const int16_t POLYNOMIAL = 0x131;//calculates 8-Bit checksum with given polynomialfor(byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr){crc ^= (data[byteCtr]);for ( bit = 8; bit > 0; --bit){if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL;else crc = (crc << 1);}}if(crc != checksum)return 1;elsereturn 0;}static float SHT3x_CalcTemperatureC(uint16_t u16sT)
{float temperatureC = 0; // variable for resultu16sT &= ~0x0003; // clear bits [1..0] (status bits)//-- calculate temperature [℃] --temperatureC = (175 * (float)u16sT / 65535 - 45); //T = -45 + 175 * rawValue / (2^16-1)return temperatureC;}static float SHT3x_CalcRH(uint16_t u16sRH)
{float humidityRH = 0; // variable for resultu16sRH &= ~0x0003; // clear bits [1..0] (status bits)//-- calculate relative humidity [%RH] --humidityRH = (100 * (float)u16sRH / 65535); // RH = rawValue / (2^16-1) * 10return humidityRH;}void hw_Init(void)
{IO_Init();Init_SHT30();
}void Read_Data(void)
{ uint8_t data[3]; uint16_t dat,tmp;uint8_t SHT3X_Data_Buffer[6]; //byte 0,1 is temperature byte 4,5 is humidityWifiIotI2cData sht30_i2c_data = { 0 };uint8_t send_data[2] = { 0xE0,0x00};sht30_i2c_data.sendBuf = send_data;sht30_i2c_data.sendLen = 2;sht30_i2c_data.receiveBuf = SHT3X_Data_Buffer;sht30_i2c_data.receiveLen = 6;I2cWriteread(WIFI_IOT_I2C_IDX_1,(SHT30_Addr<<1)|0x00,&sht30_i2c_data); //Read bh1750 sensor data // /* check tem */data[0] = SHT3X_Data_Buffer[0];data[1] = SHT3X_Data_Buffer[1];data[2] = SHT3X_Data_Buffer[2];tmp=SHT3x_CheckCrc(data, 2, data[2]);if( !tmp ) /* value is ture */{dat = ((uint16_t)data[0] << 8) | data[1];HT_Data.Temperature = SHT3x_CalcTemperatureC( dat ); }// /* check humidity */data[0] = SHT3X_Data_Buffer[3];data[1] = SHT3X_Data_Buffer[4];data[2] = SHT3X_Data_Buffer[5];tmp=SHT3x_CheckCrc(data, 2, data[2]);if( !tmp ) /* value is ture */{dat = ((uint16_t)data[0] << 8) | data[1];HT_Data.Humidity = SHT3x_CalcRH( dat ); }}
头文件sht30.h
#ifndef __SHT30_H__
#define __SHT30_H__#define SHT30_Addr 0x44typedef struct
{ float Humidity; //湿度float Temperature; //温度
} Data_TypeDef;extern Data_TypeDef HT_Data;void hw_Init(void);
void Read_Data(void);#endif
烧录测试
略,没有传感器
总结
略,没有总结(*.*)
这篇关于HI3861开发(四)驱动sht30获取温湿度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!