Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
BH1750.cpp
00001 /** 00002 * @brief BH1750.cpp 00003 * @details Digital 16-bit Serial Output Type Ambient Light Sensor IC. 00004 * Functions file. 00005 * 00006 * 00007 * @return NA 00008 * 00009 * @author Manuel Caballero 00010 * @date 10/August/2017 00011 * @version 10/August/2017 The ORIGIN 00012 * @pre NaN 00013 * @warning NaN 00014 * @pre This code belongs to AqueronteBlog ( http://unbarquero.blogspot.com ). 00015 */ 00016 00017 #include "BH1750.h" 00018 00019 00020 00021 BH1750::BH1750 ( PinName sda, PinName scl, uint32_t addr, uint32_t freq ) 00022 : i2c ( sda, scl ) 00023 , BH1750_Addr ( addr ) 00024 { 00025 i2c.frequency( freq ); 00026 } 00027 00028 00029 BH1750::~BH1750(){ 00030 } 00031 00032 00033 00034 /** 00035 * @brief uint32_t BH1750_ResetDataRegister ( void ) 00036 * @details Reseting the data register value. 00037 * 00038 * @param[in] NaN. 00039 * 00040 * @param[out] Status of BH1750_ResetDataRegister. 00041 * 00042 * 00043 * @return NA 00044 * 00045 * @author Manuel Caballero 00046 * @date 10/August/2017 00047 * @version 10/August/2017 The ORIGIN 00048 * @pre NaN 00049 * @warning Reset command is not acceptable in Power Down mode. 00050 */ 00051 uint32_t BH1750::BH1750_ResetDataRegister ( void ) 00052 { 00053 char cmd[] = { BH1750_RESET }; 00054 uint32_t aux = 0; 00055 00056 00057 aux = i2c.write ( BH1750_Addr, &cmd[0], 1 ); 00058 00059 00060 if ( aux == I2C_SUCCESS ) 00061 return BH1750_SUCCESS ; 00062 else 00063 return BH1750_FAILURE ; 00064 } 00065 00066 00067 /** 00068 * @brief uint32_t BH1750_PowerDown ( void ) 00069 * @details BH1750 in low power mode. 00070 * 00071 * @param[in] NaN 00072 * 00073 * @param[out] Status of BH1750_PowerDown. 00074 * 00075 * 00076 * @return NA 00077 * 00078 * @author Manuel Caballero 00079 * @date 10/August/2017 00080 * @version 10/August/2017 The ORIGIN 00081 * @pre NaN 00082 * @warning NaN. 00083 */ 00084 uint32_t BH1750::BH1750_PowerDown ( void ) 00085 { 00086 char cmd[] = { BH1750_POWER_DOWN }; 00087 uint32_t aux = 0; 00088 00089 00090 aux = i2c.write ( BH1750_Addr, &cmd[0], 1 ); 00091 00092 00093 if ( aux == I2C_SUCCESS ) 00094 return BH1750_SUCCESS ; 00095 else 00096 return BH1750_FAILURE ; 00097 } 00098 00099 00100 /** 00101 * @brief uint32_t BH1750_PowerOn ( void ) 00102 * @details BH1750 is on and waiting for measurement command. 00103 * 00104 * @param[in] NaN 00105 * 00106 * @param[out] Status of BH1750_PowerOn. 00107 * 00108 * 00109 * @return NA 00110 * 00111 * @author Manuel Caballero 00112 * @date 10/August/2017 00113 * @version 10/August/2017 The ORIGIN 00114 * @pre NaN 00115 * @warning NaN. 00116 */ 00117 uint32_t BH1750::BH1750_PowerOn ( void ) 00118 { 00119 char cmd[] = { BH1750_POWER_ON }; 00120 uint32_t aux = 0; 00121 00122 00123 aux = i2c.write ( BH1750_Addr, &cmd[0], 1 ); 00124 00125 00126 if ( aux == I2C_SUCCESS ) 00127 return BH1750_SUCCESS ; 00128 else 00129 return BH1750_FAILURE ; 00130 } 00131 00132 00133 /** 00134 * @brief uint32_t BH1750_TriggerMeasurement ( uint32_t ) 00135 * @details Trigger a new temperature measurement. 00136 * 00137 * @param[in] mode: ONE-SHOT or CONTINUOUSLY measurement ( Normal Mode or Mode2 ). 00138 * 00139 * @param[out] Status of BH1750_TriggerMeasurement. 00140 * 00141 * 00142 * @return NA 00143 * 00144 * @author Manuel Caballero 00145 * @date 10/August/2017 00146 * @version 10/August/2017 The ORIGIN 00147 * @pre NaN 00148 * @warning The measuring time depends on the chosen resolution ( MODE ). The user 00149 * must take this into account. 00150 */ 00151 uint32_t BH1750::BH1750_TriggerMeasurement ( BH1750_COMMANDS mode ) 00152 { 00153 char cmd[] = { mode }; 00154 uint32_t aux = 0; 00155 00156 BH1750_Mode = mode; 00157 00158 00159 aux = i2c.write ( BH1750_Addr, &cmd[0], 1 ); 00160 00161 00162 if ( aux == I2C_SUCCESS ) 00163 return BH1750_SUCCESS ; 00164 else 00165 return BH1750_FAILURE ; 00166 } 00167 00168 00169 /** 00170 * @brief uint32_t BH1750_NewSensitivity ( uint8_t ) 00171 * @details It configures the new sensitivity of the sensor. The sensitivity depends on which 00172 * resolution mode is used: 00173 * 00174 * H-reslution mode : Illuminance per 1 count ( lx / count ) = 1 / 1.2 *( 69 / X ) 00175 * H-reslution mode2 : Illuminance per 1 count ( lx / count ) = 1 / 1.2 *( 69 / X ) / 2 00176 * 00177 * So, by the default: 00178 * 00179 * H-reslution mode : Sensitivity = 1 / 1.2 *( 69 / 69 ) ~ 0.83 00180 * H-reslution mode2 : Sensitivity = 1 / 1.2 *( 69 / 69 ) / 2 ~ 1.67 00181 * 00182 * 00183 * Example ( H-reslution mode ): 00184 * 00185 * Minimum Sensitivity = 1 / 1.2 *( 69 / 31 ) ~ 0.37 00186 * Maximum Sensitivity = 1 / 1.2 *( 69 / 254 ) ~ 3.07 00187 * 00188 * 00189 * 00190 * @param[in] newSensitivity: New sensitivity value. 00191 * 00192 * @param[out] Status of BH1750_NewSensitivity. 00193 * 00194 * 00195 * @return NA 00196 * 00197 * @author Manuel Caballero 00198 * @date 10/August/2017 00199 * @version 10/August/2017 The ORIGIN 00200 * @pre NaN 00201 * @warning newSensitivity can ONLY be from 31 to 254. 00202 */ 00203 uint32_t BH1750::BH1750_NewSensitivity ( uint8_t newSensitivity ) 00204 { 00205 char cmd[] = { 0x40, 0x60 }; // 0x40 ( MT High byte ), 0x60 ( MT Low byte ) 00206 uint32_t aux = 0; 00207 00208 00209 if ( ( newSensitivity > 30 ) && ( newSensitivity < 255 ) ) 00210 { 00211 cmd[0] |= ( ( newSensitivity & 0xE0 ) >> 5 ); 00212 cmd[1] |= ( newSensitivity & 0x1F ); 00213 00214 aux = i2c.write ( BH1750_Addr, &cmd[0], 2 ); 00215 } 00216 else 00217 aux = BH1750_FAILURE ; 00218 00219 00220 00221 if ( aux == I2C_SUCCESS ) 00222 return BH1750_SUCCESS ; 00223 else 00224 return BH1750_FAILURE ; 00225 } 00226 00227 00228 00229 /** 00230 * @brief uint32_t BH1750_ReadRawData ( uint32_t* ) 00231 * @details Read a new raw temperature measurement. 00232 * 00233 * @param[in] myRawData: Variable to store the raw data. 00234 * 00235 * @param[out] Status of BH1750_ReadRawData. 00236 * 00237 * 00238 * @return NA 00239 * 00240 * @author Manuel Caballero 00241 * @date 10/August/2017 00242 * @version 10/August/2017 The ORIGIN 00243 * @pre NaN 00244 * @warning BH1750_TriggerMeasurement MUST be call before. 00245 */ 00246 uint32_t BH1750::BH1750_ReadRawData ( char* myRawData ) 00247 { 00248 uint32_t aux = 0; 00249 00250 00251 aux = i2c.read ( BH1750_Addr, &myRawData[0], 2 ); 00252 00253 00254 if ( aux == I2C_SUCCESS ) 00255 return BH1750_SUCCESS ; 00256 else 00257 return BH1750_FAILURE ; 00258 } 00259 00260 00261 /** 00262 * @brief BH1750_ReadLux ( float* ) 00263 * @details It turns the raw data into a Lux data. 00264 * 00265 * @param[in] myLux: Variable to store the Lux. 00266 * 00267 * @param[out] Status of BH1750_ReadLux. 00268 * 00269 * 00270 * @return NA 00271 * 00272 * @author Manuel Caballero 00273 * @date 10/August/2017 00274 * @version 10/August/2017 The ORIGIN 00275 * @pre NaN 00276 * @warning BH1750_TriggerMeasurement MUST be call before. 00277 */ 00278 uint32_t BH1750::BH1750_ReadLux ( float* myLux ) 00279 { 00280 uint32_t aux = 0; 00281 char myRawData[] = { 0, 0}; 00282 00283 00284 aux = BH1750_ReadRawData ( &myRawData[0] ); 00285 00286 00287 if ( ( BH1750_Mode == BH1750_CONTINUOUSLY_H_RESOLUTION_MODE ) || ( BH1750_Mode == BH1750_CONTINUOUSLY_L_RESOLUTION_MODE ) || ( BH1750_Mode == BH1750_ONE_TIME_H_RESOLUTION_MODE ) || 00288 ( BH1750_Mode == BH1750_ONE_TIME_L_RESOLUTION_MODE ) ) 00289 { 00290 *myLux = ( myRawData[0] << 8 ) | myRawData[1]; 00291 *myLux = ( uint16_t )( *myLux / 1.2 ); 00292 } 00293 else 00294 { 00295 *myLux = ( ( myRawData[0] << 8 ) | myRawData[1] ) >> 1; 00296 *myLux = ( uint16_t )( *myLux / 1.2 ); 00297 00298 if ( ( myRawData[1] & 0x01 ) == 0x01 ) 00299 *myLux += 0.5; 00300 } 00301 00302 00303 00304 if ( aux == I2C_SUCCESS ) 00305 return BH1750_SUCCESS ; 00306 else 00307 return BH1750_FAILURE ; 00308 } 00309 00310
Generated on Sat Jul 16 2022 06:05:46 by
1.7.2