Thermometer indicating temperature and humidity by LED blink pattern

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hts221.cpp Source File

hts221.cpp

00001 /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
00002  *
00003  * The information contained herein is property of Nordic Semiconductor ASA.
00004  * Terms and conditions of usage are described in detail in NORDIC
00005  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
00006  *
00007  * Licensees are granted free, non-transferable use of the information. NO
00008  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
00009  * the file.
00010  *df
00011  */
00012 
00013 #include <stdbool.h>
00014 #include <stdint.h>
00015 
00016 #include <mbed.h>
00017 #include "hts221.h"
00018 
00019 I2C i2c(p22, p20);    //SDA, SCL
00020 
00021 /*lint ++flb "Enter library region" */
00022 
00023 static const char expected_who_am_i = 0xBCU; //!< Expected value to get from WHO_AM_I register.
00024 
00025 uint8_t H0_rH_x2; 
00026 uint8_t H1_rH_x2;
00027 uint16_t T0_degC_x8;
00028 uint16_t T1_degC_x8;
00029 
00030 int16_t H0_T0_OUT;
00031 int16_t H1_T0_OUT;
00032 int16_t T0_OUT;
00033 int16_t T1_OUT;
00034 
00035 float T0_DegC_cal;
00036 float T1_DegC_cal;
00037 float H0_RH_cal;
00038 float H1_RH_cal;
00039 
00040 bool hts221_init(void)
00041 {   
00042   bool transfer_succeeded = true;
00043 
00044   i2c.frequency(400000);
00045     hts221_register_write(0x10 , TRes_4 << 3 | HRes_5); 
00046     hts221_register_write(0x20 , PD_On | BDU_Off  | ODR_1Hz); // Control register 1
00047     hts221_register_write(0x21 , NoBoot | HeaterOff  | No_OS); // Control register 2
00048     hts221_register_write(0x22 , DRDY_H | PP_OD_PP | DRDY_NON); // Control register 3
00049                                 
00050   // Read and verify product ID
00051   transfer_succeeded &= hts221_verify_product_id();
00052 
00053   return transfer_succeeded;
00054 }
00055 
00056 bool hts221_verify_product_id(void)
00057 {
00058     char who_am_i[1];
00059     hts221_register_read(ADDRESS_WHO_AM_I, &who_am_i[0], 1);
00060     if (who_am_i[0] != expected_who_am_i) return false;
00061     else return true;
00062 }
00063 
00064 void hts221_register_write(uint8_t register_address, uint8_t value)
00065 {   
00066     char w2_data[2];
00067     
00068     w2_data[0] = register_address;
00069     w2_data[1] = value;
00070     i2c.write(HTS221_WriteADDE, w2_data, 2);      
00071 
00072 }
00073 
00074 void hts221_register_read(char register_address, char *destination, uint8_t number_of_bytes)
00075 {
00076   i2c.write(HTS221_WriteADDE, &register_address, 1, 1);
00077   i2c.read(HTS221_WriteADDE, destination, number_of_bytes);   //Note by Tsungta, API may have a bug
00078   
00079   //runaboud read function added by Tsungta
00080 /*  if (number_of_bytes == 1) {
00081       i2c.write(HTS221_WriteADDE, &register_address, 1, 1);
00082       i2c.write(HTS221_ReadADDE);
00083       *destination = i2c.read(0);
00084       i2c.stop();
00085   } else {
00086       register_address |= 0x80;
00087       i2c.write(HTS221_WriteADDE, &register_address, 1, 1);
00088       i2c.write(HTS221_ReadADDE);
00089       while (number_of_bytes-- > 0)
00090         *destination++ = i2c.read(0);
00091       i2c.stop();      
00092   }*/    
00093 }
00094                
00095 void HTS221_Calib(void) 
00096 {
00097     char cal_data[16];
00098     
00099     hts221_register_read(0xB0, cal_data, 16);
00100 
00101   H0_rH_x2 = cal_data[0]; 
00102   H1_rH_x2 = cal_data[1];
00103   T0_degC_x8 = ((cal_data[5] & 0x03) << 8) + cal_data[2]; //MSB + LSB in 
00104   T1_degC_x8 = ((cal_data[5] & 0x0C) << 6) + cal_data[3]; // MSB
00105 
00106   H0_T0_OUT = (cal_data[7] << 8) + cal_data[6];  
00107   H1_T0_OUT = (cal_data[11] << 8) + cal_data[10];
00108   T0_OUT = (cal_data[13] << 8) + cal_data[12];
00109   T1_OUT = (cal_data[15] << 8) + cal_data[14];
00110     
00111     // convert negative 2's complement values to native negative value
00112     if (H0_T0_OUT&0x8000) H0_T0_OUT = -(0x8000-(0x7fff&H0_T0_OUT));
00113     if (H1_T0_OUT&0x8000) H1_T0_OUT = -(0x8000-(0x7fff&H1_T0_OUT));       //((~H1_T0_OUT)+1);//
00114     if (T0_OUT&0x8000) T0_OUT = -(0x8000-(0x7fff&T0_OUT));
00115     if (T1_OUT&0x8000) T1_OUT = -(0x8000-(0x7fff&T1_OUT));
00116     
00117   T0_DegC_cal = (float) T0_degC_x8/8;
00118   T1_DegC_cal = (float) T1_degC_x8/8;
00119   H0_RH_cal = (float) H0_rH_x2/2;
00120   H1_RH_cal = (float) H1_rH_x2/2;
00121 
00122 }
00123     
00124 void HTS221_ReadTempHumi( float *pTemp , float *pHumi)
00125 {
00126   char sensor_data[4];
00127   int16_t H_OUT;
00128   int16_t T_OUT;   
00129 
00130     hts221_register_read(0xA8, sensor_data, 4);
00131     
00132     H_OUT = (sensor_data[1] << 8) + sensor_data[0];
00133     T_OUT = (sensor_data[3] << 8) + sensor_data[2];
00134     
00135     // convert negative 2's complement values to native negative value  
00136     if (H_OUT&0x8000) H_OUT = -(0x8000-(0x7fff&H_OUT));   //((~H_OUT)+1);;
00137     if (T_OUT&0x8000) T_OUT = -(0x8000-(0x7fff&T_OUT));
00138     
00139     *pTemp = linear_interpolation(T0_OUT, T0_DegC_cal, T1_OUT, T1_DegC_cal, T_OUT);
00140     *pHumi = linear_interpolation(H0_T0_OUT, H0_RH_cal, H1_T0_OUT, H1_RH_cal, H_OUT);
00141     // Constraint  for measurement after calibration 
00142     if ((int)*pHumi>MaxHumi-1 | (int)*pHumi==-72) *pHumi = MaxHumi;  
00143     if ((int)*pHumi<MinHumi ) *pHumi = MinHumi;
00144     if ((int)*pTemp>MaxTemp-1) *pHumi = MaxTemp; 
00145     if ((int)*pHumi<MinTemp ) *pHumi = MinTemp ;  
00146 }
00147 
00148 float linear_interpolation(int16_t x0, float y0, int16_t x1, float y1, float mes)
00149 {
00150    float a = (float) ((y1 - y0) / (x1 - x0));
00151    float b = (float) -a*x0 + y0;
00152    float cal = (float) a * mes + b;
00153    return cal;
00154 }
00155 
00156 /*lint --flb "Leave library region" */
00157