Sensiron SHT 7x Temperature and humidity device library
Dependents: temp xj-Nucleo-F303K8-SHT75-TEST
sht7X.cpp
00001 // 00002 // Ian Molesworth October 2010 00003 // SHT 75 class 00004 // 00005 // 00006 // To do: 00007 // 00008 // 00009 00010 #include "sht7X.h" 00011 00012 void SHT75::reset() 00013 { 00014 _data.output(); 00015 _data = 1; // data bus high 00016 for (int i=0;i<12;i++) 00017 { 00018 _clock = 1; // clock high 00019 wait_us(1); 00020 _clock = 0; // clock lo 00021 wait_us(1); 00022 } 00023 _clock = 1; // clock high 00024 wait_us(1); 00025 _data = 0; 00026 wait_us(1); 00027 _clock = 0; // clock lo 00028 wait_us(1); 00029 _clock = 1; 00030 wait_us(1); 00031 _data = 1; 00032 wait_us(1); 00033 _clock = 0; 00034 wait_us(1); 00035 } 00036 00037 void SHT75::softReset(void) 00038 { 00039 _data.output(); 00040 start(); 00041 write(0x1E); 00042 wait_ms(12); 00043 } 00044 00045 void SHT75::start(void) 00046 { 00047 _data.output(); 00048 _clock = 1; 00049 wait_us(1); 00050 _data = 0; 00051 wait_us(1); 00052 _clock = 0; 00053 wait_us(1); 00054 _clock = 1; 00055 wait_us(1); 00056 _data = 1; 00057 wait_us(1); 00058 _clock = 0; 00059 } 00060 00061 int SHT75::readStatus(void) 00062 { 00063 int status; 00064 status = -1; 00065 start(); 00066 if (write(0x06) == 0) 00067 status = read(1); // read with a wait for ack 00068 read(0); // read without the wait 00069 return status; 00070 } 00071 00072 bool SHT75::write(char d) 00073 { 00074 auto int i; 00075 _data.output(); // bus output 00076 // Writes char and returns -1 if no ACK was sent from remote 00077 for (i=0;i<8;i++) 00078 { 00079 if (d & 0x80) 00080 _data = 1; // data high 00081 else 00082 _data = 0; // data lo 00083 // shift the data 00084 d <<= 1; 00085 wait_us(1); 00086 _clock = 1; // clock high 00087 wait_us(1); 00088 _clock = 0; // clock lo 00089 } 00090 _data.input(); // float the bus 00091 wait_us(1); 00092 _clock = 1; // clock high 00093 wait_us(1); 00094 i = _data; 00095 _clock = 0; // clock lo 00096 return i; // leave the bus in input mode and return the status of the ack bit read. 00097 } 00098 00099 int SHT75::read(char ack) 00100 { 00101 auto int i,s; 00102 auto char c; 00103 s = 0; 00104 _data.input(); // bus to input 00105 00106 for (i=0;i<8;i++) 00107 { 00108 s <<= 1; 00109 wait_us(1); 00110 _clock = 1; // clock high 00111 wait_us(1); 00112 c = _data; // get the data bit 00113 _clock = 0; // clock lo 00114 if ( c ) 00115 s |= 1; 00116 } 00117 00118 if (ack == 1) 00119 _data = 0; // data lo 00120 else 00121 _data = 1; // data hi 00122 _data.output(); 00123 _clock = 1; // clock lo 00124 wait_us(1); 00125 _clock = 0; // clock lo 00126 _data = 1; // data hi 00127 _data.input(); 00128 return s; 00129 } 00130 00131 00132 // Put the current temperature into passed variable 00133 bool SHT75::readTempTicks(int* temp) 00134 { 00135 int v, value; 00136 start(); // Start a tx ( leaves data as input ) 00137 if (write(0x03) == 0) // send the read command and get an ack 00138 { 00139 for (v=0; v<50; v ++) // wait for ready up to 500 ms 00140 { 00141 wait_ms(10); // 10 ms pause 00142 if ( _data == 0 ) // 00143 { 00144 value = read(1); // read a byte 00145 value <<= 8; // shift it in 00146 value |= read(1); // read another byte 00147 read(0); 00148 // transfer the value 00149 *temp = value; 00150 reset(); 00151 return true; 00152 } 00153 } 00154 } 00155 return false; 00156 } 00157 00158 bool SHT75::readHumidityTicks(int* humi) 00159 { 00160 start(); 00161 if (write(0x05) == 0) 00162 { 00163 for (int value=0; value<50; value ++) // wait for ready up to 500 ms 00164 { 00165 wait_ms(10); // 00166 if ( _data == 0 ) // 00167 { 00168 value = read(1); 00169 value <<= 8; 00170 value |= read(1); 00171 read(0); 00172 *humi = value; // transfer the value 00173 reset(); 00174 return true; 00175 } 00176 } 00177 } 00178 00179 return false; 00180 }
Generated on Wed Jul 13 2022 20:31:40 by 1.7.2