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.
Dependents: Coupe Robotique FIP Coupe-Robotique-FIP-Main
VL6180x.cpp
00001 /****************************************************************************** 00002 * SFE_VL6180x.cpp 00003 * Library for VL6180x time of flight range finder. 00004 * Casey Kuhns @ SparkFun Electronics 00005 * 10/29/2014 00006 * https://github.com/sparkfun/ 00007 * 00008 * The VL6180x by ST micro is a time of flight range finder that 00009 * uses pulsed IR light to determine distances from object at close 00010 * range. The average range of a sensor is between 0-200mm 00011 * 00012 * In this file are the functions in the VL6180x class 00013 * 00014 * Resources: 00015 * This library uses the Arduino Wire.h to complete I2C transactions. 00016 * 00017 * Development environment specifics: 00018 * IDE: Arduino 1.0.5 00019 * Hardware Platform: Arduino Pro 3.3V/8MHz 00020 * VL6180x Breakout Version: 1.0 00021 * 00022 * 00023 * This code is beerware. If you see me (or any other SparkFun employee) at the 00024 * local pub, and you've found our code helpful, please buy us a round! 00025 * 00026 * Distributed as-is; no warranty is given. 00027 ******************************************************************************/ 00028 00029 00030 #include "VL6180x.h" 00031 00032 VL6180x::VL6180x(PinName sda, PinName scl, uint8_t addr) : m_i2c(sda, scl), m_addr(addr) {} 00033 int VL6180x::VL6180xInit(void){ 00034 uint8_t data; //for temp data storage 00035 00036 data = VL6180x_getRegister(VL6180X_SYSTEM_FRESH_OUT_OF_RESET); 00037 wait_ms(50); 00038 if(data != 1) return VL6180x_FAILURE_RESET; 00039 00040 //Required by datasheet 00041 //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf 00042 VL6180x_setRegister(0x0207, 0x01); 00043 VL6180x_setRegister(0x0208, 0x01); 00044 VL6180x_setRegister(0x0096, 0x00); 00045 VL6180x_setRegister(0x0097, 0xfd); 00046 VL6180x_setRegister(0x00e3, 0x00); 00047 VL6180x_setRegister(0x00e4, 0x04); 00048 VL6180x_setRegister(0x00e5, 0x02); 00049 VL6180x_setRegister(0x00e6, 0x01); 00050 VL6180x_setRegister(0x00e7, 0x03); 00051 VL6180x_setRegister(0x00f5, 0x02); 00052 VL6180x_setRegister(0x00d9, 0x05); 00053 VL6180x_setRegister(0x00db, 0xce); 00054 VL6180x_setRegister(0x00dc, 0x03); 00055 VL6180x_setRegister(0x00dd, 0xf8); 00056 VL6180x_setRegister(0x009f, 0x00); 00057 VL6180x_setRegister(0x00a3, 0x3c); 00058 VL6180x_setRegister(0x00b7, 0x00); 00059 VL6180x_setRegister(0x00bb, 0x3c); 00060 VL6180x_setRegister(0x00b2, 0x09); 00061 VL6180x_setRegister(0x00ca, 0x09); 00062 VL6180x_setRegister(0x0198, 0x01); 00063 VL6180x_setRegister(0x01b0, 0x17); 00064 VL6180x_setRegister(0x01ad, 0x00); 00065 VL6180x_setRegister(0x00ff, 0x05); 00066 VL6180x_setRegister(0x0100, 0x05); 00067 VL6180x_setRegister(0x0199, 0x05); 00068 VL6180x_setRegister(0x01a6, 0x1b); 00069 VL6180x_setRegister(0x01ac, 0x3e); 00070 VL6180x_setRegister(0x01a7, 0x1f); 00071 VL6180x_setRegister(0x0030, 0x00); 00072 00073 return 0; 00074 } 00075 VL6180x::~VL6180x(void) { 00076 }; 00077 00078 void VL6180x::VL6180xDefautSettings(void){ 00079 //Recommended settings from datasheet 00080 //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf 00081 00082 //Enable Interrupts on Conversion Complete (any source) 00083 VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, (4 << 3)|(4) ); // Set GPIO1 high when sample complete 00084 00085 00086 VL6180x_setRegister(VL6180X_SYSTEM_MODE_GPIO1, 0x10); // Set GPIO1 high when sample complete 00087 VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD, 0x30); //Set Avg sample period 00088 VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, 0x46); // Set the ALS gain 00089 VL6180x_setRegister(VL6180X_SYSRANGE_VHV_REPEAT_RATE, 0xFF); // Set auto calibration period (Max = 255)/(OFF = 0) 00090 VL6180x_setRegister(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x63); // Set ALS integration time to 100ms 00091 VL6180x_setRegister(VL6180X_SYSRANGE_VHV_RECALIBRATE, 0x01); // perform a single temperature calibration 00092 //Optional settings from datasheet 00093 //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf 00094 VL6180x_setRegister(VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); // Set default ranging inter-measurement period to 100ms 00095 VL6180x_setRegister(VL6180X_SYSALS_INTERMEASUREMENT_PERIOD, 0x0A); // Set default ALS inter-measurement period to 100ms 00096 VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24); // Configures interrupt on ‘New Sample Ready threshold event’ 00097 //Additional settings defaults from community 00098 VL6180x_setRegister(VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME, 0x32); 00099 VL6180x_setRegister(VL6180X_SYSRANGE_RANGE_CHECK_ENABLES, 0x10 | 0x01); 00100 VL6180x_setRegister16bit(VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, 0x7B ); 00101 VL6180x_setRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x64); 00102 00103 VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD,0x30); 00104 VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN,0x40); 00105 VL6180x_setRegister(VL6180X_FIRMWARE_RESULT_SCALER,0x01); 00106 } 00107 void VL6180x::getIdentification(struct VL6180xIdentification *temp){ 00108 00109 temp->idModel = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_ID); 00110 temp->idModelRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MAJOR); 00111 temp->idModelRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MINOR); 00112 temp->idModuleRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MAJOR); 00113 temp->idModuleRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MINOR); 00114 00115 temp->idDate = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_DATE); 00116 temp->idTime = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_TIME); 00117 } 00118 00119 00120 uint8_t VL6180x::changeAddress(uint8_t old_address, uint8_t new_address){ 00121 00122 //NOTICE: IT APPEARS THAT CHANGING THE ADDRESS IS NOT STORED IN NON-VOLATILE MEMORY 00123 // POWER CYCLING THE DEVICE REVERTS ADDRESS BACK TO 0X29 00124 00125 if( old_address == new_address) return old_address; 00126 if( new_address > 127) return old_address; 00127 00128 VL6180x_setRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS, new_address); 00129 00130 return VL6180x_getRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS); 00131 } 00132 00133 00134 00135 uint8_t VL6180x::getDistance() { 00136 uint8_t distance; 00137 VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); //Start Single shot mode 00138 wait_ms(10); 00139 distance = VL6180x_getRegister(VL6180X_RESULT_RANGE_VAL); 00140 VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07); 00141 return distance; 00142 } 00143 00144 float VL6180x::getAmbientLight(vl6180x_als_gain VL6180X_ALS_GAIN) 00145 { 00146 //First load in Gain we are using, do it everytime incase someone changes it on us. 00147 //Note: Upper nibble shoudl be set to 0x4 i.e. for ALS gain of 1.0 write 0x46 00148 VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, (0x40 | VL6180X_ALS_GAIN)); // Set the ALS gain 00149 00150 //Start ALS Measurement 00151 VL6180x_setRegister(VL6180X_SYSALS_START, 0x01); 00152 00153 wait_ms(100); //give it time... 00154 00155 VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07); 00156 00157 //Retrieve the Raw ALS value from the sensoe 00158 unsigned int alsRaw = VL6180x_getRegister16bit(VL6180X_RESULT_ALS_VAL); 00159 00160 //Get Integration Period for calculation, we do this everytime incase someone changes it on us. 00161 unsigned int alsIntegrationPeriodRaw = VL6180x_getRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD); 00162 00163 float alsIntegrationPeriod = 100.0 / alsIntegrationPeriodRaw ; 00164 00165 //Calculate actual LUX from Appnotes 00166 00167 float alsGain = 0.0; 00168 00169 switch (VL6180X_ALS_GAIN){ 00170 case GAIN_20: alsGain = 20.0; break; 00171 case GAIN_10: alsGain = 10.32; break; 00172 case GAIN_5: alsGain = 5.21; break; 00173 case GAIN_2_5: alsGain = 2.60; break; 00174 case GAIN_1_67: alsGain = 1.72; break; 00175 case GAIN_1_25: alsGain = 1.28; break; 00176 case GAIN_1: alsGain = 1.01; break; 00177 case GAIN_40: alsGain = 40.0; break; 00178 } 00179 00180 //Calculate LUX from formula in AppNotes 00181 00182 float alsCalculated = (float)0.32 * ((float)alsRaw / alsGain) * alsIntegrationPeriod; 00183 00184 return alsCalculated; 00185 } 00186 00187 // --- Private Functions --- // 00188 00189 uint8_t VL6180x::VL6180x_getRegister(uint16_t registerAddr) 00190 { 00191 uint8_t data; 00192 char data_write[2]; 00193 char data_read[1]; 00194 data_write[0] = (registerAddr >> 8) & 0xFF; //MSB of register address 00195 data_write[1] = registerAddr & 0xFF; //LSB of register address 00196 m_i2c.write(m_addr, data_write, 2,0); 00197 m_i2c.read(m_addr,data_read,1,1); 00198 //Read Data from selected register 00199 data=data_read[0]; 00200 return data; 00201 } 00202 00203 uint16_t VL6180x::VL6180x_getRegister16bit(uint16_t registerAddr) 00204 { 00205 uint8_t data_low; 00206 uint8_t data_high; 00207 uint16_t data; 00208 00209 char data_write[2]; 00210 char data_read[2]; 00211 data_write[0] = (registerAddr >> 8) & 0xFF; //MSB of register address 00212 data_write[1] = registerAddr & 0xFF; //LSB of register address 00213 m_i2c.write(m_addr, data_write, 2,0); 00214 m_i2c.read(m_addr,data_read,2,1); 00215 data_high = data_read[0]; //Read Data from selected register 00216 data_low = data_read[1]; //Read Data from selected register 00217 data = (data_high << 8)|data_low; 00218 00219 return data; 00220 } 00221 00222 void VL6180x::VL6180x_setRegister(uint16_t registerAddr, uint8_t data) 00223 { 00224 char data_write[3]; 00225 data_write[0] = (registerAddr >> 8) & 0xFF; //MSB of register address 00226 data_write[1] = registerAddr & 0xFF; //LSB of register address 00227 data_write[2] = data & 0xFF; 00228 m_i2c.write(m_addr, data_write, 3); 00229 } 00230 00231 void VL6180x::VL6180x_setRegister16bit(uint16_t registerAddr, uint16_t data) 00232 { 00233 char data_write[4]; 00234 data_write[0] = (registerAddr >> 8) & 0xFF; //MSB of register address 00235 data_write[1] = registerAddr & 0xFF; //LSB of register address 00236 data_write[2] = (data >> 8) & 0xFF; 00237 data_write[3] = data & 0xFF; 00238 m_i2c.write(m_addr, data_write, 4); 00239 }
Generated on Wed Jul 13 2022 18:00:06 by
1.7.2