本工程使用ESP-IDF5.0+VScode编程。
代码基于官方simple_i2c例程修改。
main函数内容。
#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "./../Include/SHT20.h"
static const char *TAG = "i2c-simple-example";
void app_main(void)
{
ESP_ERROR_CHECK(i2c_master_init());
ESP_LOGI(TAG, "I2C initialized successfully");
while (1)
{
printf("Tempreture = %.2f℃\n", SHT20_Get_Tempreture());
printf("Humidity = %.2f%%\n\n", SHT20_Get_Humidity());
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
SHT20.c文件内容
#include <stdio.h>
#include "driver/i2c.h"
#include "esp_log.h"
#include "./../Include/SHT20.h"
esp_err_t i2c_master_init(void)
{
int i2c_master_port = I2C_MASTER_NUM;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
i2c_param_config(i2c_master_port, &conf);
return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}
// 非占用总线的温度测量,默认14bit数据。
float SHT20_Get_Tempreture(void)
{
uint8_t data[2] = {0, 0};
float Tempreture = 0;
i2c_cmd_handle_t handle = i2c_cmd_link_create();
i2c_master_start(handle);
i2c_master_write_byte(handle, SHT20_SENSOR_ADDR << 1 | 0, ACK_CHECK_EN);
i2c_master_write_byte(handle, SHT20_SENSOR_TMEASUREMENT_NOHOLD_ADDR, ACK_CHECK_EN);
vTaskDelay(0.02 / portTICK_PERIOD_MS);
i2c_master_stop(handle);
i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(handle);
vTaskDelay(90 / portTICK_PERIOD_MS);
handle = i2c_cmd_link_create();
i2c_master_start(handle);
i2c_master_write_byte(handle, SHT20_SENSOR_ADDR << 1 | 1, ACK_CHECK_EN);
i2c_master_read_byte(handle, &data[0], ACK_VAL);
i2c_master_read_byte(handle, &data[1], NACK_VAL);
// i2c_master_read_byte(handle,&data[2],NACK_VAL);抛弃校验和,想要校验和,上一行改为ACK_VAL,取消注释此行
i2c_master_stop(handle);
i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(handle);
Tempreture = (data[0] << 8) | (data[1] & 0xFC);
Tempreture = 175.72 * ((float)Tempreture / 65536) - 46.85;
return Tempreture;
}
// 非占用总线的湿度测量,默认12bit数据。
float SHT20_Get_Humidity(void)
{
uint8_t data[2] = {0, 0};
float Humidity = 0;
i2c_cmd_handle_t handle = i2c_cmd_link_create();
i2c_master_start(handle);
i2c_master_write_byte(handle, SHT20_SENSOR_ADDR << 1 | 0, ACK_CHECK_EN);
i2c_master_write_byte(handle, SHT20_SENSOR_RHMEASUREMENT_NOHOLD_ADDR, ACK_CHECK_EN);
vTaskDelay(0.02 / portTICK_PERIOD_MS);
i2c_master_stop(handle);
i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(handle);
vTaskDelay(35 / portTICK_PERIOD_MS);
handle = i2c_cmd_link_create();
i2c_master_start(handle);
i2c_master_write_byte(handle, SHT20_SENSOR_ADDR << 1 | 1, ACK_CHECK_EN);
i2c_master_read_byte(handle, &data[0], ACK_VAL);
i2c_master_read_byte(handle, &data[1], NACK_VAL);
// i2c_master_read_byte(handle,&data[2],NACK_VAL);抛弃校验和,想要校验和,上一行改为ACK_VAL,取消注释此行
i2c_master_stop(handle);
i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(handle);
Humidity = (data[0] << 8) | (data[1] & 0xFC);
Humidity = 125 * ((float)Humidity / 65536) - 6;
return Humidity;
}
SHT20.h文件内容
#ifndef _SHT20_H_
#define _SHT20_H_
#define I2C_MASTER_SCL_IO 4 /*!< GPIO number used for I2C master clock */
#define I2C_MASTER_SDA_IO 15 /*!< GPIO number used for I2C master data */
#define I2C_MASTER_NUM 1 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_TIMEOUT_MS 1000
#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 SHT20_SENSOR_ADDR 0x40 /*!< Slave address of the SHT20 sensor */
#define SHT20_SENSOR_TMEASUREMENT_NOHOLD_ADDR 0xF3 /*!< Register addresses of the "who am I" register */
#define SHT20_SENSOR_RHMEASUREMENT_NOHOLD_ADDR 0xF5 /*!< Register addresses of the "who am I" register */
// #define SHT20_SENSOR_USER_REG_ADDR 0xE7 /*!< Register addresses of the "who am I" register */
esp_err_t i2c_master_init(void);
float SHT20_Get_Tempreture(void);
float SHT20_Get_Humidity(void);
#endif /*_SHT20_H_*/