Sensiron SHT 7x Temperature and humidity device library
Dependents: temp xj-Nucleo-F303K8-SHT75-TEST
sht7X.h@5:db6b417dfa74, 2010-10-27 (annotated)
- Committer:
- nimbusgb
- Date:
- Wed Oct 27 16:37:33 2010 +0000
- Revision:
- 5:db6b417dfa74
- Parent:
- 4:9a5e846258a9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nimbusgb | 1:20aa2d4a28bf | 1 | /* mbed Sensiron SHT7x temperature and humidity sensor library |
nimbusgb | 1:20aa2d4a28bf | 2 | * |
nimbusgb | 1:20aa2d4a28bf | 3 | * Sensiron data at http://www.sensirion.com/en/01_humidity_sensors/06_humidity_sensor_sht75.htm |
nimbusgb | 1:20aa2d4a28bf | 4 | * |
nimbusgb | 1:20aa2d4a28bf | 5 | * Copyright (c) Ian Molesworth October 2010 |
nimbusgb | 1:20aa2d4a28bf | 6 | * |
nimbusgb | 1:20aa2d4a28bf | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
nimbusgb | 1:20aa2d4a28bf | 8 | * of this software and associated documentation files (the "Software"), to use |
nimbusgb | 1:20aa2d4a28bf | 9 | * copy or modify the software for private or non-commercial purposes only. |
nimbusgb | 1:20aa2d4a28bf | 10 | * |
nimbusgb | 1:20aa2d4a28bf | 11 | * The above copyright notice and this permission notice shall be included in |
nimbusgb | 1:20aa2d4a28bf | 12 | * all copies or substantial portions of the Software. |
nimbusgb | 1:20aa2d4a28bf | 13 | * |
nimbusgb | 1:20aa2d4a28bf | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
nimbusgb | 1:20aa2d4a28bf | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
nimbusgb | 1:20aa2d4a28bf | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
nimbusgb | 1:20aa2d4a28bf | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
nimbusgb | 1:20aa2d4a28bf | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
nimbusgb | 1:20aa2d4a28bf | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
nimbusgb | 1:20aa2d4a28bf | 20 | * THE SOFTWARE. |
nimbusgb | 1:20aa2d4a28bf | 21 | */ |
nimbusgb | 1:20aa2d4a28bf | 22 | |
nimbusgb | 1:20aa2d4a28bf | 23 | |
nimbusgb | 1:20aa2d4a28bf | 24 | #ifndef SHT75_H |
nimbusgb | 1:20aa2d4a28bf | 25 | #define SHT75_H |
nimbusgb | 1:20aa2d4a28bf | 26 | |
nimbusgb | 1:20aa2d4a28bf | 27 | #include "mbed.h" |
nimbusgb | 1:20aa2d4a28bf | 28 | |
nimbusgb | 1:20aa2d4a28bf | 29 | const float C1= -4.0; // for 12 Bit humi |
nimbusgb | 1:20aa2d4a28bf | 30 | const float C2= +0.0405; // for 12 Bit humi |
nimbusgb | 1:20aa2d4a28bf | 31 | const float C3= -0.0000028; // for 12 Bit hum |
nimbusgb | 1:20aa2d4a28bf | 32 | const float TL= -39.61; // 3v 14 bit |
nimbusgb | 1:20aa2d4a28bf | 33 | const float T1= +0.01; // for 14 Bit @ 5V |
nimbusgb | 1:20aa2d4a28bf | 34 | const float T2= +0.00008; // for 14 Bit @ 5V |
nimbusgb | 1:20aa2d4a28bf | 35 | |
nimbusgb | 4:9a5e846258a9 | 36 | /** SHT7X driver class |
nimbusgb | 1:20aa2d4a28bf | 37 | * |
nimbusgb | 1:20aa2d4a28bf | 38 | * Example: |
nimbusgb | 1:20aa2d4a28bf | 39 | * @code |
nimbusgb | 1:20aa2d4a28bf | 40 | * //initialise the device read temperature ticks, read humidity ticks and then |
nimbusgb | 3:c6a7a49099fe | 41 | * // calculate the liniarised humidity value. |
nimbusgb | 1:20aa2d4a28bf | 42 | * #include "mbed.h" |
nimbusgb | 3:c6a7a49099fe | 43 | * #include "sht7X.h" |
nimbusgb | 2:dd218144f9fe | 44 | * |
nimbusgb | 2:dd218144f9fe | 45 | * SHT75 sht(p12, p11); |
nimbusgb | 2:dd218144f9fe | 46 | * Serial pc(USBTX, USBRX); |
nimbusgb | 2:dd218144f9fe | 47 | * |
nimbusgb | 2:dd218144f9fe | 48 | * int main() |
nimbusgb | 2:dd218144f9fe | 49 | * { |
nimbusgb | 2:dd218144f9fe | 50 | * float temperature; // temperature -40 to 120 deg C |
nimbusgb | 2:dd218144f9fe | 51 | * float humidity; // relative humidity 1% to 100% |
nimbusgb | 3:c6a7a49099fe | 52 | * float humi_f,rh_lin,rh_true; // working registers for Illustration purposes |
nimbusgb | 2:dd218144f9fe | 53 | * int t; // temporary store for the temp ticks |
nimbusgb | 2:dd218144f9fe | 54 | * int h; // temp store for the humidity ticks |
nimbusgb | 3:c6a7a49099fe | 55 | * |
nimbusgb | 2:dd218144f9fe | 56 | * pc.baud(115200); |
nimbusgb | 2:dd218144f9fe | 57 | * |
nimbusgb | 2:dd218144f9fe | 58 | * while(1) |
nimbusgb | 2:dd218144f9fe | 59 | * { |
nimbusgb | 2:dd218144f9fe | 60 | * sht.readTempTicks(&t); |
nimbusgb | 2:dd218144f9fe | 61 | * temperature = ((float)(t) * 0.01) - 39.61; |
nimbusgb | 2:dd218144f9fe | 62 | * |
nimbusgb | 2:dd218144f9fe | 63 | * sht.readHumidityTicks(&h); |
nimbusgb | 2:dd218144f9fe | 64 | * humi_f = (float)(h); |
nimbusgb | 2:dd218144f9fe | 65 | * rh_lin = C3 * humi_f * humi_f + C2 * humi_f + C1; |
nimbusgb | 3:c6a7a49099fe | 66 | * rh_true=(((temperature/100)-25)*(T1+T2*humi_f)+rh_lin); |
nimbusgb | 2:dd218144f9fe | 67 | * if(rh_true>100)rh_true=100; //cut if the value is outside |
nimbusgb | 2:dd218144f9fe | 68 | * if(rh_true<1)rh_true=1; //the physical possible range |
nimbusgb | 3:c6a7a49099fe | 69 | * humidity = rh_true; |
nimbusgb | 2:dd218144f9fe | 70 | * pc.printf("Temp: %2.2f RH %2.2f\n\r",temperature, humidity); |
nimbusgb | 2:dd218144f9fe | 71 | * } |
nimbusgb | 2:dd218144f9fe | 72 | * } |
nimbusgb | 2:dd218144f9fe | 73 | * @endcode |
nimbusgb | 2:dd218144f9fe | 74 | */ |
nimbusgb | 2:dd218144f9fe | 75 | |
nimbusgb | 1:20aa2d4a28bf | 76 | class SHT75 |
nimbusgb | 1:20aa2d4a28bf | 77 | { |
nimbusgb | 1:20aa2d4a28bf | 78 | public: |
nimbusgb | 5:db6b417dfa74 | 79 | /** |
nimbusgb | 5:db6b417dfa74 | 80 | * @var temperature float to hold the temp |
nimbusgb | 5:db6b417dfa74 | 81 | */ |
nimbusgb | 5:db6b417dfa74 | 82 | float temperature; |
nimbusgb | 5:db6b417dfa74 | 83 | /** |
nimbusgb | 5:db6b417dfa74 | 84 | * @var humidity float to hold the calculated humidity |
nimbusgb | 5:db6b417dfa74 | 85 | */ |
nimbusgb | 5:db6b417dfa74 | 86 | |
nimbusgb | 5:db6b417dfa74 | 87 | float humidity; |
nimbusgb | 5:db6b417dfa74 | 88 | |
nimbusgb | 1:20aa2d4a28bf | 89 | /** Create an SHT object connected to the specified Digital pins |
nimbusgb | 1:20aa2d4a28bf | 90 | * |
nimbusgb | 1:20aa2d4a28bf | 91 | * @param pclock digital pin to use as clock |
nimbusgb | 1:20aa2d4a28bf | 92 | * @param pdata digital pin to use as data bus ( bidirectional ) |
nimbusgb | 1:20aa2d4a28bf | 93 | */ |
nimbusgb | 1:20aa2d4a28bf | 94 | SHT75(PinName pclock, PinName pdata): _clock(pclock), _data(pdata) {}; |
nimbusgb | 1:20aa2d4a28bf | 95 | /** read the temperature ticks value 14 bit resolution |
nimbusgb | 1:20aa2d4a28bf | 96 | * |
nimbusgb | 1:20aa2d4a28bf | 97 | * @param int *temp pointer to an integer to hold the tick value |
nimbusgb | 1:20aa2d4a28bf | 98 | * @returns boolean true if read acknowledges |
nimbusgb | 1:20aa2d4a28bf | 99 | */ |
nimbusgb | 1:20aa2d4a28bf | 100 | bool readTempTicks(int* temp); |
nimbusgb | 1:20aa2d4a28bf | 101 | /** read the humidity ticks value 12 bit resolution |
nimbusgb | 1:20aa2d4a28bf | 102 | * |
nimbusgb | 1:20aa2d4a28bf | 103 | * @param int *temp pointer to an integer to hold the tick value |
nimbusgb | 1:20aa2d4a28bf | 104 | * @returns boolean true if read acknowledges |
nimbusgb | 1:20aa2d4a28bf | 105 | */ |
nimbusgb | 1:20aa2d4a28bf | 106 | bool readHumidityTicks(int* temp); |
nimbusgb | 1:20aa2d4a28bf | 107 | /** start up reset |
nimbusgb | 1:20aa2d4a28bf | 108 | * |
nimbusgb | 1:20aa2d4a28bf | 109 | * call to resync or abort current operation. |
nimbusgb | 1:20aa2d4a28bf | 110 | * worth calling every now and then to make sure your system is not hung. |
nimbusgb | 1:20aa2d4a28bf | 111 | */ |
nimbusgb | 1:20aa2d4a28bf | 112 | void reset(void); |
nimbusgb | 1:20aa2d4a28bf | 113 | void softReset(void); |
nimbusgb | 1:20aa2d4a28bf | 114 | int readStatus(void); |
nimbusgb | 1:20aa2d4a28bf | 115 | |
nimbusgb | 1:20aa2d4a28bf | 116 | private: |
nimbusgb | 1:20aa2d4a28bf | 117 | DigitalInOut _data; |
nimbusgb | 1:20aa2d4a28bf | 118 | DigitalOut _clock; |
nimbusgb | 1:20aa2d4a28bf | 119 | |
nimbusgb | 1:20aa2d4a28bf | 120 | void start(void); |
nimbusgb | 1:20aa2d4a28bf | 121 | int read(char); |
nimbusgb | 1:20aa2d4a28bf | 122 | bool write(char); |
nimbusgb | 1:20aa2d4a28bf | 123 | }; |
nimbusgb | 1:20aa2d4a28bf | 124 | |
nimbusgb | 1:20aa2d4a28bf | 125 | #endif |