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

[复制链接]
 楼主| roc2two 发表于 2019-5-10 11:17 | 显示全部楼层 |阅读模式
1、说明
MCU:零知开源开发板
零知标准板 (2).jpg
温湿度模块:SHT3X
DSCN7538 (2).JPG
开发工具:零知实验室软件开发工具
开发工具界面.png

功能:测量环境的温度和湿度信息

2、硬件连接
将模块的SCL,SDA分别于零知标准板的 A5,A4, I2C接口连接即可。

514635cd4edf273b7f.png        DSCN7536 (2).JPG

3、测试
第一步:打开零知实验室开发工具,在温湿度检测可以看到SHT3X的示例程序
SHT3X-1.png
第二步:连接正确的串口,然后上传程序
3x.png
第三步:打开调试窗口即可看到结果
SHT3X.png
从调试结果是可以看到温度和湿度都有在变化的,而且SHT3X这个模块比较灵敏哦,轻轻用手捂一下或轻吹一口气都可看到变化的。
3、核心代码
[C++]
  1. #include "HTU3X.h"

  2. #define LED1        PA8

  3. //Create an instance of the object
  4. HTU3X myHumidity;

  5. void setup()
  6. {
  7.   Serial.begin(9600);
  8.   Serial.println("HTU21D Example!");

  9.         pinMode(LED1, OUTPUT);

  10.   myHumidity.begin();
  11. }

  12. void loop()
  13. {
  14.         digitalWrite(LED1, HIGH);
  15.         delay(500);
  16.         digitalWrite(LED1, LOW);
  17.          
  18.   float humd, temp;
  19.   myHumidity.readTempAndHumi(&temp, &humd);

  20.   Serial.print("时间:");
  21.   Serial.print(millis());
  22.   Serial.print(" 温度:");
  23.   Serial.print(temp, 1);
  24.   Serial.print(" °C");
  25.   Serial.print(" 湿度:");
  26.   Serial.print(humd, 1);
  27.   Serial.print("%");

  28.   Serial.println();
  29.   delay(1000);
  30. }
  1. /**
  2. *   file : HTU3X.H
  3. */

  4. #if defined(ARDUINO) && ARDUINO >= 100
  5. #include "Arduino.h"
  6. #else
  7. #include "WProgram.h"
  8. #endif

  9. #include <SoftWire.h>

  10. #define HTU3X_ADDR 0x44  //Unshifted 7-bit I2C address for the sensor

  11. #define ERROR_I2C_TIMEOUT         2
  12. #define ERROR_BAD_CRC                    3

  13. #define CMD_MEAS_PERI_2_H   0x2236
  14. #define CMD_MEAS_CLOCKSTR_H 0x2C06

  15. class HTU3X {

  16. public:
  17.   HTU3X();

  18.   //Public Functions
  19.   void begin(SoftWire &wirePort = Wire); //If user doesn't specificy then Wire will be used
  20.   byte readTempAndHumi(float *temp, float *humi);

  21.   //Public Variables

  22. private:
  23.   //Private Functions
  24.   SoftWire *_i2cPort; //The generic connection to user's chosen I2C hardware

  25.   byte checkCRC(uint16_t message_from_sensor, uint8_t check_value_from_sensor);
  26.   byte readValue(uint16_t *res);

  27.   //Private Variables

  28. };
  1. /**
  2. *   file : HTU3X.cpp
  3. */
  4. #include "HTU3X.h"

  5. HTU3X::HTU3X()
  6. {
  7.   //Set initial values for private vars
  8. }

  9. //Begin
  10. /*******************************************************************************************/
  11. //Start I2C communication
  12. void HTU3X::begin(SoftWire &wirePort)
  13. {
  14.   _i2cPort = &wirePort; //Grab which port the user wants us to use
  15.    
  16.   _i2cPort->begin();
  17. }

  18. #define MAX_WAIT 100
  19. #define DELAY_INTERVAL 10
  20. #define MAX_COUNTER (MAX_WAIT/DELAY_INTERVAL)

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

  24.   byte msb, lsb, checksum;

  25.   msb = _i2cPort->read();
  26.   lsb = _i2cPort->read();
  27.   checksum = _i2cPort->read();

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

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

  31.   *res = rawValue;

  32.   return true;
  33. }

  34. byte HTU3X::readTempAndHumi(float *temp, float *humi)
  35. {
  36.     //Request a humidity reading
  37.   _i2cPort->beginTransmission(HTU3X_ADDR);
  38.   uint16_t cmd = CMD_MEAS_CLOCKSTR_H;
  39.   _i2cPort->write(cmd>>8); //Measure value (prefer no hold!)
  40.   _i2cPort->write(cmd);
  41.   _i2cPort->endTransmission();
  42.    
  43.   delay(500);

  44.   _i2cPort->requestFrom(HTU3X_ADDR,6);

  45.   uint16_t rawTemp, rawHumi;
  46.   readValue(&rawTemp);
  47.   readValue(&rawHumi);

  48.   _i2cPort->endTransmission();

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

  51. }
  52.   
  53. #define SHIFTED_DIVISOR 0x988000 //This is the 0x0131 polynomial shifted to farthest left of three bytes

  54. byte HTU3X::checkCRC(uint16_t message_from_sensor, uint8_t check_value_from_sensor)
  55. {
  56.   uint8_t bit;        
  57.   uint8_t crc = 0xFF;
  58.   uint8_t byteCtr;   
  59.    uint8_t data[2] = {message_from_sensor>>8, (uint8_t)message_from_sensor};
  60.   for(byteCtr = 0; byteCtr < 2; byteCtr++)
  61.   {
  62.           crc ^= (data[byteCtr]);
  63.           for(bit = 8; bit > 0; --bit)
  64.           {
  65.                   if(crc & 0x80)
  66.                           crc = (crc << 1) ^ 0x131;
  67.                   else         
  68.                           crc = (crc << 1);
  69.           }
  70.   }
  71.    
  72.   if(crc != check_value_from_sensor)
  73.           return 1;
  74.   else                              
  75.           return 0;
  76. }

541085cd4eec2c414c.png
cj8510 发表于 2019-12-12 14:11 | 显示全部楼层
语言编程不会,用ardublock能玩这个模块吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

75

主题

85

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部

75

主题

85

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部