Defines | |
#define | CHECK_HUMD 0b00000101 |
#define | CHECK_STAT 0b00000111 |
#define | CHECK_TEMP 0b00000011 |
#define | sht15_read_sda() set_bit(tris_array[sht15_sda_port - PORTA], sht15_sda_pin); |
#define | sht15_write_sda() clear_bit(tris_array[sht15_sda_port - PORTA], sht15_sda_pin); |
#define | WRITE_STAT 0b00000110 |
Functions | |
uns16 | sht15_fix_humidity (uns16 sensor_out) |
uns16 | sht15_fix_humidity_l (uns8 sensor_out) |
uns16 | sht15_fix_humidity_r (uns16 sensor_out) |
int16 | sht15_fix_temperature_h (uns16 sensor_out) |
void | sht15_read (void) |
uns16 | sht15_read_byte16 (void) |
uns16 | sht15_read_humidity (void) |
uns16 | sht15_read_temperature (void) |
void | sht15_send_byte (uns8 sht15_command) |
void | sht15_setup_io (void) |
void | sht15_start (void) |
#define CHECK_HUMD 0b00000101 |
#define CHECK_STAT 0b00000111 |
#define CHECK_TEMP 0b00000011 |
#define sht15_read_sda | ( | ) | set_bit(tris_array[sht15_sda_port - PORTA], sht15_sda_pin); |
#define sht15_write_sda | ( | ) | clear_bit(tris_array[sht15_sda_port - PORTA], sht15_sda_pin); |
#define WRITE_STAT 0b00000110 |
uns16 sht15_fix_humidity | ( | uns16 | sensor_out | ) |
00328 { 00329 00330 long result; // 16Bit unsigned for the result 00331 00332 if ( sensor_out <= 1712 ) { 00333 result = 143 * sensor_out; 00334 result = result - 8192; // result = a * sensor_out 00335 //result < 512 ? result = 512; // check for underflow 00336 //result = result – 8192; // result = result + b 00337 } else { 00338 result = 111 *sensor_out + 46288; // result = a * sensor_out 00339 //result = result + 46288; // result = result + b 00340 //result > 25600 ? result = 25600; // check for overflow (optional) 00341 } 00342 //8 MSB’s are 0-100%RH integers, 8 LSB’s are remainder 00343 result = result / 4096; 00344 return result; 00345 }
uns16 sht15_fix_humidity_l | ( | uns8 | sensor_out | ) |
00348 { 00349 00350 uns16 result; // 16Bit unsigned for the result 00351 00352 if ( sensor_out <= 107 ) { 00353 result = 143 * sensor_out; // result = a * sensor_out 00354 if (result < 512) { result = 512; } // check for underflow 00355 result -= 512; 00356 } else { 00357 result = 111 * sensor_out; // result = a * sensor_out 00358 result += 2893; // result = result + b 00359 if (result > 25600) { result = 25600; } // check for overflow (optional) 00360 } 00361 //8 MSB’s are 0-100%RH integers, 8 LSB’s are remainder 00362 result = result >> 8; // result = result / 256 00363 return result; 00364 }
uns16 sht15_fix_humidity_r | ( | uns16 | sensor_out | ) |
00305 { 00306 00307 int32 c1 = -40000000; 00308 int32 c2 = 405000; 00309 int32 c3 = 28; 00310 int32 s; 00311 int32 final; 00312 00313 00314 s = sensor_out; 00315 00316 s = s * 10000000; 00317 00318 final = c1 + c2 * s + c3 * s * s; 00319 00320 final = final / 10000; 00321 00322 return final; 00323 }
int16 sht15_fix_temperature_h | ( | uns16 | sensor_out | ) |
00367 { 00368 int16 result; 00369 00370 // result = -3963 + sensor_out; 00371 result = -4001 + sensor_out; 00372 return result; 00373 }
void sht15_read | ( | void | ) |
00088 { 00089 uns16 response; 00090 00091 //Issue command start 00092 sht15_start(); 00093 00094 //Now send command code 00095 sht15_send_byte(CHECK_HUMD); 00096 00097 response = sht15_read_byte16(); 00098 00099 00100 serial_print_str("\nH="); 00101 serial_print_int(response); 00102 //response = response >> 4; 00103 response = sht15_fix_humidity(response); 00104 serial_print_str("\nH="); 00105 serial_print_int(response); 00106 00107 sht15_start(); 00108 sht15_send_byte(CHECK_TEMP); 00109 response = sht15_read_byte16(); //Listen for response from SHT15 00110 serial_print_str("\nT="); 00111 serial_print_int(response);// 00112 response = sht15_fix_temperature_h(response); 00113 serial_print_str("\nT="); 00114 serial_print_int(response); 00115 00116 }
uns16 sht15_read_byte16 | ( | void | ) |
00217 { 00218 uns8 j; 00219 uns16 in_byte; 00220 uns8 crc; 00221 //bit my_store_gie = intcon.GIE; 00222 //kill_interrupts(); 00223 00224 clear_pin(sht15_sck_port, sht15_sck_pin); 00225 00226 sht15_read_sda(); 00227 00228 for(j = 0 ; j < 8 ; j++) 00229 { 00230 set_pin(sht15_sck_port, sht15_sck_pin); 00231 delay_us(10); 00232 in_byte = in_byte << 1; 00233 in_byte.0 = test_pin(sht15_sda_port, sht15_sda_pin); 00234 00235 clear_pin(sht15_sck_port, sht15_sck_pin); 00236 delay_us(10); 00237 } 00238 delay_us(100); 00239 00240 // send ack 00241 sht15_write_sda(); 00242 clear_pin(sht15_sda_port, sht15_sda_pin); 00243 00244 set_pin(sht15_sck_port, sht15_sck_pin); 00245 delay_us(10); 00246 clear_pin(sht15_sck_port, sht15_sck_pin); 00247 delay_us(10); 00248 00249 sht15_read_sda(); 00250 delay_us(100); 00251 00252 00253 for(j = 0 ; j < 8 ; j++) 00254 { 00255 set_pin(sht15_sck_port, sht15_sck_pin); 00256 delay_us(10); 00257 in_byte = in_byte << 1; 00258 in_byte.0 = test_pin(sht15_sda_port, sht15_sda_pin); 00259 00260 clear_pin(sht15_sck_port, sht15_sck_pin); 00261 delay_us(10); 00262 } 00263 00264 // send ack 00265 sht15_write_sda(); 00266 clear_pin(sht15_sda_port, sht15_sda_pin); 00267 00268 set_pin(sht15_sck_port, sht15_sck_pin); 00269 delay_us(10); 00270 clear_pin(sht15_sck_port, sht15_sck_pin); 00271 delay_us(10); 00272 00273 sht15_read_sda(); 00274 delay_us(100); 00275 00276 00277 for(j = 0 ; j < 8 ; j++) 00278 { 00279 set_pin(sht15_sck_port, sht15_sck_pin); 00280 delay_us(10); 00281 crc = crc << 1; 00282 crc.0 = test_pin(sht15_sda_port, sht15_sda_pin); 00283 00284 clear_pin(sht15_sck_port, sht15_sck_pin); 00285 delay_us(10); 00286 } 00287 00288 // send ack 00289 sht15_write_sda(); 00290 clear_pin(sht15_sda_port, sht15_sda_pin); 00291 00292 set_pin(sht15_sck_port, sht15_sck_pin); 00293 delay_us(10); 00294 clear_pin(sht15_sck_port, sht15_sck_pin); 00295 delay_us(10); 00296 00297 sht15_read_sda(); 00298 delay_us(100); 00299 00300 //intcon.GIE = my_store_gie; 00301 return(in_byte); 00302 }
uns16 sht15_read_humidity | ( | void | ) |
00059 { 00060 00061 uns16 response; 00062 00063 sht15_start(); 00064 sht15_send_byte(CHECK_HUMD); 00065 response = sht15_read_byte16(); 00066 response = response >> 4; 00067 response = sht15_fix_humidity_l(response); 00068 return response; 00069 }
uns16 sht15_read_temperature | ( | void | ) |
00071 { 00072 00073 uns16 response; 00074 00075 sht15_start(); 00076 sht15_send_byte(CHECK_TEMP); 00077 response = sht15_read_byte16(); //Listen for response from SHT15 00078 serial_print_str("\nT="); 00079 serial_print_int(response); 00080 response = sht15_fix_temperature_h(response); 00081 serial_print_str("\nT="); 00082 serial_print_int(response); 00083 00084 }
void sht15_send_byte | ( | uns8 | sht15_command | ) |
00119 { 00120 uns8 i; 00121 00122 00123 sht15_write_sda(); 00124 00125 00126 clear_pin(sht15_sck_port, sht15_sck_pin); 00127 for(i = 0 ; i < 8 ; i++) 00128 { 00129 delay_us(10); 00130 change_pin(sht15_sda_port, sht15_sda_pin, sht15_command.7); 00131 sht15_command = sht15_command << 1; 00132 delay_us(10); 00133 set_pin(sht15_sck_port, sht15_sck_pin); 00134 delay_us(10); 00135 clear_pin(sht15_sck_port, sht15_sck_pin); 00136 } 00137 delay_us(100); 00138 //Wait for SHT15 to acknowledge. 00139 // clear_pin(sht15_sck_port, sht15_sck_pin); 00140 sht15_read_sda(); 00141 // <<!!>> Fix following 00142 // while (test_pin(sht15_sda_port, sht15_sda_pin) == 1); //Wait for SHT to pull line low 00143 00144 set_pin(sht15_sck_port, sht15_sck_pin); 00145 // read ack here 00146 00147 delay_us(10); 00148 clear_pin(sht15_sck_port, sht15_sck_pin); //Falling edge of 9th clock 00149 00150 while (test_pin(sht15_sda_port, sht15_sda_pin) == 0); //Wait for SHT to release line 00151 00152 //Wait for SHT15 to pull SDA low to signal measurement completion. 00153 //This can take up to 210ms for 14 bit measurements 00154 i = 0; 00155 while ((test_pin(sht15_sda_port, sht15_sda_pin) == 1)) //Wait for SHT to pull line low 00156 { 00157 i++; 00158 if (i == 255) break; 00159 00160 delay_ms(10); 00161 } 00162 00163 //Debug info 00164 i *= 10; //Convert to ms 00165 serial_print_str("\nRt="); 00166 serial_print_int(i); 00167 serial_print_str("ms\n"); //Debug to see how long response took 00168 00169 }
void sht15_setup_io | ( | void | ) |
00052 { 00053 clear_pin(sht15_sck_port, sht15_sck_pin); 00054 clear_pin(sht15_sda_pin, sht15_sda_pin); 00055 make_output(sht15_sda_port, sht15_sda_pin); 00056 make_output(sht15_sck_port, sht15_sck_pin); 00057 }
void sht15_start | ( | void | ) |
00173 { 00174 /* sht15_write_sda(); 00175 set_pin(sht15_sda_port, sht15_sda_pin); //SDA = 1; 00176 set_pin(sht15_sck_port, sht15_sck_pin); //SCK = 1; 00177 00178 clear_pin(sht15_sda_port, sht15_sda_pin); //SDA = 0; 00179 clear_pin(sht15_sck_port, sht15_sck_pin); //SCK = 0; 00180 set_pin(sht15_sck_port, sht15_sck_pin); // SCK = 1; 00181 set_pin(sht15_sda_port, sht15_sda_pin); //SDA = 1; 00182 clear_pin(sht15_sck_port, sht15_sck_pin); //SCK = 0; 00183 */ 00184 sht15_write_sda(); 00185 00186 // initial state 00187 00188 clear_pin(sht15_sck_port, sht15_sck_pin); //SCK = 0; 00189 set_pin(sht15_sda_port, sht15_sda_pin); //SDA = 1; 00190 delay_us(10); 00191 00192 set_pin(sht15_sck_port, sht15_sck_pin); // SCK = 1; 00193 delay_us(10); 00194 00195 // sck is high, so lower the data ling 00196 clear_pin(sht15_sda_port, sht15_sda_pin); //SDA = 0; 00197 delay_us(10); 00198 00199 // low pulse SCK 00200 clear_pin(sht15_sck_port, sht15_sck_pin); //SCK = 0; 00201 delay_us(10); 00202 set_pin(sht15_sck_port, sht15_sck_pin); // SCK = 1; 00203 delay_us(10); 00204 00205 // now raise SDA while SCK is high 00206 set_pin(sht15_sda_port, sht15_sda_pin); //SDA = 1; 00207 delay_us(10); 00208 00209 // return sck to normal state 00210 clear_pin(sht15_sck_port, sht15_sck_pin); //SCK = 0; 00211 delay_us(10); 00212 00213 }