【ESP32-C3-DevKitM-1】ESP32-C3读取SHT3X温湿度传感器的温湿度值
[复制链接]
本次使用SHT3X温湿度传感器获取当前环境的温湿度。SHT3X温湿度传感器,它具备校准、线性、温度补偿、快速测量、快速启动的特点,并且精度比较高,温度精度在±0.3℃,湿度精度在±2%RH;供电范围为2.4V-5.5v,故使用ESP32的3.3V引脚进行供电;接口是使用I2C,速度可达1MHz,有两个可选地址。它的引脚电路图和实物图如下所示。
使用的引脚:
PIN18 SDA
PIN19 SCL
VIN 3.3V
AD GND
程序编写:
1、配置I2C接口参数,主机模式、IO口、通信速度等
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] i2c master initialization
*/
esp_err_t i2c_master_init(void)
{
int i2c_master_port = IIC_CTRL_NUM;
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;//主机模式
conf.sda_io_num = SDA_PIN_NUM;//使用IO作为SDA引脚
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;//上拉使能
conf.scl_io_num = SCL_PIN_NUM;//使用IO作为SCL引脚
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;//上拉使能
conf.master.clk_speed = 100000; /* 标准模式(100 kbit/s) */
i2c_param_config(i2c_master_port, &conf);
return i2c_driver_install(i2c_master_port, conf.mode, 0, 0, 0);
}
- 编写发命令函数
/* 描述:向SHT30发送一条16bit指令
* 参数cmd:SHT30指令(在SHT30_MODE中枚举定义)
* 返回值:成功返回ESP_OK */
esp_err_t SHT3x_Send_Cmd(sht3x_cmd_t sht3x_cmd)
{
uint8_t cmd_buffer[2];
//发送16位的命令头
//先将低8位右移8位
cmd_buffer[0] = sht3x_cmd >> 8;
cmd_buffer[1] = sht3x_cmd;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();//创建连接
i2c_master_start(cmd);//传输开始
i2c_master_write_byte(cmd, SHT3X_DeviceAddr | WRITE_BIT, ACK_CHECK_EN);//写命令
i2c_master_write_byte(cmd, cmd_buffer[0], ACK_CHECK_EN);//发送低字节
i2c_master_write_byte(cmd, cmd_buffer[1], ACK_CHECK_EN);//发送高字节
i2c_master_stop(cmd);//停止传输
esp_err_t ret = i2c_master_cmd_begin(IIC_CTRL_NUM, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);//删除连接
return ret;
}
- 编写读数据函数。
/* 描述:从SHT3x读取数据
* 参数data_len:读取多少个字节数据
* 参数data_arr:读取的数据存放在一个数组里
* 返回值:读取成功返回ESP_OK
*/
esp_err_t SHT3x_Recv_Data(size_t data_len, uint8_t* data_arr)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, SHT3X_DeviceAddr | READ_BIT, ACK_CHECK_EN);
if (data_len > 1) {
i2c_master_read(cmd, data_arr, data_len - 1, ACK_VAL);
}
i2c_master_read_byte(cmd, data_arr + data_len - 1, NACK_VAL);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(IIC_CTRL_NUM, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
- 编写SHT3X初始化函数。
/* 描述:SHT3x初始化函数,并将其设置为周期测量模式
* 参数:无
* 返回值:初始化成功返回ESP_OK */
esp_err_t sht3x_mode_init(void)
{
/* 初始化IIC控制器 */
ESP_ERROR_CHECK(i2c_master_init());
/* 设置为周期测量模式 */
esp_err_t ret = SHT3x_Send_Cmd(MEDIUM_2_CMD);
return ret;
}
- CRC校验函数
/* 描述:数据CRC校验
* 参数message:需要校验的数据
* 参数initial_value:crc初始值
* 返回值:计算得到的CRC码 */
#define CRC8_POLYNOMIAL 0x131
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];
uint16_t tem,hum;
double Temperature=0;
double Humidity=0;
ret = SHT3x_Send_Cmd(READOUT_FOR_PERIODIC_MODE);
ret = SHT3x_Recv_Data(6,buff);
/* 校验温度数据和湿度数据是否接收正确 */
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;
}
7、主函数
void app_main(void)
{
vTaskDelay(200);
double Tem_val,Hum_val;
ESP_ERROR_CHECK(sht3x_mode_init());
while(1)
{
/* 采集温湿度数据 */
if(sht3x_get_humiture_periodic(&Tem_val,&Hum_val) == 0)
{
printf("temperature:%6.2lf °C, humidity:%6.2lf %% \n",Tem_val,Hum_val);
printf("\r\n************************************************************\r\n");
}
else
printf("Get_Humiture ERR\r\n");
vTaskDelay(200);
}
}
8、头文件
#ifndef _SHT3x_H
#define _SHT3x_H
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include "driver/i2c.h"
#define WRITE_BIT 0x00 /*!< I2C master write */
#define READ_BIT 0x01 /*!< I2C master read */
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave */
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
#define ACK_VAL 0x0 /*!< I2C ack value */
#define NACK_VAL 0x1 /*!< I2C nack value */
#define IIC_CTRL_NUM I2C_NUM_0 /*!< I2C port number */
#define SDA_PIN_NUM 18 /*!< gpio number for I2C data */
#define SCL_PIN_NUM 19 /*!< gpio number for I2C clock */
#define SHT3X_DeviceAddr (0x44<<1) /* SHT3X的器件地址 */
/* 枚举SHT3x命令列表 */
typedef enum
{
/* 软件复位命令 */
SOFT_RESET_CMD = 0x30A2,
/* 单次测量模式
命名格式:Repeatability_CS_CMD
CS: Clock stretching */
HIGH_ENABLED_CMD = 0x2C06,
MEDIUM_ENABLED_CMD = 0x2C0D,
LOW_ENABLED_CMD = 0x2C10,
HIGH_DISABLED_CMD = 0x2400,
MEDIUM_DISABLED_CMD = 0x240B,
LOW_DISABLED_CMD = 0x2416,
/* 周期测量模式
命名格式:Repeatability_MPS_CMD
MPS:measurement per second */
HIGH_0_5_CMD = 0x2032,
MEDIUM_0_5_CMD = 0x2024,
LOW_0_5_CMD = 0x202F,
HIGH_1_CMD = 0x2130,
MEDIUM_1_CMD = 0x2126,
LOW_1_CMD = 0x212D,
HIGH_2_CMD = 0x2236,
MEDIUM_2_CMD = 0x2220,
LOW_2_CMD = 0x222B,
HIGH_4_CMD = 0x2334,
MEDIUM_4_CMD = 0x2322,
LOW_4_CMD = 0x2329,
HIGH_10_CMD = 0x2737,
MEDIUM_10_CMD = 0x2721,
LOW_10_CMD = 0x272A,
/* 周期测量模式读取数据命令 */
READOUT_FOR_PERIODIC_MODE = 0xE000,
/* 读取传感器编号命令 */
READ_SERIAL_NUMBER = 0x3780,
} sht3x_cmd_t;
esp_err_t SHT3x_Send_Cmd(sht3x_cmd_t sht3x_cmd);
esp_err_t SHT3x_Recv_Data(size_t data_len, uint8_t* data_arr);
esp_err_t sht3x_mode_init(void);
uint8_t CheckCrc8(uint8_t* const message, uint8_t initial_value);
uint8_t sht3x_get_humiture_periodic(double *Tem_val,double *Hum_val);
#endif
9、效果图
当前手机显示温度,可见温度挺准的
|