Grove Temp + Humidity sensor
Fork of driver_mbed_TH02 by
driver_mbed_TH02.cpp@0:89151affda00, 2017-03-29 (annotated)
- Committer:
- TaylorGy
- Date:
- Wed Mar 29 03:09:54 2017 +0000
- Revision:
- 0:89151affda00
- Child:
- 1:acf474af97a7
driver for TH02 sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TaylorGy | 0:89151affda00 | 1 | /* |
TaylorGy | 0:89151affda00 | 2 | * TH02_dev.cpp |
TaylorGy | 0:89151affda00 | 3 | * Driver for DIGITAL I2C HUMIDITY AND TEMPERATURE SENSOR |
TaylorGy | 0:89151affda00 | 4 | * |
TaylorGy | 0:89151affda00 | 5 | * Copyright (c) 2014 seeed technology inc. |
TaylorGy | 0:89151affda00 | 6 | * Website : www.seeed.cc |
TaylorGy | 0:89151affda00 | 7 | * Author : Oliver Wang |
TaylorGy | 0:89151affda00 | 8 | * Create Time: April 2014 |
TaylorGy | 0:89151affda00 | 9 | * Change Log : |
TaylorGy | 0:89151affda00 | 10 | * |
TaylorGy | 0:89151affda00 | 11 | * The MIT License (MIT) |
TaylorGy | 0:89151affda00 | 12 | * |
TaylorGy | 0:89151affda00 | 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
TaylorGy | 0:89151affda00 | 14 | * of this software and associated documentation files (the "Software"), to deal |
TaylorGy | 0:89151affda00 | 15 | * in the Software without restriction, including without limitation the rights |
TaylorGy | 0:89151affda00 | 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
TaylorGy | 0:89151affda00 | 17 | * copies of the Software, and to permit persons to whom the Software is |
TaylorGy | 0:89151affda00 | 18 | * furnished to do so, subject to the following conditions: |
TaylorGy | 0:89151affda00 | 19 | * |
TaylorGy | 0:89151affda00 | 20 | * The above copyright notice and this permission notice shall be included in |
TaylorGy | 0:89151affda00 | 21 | * all copies or substantial portions of the Software. |
TaylorGy | 0:89151affda00 | 22 | * |
TaylorGy | 0:89151affda00 | 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
TaylorGy | 0:89151affda00 | 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
TaylorGy | 0:89151affda00 | 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
TaylorGy | 0:89151affda00 | 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
TaylorGy | 0:89151affda00 | 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
TaylorGy | 0:89151affda00 | 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
TaylorGy | 0:89151affda00 | 29 | * THE SOFTWARE. |
TaylorGy | 0:89151affda00 | 30 | */ |
TaylorGy | 0:89151affda00 | 31 | |
TaylorGy | 0:89151affda00 | 32 | /****************************************************************************/ |
TaylorGy | 0:89151affda00 | 33 | /*** Include files ***/ |
TaylorGy | 0:89151affda00 | 34 | /****************************************************************************/ |
TaylorGy | 0:89151affda00 | 35 | #include "driver_mbed_TH02.h" |
TaylorGy | 0:89151affda00 | 36 | // #include <Wire.h> |
TaylorGy | 0:89151affda00 | 37 | // #include <Arduino.h> |
TaylorGy | 0:89151affda00 | 38 | |
TaylorGy | 0:89151affda00 | 39 | /* Use Serial IIC */ |
TaylorGy | 0:89151affda00 | 40 | #ifdef SERIAL_IIC |
TaylorGy | 0:89151affda00 | 41 | #endif |
TaylorGy | 0:89151affda00 | 42 | |
TaylorGy | 0:89151affda00 | 43 | TH02_dev myTH02; |
TaylorGy | 0:89151affda00 | 44 | I2C i2cth02(PB_9, PB_8); |
TaylorGy | 0:89151affda00 | 45 | /****************************************************************************/ |
TaylorGy | 0:89151affda00 | 46 | /*** Local Variable ***/ |
TaylorGy | 0:89151affda00 | 47 | /****************************************************************************/ |
TaylorGy | 0:89151affda00 | 48 | |
TaylorGy | 0:89151affda00 | 49 | |
TaylorGy | 0:89151affda00 | 50 | /****************************************************************************/ |
TaylorGy | 0:89151affda00 | 51 | /*** Class member Functions ***/ |
TaylorGy | 0:89151affda00 | 52 | /****************************************************************************/ |
TaylorGy | 0:89151affda00 | 53 | |
TaylorGy | 0:89151affda00 | 54 | void TH02_dev::begin(void) |
TaylorGy | 0:89151affda00 | 55 | { |
TaylorGy | 0:89151affda00 | 56 | /* Start IIC */ |
TaylorGy | 0:89151affda00 | 57 | // Wire.begin(); |
TaylorGy | 0:89151affda00 | 58 | /* TH02 don't need to software reset */ |
TaylorGy | 0:89151affda00 | 59 | printf("System Start !\r\n\r\n"); |
TaylorGy | 0:89151affda00 | 60 | } |
TaylorGy | 0:89151affda00 | 61 | |
TaylorGy | 0:89151affda00 | 62 | float TH02_dev::ReadTemperature(void) |
TaylorGy | 0:89151affda00 | 63 | { |
TaylorGy | 0:89151affda00 | 64 | /* Start a new temperature conversion */ |
TaylorGy | 0:89151affda00 | 65 | TH02_IIC_WriteReg(REG_CONFIG, CMD_MEASURE_TEMP); |
TaylorGy | 0:89151affda00 | 66 | //delay(100); |
TaylorGy | 0:89151affda00 | 67 | /* Wait until conversion is done */ |
TaylorGy | 0:89151affda00 | 68 | while(!isAvailable()); |
TaylorGy | 0:89151affda00 | 69 | uint16_t value = TH02_IIC_ReadData(); |
TaylorGy | 0:89151affda00 | 70 | |
TaylorGy | 0:89151affda00 | 71 | value = value >> 2; |
TaylorGy | 0:89151affda00 | 72 | /* |
TaylorGy | 0:89151affda00 | 73 | Formula: |
TaylorGy | 0:89151affda00 | 74 | Temperature(C) = (Value/32) - 50 |
TaylorGy | 0:89151affda00 | 75 | */ |
TaylorGy | 0:89151affda00 | 76 | float temper = (value/32.0)-50.0; |
TaylorGy | 0:89151affda00 | 77 | |
TaylorGy | 0:89151affda00 | 78 | return temper; |
TaylorGy | 0:89151affda00 | 79 | } |
TaylorGy | 0:89151affda00 | 80 | |
TaylorGy | 0:89151affda00 | 81 | float TH02_dev::ReadHumidity(void) |
TaylorGy | 0:89151affda00 | 82 | { |
TaylorGy | 0:89151affda00 | 83 | /* Start a new humility conversion */ |
TaylorGy | 0:89151affda00 | 84 | TH02_IIC_WriteReg(REG_CONFIG, CMD_MEASURE_HUMI); |
TaylorGy | 0:89151affda00 | 85 | |
TaylorGy | 0:89151affda00 | 86 | /* Wait until conversion is done */ |
TaylorGy | 0:89151affda00 | 87 | //delay(100); |
TaylorGy | 0:89151affda00 | 88 | while(!isAvailable()); |
TaylorGy | 0:89151affda00 | 89 | uint16_t value = TH02_IIC_ReadData(); |
TaylorGy | 0:89151affda00 | 90 | |
TaylorGy | 0:89151affda00 | 91 | value = value >> 4; |
TaylorGy | 0:89151affda00 | 92 | |
TaylorGy | 0:89151affda00 | 93 | /* |
TaylorGy | 0:89151affda00 | 94 | Formula: |
TaylorGy | 0:89151affda00 | 95 | Humidity(%) = (Value/16) - 24 |
TaylorGy | 0:89151affda00 | 96 | */ |
TaylorGy | 0:89151affda00 | 97 | |
TaylorGy | 0:89151affda00 | 98 | float humility = (value/16.0)-24.0; |
TaylorGy | 0:89151affda00 | 99 | |
TaylorGy | 0:89151affda00 | 100 | return humility; |
TaylorGy | 0:89151affda00 | 101 | } |
TaylorGy | 0:89151affda00 | 102 | |
TaylorGy | 0:89151affda00 | 103 | /****************************************************************************/ |
TaylorGy | 0:89151affda00 | 104 | /*** Local Functions ***/ |
TaylorGy | 0:89151affda00 | 105 | /****************************************************************************/ |
TaylorGy | 0:89151affda00 | 106 | uint8_t TH02_dev::isAvailable() |
TaylorGy | 0:89151affda00 | 107 | { |
TaylorGy | 0:89151affda00 | 108 | uint8_t status = TH02_IIC_ReadReg(REG_STATUS); |
TaylorGy | 0:89151affda00 | 109 | if(status & STATUS_RDY_MASK) |
TaylorGy | 0:89151affda00 | 110 | { |
TaylorGy | 0:89151affda00 | 111 | return 0; //ready |
TaylorGy | 0:89151affda00 | 112 | } |
TaylorGy | 0:89151affda00 | 113 | else |
TaylorGy | 0:89151affda00 | 114 | { |
TaylorGy | 0:89151affda00 | 115 | return 1; //not ready yet |
TaylorGy | 0:89151affda00 | 116 | } |
TaylorGy | 0:89151affda00 | 117 | } |
TaylorGy | 0:89151affda00 | 118 | |
TaylorGy | 0:89151affda00 | 119 | void TH02_dev::TH02_IIC_WriteCmd(uint8_t u8Cmd) |
TaylorGy | 0:89151affda00 | 120 | { |
TaylorGy | 0:89151affda00 | 121 | char cmd = u8Cmd; |
TaylorGy | 0:89151affda00 | 122 | /* Port to arduino */ |
TaylorGy | 0:89151affda00 | 123 | // Wire.beginTransmission(TH02_I2C_DEV_ID); |
TaylorGy | 0:89151affda00 | 124 | // Wire.write(u8Cmd); |
TaylorGy | 0:89151affda00 | 125 | // Wire.endTransmission(); |
TaylorGy | 0:89151affda00 | 126 | i2cth02.write(TH02_I2C_DEV_ID, &cmd, 1); |
TaylorGy | 0:89151affda00 | 127 | |
TaylorGy | 0:89151affda00 | 128 | } |
TaylorGy | 0:89151affda00 | 129 | |
TaylorGy | 0:89151affda00 | 130 | uint8_t TH02_dev::TH02_IIC_ReadReg(uint8_t u8Reg) |
TaylorGy | 0:89151affda00 | 131 | { |
TaylorGy | 0:89151affda00 | 132 | /* Port to arduino */ |
TaylorGy | 0:89151affda00 | 133 | char Temp = 0; |
TaylorGy | 0:89151affda00 | 134 | |
TaylorGy | 0:89151affda00 | 135 | /* Send a register reading command */ |
TaylorGy | 0:89151affda00 | 136 | TH02_IIC_WriteCmd(u8Reg); |
TaylorGy | 0:89151affda00 | 137 | |
TaylorGy | 0:89151affda00 | 138 | // Wire.requestFrom(TH02_I2C_DEV_ID, 1); |
TaylorGy | 0:89151affda00 | 139 | // while(Wire.available()) |
TaylorGy | 0:89151affda00 | 140 | // { |
TaylorGy | 0:89151affda00 | 141 | // Temp = Wire.read(); |
TaylorGy | 0:89151affda00 | 142 | // } |
TaylorGy | 0:89151affda00 | 143 | |
TaylorGy | 0:89151affda00 | 144 | i2cth02.read(TH02_I2C_DEV_ID, &Temp, 1); |
TaylorGy | 0:89151affda00 | 145 | |
TaylorGy | 0:89151affda00 | 146 | return Temp; |
TaylorGy | 0:89151affda00 | 147 | } |
TaylorGy | 0:89151affda00 | 148 | |
TaylorGy | 0:89151affda00 | 149 | void TH02_dev::TH02_IIC_WriteReg(uint8_t u8Reg,uint8_t u8Data) |
TaylorGy | 0:89151affda00 | 150 | { |
TaylorGy | 0:89151affda00 | 151 | // Wire.beginTransmission(TH02_I2C_DEV_ID); |
TaylorGy | 0:89151affda00 | 152 | // Wire.write(u8Reg); |
TaylorGy | 0:89151affda00 | 153 | // Wire.write(u8Data); |
TaylorGy | 0:89151affda00 | 154 | // Wire.endTransmission(); |
TaylorGy | 0:89151affda00 | 155 | char cmd[2]; |
TaylorGy | 0:89151affda00 | 156 | cmd[0] = u8Reg; |
TaylorGy | 0:89151affda00 | 157 | cmd[1] = u8Data; |
TaylorGy | 0:89151affda00 | 158 | i2cth02.write(TH02_I2C_DEV_ID, cmd, 2); |
TaylorGy | 0:89151affda00 | 159 | } |
TaylorGy | 0:89151affda00 | 160 | |
TaylorGy | 0:89151affda00 | 161 | uint16_t TH02_dev::TH02_IIC_ReadData(void) |
TaylorGy | 0:89151affda00 | 162 | { |
TaylorGy | 0:89151affda00 | 163 | /* Port to arduino */ |
TaylorGy | 0:89151affda00 | 164 | uint16_t Temp = TH02_IIC_ReadData2byte(); |
TaylorGy | 0:89151affda00 | 165 | return Temp; |
TaylorGy | 0:89151affda00 | 166 | } |
TaylorGy | 0:89151affda00 | 167 | |
TaylorGy | 0:89151affda00 | 168 | uint16_t TH02_dev::TH02_IIC_ReadData2byte() |
TaylorGy | 0:89151affda00 | 169 | { |
TaylorGy | 0:89151affda00 | 170 | uint16_t TempData = 0; |
TaylorGy | 0:89151affda00 | 171 | // uint16_t tmpArray[3]={0}; |
TaylorGy | 0:89151affda00 | 172 | char tmpArray[3]; |
TaylorGy | 0:89151affda00 | 173 | |
TaylorGy | 0:89151affda00 | 174 | TH02_IIC_WriteCmd(REG_DATA_H); |
TaylorGy | 0:89151affda00 | 175 | |
TaylorGy | 0:89151affda00 | 176 | // Wire.requestFrom(TH02_I2C_DEV_ID, 3); |
TaylorGy | 0:89151affda00 | 177 | // while(Wire.available()) |
TaylorGy | 0:89151affda00 | 178 | // { |
TaylorGy | 0:89151affda00 | 179 | // tmpArray[cnt] = (uint16_t)Wire.read(); |
TaylorGy | 0:89151affda00 | 180 | // cnt++; |
TaylorGy | 0:89151affda00 | 181 | // } |
TaylorGy | 0:89151affda00 | 182 | i2cth02.read(TH02_I2C_DEV_ID, tmpArray, 3); |
TaylorGy | 0:89151affda00 | 183 | |
TaylorGy | 0:89151affda00 | 184 | /* MSB */ |
TaylorGy | 0:89151affda00 | 185 | TempData = (tmpArray[1]<<8)|(tmpArray[2]); |
TaylorGy | 0:89151affda00 | 186 | return TempData; |
TaylorGy | 0:89151affda00 | 187 | } |