零知开源分享-温湿度模块 SHT3X SHT30 SHT31 SHT35-Arduino中文社区 - Powered by Discuz! Archiver

零知实验室-roc 发表于 2019-5-10 11:49

零知开源分享-温湿度模块 SHT3X SHT30 SHT31 SHT35

本帖最后由 零知实验室-roc 于 2019-5-13 16:43 编辑

1、说明
MCU:零知开源开发板-标准板https://www.amobbs.com/data/attachment/forum/201905/13/151704uws3p70j0p6lcc2j.jpg
温湿度模块:SHT3X开发工具:零知实验室软件开发工具https://www.amobbs.com/data/attachment/forum/201905/13/151335qhjxv8777nv7nt2t.png
功能:测量环境的温度和湿度信息
2、硬件连接
将模块的SCL,SDA分别于零知标准板的 A5,A4, I2C接口连接即可。http://www.lingzhilab.com/ueditor/php/upload/image/20190430/1556590795737684.png      
3、测试连线接好后,只需打开零知开源开发工具找到SHT3X示例的程序代码即可上传调试了。第一步:找到示例程序第二步:连接相应串口,上传程序第三步:打开调试窗口,可以看到温湿度的情况
SHT3X模块比较灵敏,用手轻轻捂一下或者轻轻吹口气就能看到温度或湿度的变化啦。
3、核心代码
/**
*      使用SHT3X 模块测试温湿度信息      www.lingzhilab.com
*/

#include "HTU3X.h"

#define LED1      PA8

//Create an instance of the object
HTU3X myHumidity;

void setup()
{
Serial.begin(9600);
Serial.println("HTU21D Example!");

      pinMode(LED1, OUTPUT);

myHumidity.begin();
}

void loop()
{
      digitalWrite(LED1, HIGH);
      delay(500);
      digitalWrite(LED1, LOW);
         
float humd, temp;
myHumidity.readTempAndHumi(&temp, &humd);

Serial.print("时间:");
Serial.print(millis());
Serial.print(" 温度:");
Serial.print(temp, 1);
Serial.print(" °C");
Serial.print(" 湿度:");
Serial.print(humd, 1);
Serial.print("%");

Serial.println();
delay(1000);
}/**
*   file : HTU3X.cpp
*/
#include "HTU3X.h"

HTU3X::HTU3X()
{
//Set initial values for private vars
}

//Begin
/*******************************************************************************************/
//Start I2C communication
void HTU3X::begin(SoftWire &wirePort)
{
_i2cPort = &wirePort; //Grab which port the user wants us to use
   
_i2cPort->begin();
}

#define MAX_WAIT 100
#define DELAY_INTERVAL 10
#define MAX_COUNTER (MAX_WAIT/DELAY_INTERVAL)

//read 2-byte value with CRC from the HTU3X
byte HTU3X::readValue(uint16_t *res)
{

byte msb, lsb, checksum;

msb = _i2cPort->read();
lsb = _i2cPort->read();
checksum = _i2cPort->read();

uint16_t rawValue = ((uint16_t) msb << 8) | (uint16_t) lsb;

if (checkCRC(rawValue, checksum) != 0)
    return (ERROR_BAD_CRC); //Error out

*res = rawValue;

return true;
}

byte HTU3X::readTempAndHumi(float *temp, float *humi)
{
    //Request a humidity reading
_i2cPort->beginTransmission(HTU3X_ADDR);
uint16_t cmd = CMD_MEAS_CLOCKSTR_H;
_i2cPort->write(cmd>>8); //Measure value (prefer no hold!)
_i2cPort->write(cmd);
_i2cPort->endTransmission();
   
delay(500);

_i2cPort->requestFrom(HTU3X_ADDR,6);

uint16_t rawTemp, rawHumi;
readValue(&rawTemp);
readValue(&rawHumi);

_i2cPort->endTransmission();

*temp = 175.0f * (float)rawTemp / 65535.0f - 45.0f;
*humi = 100.0f * (float)rawHumi / 65535.0f;

}

#define SHIFTED_DIVISOR 0x988000 //This is the 0x0131 polynomial shifted to farthest left of three bytes

byte HTU3X::checkCRC(uint16_t message_from_sensor, uint8_t check_value_from_sensor)
{
uint8_t bit;      
uint8_t crc = 0xFF;
uint8_t byteCtr;   
   uint8_t data = {message_from_sensor>>8, (uint8_t)message_from_sensor};
for(byteCtr = 0; byteCtr < 2; byteCtr++)
{
          crc ^= (data);
          for(bit = 8; bit > 0; --bit)
          {
                  if(crc & 0x80)
                        crc = (crc << 1) ^ 0x131;
                  else         
                        crc = (crc << 1);
          }
}
   
if(crc != check_value_from_sensor)
          return 1;
else                              
          return 0;
}更多详细资料可到零知官网免费获取哦。

gaobao_1 发表于 2019-6-24 06:19

学习了,不错

wison 发表于 2020-6-25 18:47

本帖最后由 wison 于 2020-6-25 19:12 编辑

:):):):):):)
页: [1]
查看完整版本: 零知开源分享-温湿度模块 SHT3X SHT30 SHT31 SHT35