Skip to content

Commit

Permalink
2020-09-27 Add driver sht20 for esp32s2
Browse files Browse the repository at this point in the history
  • Loading branch information
xuhongv committed Sep 27, 2020
1 parent f738e8a commit 8101841
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 21_s2_sht20/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(SHT20)
9 changes: 9 additions & 0 deletions 21_s2_sht20/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := sht20

include $(IDF_PATH)/make/project.mk

3 changes: 3 additions & 0 deletions 21_s2_sht20/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## ESP32-S2 驱动 SHT20 获取温湿度

1. blog:https://blog.csdn.net/xh870189248/article/details/108824144
5 changes: 5 additions & 0 deletions 21_s2_sht20/components/SHT20/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
idf_component_register(
SRCS "library/sht20.c"
INCLUDE_DIRS "include"
)

3 changes: 3 additions & 0 deletions 21_s2_sht20/components/SHT20/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
COMPONENT_ADD_INCLUDEDIRS := include

COMPONENT_SRCDIRS := library
60 changes: 60 additions & 0 deletions 21_s2_sht20/components/SHT20/include/sht20.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* @Author: your name
* @Date: 2020-09-27 10:21:48
* @LastEditTime: 2020-09-27 10:42:19
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \esp-idf\examples\me\sht20\components\SHT20\include\sht20.h
*/
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#define SHT20_I2C_NUM (1)
#define SHT20_SCL_NUM (GPIO_NUM_19)
#define SHT20_SDA_NUM (GPIO_NUM_18)

#define DEV_SHT20_SCL_NUM SHT20_SCL_NUM /*!< gpio number for I2C master clock */
#define DEV_SHT20_SDA_NUM SHT20_SDA_NUM /*!< gpio number for I2C master data */
#define DEV_SHT20_I2C_NUM SHT20_I2C_NUM /*!< I2C port number for master dev */
#define I2C_MASTER_FREQ_HZ (100000) /*!< 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 WRITE_BIT 0 /*!< I2C master write */
#define READ_BIT 1 /*!< 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 ESP_SLAVE_ADDR 0x40
#define HOLD_AT_START 0xe3 //触发温度测量
#define HOLD_AH_START 0xe5 //触发湿度测量
#define REST 0xfe //软件复位


/*
*
*/
void init_sht20(void);


/*
*
*/
float get_sht20_Temperature(void);


/*
*
*/
float get_sht20_Humidity(void);


#ifdef __cplusplus
}
#endif
147 changes: 147 additions & 0 deletions 21_s2_sht20/components/SHT20/library/sht20.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
#include "string.h"

#include "sht20.h"

static const char *TAG = "sht20_drive.c";

static esp_err_t i2c_master_read_slave(i2c_port_t i2c_num, uint8_t *data_rd, size_t size)
{
if (size == 0)
{
return ESP_OK;
}

i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (ESP_SLAVE_ADDR << 1) | READ_BIT, ACK_CHECK_EN);


if (size > 1)
{
i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
}

i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);

i2c_cmd_link_delete(cmd);

return ret;
}

/**
* @brief Test code to write esp-i2c-slave
* Master device write data to slave(both esp32),
* the data will be stored in slave buffer.
* We can read them out from slave buffer.
*
* ___________________________________________________________________
* | start | slave_addr + wr_bit + ack | write n bytes + ack | stop |
* --------|---------------------------|----------------------|------|
*
*/
static esp_err_t i2c_master_write_slave(i2c_port_t i2c_num, uint8_t *data_wr, size_t size)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (ESP_SLAVE_ADDR << 1) | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write(cmd, data_wr, size, ACK_CHECK_EN);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}

/**
* @brief i2c master initialization
*/
static esp_err_t i2c_master_init(void)
{
int i2c_master_port = DEV_SHT20_I2C_NUM;
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = DEV_SHT20_SDA_NUM;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_io_num = DEV_SHT20_SCL_NUM;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.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);
}


void init_sht20(void)
{
static bool tmp = true;
if (tmp)
{
tmp = false;
ESP_ERROR_CHECK(i2c_master_init());
}
}

float get_sht20_Temperature(void)
{
uint8_t data_rx[8] = {0};
uint8_t data_wr[8] = {0};
unsigned int dat = 0;
float temp = 0;

//懒加载
init_sht20();

data_wr[0] = HOLD_AT_START;
i2c_master_write_slave(DEV_SHT20_I2C_NUM, data_wr, strlen((char *)data_wr));
vTaskDelay(20 / portTICK_RATE_MS);

i2c_master_read_slave(DEV_SHT20_I2C_NUM, data_rx, 3);

if(!data_rx[0]&&!data_rx[1]){
return -1;
}

data_rx[1] &= 0xfc;
dat = (data_rx[0] << 8) | data_rx[1];

temp = ((float)dat * 175.72) / 65536.0 - 46.85; // ℃

ESP_LOGD(TAG, "temp=%.3f℃", temp);

return temp;
}

float get_sht20_Humidity(void)
{

uint8_t data_rx[8] = {0};
uint8_t data_wr[8] = {0};
unsigned int dat = 0;
float temp = 0;

//懒加载
init_sht20();

data_wr[0] = HOLD_AH_START;
i2c_master_write_slave(DEV_SHT20_I2C_NUM, data_wr, strlen((char *)data_wr));
vTaskDelay(20 / portTICK_RATE_MS);

i2c_master_read_slave(DEV_SHT20_I2C_NUM, data_rx, 3);

if(!data_rx[0]&&!data_rx[1]){
return -1;
}

data_rx[1] &= 0xfc;
dat = (data_rx[0] << 8) | data_rx[1];

temp = (float)((dat * 125.0) / 65536.0 - 6); //%RH

ESP_LOGD(TAG, "hum=%.2f", temp);

return temp;
}
6 changes: 6 additions & 0 deletions 21_s2_sht20/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Slip client example

idf_component_register(
SRCS "slip_client_main.c"
INCLUDE_DIRS "."
)
38 changes: 38 additions & 0 deletions 21_s2_sht20/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
menu "Example Configuration"

menu "UART Configuration"
config EXAMPLE_UART_TX_PIN
int "TXD Pin Number"
default 4
range 0 36
help
Pin number of UART TX.

config EXAMPLE_UART_RX_PIN
int "RXD Pin Number"
default 36
range 0 36
help
Pin number of UART RX.

config EXAMPLE_UART_BAUD
int "UART baud rate"
default 115200
help
Baud rate for UART communication

endmenu

config EXAMPLE_UDP_PORT
int "Port for UDP echo server"
default 5678
help
Port for UDP echo server in example

config EXAMPLE_IPV4
bool "Test with IPv4 address"
default n
help
Test interface using IPv4

endmenu
4 changes: 4 additions & 0 deletions 21_s2_sht20/main/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
30 changes: 30 additions & 0 deletions 21_s2_sht20/main/slip_client_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* @Author: your name
* @Date: 2020-09-27 10:09:49
* @LastEditTime: 2020-09-27 11:07:45
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \esp-idf\examples\me\SHT20_ESP32S2\DEMO\main\slip_client_main.c
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "esp_system.h"
#include "esp_log.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "sht20.h"

static const char *TAG = "SHT20_EXAMPLE";

void app_main()
{
while (true)
{
ESP_LOGI(TAG, "-----------------");
ESP_LOGI(TAG, "T=%.2f℃", get_sht20_Temperature());
ESP_LOGI(TAG, "H=%.2f%%", get_sht20_Humidity());
vTaskDelay(2000 / portTICK_RATE_MS);
}
}

0 comments on commit 8101841

Please sign in to comment.