Deepti AST / Envt_Shield_F4_API
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hts221.c Source File

hts221.c

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    hts221.c
00004  * @author  AST Robotics Team
00005  * @version V0.0.1
00006  * @date    08-April-2014
00007  * @brief   This file provides a set of functions needed to manage the hts221.
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 /* Includes ------------------------------------------------------------------*/
00038 #include "hts221.h"
00039 #include <math.h>
00040 
00041 /** @addtogroup BSP
00042  * @{
00043  */
00044 
00045 /** @addtogroup STM32F439_SENSITRON
00046  * @{
00047  */
00048 
00049 /** @addtogroup HTS221
00050  * @{
00051  */
00052 
00053 
00054 /** @defgroup HTS221_Private_TypesDefinitions
00055  * @{
00056  */
00057 
00058 /**
00059  * @}
00060  */
00061 
00062 /** @defgroup HTS221_Private_Defines
00063  * @{
00064  */
00065 
00066 /**
00067  * @}
00068  */
00069 
00070 /** @defgroup HTS221_Private_Macros
00071  * @{
00072  */
00073 
00074 /**
00075  * @}
00076  */
00077 
00078 /** @defgroup HTS221_Private_Variables
00079  * @{
00080  */
00081 
00082 HUM_TEMP_DrvTypeDef Hts221Drv =
00083 {
00084  HTS221_Init,
00085  HTS221_Power_OFF,
00086  HTS221_ReadID,
00087  HTS221_RebootCmd,
00088  HTS221_INT1InterruptConfig,
00089  HTS221_EnableIT,
00090  HTS221_DisableIT,
00091  HTS221_GetHumidity,
00092  HTS221_GetTemperature
00093 };
00094 
00095 
00096 /* Temperature in degree for calibration  */
00097 float T0_degC, T1_degC;
00098 
00099 /* Output temperature value for calibration */
00100 int16_t T0_out, T1_out;
00101 
00102 
00103 /* Humidity for calibration  */
00104 float H0_rh, H1_rh;
00105 
00106 /* Output Humidity value for calibration */
00107 int16_t H0_T0_out, H1_T0_out;
00108 
00109 
00110 /**
00111  * @}
00112  */
00113 
00114 /** @defgroup HTS221_Private_FunctionPrototypes
00115  * @{
00116  */
00117 static void HTS221_Power_On(void);
00118 
00119 static void HTS221_Calibration(void);
00120 /**
00121  * @}
00122  */
00123 
00124 /** @defgroup HTS221_Private_Functions
00125  * @{
00126  */
00127 
00128 
00129 
00130 /**
00131  * @brief  HTS221 Calibration procedure.
00132  * @param  None
00133  * @retval None
00134  */
00135 static void HTS221_Calibration(void)
00136 {
00137     /* Temperature Calibration */
00138     /* Temperature in degree for calibration ( "/8" to obtain float) */
00139     uint16_t T0_degC_x8_L, T0_degC_x8_H, T1_degC_x8_L, T1_degC_x8_H;
00140     uint8_t H0_rh_x2, H1_rh_x2;
00141     uint8_t tempReg[2] = {0,0};
00142 
00143     HUM_TEMP_IO_Read(tempReg, HTS221_ADDRESS, HTS221_T0_degC_X8_ADDR, 1);
00144     T0_degC_x8_L = (uint16_t)tempReg[0];
00145 
00146     HUM_TEMP_IO_Read(tempReg, HTS221_ADDRESS, HTS221_T1_T0_MSB_X8_ADDR, 1);
00147     T0_degC_x8_H = (uint16_t) (tempReg[0] & 0x03);
00148 
00149     T0_degC = ((float)((T0_degC_x8_H<<8) | (T0_degC_x8_L)))/8;
00150 
00151     HUM_TEMP_IO_Read(tempReg, HTS221_ADDRESS, HTS221_T1_degC_X8_ADDR, 1);
00152     T1_degC_x8_L = (uint16_t)tempReg[0];
00153 
00154     HUM_TEMP_IO_Read(tempReg, HTS221_ADDRESS, HTS221_T1_T0_MSB_X8_ADDR, 1);
00155     T1_degC_x8_H = (uint16_t) (tempReg[0] & 0x0C);
00156     T1_degC_x8_H = T1_degC_x8_H >> 2;
00157 
00158     T1_degC = ((float)((T1_degC_x8_H<<8) | (T1_degC_x8_L)))/8;
00159 
00160     HUM_TEMP_IO_Read(tempReg, HTS221_ADDRESS, HTS221_T0_OUT_L_ADDR + 0x80, 2);
00161     T0_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
00162 
00163     HUM_TEMP_IO_Read(tempReg, HTS221_ADDRESS, HTS221_T1_OUT_L_ADDR + 0x80, 2);
00164     T1_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
00165 
00166     /* Humidity Calibration */
00167     /* Humidity in degree for calibration ( "/2" to obtain float) */
00168 
00169     HUM_TEMP_IO_Read(&H0_rh_x2, HTS221_ADDRESS, HTS221_H0_RH_X2_ADDR, 1);
00170 
00171     HUM_TEMP_IO_Read(&H1_rh_x2, HTS221_ADDRESS, HTS221_H1_RH_X2_ADDR, 1);
00172 
00173     HUM_TEMP_IO_Read(&tempReg[0], HTS221_ADDRESS, HTS221_H0_T0_OUT_L_ADDR + 0x80, 2);
00174     H0_T0_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
00175 
00176     HUM_TEMP_IO_Read(&tempReg[0], HTS221_ADDRESS, HTS221_H1_T0_OUT_L_ADDR  + 0x80, 2);
00177     H1_T0_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
00178 
00179     H0_rh = ((float)H0_rh_x2)/2;
00180     H1_rh = ((float)H1_rh_x2)/2;
00181 }
00182 
00183 
00184 /**
00185  * @brief  Set HTS221 Initialization.
00186  * @param  InitStruct: it contains the configuration setting for the HTS221.
00187  * @retval None
00188  */
00189 void HTS221_Init(HUM_TEMP_InitTypeDef *HTS221_Init)
00190 {  
00191     uint8_t tmp = 0x00;
00192 
00193     /* Configure the low level interface ---------------------------------------*/
00194     HUM_TEMP_IO_Init();
00195 
00196     HTS221_Power_On();
00197 
00198     HTS221_Calibration();
00199 
00200     HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1);
00201 
00202     /* Output Data Rate selection */
00203     tmp &= ~(HTS221_ODR_MASK);
00204     tmp |= HTS221_Init->OutputDataRate;
00205 
00206     HUM_TEMP_IO_Write(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1);
00207 }
00208 
00209 /**
00210  * @brief  Read ID address of HTS221
00211  * @param  Device ID address
00212  * @retval ID name
00213  */
00214 uint8_t HTS221_ReadID(void)
00215 {
00216     uint8_t tmp;
00217 
00218     /* Read WHO I AM register */
00219     HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_WHO_AM_I_ADDR, 1);
00220 
00221     /* Return the ID */
00222     return (uint8_t)tmp;
00223 }
00224 
00225 /**
00226  * @brief  Reboot memory content of HTS221
00227  * @param  None
00228  * @retval None
00229  */
00230 void HTS221_RebootCmd(void)
00231 {
00232     uint8_t tmpreg;
00233 
00234     /* Read CTRL_REG2 register */
00235     HUM_TEMP_IO_Read(&tmpreg, HTS221_ADDRESS, HTS221_CTRL_REG2_ADDR, 1);
00236 
00237     /* Enable or Disable the reboot memory */
00238     tmpreg |= HTS221_BOOT_REBOOTMEMORY;
00239 
00240     /* Write value to MEMS CTRL_REG2 regsister */
00241     HUM_TEMP_IO_Write(&tmpreg, HTS221_ADDRESS, HTS221_CTRL_REG2_ADDR, 1);
00242 }
00243 
00244 /**
00245  * @brief Set HTS221 Interrupt INT1 configuration
00246  * @param  HTS221_InterruptConfig_TypeDef: pointer to a HTS221_InterruptConfig_TypeDef
00247  *         structure that contains the configuration setting for the HTS221 Interrupt.
00248  * @retval None
00249  */
00250 void HTS221_INT1InterruptConfig(uint16_t Int1Config)
00251 {
00252 
00253 }
00254 
00255 /**
00256  * @brief  Enable INT1
00257  * @retval None
00258  */
00259 void HTS221_EnableIT(uint8_t IntPin)
00260 {  
00261 
00262 }
00263 
00264 /**
00265  * @brief  Disable  INT1
00266  * @retval None
00267  */
00268 void HTS221_DisableIT(uint8_t IntPin)
00269 {  
00270 
00271 }
00272 
00273 
00274 /**
00275  * @brief  Read HTS221 output register, and calculate the humidity.
00276  * @param  pfData : Data out pointer
00277  * @retval None
00278  */
00279 void HTS221_GetHumidity(float* pfData)
00280 {
00281     int16_t H_T_out, humidity_t;
00282     uint8_t tempReg[2] = {0,0};
00283     uint8_t tmp = 0x00;
00284     float H_rh;
00285     
00286     HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1);
00287 
00288     /* Output Data Rate selection */
00289     tmp &= (HTS221_ODR_MASK);
00290     
00291     if(tmp == 0x00){
00292     
00293       HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG2_ADDR, 1);
00294 
00295       /* Serial Interface Mode selection */
00296       tmp &= ~(HTS221_ONE_SHOT_MASK);
00297       tmp |= HTS221_ONE_SHOT_START;
00298 
00299       HUM_TEMP_IO_Write(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG2_ADDR, 1);
00300     
00301       do{
00302       
00303         HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_STATUS_REG_ADDR, 1);
00304          
00305       }while(!(tmp&&0x02));
00306     
00307     }
00308     
00309     
00310     HUM_TEMP_IO_Read(&tempReg[0], HTS221_ADDRESS, HTS221_HUMIDITY_OUT_L_ADDR + 0x80, 2);
00311     H_T_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
00312 
00313     H_rh = ((float)(H_T_out - H0_T0_out))/(H1_T0_out - H0_T0_out) * (H1_rh - H0_rh) + H0_rh;
00314 
00315     humidity_t = (uint16_t)(H_rh * pow(10,HUM_DECIMAL_DIGITS));
00316 
00317     *pfData = ((float)humidity_t)/pow(10,HUM_DECIMAL_DIGITS);
00318 }
00319 
00320 /**
00321  * @brief  Read HTS221 output register, and calculate the temperature.
00322  * @param  pfData : Data out pointer
00323  * @retval None
00324  */
00325 void HTS221_GetTemperature(float* pfData)
00326 {
00327     int16_t T_out, temperature_t;
00328     uint8_t tempReg[2] = {0,0};
00329     uint8_t tmp = 0x00;
00330     float T_degC;
00331     
00332     HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1);
00333 
00334     /* Output Data Rate selection */
00335     tmp &= (HTS221_ODR_MASK);
00336     
00337     if(tmp == 0x00){
00338     
00339       HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG2_ADDR, 1);
00340 
00341       /* Serial Interface Mode selection */
00342       tmp &= ~(HTS221_ONE_SHOT_MASK);
00343       tmp |= HTS221_ONE_SHOT_START;
00344 
00345       HUM_TEMP_IO_Write(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG2_ADDR, 1);
00346     
00347       do{
00348       
00349         HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_STATUS_REG_ADDR, 1);
00350        
00351       }while(!(tmp&&0x01));
00352     
00353     }
00354 
00355     HUM_TEMP_IO_Read(&tempReg[0], HTS221_ADDRESS, HTS221_TEMP_OUT_L_ADDR + 0x80, 2);
00356     T_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
00357 
00358     T_degC = ((float)(T_out - T0_out))/(T1_out - T0_out) * (T1_degC - T0_degC) + T0_degC;
00359 
00360     temperature_t = (int16_t)(T_degC * pow(10,TEMP_DECIMAL_DIGITS));
00361 
00362     *pfData = ((float)temperature_t)/pow(10,TEMP_DECIMAL_DIGITS);
00363 }
00364 
00365 
00366 /**
00367  * @brief  Exit the shutdown mode for HTS221.
00368  * @retval None
00369  */
00370 static void HTS221_Power_On()
00371 {
00372     uint8_t tmpReg;
00373 
00374     /* Read the register content */
00375     HUM_TEMP_IO_Read(&tmpReg, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1);
00376 
00377     /* Set the power down bit */
00378     tmpReg |= HTS221_MODE_ACTIVE;
00379 
00380     /* Write register */
00381     HUM_TEMP_IO_Write(&tmpReg, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1);
00382 }
00383 
00384 /**
00385  * @brief  Enter the shutdown mode for HTS221.
00386  * @retval None
00387  */
00388 void HTS221_Power_OFF()
00389 {
00390     uint8_t tmpReg;
00391 
00392     /* Read the register content */
00393     HUM_TEMP_IO_Read(&tmpReg, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1);
00394 
00395     /* Reset the power down bit */
00396     tmpReg &= ~(HTS221_MODE_ACTIVE);
00397 
00398     /* Write register */
00399     HUM_TEMP_IO_Write(&tmpReg, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1);
00400 }
00401 
00402 /**
00403  * @}
00404  */
00405 
00406 /**
00407  * @}
00408  */
00409 
00410 /**
00411  * @}
00412  */
00413 
00414 /**
00415  * @}
00416  */
00417 
00418 
00419 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/     
00420