【BSP】 SHT10

Time:2021-1-13

Applicable platform

  • There are no specific requirements for STM32, m5311 (requiring level conversion circuit), da14531, etc

Driving advantage

  • Separation of implementation and interface (object encapsulation)
  • Platform migration layer separation (GPIO mode)
  • Automatic bus management (low power processing when bus is idle)
  • Correction coefficient management of different working voltages

Special notes

  • SHT10 is not systematically introduced in this document, so users need to refer to the specification for understanding
  • Users only need to modify the migration layer of the. H file

    • Connected GPIO (SCK, SDA)
    • Working voltage of SHT10

File: main. C

#include "bsp_sht10.h"

int main()
{
    float temp = 0.0f;
    float humi = 0.0f;
    float dew_poiusernt = 0.0f;
    
    SHT_ HandlerSdef *sht_ handler = sht_ Init(); // parameter initialization
    
    for (;;)
    {
        if (sht_ handler->user_ Gettempandhumi (& temp, & humi)) // read temperature and humidity
        {
            printf("temp:%f, humi:%f\n", temp, humi);
        }
        
        if (sht_ handler->user_ Getdewpoiusernt()) // dew point reading
        {
            printf("DewPoiusernt:%f\n", dew_poiusernt);
        }
    }
}

File: BSP_ sht10.h

#ifndef BSP_SHT10_H
#define BSP_SHT10_H

/** Inlcude --------------------------------------------------*/
#include <stdint.h>

/**
 *Transplantation layer of @ brief system
 * @{
 */
#include "gpio.h"

#define SHT10_DATA_PIN  GPIO_PIN_1
#define SHT10_SCK_PIN   GPIO_PIN_0
#define SHT10_DATA_PORT GPIO_PORT_0
#define SHT10_SCK_PORT  GPIO_PORT_0
 
#define SHT10_DATA_H()  GPIO_SetActive(SHT10_DATA_PORT, SHT10_DATA_PIN)
#define SHT10_DATA_L()  GPIO_SetInactive(SHT10_DATA_PORT, SHT10_DATA_PIN)
#define SHT10_DATA_R()  GPIO_GetPinStatus(SHT10_DATA_PORT, SHT10_DATA_PIN)
 
#define SHT10_SCK_H()   GPIO_SetActive(SHT10_SCK_PORT, SHT10_SCK_PIN)
#define SHT10_SCK_L()   GPIO_SetInactive(SHT10_SCK_PORT, SHT10_SCK_PIN)

#define sht10_pinConfig()  {GPIO_ConfigurePin(SHT10_DATA_PORT, SHT10_DATA_PIN, OUTPUT, PID_GPIO, true);GPIO_ConfigurePin(SHT10_SCK_PORT, SHT10_SCK_PIN, OUTPUT, PID_GPIO, true);}
//Low power processing of bus release
#define sht10_pinRelease() {GPIO_ConfigurePin(SHT10_DATA_PORT, SHT10_DATA_PIN, INPUT, PID_GPIO, false);GPIO_ConfigurePin(SHT10_SCK_PORT, SHT10_SCK_PIN, INPUT, PID_GPIO, false);}
#define sht10_setDataOut() GPIO_ConfigurePin(SHT10_DATA_PORT, SHT10_DATA_PIN, OUTPUT, PID_GPIO, true)
#define sht10_setDataIn()  GPIO_ConfigurePin(SHT10_DATA_PORT, SHT10_DATA_PIN, INPUT_PULLUP, PID_GPIO, false)

/*
__inline void sht10_pinConfig(void)
{
   GPIO_ConfigurePin(SHT10_DATA_PORT, SHT10_DATA_PIN, OUTPUT, PID_GPIO, true);
   GPIO_ConfigurePin(SHT10_SCK_PORT, SHT10_SCK_PIN, OUTPUT, PID_GPIO, true);
}
//Low power processing of bus release
__inline void sht10_pinRelease(void)
{
    GPIO_ConfigurePin(SHT10_DATA_PORT, SHT10_DATA_PIN, INPUT, PID_SPI_DI, false);
    GPIO_ConfigurePin(SHT10_SCK_PORT, SHT10_SCK_PIN, INPUT, PID_SPI_DI, false);
}

__inline void sht10_setDataOut(void)
{
   GPIO_ConfigurePin(SHT10_DATA_PORT, SHT10_DATA_PIN, OUTPUT, PID_GPIO, true);
}

__inline void sht10_setDataIn(void)
{
    GPIO_ConfigurePin(SHT10_DATA_PORT, SHT10_DATA_PIN, INPUT_PULLUP, PID_GPIO, false);
}
*/

 /**
 *Current working voltage value of @ brief SHT10 (influence correction coefficient)
 */
//#define SHT10_POWER_LEVEL_5V 
//#define SHT10_POWER_LEVEL_4V 
//#define SHT10_POWER_LEVEL_3_5V 
#define SHT10_POWER_LEVEL_3V 
//#define SHT10_POWER_LEVEL_2_5V 

/**
 * @}
 */

 /**
 * @brief SHT10 handler
 */
typedef struct 
{
    uint8_t (*user_reset)(void);
    uint8_t (*user_getTempAndHumi)(float *temp, float *humi);
    uint8_t (*user_getDewPoiusernt)(float *value);
    
}SHT_HandlerSdef;

/** Export function ------------------------------------*/
SHT_HandlerSdef* sht_init(void);

#endif

File: BSP_ sht10.c

/**
  **************************************************
  *@file    bsp_sht10.c
  *@Brief SHT10 bottom driver file
  *@version v0.0.1
  *@history
  
    <version>  <data>      <author>  <description>
    V0.0.1 2020 / 07 / 06 Wu Tiansong officially released
    
  *************************************************
  */

/** Inlcude --------------------------------------------------*/
#include "bsp_sht10.h"

#include <math.h>

/**Private macros ----------------------*/
                                      //addr  command     r/w
#define SHT10_STATUS_REG_W    0x06    //000     0011      0
#define SHT10_STATUS_REG_R    0x07    //000     0011      1
#define SHT10_MEASURE_TEMP    0x03    //000     0001      1
#define SHT10_MEASURE_HUMI    0x05    //000     0010      1
#define SHT10_SOFTRESET       0x1E    //000     1111      0

#define SHT10_NOACK    0
#define SHT10_ACK      1

#ifdef SHT10_POWER_LEVEL_5V
const float SHT10_D1 = -40.1;
#elif defined SHT10_POWER_LEVEL_4V
const float SHT10_D1 = -39.8;
#elif defined SHT10_POWER_LEVEL_3_5V
const float SHT10_D1 = -39.7;
#elif defined SHT10_POWER_LEVEL_3V
const float SHT10_D1 = -39.6;
#elif defined SHT10_POWER_LEVEL_2_5V
const float SHT10_D1 = -39.4;
#else
#Error "SHT10 working voltage not specified"
#endif

typedef enum 
{
    SHT_Temp = 0u,
    SHT_Humi
    
}SHT_ModeEdef;

/** Pravite function prototypes ------------------------------*/
static uint8_t sht10_writeByte(uint8_t data);
static uint8_t sht10_readByte(uint8_t is_ack);
static void sht10_transStart(void);
static void sht10_conReset(void);
static uint8_t sht10_reset(void);
static uint8_t sht10_measure(uint16_t *p_value, SHT_ModeEdef mode);
static uint8_t sht10_getTempAndHumi(float *temp, float *humi);
static uint8_t get_getDewPoint(float *value);

/** Pravite variable -----------------------------------------*/
static SHT_HandlerSdef g_sht_handler;

/** Applation */

/**
  *@Brief important data registration and initialization
  *@param  none
  *@retval none
  */
SHT_HandlerSdef* sht_init(void)
{
    g_sht_handler.user_reset           = sht10_reset;
    g_sht_handler.user_getTempAndHumi  = sht10_getTempAndHumi;
    g_sht_handler.user_getDewPoiusernt = get_getDewPoint;
    
    sht10_reset();
    
    return &g_sht_handler;
}

/**
  *@Brief delay function (> 10us)
  *@param  none
  *@retval none
  */
static void sht10_delay(void)
{
    for (uint16_t i=0; i<100; ++i)
    { }
}

/**
  *@Brief write a byte
  *@Param data: bytes to write
  *@Retval 0 succeeded
  *@Retval other failures
  */
static uint8_t sht10_writeByte(uint8_t data)
{
    uint8_t ret = 0;
    
    sht10_setDataOut();
 
    for(uint8_t i = 0x80; i > 0; i>>=1)
    {
        if(i & data)
            SHT10_DATA_H();
        else
            SHT10_DATA_L();
        sht10_delay();
        SHT10_SCK_H();
        sht10_delay();
        SHT10_SCK_L();
        sht10_delay();
    }
    
    sht10_setDataIn();
    SHT10_SCK_H();
    ret = SHT10_DATA_R();
    SHT10_SCK_L();
 
    return ret;
}

/**
  *@Brief reads a byte
  *@param  is_ ACK: 0 does not respond; 1 responds
  *@Retval 0 succeeded
  *@Retval other failures
  */
static uint8_t sht10_readByte(uint8_t is_ack)
{
    uint8_t ret = 0;
 
    sht10_setDataIn();
    
    for(uint8_t i = 0x80; i > 0; i>>=1)
    {
        sht10_delay();
        SHT10_SCK_H();
        sht10_delay();
        if(SHT10_DATA_R())
            ret |= i;
        SHT10_SCK_L();
    }
    
    sht10_setDataOut();
    if(is_ack)
        SHT10_DATA_L();
    else
        SHT10_DATA_H();
    sht10_delay();
    SHT10_SCK_H();
    sht10_delay();
    SHT10_SCK_L();
    sht10_delay();
 
    return ret;
}

/**
  *@Brief starts transmitting the signal
  *@param  none
  *@retval none
  */
static void sht10_transStart(void)
{
    sht10_setDataOut();
 
    SHT10_DATA_H();
    SHT10_SCK_L();
    sht10_delay();
    SHT10_SCK_H();
    sht10_delay();
    SHT10_DATA_L();
    sht10_delay();
    SHT10_SCK_L();
    sht10_delay();
    SHT10_SCK_H();
    sht10_delay();
    SHT10_DATA_H();
    sht10_delay();
    SHT10_SCK_L();
}

/**
  *@Brief communication reset
  *@param  none
  *@retval none
  */
static void sht10_conReset(void)
{
    sht10_setDataOut();
 
    SHT10_DATA_H();
    SHT10_SCK_L();
 
    for(uint8_t i = 0; i < 9; i++)
    {
        SHT10_SCK_H();
        sht10_delay();
        SHT10_SCK_L();
        sht10_delay();
    }
    
    sht10_transStart();
}

/**
  *@Brief soft reset
  *@param  none
  *@Retval 0 succeeded
  *@Retval other failures
  */
static uint8_t sht10_reset(void)
{
    uint8_t ret = 0;
    
    sht10_pinConfig();
    
    sht10_conReset();
    
    ret = sht10_writeByte(SHT10_SOFTRESET);
    
    sht10_pinRelease();
    
    return ret;
}

/**
  *@Brief temperature / humidity register original value reading
  *@param  p_ Value: data storage address
  *@param  mode    : SHT_ Sht temperature_ Humi humidity
  *@Retval 0 succeeded
  *@Retval other failures
  */
static uint8_t sht10_measure(uint16_t *p_value, SHT_ModeEdef mode)
{
    uint8_t status = 0;
    
    sht10_transStart();
    
    switch(mode)
    {
        case SHT_Temp:
            status = sht10_writeByte(SHT10_MEASURE_TEMP);
        break;
        case SHT_Humi:
            status =sht10_writeByte(SHT10_MEASURE_HUMI);
        break;
    }
    
    sht10_setDataIn();
    
    if (status == 1)
    {
        sht10_conReset();
        return 1;
    }
    
    for(uint32_t i = 0; i < 72000000; ++i) 
        if(SHT10_DATA_R() == 0) break;  
    
    if (SHT10_DATA_R() == 1)
    {
        sht10_conReset();
        return 1;
    }
    
    uint8_t value_H = 0;
    uint8_t value_L = 0;
    
    value_H = sht10_readByte(SHT10_ACK);
    value_L = sht10_readByte(SHT10_ACK);
    
    sht10_readByte(SHT10_NOACK);
    
    *p_value = (value_H << 8) | value_L;
    
    return 0;
}

/**
  *@Brief readable temperature and humidity reading
  *@Param temp: temperature value
  *@Param humi: humidity value
  *@Retval 0 succeeded
  *@Retval other failures
  */
static uint8_t sht10_getTempAndHumi(float *temp, float *humi)
{
    const float d1 = SHT10_D1;
    const float d2 = +0.01;
    const float C1 = -2.0468;
    const float C2 = +0.0367;
    const float C3 = -0.0000015955;
    const float T1 = +0.01;
    const float T2 = +0.00008;
    
    float rh_line = 0;
    
    uint16_t temp_value_original = 0;
    uint16_t humi_value_original = 0;
    
    sht10_pinConfig();
    
    if ((sht10_measure(&temp_value_original, SHT_Temp) != 0) || (sht10_measure(&humi_value_original, SHT_Humi) != 0))
    {
        sht10_pinRelease();
        return 1;
    }
    
    rh_line = C1 + C2 * humi_value_original + C3 * humi_value_original * humi_value_original;
    
    *temp = d1 + d2 * temp_value_original;
    *humi = (*temp -25) * (T1 + T2 * humi_value_original) + rh_line;
    
    sht10_pinRelease();
    
    return 0;
}

/**
  *@Brief calculation of dew point
  *@Param value: the dew point value read
  *@Retval 0 succeeded
  *@Retval other failures
  */
static uint8_t get_getDewPoint(float *value)
{
    float temp = 0;
    float humi = 0;
    float logEx =0.0f;
    
    sht10_pinConfig();
    
    if (sht10_getTempAndHumi(&temp, &humi) != 0)
    {
        sht10_pinRelease();
        return 1;
    }
    
    logEx = 0.66077 + 7.5 * temp / (237.3 + temp) + (log10(humi) - 2);
    *value = ((0.66077 - logEx) * 237.3) / (logEx - 8.16077);
    
    sht10_pinRelease();
    
    return 0;
}

Recommended Today

The use of springboot Ajax

Ajax overview What is Ajax? data Ajax application scenarios? project Commodity system. Evaluation system. Map system. ….. Ajax can only send and retrieve the necessary data to the server, and use JavaScript to process the response from the server on the client side. data But Ajax technology also has disadvantages, the biggest disadvantage is that […]