Sensiron SHT 7x Temperature and humidity device library
Dependents: temp xj-Nucleo-F303K8-SHT75-TEST
sht7X.h@2:dd218144f9fe, 2010-10-27 (annotated)
- Committer:
- nimbusgb
- Date:
- Wed Oct 27 16:09:11 2010 +0000
- Revision:
- 2:dd218144f9fe
- Parent:
- 1:20aa2d4a28bf
- Child:
- 3:c6a7a49099fe
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 | 1:20aa2d4a28bf | 36 | /** SHT7x 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 | 1:20aa2d4a28bf | 41 | * calculate the liniarised humidity value. |
nimbusgb | 1:20aa2d4a28bf | 42 | * #include "mbed.h" |
nimbusgb | 1:20aa2d4a28bf | 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 | 2:dd218144f9fe | 52 | * float humi_f,rh_li,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 | 2:dd218144f9fe | 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 | 2:dd218144f9fe | 66 | * rh_true=(((tv[a]/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 | 2:dd218144f9fe | 69 | * huididty = 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 | * } |
nimbusgb | 2:dd218144f9fe | 74 | * @endcode |
nimbusgb | 2:dd218144f9fe | 75 | */ |
nimbusgb | 2:dd218144f9fe | 76 | |
nimbusgb | 1:20aa2d4a28bf | 77 | class SHT75 |
nimbusgb | 1:20aa2d4a28bf | 78 | { |
nimbusgb | 1:20aa2d4a28bf | 79 | public: |
nimbusgb | 1:20aa2d4a28bf | 80 | /** Create an SHT object connected to the specified Digital pins |
nimbusgb | 1:20aa2d4a28bf | 81 | * |
nimbusgb | 1:20aa2d4a28bf | 82 | * @param pclock digital pin to use as clock |
nimbusgb | 1:20aa2d4a28bf | 83 | * @param pdata digital pin to use as data bus ( bidirectional ) |
nimbusgb | 1:20aa2d4a28bf | 84 | */ |
nimbusgb | 1:20aa2d4a28bf | 85 | SHT75(PinName pclock, PinName pdata): _clock(pclock), _data(pdata) {}; |
nimbusgb | 1:20aa2d4a28bf | 86 | /** read the temperature ticks value 14 bit resolution |
nimbusgb | 1:20aa2d4a28bf | 87 | * |
nimbusgb | 1:20aa2d4a28bf | 88 | * @param int *temp pointer to an integer to hold the tick value |
nimbusgb | 1:20aa2d4a28bf | 89 | * @returns boolean true if read acknowledges |
nimbusgb | 1:20aa2d4a28bf | 90 | */ |
nimbusgb | 1:20aa2d4a28bf | 91 | bool readTempTicks(int* temp); |
nimbusgb | 1:20aa2d4a28bf | 92 | /** read the humidity ticks value 12 bit resolution |
nimbusgb | 1:20aa2d4a28bf | 93 | * |
nimbusgb | 1:20aa2d4a28bf | 94 | * @param int *temp pointer to an integer to hold the tick value |
nimbusgb | 1:20aa2d4a28bf | 95 | * @returns boolean true if read acknowledges |
nimbusgb | 1:20aa2d4a28bf | 96 | */ |
nimbusgb | 1:20aa2d4a28bf | 97 | bool readHumidityTicks(int* temp); |
nimbusgb | 1:20aa2d4a28bf | 98 | /** start up reset |
nimbusgb | 1:20aa2d4a28bf | 99 | * |
nimbusgb | 1:20aa2d4a28bf | 100 | * call to resync or abort current operation. |
nimbusgb | 1:20aa2d4a28bf | 101 | * worth calling every now and then to make sure your system is not hung. |
nimbusgb | 1:20aa2d4a28bf | 102 | */ |
nimbusgb | 1:20aa2d4a28bf | 103 | void reset(void); |
nimbusgb | 1:20aa2d4a28bf | 104 | void softReset(void); |
nimbusgb | 1:20aa2d4a28bf | 105 | int readStatus(void); |
nimbusgb | 1:20aa2d4a28bf | 106 | |
nimbusgb | 1:20aa2d4a28bf | 107 | private: |
nimbusgb | 1:20aa2d4a28bf | 108 | DigitalInOut _data; |
nimbusgb | 1:20aa2d4a28bf | 109 | DigitalOut _clock; |
nimbusgb | 1:20aa2d4a28bf | 110 | |
nimbusgb | 1:20aa2d4a28bf | 111 | void start(void); |
nimbusgb | 1:20aa2d4a28bf | 112 | int read(char); |
nimbusgb | 1:20aa2d4a28bf | 113 | bool write(char); |
nimbusgb | 1:20aa2d4a28bf | 114 | }; |
nimbusgb | 1:20aa2d4a28bf | 115 | |
nimbusgb | 1:20aa2d4a28bf | 116 | #endif |