Sensiron SHT 7x Temperature and humidity device library
Dependents: temp xj-Nucleo-F303K8-SHT75-TEST
Revision 0:f401474a3e96, committed 2010-10-27
- Comitter:
- nimbusgb
- Date:
- Wed Oct 27 15:27:38 2010 +0000
- Child:
- 1:20aa2d4a28bf
- Commit message:
- 0.8
Changed in this revision
| sht75.cpp | Show annotated file Show diff for this revision Revisions of this file |
| sht75.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sht75.cpp Wed Oct 27 15:27:38 2010 +0000
@@ -0,0 +1,180 @@
+//
+// Ian Molesworth October 2010
+// SHT 75 class
+//
+//
+// To do:
+//
+//
+
+#include "sht75.h"
+
+void SHT75::reset()
+ {
+ _data.output();
+ _data = 1; // data bus high
+ for (int i=0;i<12;i++)
+ {
+ _clock = 1; // clock high
+ wait_us(1);
+ _clock = 0; // clock lo
+ wait_us(1);
+ }
+ _clock = 1; // clock high
+ wait_us(1);
+ _data = 0;
+ wait_us(1);
+ _clock = 0; // clock lo
+ wait_us(1);
+ _clock = 1;
+ wait_us(1);
+ _data = 1;
+ wait_us(1);
+ _clock = 0;
+ wait_us(1);
+ }
+
+void SHT75::softReset(void)
+ {
+ _data.output();
+ start();
+ write(0x1E);
+ wait_ms(12);
+ }
+
+void SHT75::start(void)
+ {
+ _data.output();
+ _clock = 1;
+ wait_us(1);
+ _data = 0;
+ wait_us(1);
+ _clock = 0;
+ wait_us(1);
+ _clock = 1;
+ wait_us(1);
+ _data = 1;
+ wait_us(1);
+ _clock = 0;
+ }
+
+int SHT75::readStatus(void)
+ {
+ int status;
+ status = -1;
+ start();
+ if (write(0x06) == 0)
+ status = read(1); // read with a wait for ack
+ read(0); // read without the wait
+ return status;
+ }
+
+bool SHT75::write(char d)
+ {
+ auto int i;
+ _data.output(); // bus output
+ // Writes char and returns -1 if no ACK was sent from remote
+ for (i=0;i<8;i++)
+ {
+ if (d & 0x80)
+ _data = 1; // data high
+ else
+ _data = 0; // data lo
+ // shift the data
+ d <<= 1;
+ wait_us(1);
+ _clock = 1; // clock high
+ wait_us(1);
+ _clock = 0; // clock lo
+ }
+ _data.input(); // float the bus
+ wait_us(1);
+ _clock = 1; // clock high
+ wait_us(1);
+ i = _data;
+ _clock = 0; // clock lo
+ return i; // leave the bus in input mode and return the status of the ack bit read.
+ }
+
+int SHT75::read(char ack)
+ {
+ auto int i,s;
+ auto char c;
+ s = 0;
+ _data.input(); // bus to input
+
+ for (i=0;i<8;i++)
+ {
+ s <<= 1;
+ wait_us(1);
+ _clock = 1; // clock high
+ wait_us(1);
+ c = _data; // get the data bit
+ _clock = 0; // clock lo
+ if ( c )
+ s |= 1;
+ }
+
+ if (ack == 1)
+ _data = 0; // data lo
+ else
+ _data = 1; // data hi
+ _data.output();
+ _clock = 1; // clock lo
+ wait_us(1);
+ _clock = 0; // clock lo
+ _data = 1; // data hi
+ _data.input();
+ return s;
+ }
+
+
+// Put the current temperature into passed variable
+bool SHT75::readTempTicks(int* temp)
+{
+ int v, value;
+ start(); // Start a tx ( leaves data as input )
+ if (write(0x03) == 0) // send the read command and get an ack
+ {
+ for (v=0; v<50; v ++) // wait for ready up to 500 ms
+ {
+ wait_ms(10); // 10 ms pause
+ if ( _data == 0 ) //
+ {
+ value = read(1); // read a byte
+ value <<= 8; // shift it in
+ value |= read(1); // read another byte
+ read(0);
+ // transfer the value
+ *temp = value;
+ reset();
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+bool SHT75::readHumidityTicks(int* humi)
+ {
+ start();
+ if (write(0x05) == 0)
+ {
+ for (int value=0; value<50; value ++) // wait for ready up to 500 ms
+ {
+ wait_ms(10); //
+ if ( _data == 0 ) //
+ {
+ value = read(1);
+ value <<= 8;
+ value |= read(1);
+ read(0);
+ *humi = value; // transfer the value
+ reset();
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sht75.h Wed Oct 27 15:27:38 2010 +0000
@@ -0,0 +1,75 @@
+/* mbed Sensiron SHT7x temperature and humidity sensor library
+*
+* Sensiron data at http://www.sensirion.com/en/01_humidity_sensors/06_humidity_sensor_sht75.htm
+*
+* Copyright (c) Ian Molesworth October 2010
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy
+* of this software and associated documentation files (the "Software"), to use
+* copy or modify the software for private or non-commercial purposes only.
+*
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*/
+
+
+#ifndef SHT75_H
+#define SHT75_H
+
+#include "mbed.h"
+
+const float C1= -4.0; // for 12 Bit humi
+const float C2= +0.0405; // for 12 Bit humi
+const float C3= -0.0000028; // for 12 Bit hum
+const float TL= -39.61; // 3v 14 bit
+const float T1= +0.01; // for 14 Bit @ 5V
+const float T2= +0.00008; // for 14 Bit @ 5V
+
+class SHT75
+ {
+ public:
+ /** Create an SHT object connected to the specified Digital pins
+ *
+ * @param pclock digital pin to use as clock
+ * @param pdata digital pin to use as data bus ( bidirectional )
+ */
+ SHT75(PinName pclock, PinName pdata): _clock(pclock), _data(pdata) {};
+ /** read the temperature ticks value 14 bit resolution
+ *
+ * @param int *temp pointer to an integer to hold the tick value
+ * @returns boolean true if read acknowledges
+ */
+ bool readTempTicks(int* temp);
+ /** read the humidity ticks value 12 bit resolution
+ *
+ * @param int *temp pointer to an integer to hold the tick value
+ * @returns boolean true if read acknowledges
+ */
+ bool readHumidityTicks(int* temp);
+ /** start up reset
+ *
+ * call to resync or abort current operation.
+ * worth calling every now and then to make sure your system is not hung.
+ */
+ void reset(void);
+ void softReset(void);
+ int readStatus(void);
+
+ private:
+ DigitalInOut _data;
+ DigitalOut _clock;
+
+ void start(void);
+ int read(char);
+ bool write(char);
+ };
+
+#endif