Simple VL6180x demo for VL6180x Explorer shield with Nucleo F401 adapted from SparkFun example.

Dependents:   VL6180X VL6180X_Explorer VL6180X_Explorer VL6180X ... more

Committer:
highroads
Date:
Fri Jun 17 17:11:04 2016 +0000
Revision:
6:99fb447bba81
Parent:
5:fa7d17a114d2
Change address should change m_addr

Who changed what in which revision?

UserRevisionLine numberNew contents of line
highroads 0:4a5d4b4a40c4 1 /******************************************************************************
highroads 3:4a20f2102acf 2 * @file VL6180x.cpp
highroads 3:4a20f2102acf 3 * @brief Library for VL6180x time of flight range finder.
highroads 3:4a20f2102acf 4 * Modified for mbed Ian Kilburn 5/10/2015
highroads 0:4a5d4b4a40c4 5 * Casey Kuhns @ SparkFun Electronics
highroads 0:4a5d4b4a40c4 6 * 10/29/2014
highroads 0:4a5d4b4a40c4 7 * https://github.com/sparkfun/
highroads 0:4a5d4b4a40c4 8 *
highroads 0:4a5d4b4a40c4 9 * The VL6180x by ST micro is a time of flight range finder that
highroads 0:4a5d4b4a40c4 10 * uses pulsed IR light to determine distances from object at close
highroads 0:4a5d4b4a40c4 11 * range. The average range of a sensor is between 0-200mm
highroads 0:4a5d4b4a40c4 12 *
highroads 0:4a5d4b4a40c4 13 * In this file are the functions in the VL6180x class
highroads 0:4a5d4b4a40c4 14 *
highroads 0:4a5d4b4a40c4 15 * Resources:
highroads 0:4a5d4b4a40c4 16 * This library uses the Arduino Wire.h to complete I2C transactions.
highroads 0:4a5d4b4a40c4 17 *
highroads 0:4a5d4b4a40c4 18 * Development environment specifics:
highroads 0:4a5d4b4a40c4 19 * IDE: Arduino 1.0.5
highroads 0:4a5d4b4a40c4 20 * Hardware Platform: Arduino Pro 3.3V/8MHz
highroads 0:4a5d4b4a40c4 21 * VL6180x Breakout Version: 1.0
highroads 0:4a5d4b4a40c4 22 *
highroads 0:4a5d4b4a40c4 23 *
highroads 0:4a5d4b4a40c4 24 * This code is beerware. If you see me (or any other SparkFun employee) at the
highroads 0:4a5d4b4a40c4 25 * local pub, and you've found our code helpful, please buy us a round!
highroads 0:4a5d4b4a40c4 26 *
highroads 0:4a5d4b4a40c4 27 * Distributed as-is; no warranty is given.
highroads 0:4a5d4b4a40c4 28 ******************************************************************************/
highroads 0:4a5d4b4a40c4 29
highroads 0:4a5d4b4a40c4 30
highroads 0:4a5d4b4a40c4 31 #include "VL6180x.h"
highroads 0:4a5d4b4a40c4 32
highroads 2:31313127c530 33 VL6180x::VL6180x(PinName sda, PinName scl, uint8_t addr) : m_i2c(sda, scl), m_addr(addr) {}
highroads 2:31313127c530 34 int VL6180x::VL6180xInit(void){
highroads 0:4a5d4b4a40c4 35 uint8_t data; //for temp data storage
highroads 0:4a5d4b4a40c4 36
highroads 0:4a5d4b4a40c4 37 data = VL6180x_getRegister(VL6180X_SYSTEM_FRESH_OUT_OF_RESET);
highroads 0:4a5d4b4a40c4 38 wait_ms(50);
highroads 0:4a5d4b4a40c4 39 if(data != 1) return VL6180x_FAILURE_RESET;
highroads 0:4a5d4b4a40c4 40
highroads 0:4a5d4b4a40c4 41 //Required by datasheet
highroads 0:4a5d4b4a40c4 42 //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
highroads 0:4a5d4b4a40c4 43 VL6180x_setRegister(0x0207, 0x01);
highroads 0:4a5d4b4a40c4 44 VL6180x_setRegister(0x0208, 0x01);
highroads 0:4a5d4b4a40c4 45 VL6180x_setRegister(0x0096, 0x00);
highroads 0:4a5d4b4a40c4 46 VL6180x_setRegister(0x0097, 0xfd);
highroads 0:4a5d4b4a40c4 47 VL6180x_setRegister(0x00e3, 0x00);
highroads 0:4a5d4b4a40c4 48 VL6180x_setRegister(0x00e4, 0x04);
highroads 0:4a5d4b4a40c4 49 VL6180x_setRegister(0x00e5, 0x02);
highroads 0:4a5d4b4a40c4 50 VL6180x_setRegister(0x00e6, 0x01);
highroads 0:4a5d4b4a40c4 51 VL6180x_setRegister(0x00e7, 0x03);
highroads 0:4a5d4b4a40c4 52 VL6180x_setRegister(0x00f5, 0x02);
highroads 0:4a5d4b4a40c4 53 VL6180x_setRegister(0x00d9, 0x05);
highroads 0:4a5d4b4a40c4 54 VL6180x_setRegister(0x00db, 0xce);
highroads 0:4a5d4b4a40c4 55 VL6180x_setRegister(0x00dc, 0x03);
highroads 0:4a5d4b4a40c4 56 VL6180x_setRegister(0x00dd, 0xf8);
highroads 0:4a5d4b4a40c4 57 VL6180x_setRegister(0x009f, 0x00);
highroads 0:4a5d4b4a40c4 58 VL6180x_setRegister(0x00a3, 0x3c);
highroads 0:4a5d4b4a40c4 59 VL6180x_setRegister(0x00b7, 0x00);
highroads 0:4a5d4b4a40c4 60 VL6180x_setRegister(0x00bb, 0x3c);
highroads 0:4a5d4b4a40c4 61 VL6180x_setRegister(0x00b2, 0x09);
highroads 0:4a5d4b4a40c4 62 VL6180x_setRegister(0x00ca, 0x09);
highroads 0:4a5d4b4a40c4 63 VL6180x_setRegister(0x0198, 0x01);
highroads 0:4a5d4b4a40c4 64 VL6180x_setRegister(0x01b0, 0x17);
highroads 0:4a5d4b4a40c4 65 VL6180x_setRegister(0x01ad, 0x00);
highroads 0:4a5d4b4a40c4 66 VL6180x_setRegister(0x00ff, 0x05);
highroads 0:4a5d4b4a40c4 67 VL6180x_setRegister(0x0100, 0x05);
highroads 0:4a5d4b4a40c4 68 VL6180x_setRegister(0x0199, 0x05);
highroads 0:4a5d4b4a40c4 69 VL6180x_setRegister(0x01a6, 0x1b);
highroads 0:4a5d4b4a40c4 70 VL6180x_setRegister(0x01ac, 0x3e);
highroads 0:4a5d4b4a40c4 71 VL6180x_setRegister(0x01a7, 0x1f);
highroads 0:4a5d4b4a40c4 72 VL6180x_setRegister(0x0030, 0x00);
highroads 0:4a5d4b4a40c4 73
highroads 0:4a5d4b4a40c4 74 return 0;
highroads 0:4a5d4b4a40c4 75 }
highroads 0:4a5d4b4a40c4 76 VL6180x::~VL6180x(void) {
highroads 0:4a5d4b4a40c4 77 };
highroads 0:4a5d4b4a40c4 78
highroads 0:4a5d4b4a40c4 79 void VL6180x::VL6180xDefautSettings(void){
highroads 0:4a5d4b4a40c4 80 //Recommended settings from datasheet
highroads 0:4a5d4b4a40c4 81 //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
highroads 0:4a5d4b4a40c4 82
highroads 0:4a5d4b4a40c4 83 //Enable Interrupts on Conversion Complete (any source)
highroads 0:4a5d4b4a40c4 84 VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, (4 << 3)|(4) ); // Set GPIO1 high when sample complete
highroads 0:4a5d4b4a40c4 85
highroads 0:4a5d4b4a40c4 86
highroads 0:4a5d4b4a40c4 87 VL6180x_setRegister(VL6180X_SYSTEM_MODE_GPIO1, 0x10); // Set GPIO1 high when sample complete
highroads 0:4a5d4b4a40c4 88 VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD, 0x30); //Set Avg sample period
highroads 0:4a5d4b4a40c4 89 VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, 0x46); // Set the ALS gain
highroads 0:4a5d4b4a40c4 90 VL6180x_setRegister(VL6180X_SYSRANGE_VHV_REPEAT_RATE, 0xFF); // Set auto calibration period (Max = 255)/(OFF = 0)
highroads 0:4a5d4b4a40c4 91 VL6180x_setRegister(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x63); // Set ALS integration time to 100ms
highroads 0:4a5d4b4a40c4 92 VL6180x_setRegister(VL6180X_SYSRANGE_VHV_RECALIBRATE, 0x01); // perform a single temperature calibration
highroads 0:4a5d4b4a40c4 93 //Optional settings from datasheet
highroads 0:4a5d4b4a40c4 94 //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
highroads 0:4a5d4b4a40c4 95 VL6180x_setRegister(VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); // Set default ranging inter-measurement period to 100ms
highroads 0:4a5d4b4a40c4 96 VL6180x_setRegister(VL6180X_SYSALS_INTERMEASUREMENT_PERIOD, 0x0A); // Set default ALS inter-measurement period to 100ms
highroads 0:4a5d4b4a40c4 97 VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24); // Configures interrupt on ‘New Sample Ready threshold event’
highroads 0:4a5d4b4a40c4 98 //Additional settings defaults from community
highroads 0:4a5d4b4a40c4 99 VL6180x_setRegister(VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME, 0x32);
highroads 0:4a5d4b4a40c4 100 VL6180x_setRegister(VL6180X_SYSRANGE_RANGE_CHECK_ENABLES, 0x10 | 0x01);
highroads 0:4a5d4b4a40c4 101 VL6180x_setRegister16bit(VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, 0x7B );
highroads 0:4a5d4b4a40c4 102 VL6180x_setRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x64);
highroads 0:4a5d4b4a40c4 103
highroads 0:4a5d4b4a40c4 104 VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD,0x30);
highroads 0:4a5d4b4a40c4 105 VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN,0x40);
highroads 0:4a5d4b4a40c4 106 VL6180x_setRegister(VL6180X_FIRMWARE_RESULT_SCALER,0x01);
highroads 0:4a5d4b4a40c4 107 }
highroads 0:4a5d4b4a40c4 108 void VL6180x::getIdentification(struct VL6180xIdentification *temp){
highroads 0:4a5d4b4a40c4 109
highroads 0:4a5d4b4a40c4 110 temp->idModel = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_ID);
highroads 0:4a5d4b4a40c4 111 temp->idModelRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MAJOR);
highroads 0:4a5d4b4a40c4 112 temp->idModelRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MINOR);
highroads 0:4a5d4b4a40c4 113 temp->idModuleRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MAJOR);
highroads 0:4a5d4b4a40c4 114 temp->idModuleRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MINOR);
highroads 0:4a5d4b4a40c4 115
highroads 0:4a5d4b4a40c4 116 temp->idDate = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_DATE);
highroads 0:4a5d4b4a40c4 117 temp->idTime = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_TIME);
highroads 0:4a5d4b4a40c4 118 }
highroads 0:4a5d4b4a40c4 119
highroads 0:4a5d4b4a40c4 120
highroads 0:4a5d4b4a40c4 121 uint8_t VL6180x::changeAddress(uint8_t old_address, uint8_t new_address){
highroads 0:4a5d4b4a40c4 122
highroads 0:4a5d4b4a40c4 123 //NOTICE: IT APPEARS THAT CHANGING THE ADDRESS IS NOT STORED IN NON-VOLATILE MEMORY
highroads 0:4a5d4b4a40c4 124 // POWER CYCLING THE DEVICE REVERTS ADDRESS BACK TO 0X29
highroads 0:4a5d4b4a40c4 125
highroads 0:4a5d4b4a40c4 126 if( old_address == new_address) return old_address;
highroads 0:4a5d4b4a40c4 127 if( new_address > 127) return old_address;
highroads 0:4a5d4b4a40c4 128
highroads 0:4a5d4b4a40c4 129 VL6180x_setRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS, new_address);
highroads 6:99fb447bba81 130 // mbed needs the new address
highroads 5:fa7d17a114d2 131 m_addr=new_address<<1;
highroads 0:4a5d4b4a40c4 132 return VL6180x_getRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS);
highroads 0:4a5d4b4a40c4 133 }
highroads 0:4a5d4b4a40c4 134
highroads 0:4a5d4b4a40c4 135
highroads 0:4a5d4b4a40c4 136
highroads 0:4a5d4b4a40c4 137 uint8_t VL6180x::getDistance() {
highroads 0:4a5d4b4a40c4 138 uint8_t distance;
highroads 0:4a5d4b4a40c4 139 VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); //Start Single shot mode
highroads 0:4a5d4b4a40c4 140 wait_ms(10);
highroads 0:4a5d4b4a40c4 141 distance = VL6180x_getRegister(VL6180X_RESULT_RANGE_VAL);
highroads 0:4a5d4b4a40c4 142 VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
highroads 0:4a5d4b4a40c4 143 return distance;
highroads 0:4a5d4b4a40c4 144 }
highroads 0:4a5d4b4a40c4 145
highroads 5:fa7d17a114d2 146 float VL6180x::getDistance_m() {
highroads 5:fa7d17a114d2 147 float distance;
highroads 5:fa7d17a114d2 148 VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); //Start Single shot mode
highroads 5:fa7d17a114d2 149 wait_ms(10);
highroads 5:fa7d17a114d2 150 distance = 0.001*(float)VL6180x_getRegister(VL6180X_RESULT_RANGE_VAL);
highroads 5:fa7d17a114d2 151 VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
highroads 5:fa7d17a114d2 152 return distance;
highroads 5:fa7d17a114d2 153 }
highroads 5:fa7d17a114d2 154
highroads 0:4a5d4b4a40c4 155 float VL6180x::getAmbientLight(vl6180x_als_gain VL6180X_ALS_GAIN)
highroads 0:4a5d4b4a40c4 156 {
highroads 0:4a5d4b4a40c4 157 //First load in Gain we are using, do it everytime incase someone changes it on us.
highroads 0:4a5d4b4a40c4 158 //Note: Upper nibble shoudl be set to 0x4 i.e. for ALS gain of 1.0 write 0x46
highroads 0:4a5d4b4a40c4 159 VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, (0x40 | VL6180X_ALS_GAIN)); // Set the ALS gain
highroads 0:4a5d4b4a40c4 160
highroads 0:4a5d4b4a40c4 161 //Start ALS Measurement
highroads 0:4a5d4b4a40c4 162 VL6180x_setRegister(VL6180X_SYSALS_START, 0x01);
highroads 0:4a5d4b4a40c4 163
highroads 0:4a5d4b4a40c4 164 wait_ms(100); //give it time...
highroads 0:4a5d4b4a40c4 165
highroads 0:4a5d4b4a40c4 166 VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
highroads 0:4a5d4b4a40c4 167
highroads 0:4a5d4b4a40c4 168 //Retrieve the Raw ALS value from the sensoe
highroads 0:4a5d4b4a40c4 169 unsigned int alsRaw = VL6180x_getRegister16bit(VL6180X_RESULT_ALS_VAL);
highroads 0:4a5d4b4a40c4 170
highroads 0:4a5d4b4a40c4 171 //Get Integration Period for calculation, we do this everytime incase someone changes it on us.
highroads 0:4a5d4b4a40c4 172 unsigned int alsIntegrationPeriodRaw = VL6180x_getRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD);
highroads 0:4a5d4b4a40c4 173
highroads 0:4a5d4b4a40c4 174 float alsIntegrationPeriod = 100.0 / alsIntegrationPeriodRaw ;
highroads 0:4a5d4b4a40c4 175
highroads 0:4a5d4b4a40c4 176 //Calculate actual LUX from Appnotes
highroads 0:4a5d4b4a40c4 177
highroads 0:4a5d4b4a40c4 178 float alsGain = 0.0;
highroads 0:4a5d4b4a40c4 179
highroads 0:4a5d4b4a40c4 180 switch (VL6180X_ALS_GAIN){
highroads 0:4a5d4b4a40c4 181 case GAIN_20: alsGain = 20.0; break;
highroads 0:4a5d4b4a40c4 182 case GAIN_10: alsGain = 10.32; break;
highroads 0:4a5d4b4a40c4 183 case GAIN_5: alsGain = 5.21; break;
highroads 0:4a5d4b4a40c4 184 case GAIN_2_5: alsGain = 2.60; break;
highroads 0:4a5d4b4a40c4 185 case GAIN_1_67: alsGain = 1.72; break;
highroads 0:4a5d4b4a40c4 186 case GAIN_1_25: alsGain = 1.28; break;
highroads 0:4a5d4b4a40c4 187 case GAIN_1: alsGain = 1.01; break;
highroads 0:4a5d4b4a40c4 188 case GAIN_40: alsGain = 40.0; break;
highroads 0:4a5d4b4a40c4 189 }
highroads 0:4a5d4b4a40c4 190
highroads 0:4a5d4b4a40c4 191 //Calculate LUX from formula in AppNotes
highroads 0:4a5d4b4a40c4 192
highroads 0:4a5d4b4a40c4 193 float alsCalculated = (float)0.32 * ((float)alsRaw / alsGain) * alsIntegrationPeriod;
highroads 0:4a5d4b4a40c4 194
highroads 0:4a5d4b4a40c4 195 return alsCalculated;
highroads 0:4a5d4b4a40c4 196 }
highroads 0:4a5d4b4a40c4 197
highroads 0:4a5d4b4a40c4 198 // --- Private Functions --- //
highroads 0:4a5d4b4a40c4 199
highroads 0:4a5d4b4a40c4 200 uint8_t VL6180x::VL6180x_getRegister(uint16_t registerAddr)
highroads 0:4a5d4b4a40c4 201 {
highroads 0:4a5d4b4a40c4 202 uint8_t data;
highroads 0:4a5d4b4a40c4 203 char data_write[2];
highroads 0:4a5d4b4a40c4 204 char data_read[1];
highroads 0:4a5d4b4a40c4 205 data_write[0] = (registerAddr >> 8) & 0xFF; //MSB of register address
highroads 0:4a5d4b4a40c4 206 data_write[1] = registerAddr & 0xFF; //LSB of register address
highroads 0:4a5d4b4a40c4 207 m_i2c.write(m_addr, data_write, 2,0);
highroads 0:4a5d4b4a40c4 208 m_i2c.read(m_addr,data_read,1,1);
highroads 0:4a5d4b4a40c4 209 //Read Data from selected register
highroads 0:4a5d4b4a40c4 210 data=data_read[0];
highroads 0:4a5d4b4a40c4 211 return data;
highroads 0:4a5d4b4a40c4 212 }
highroads 0:4a5d4b4a40c4 213
highroads 0:4a5d4b4a40c4 214 uint16_t VL6180x::VL6180x_getRegister16bit(uint16_t registerAddr)
highroads 0:4a5d4b4a40c4 215 {
highroads 0:4a5d4b4a40c4 216 uint8_t data_low;
highroads 0:4a5d4b4a40c4 217 uint8_t data_high;
highroads 0:4a5d4b4a40c4 218 uint16_t data;
highroads 0:4a5d4b4a40c4 219
highroads 0:4a5d4b4a40c4 220 char data_write[2];
highroads 0:4a5d4b4a40c4 221 char data_read[2];
highroads 0:4a5d4b4a40c4 222 data_write[0] = (registerAddr >> 8) & 0xFF; //MSB of register address
highroads 0:4a5d4b4a40c4 223 data_write[1] = registerAddr & 0xFF; //LSB of register address
highroads 0:4a5d4b4a40c4 224 m_i2c.write(m_addr, data_write, 2,0);
highroads 0:4a5d4b4a40c4 225 m_i2c.read(m_addr,data_read,2,1);
highroads 0:4a5d4b4a40c4 226 data_high = data_read[0]; //Read Data from selected register
highroads 0:4a5d4b4a40c4 227 data_low = data_read[1]; //Read Data from selected register
highroads 0:4a5d4b4a40c4 228 data = (data_high << 8)|data_low;
highroads 0:4a5d4b4a40c4 229
highroads 0:4a5d4b4a40c4 230 return data;
highroads 0:4a5d4b4a40c4 231 }
highroads 0:4a5d4b4a40c4 232
highroads 0:4a5d4b4a40c4 233 void VL6180x::VL6180x_setRegister(uint16_t registerAddr, uint8_t data)
highroads 0:4a5d4b4a40c4 234 {
highroads 0:4a5d4b4a40c4 235 char data_write[3];
highroads 0:4a5d4b4a40c4 236 data_write[0] = (registerAddr >> 8) & 0xFF; //MSB of register address
highroads 0:4a5d4b4a40c4 237 data_write[1] = registerAddr & 0xFF; //LSB of register address
highroads 0:4a5d4b4a40c4 238 data_write[2] = data & 0xFF;
highroads 0:4a5d4b4a40c4 239 m_i2c.write(m_addr, data_write, 3);
highroads 0:4a5d4b4a40c4 240 }
highroads 0:4a5d4b4a40c4 241
highroads 0:4a5d4b4a40c4 242 void VL6180x::VL6180x_setRegister16bit(uint16_t registerAddr, uint16_t data)
highroads 0:4a5d4b4a40c4 243 {
highroads 0:4a5d4b4a40c4 244 char data_write[4];
highroads 0:4a5d4b4a40c4 245 data_write[0] = (registerAddr >> 8) & 0xFF; //MSB of register address
highroads 0:4a5d4b4a40c4 246 data_write[1] = registerAddr & 0xFF; //LSB of register address
highroads 0:4a5d4b4a40c4 247 data_write[2] = (data >> 8) & 0xFF;
highroads 0:4a5d4b4a40c4 248 data_write[3] = data & 0xFF;
highroads 0:4a5d4b4a40c4 249 m_i2c.write(m_addr, data_write, 4);
highroads 0:4a5d4b4a40c4 250 }