初始化提交
This commit is contained in:
114
arduino-cli/libraries/DFRobot_SHT20/DFRobot_SHT20.cpp
Normal file
114
arduino-cli/libraries/DFRobot_SHT20/DFRobot_SHT20.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "DFRobot_SHT20.h"
|
||||
|
||||
void DFRobot_SHT20::initSHT20(TwoWire &wirePort)
|
||||
{
|
||||
i2cPort = &wirePort;
|
||||
i2cPort->begin();
|
||||
}
|
||||
|
||||
uint16_t DFRobot_SHT20::readValue(byte cmd)
|
||||
{
|
||||
i2cPort->beginTransmission(SLAVE_ADDRESS);
|
||||
i2cPort->write(cmd);
|
||||
i2cPort->endTransmission();
|
||||
byte toRead;
|
||||
byte counter;
|
||||
for(counter = 0, toRead = 0 ; counter < MAX_COUNTER && toRead != 3; counter++){
|
||||
delay(DELAY_INTERVAL);
|
||||
toRead = i2cPort->requestFrom(SLAVE_ADDRESS, 3);
|
||||
}
|
||||
if(counter == MAX_COUNTER){
|
||||
return (ERROR_I2C_TIMEOUT);
|
||||
}
|
||||
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);
|
||||
}
|
||||
return rawValue & 0xFFFC;
|
||||
}
|
||||
|
||||
float DFRobot_SHT20::readHumidity(void)
|
||||
{
|
||||
uint16_t rawHumidity = readValue(TRIGGER_HUMD_MEASURE_NOHOLD);
|
||||
if(rawHumidity == ERROR_I2C_TIMEOUT || rawHumidity == ERROR_BAD_CRC){
|
||||
return(rawHumidity);
|
||||
}
|
||||
float tempRH = rawHumidity * (125.0 / 65536.0);
|
||||
float rh = tempRH - 6.0;
|
||||
return (rh);
|
||||
}
|
||||
|
||||
float DFRobot_SHT20::readTemperature(void)
|
||||
{
|
||||
uint16_t rawTemperature = readValue(TRIGGER_TEMP_MEASURE_NOHOLD);
|
||||
if(rawTemperature == ERROR_I2C_TIMEOUT || rawTemperature == ERROR_BAD_CRC){
|
||||
return(rawTemperature);
|
||||
}
|
||||
float tempTemperature = rawTemperature * (175.72 / 65536.0);
|
||||
float realTemperature = tempTemperature - 46.85;
|
||||
return (realTemperature);
|
||||
}
|
||||
|
||||
void DFRobot_SHT20::setResolution(byte resolution)
|
||||
{
|
||||
byte userRegister = readUserRegister();
|
||||
userRegister &= B01111110;
|
||||
resolution &= B10000001;
|
||||
userRegister |= resolution;
|
||||
writeUserRegister(userRegister);
|
||||
}
|
||||
|
||||
byte DFRobot_SHT20::readUserRegister(void)
|
||||
{
|
||||
byte userRegister;
|
||||
i2cPort->beginTransmission(SLAVE_ADDRESS);
|
||||
i2cPort->write(READ_USER_REG);
|
||||
i2cPort->endTransmission();
|
||||
i2cPort->requestFrom(SLAVE_ADDRESS, 1);
|
||||
userRegister = i2cPort->read();
|
||||
return (userRegister);
|
||||
}
|
||||
|
||||
void DFRobot_SHT20::writeUserRegister(byte val)
|
||||
{
|
||||
i2cPort->beginTransmission(SLAVE_ADDRESS);
|
||||
i2cPort->write(WRITE_USER_REG);
|
||||
i2cPort->write(val);
|
||||
i2cPort->endTransmission();
|
||||
}
|
||||
|
||||
byte DFRobot_SHT20::checkCRC(uint16_t message_from_sensor, uint8_t check_value_from_sensor)
|
||||
{
|
||||
uint32_t remainder = (uint32_t)message_from_sensor << 8;
|
||||
remainder |= check_value_from_sensor;
|
||||
uint32_t divsor = (uint32_t)SHIFTED_DIVISOR;
|
||||
for(int i = 0 ; i < 16 ; i++){
|
||||
if(remainder & (uint32_t)1 << (23 - i)){
|
||||
remainder ^= divsor;
|
||||
}
|
||||
divsor >>= 1;
|
||||
}
|
||||
return (byte)remainder;
|
||||
}
|
||||
|
||||
void DFRobot_SHT20::showReslut(const char *prefix, int val)
|
||||
{
|
||||
Serial.print(prefix);
|
||||
if(val){
|
||||
Serial.println("yes");
|
||||
}else{
|
||||
Serial.println("no");
|
||||
}
|
||||
}
|
||||
|
||||
void DFRobot_SHT20::checkSHT20(void)
|
||||
{
|
||||
byte reg = readUserRegister();
|
||||
showReslut("End of battery: ", reg & USER_REGISTER_END_OF_BATTERY);
|
||||
showReslut("Heater enabled: ", reg & USER_REGISTER_HEATER_ENABLED);
|
||||
showReslut("Disable OTP reload: ", reg & USER_REGISTER_DISABLE_OTP_RELOAD);
|
||||
}
|
||||
52
arduino-cli/libraries/DFRobot_SHT20/DFRobot_SHT20.h
Normal file
52
arduino-cli/libraries/DFRobot_SHT20/DFRobot_SHT20.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef DFRobot_SHT20_h
|
||||
#define DFRobot_SHT20_h
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
#include <Wire.h>
|
||||
|
||||
#define ERROR_I2C_TIMEOUT 998
|
||||
#define ERROR_BAD_CRC 999
|
||||
#define SLAVE_ADDRESS 0x40
|
||||
#define TRIGGER_TEMP_MEASURE_HOLD 0xE3
|
||||
#define TRIGGER_HUMD_MEASURE_HOLD 0xE5
|
||||
#define TRIGGER_TEMP_MEASURE_NOHOLD 0xF3
|
||||
#define TRIGGER_HUMD_MEASURE_NOHOLD 0xF5
|
||||
#define WRITE_USER_REG 0xE6
|
||||
#define READ_USER_REG 0xE7
|
||||
#define SOFT_RESET 0xFE
|
||||
#define USER_REGISTER_RESOLUTION_MASK 0x81
|
||||
#define USER_REGISTER_RESOLUTION_RH12_TEMP14 0x00
|
||||
#define USER_REGISTER_RESOLUTION_RH8_TEMP12 0x01
|
||||
#define USER_REGISTER_RESOLUTION_RH10_TEMP13 0x80
|
||||
#define USER_REGISTER_RESOLUTION_RH11_TEMP11 0x81
|
||||
#define USER_REGISTER_END_OF_BATTERY 0x40
|
||||
#define USER_REGISTER_HEATER_ENABLED 0x04
|
||||
#define USER_REGISTER_DISABLE_OTP_RELOAD 0x02
|
||||
#define MAX_WAIT 100
|
||||
#define DELAY_INTERVAL 10
|
||||
#define SHIFTED_DIVISOR 0x988000
|
||||
#define MAX_COUNTER (MAX_WAIT/DELAY_INTERVAL)
|
||||
|
||||
class DFRobot_SHT20
|
||||
{
|
||||
public:
|
||||
void checkSHT20(void);
|
||||
void setResolution(byte resBits);
|
||||
void writeUserRegister(byte val);
|
||||
void initSHT20(TwoWire &wirePort = Wire);
|
||||
void showReslut(const char *prefix, int val);
|
||||
float readHumidity(void);
|
||||
float readTemperature(void);
|
||||
byte readUserRegister(void);
|
||||
|
||||
private:
|
||||
TwoWire *i2cPort;
|
||||
byte checkCRC(uint16_t message_from_sensor, uint8_t check_value_from_sensor);
|
||||
uint16_t readValue(byte cmd);
|
||||
};
|
||||
|
||||
#endif
|
||||
53
arduino-cli/libraries/DFRobot_SHT20/README.md
Normal file
53
arduino-cli/libraries/DFRobot_SHT20/README.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# DFRobot_SHT20 Library for Arduino
|
||||
Provides an Arduino library for SHT20 Humidity And Temperature Sensor
|
||||
## Table of Contents
|
||||
|
||||
* [Summary](#summary)
|
||||
* [Methods](#methods)
|
||||
* [History](#history)
|
||||
* [Credits](#credits)
|
||||
<snippet>
|
||||
<content>
|
||||
|
||||
## Summary
|
||||
|
||||
The library is used to Use the SHT20 Sensor to obtain humidity and temperature.
|
||||
|
||||
## Methods
|
||||
|
||||
```C++
|
||||
|
||||
/*
|
||||
* @brief Init SHT20 Sensor
|
||||
*/
|
||||
void initSHT20(void);
|
||||
|
||||
/*
|
||||
* @brief Check SHT20 Sensor
|
||||
*/
|
||||
void checkSHT20(void);
|
||||
|
||||
/*
|
||||
* @brief Read humidity
|
||||
*
|
||||
* @return The value of humidity
|
||||
*/
|
||||
float readHumidity(void);
|
||||
|
||||
/*
|
||||
* @breif Read Temperature
|
||||
*
|
||||
* @return The value of temperature
|
||||
*/
|
||||
float readTemperature(void);
|
||||
|
||||
```
|
||||
|
||||
## History
|
||||
|
||||
- data 2017-9-12
|
||||
- version V1.0
|
||||
|
||||
## Credits
|
||||
|
||||
- author [Zhangjiawei <jiawei.zhang@dfrobot.com>]
|
||||
@@ -0,0 +1,43 @@
|
||||
/*!
|
||||
* @file DFRobot_SHT20_test.ino
|
||||
* @brief DFRobot's SHT20 Humidity And Temperature Sensor Module
|
||||
* @n This example demonstrates how to read the user registers to display resolution and other settings.
|
||||
* Uses the SHT20 library to display the current humidity and temperature.
|
||||
* Open serial monitor at 9600 baud to see readings.
|
||||
* Errors 998 if not sensor is detected. Error 999 if CRC is bad.
|
||||
* Hardware Connections:
|
||||
* -VCC = 3.3V
|
||||
* -GND = GND
|
||||
* -SDA = A4 (use inline 330 ohm resistor if your board is 5V)
|
||||
* -SCL = A5 (use inline 330 ohm resistor if your board is 5V)
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#include "DFRobot_SHT20.h"
|
||||
|
||||
DFRobot_SHT20 sht20;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println("SHT20 Example!");
|
||||
sht20.initSHT20(); // Init SHT20 Sensor
|
||||
delay(100);
|
||||
sht20.checkSHT20(); // Check SHT20 Sensor
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
float humd = sht20.readHumidity(); // Read Humidity
|
||||
float temp = sht20.readTemperature(); // Read Temperature
|
||||
Serial.print("Time:");
|
||||
Serial.print(millis());
|
||||
Serial.print(" Temperature:");
|
||||
Serial.print(temp, 1);
|
||||
Serial.print("C");
|
||||
Serial.print(" Humidity:");
|
||||
Serial.print(humd, 1);
|
||||
Serial.print("%");
|
||||
Serial.println();
|
||||
delay(1000);
|
||||
}
|
||||
22
arduino-cli/libraries/DFRobot_SHT20/keywords.txt
Normal file
22
arduino-cli/libraries/DFRobot_SHT20/keywords.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
DFRobot_SHT20 KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
initSHT20 KEYWORD2
|
||||
readHumidity KEYWORD2
|
||||
readTemperature KEYWORD2
|
||||
checkSHT20 KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
Reference in New Issue
Block a user