Drive of DHTXX transmitter under STM32

Source: Internet
Author: User

DHT11 and DHT10 sensors, mainly collect temperature and humidity, based on my I2C simulation library implementation.

The main code is as follows:

#ifndef _sh10_drv_h_
#define _sh10_drv_h_

#include "stm32f10x_lib.h"

void Sh_i2cinit (void);

U8 Sh_writereg (U8 val);

U8 Sh_readreg (vu8 *val);

U8 sh_readtemprature (float *val);

U8 sh_readhumility (float *val);

U8 Sh_readtempandhum (float *t, float *h);

#endif//_sh10_drv_h_

#include "hky_sht10.h" #undef PIN_SCL #define PIN_SCL gpio_pin_6 #undef PIN_SDA #define PIN_SDA gpio_pin_7 #inclu De "hky_i2c.h" #define SH_CMD_TEMP 0x03 #define Sh_cmd_hum 0x05 #define SH_CMD_RD_REG 0x07 #define SH_CMD_WR_REG 0x0
    6 #define Sh_cmd_rst 0x1E void Sh_i2cinit () {U8 I;
    Twi_initialize ();
	for (i = 0; i < i++) {twi_scl_0 ();
	Twi_nop ();
	Twi_scl_1 ();
    Twi_nop ();
    } U8 Sh_writereg (U8 val) {U8 ret = Twi_ack;
    ret = Twi_start_sht ();

    if (ret = = Twi_bus_error) return ret;
    ret = Twi_sendbyte (Sh_cmd_wr_reg);
    
    if (ret = = Twi_nack) return ret;

    ret = Twi_sendbyte (val);
    Twi_stop ();
return ret;
    } U8 Sh_readreg (Vu8 *val) {U8 ret = Twi_ack;
    
    U8 sum = 0;
    ret = Twi_start_sht ();

    if (ret = = Twi_bus_error) return ret;
    Send address ret = Twi_sendbyte (Sh_cmd_rd_reg);
	
    if (ret = = Twi_nack) return ret;
    Read one byte *val = Twi_receivebyte (); Twi_sendnACK ();
    sum = Twi_receivebyte ();
    
    Twi_sendnack ();

    Twi_stop ();
return ret;
    } U8 Sh_readtempraturereg (u32 *val) {U8 ret = Twi_ack;
    U16 TMP;

    U8 Check_sum;
    ret = Twi_start_sht ();

    if (ret = = Twi_bus_error) return ret;
    Send address ret = Twi_sendbyte (sh_cmd_temp);
    if (ret = = Twi_nack) return ret;
    				       
    Delay_ms (100);
    TMP = Twi_receivebyte ();
    Twi_sendack ();
    TMP <<= 8;
    TMP |= twi_receivebyte ();
    
    Twi_sendack ();
    Check_sum = Twi_receivebyte ();

    Twi_stop ();

    *val = tmp;
return 0;
    } U8 Sh_readtemprature (float *val) {float D1, D2;
    U32 tmp;

    int ret; D1 = -39.66f;    3.5V D2 = 0.01;

    3.5V ret = Sh_readtempraturereg (&tmp);
    
    temprature = D1 + D2 * val *val = d1 + tmp * D2;
return ret;
    } U8 Sh_readhumilityreg (u32 *val) {U8 ret = Twi_ack;
    U16 TMP;

    U8 Check_sum;
    ret = Twi_start_sht (); if (ret = TWi_bus_error) return ret;
    Send address ret = Twi_sendbyte (Sh_cmd_hum);
   	 
	if (ret = = Twi_nack) return ret;

    Delay_ms (100);
    TMP = Twi_receivebyte ();
    Twi_sendack ();
    TMP <<= 8;
    TMP |= twi_receivebyte ();
    
    Twi_sendack ();
    Check_sum = Twi_receivebyte ();

    Twi_stop ();

    *val = tmp;
return 0;
    } U8 sh_readhumility (float *val) {float C1, C2, C3;
    U32 tmp;

    int ret;
    C1 =-4;
    C2 = 0.0405;
    
    C3 = -2.8 * 1E-6;
    ret = Sh_readhumilityreg (&tmp);
    
    *val = c1 + C2 * tmp + c3 * TMP * TMP;
return ret;
	} U8 Sh_readtempandhum (float *t, float *h) {float TT;
    U32 hh;
    int ret;
    float T1, T2;
    Float C1, C2, C3;

    float rh_linear;
    C1 = -4.0f;
    C2 = 0.0405f;

    C3 = -2.8 * 1e-6f;
    Humility adjust parameter T1 = 0.01f;

    t2 = 0.00008f;
    Read temprature ret = sh_readtemprature (&AMP;TT);
    if (Ret!= 0) return ret;

    *t = TT; Read Humility ret = Sh_readhumilityreg (&AMP;HH);

    if (Ret!= 0) return ret;

    Calculate Humility rh_linear = c1 + C2 * hh + c3 * hh * HH; Adjust humility if (tt-25.0f) > 10.0f | |
    (25.0F-TT) > 10.0f) {rh_linear = (tt-25.0f) * (t1 + t2 * hh) + rh_linear;

    } *h = Rh_linear;
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.