|
| 1 | +/****************************************************************************** |
| 2 | +SFE_VL6180x.cpp |
| 3 | +Library for VL6180x time of flight range finder. |
| 4 | +Casey Kuhns @ SparkFun Electronics |
| 5 | +10/29/2014 |
| 6 | +https://github.com/sparkfun/ |
| 7 | +
|
| 8 | +The VL6180x by ST micro is a time of flight range finder that |
| 9 | +uses pulsed IR light to determine distances from object at close |
| 10 | +range. The average range of a sensor is between 0-200mm |
| 11 | +
|
| 12 | +In this file are the functions in the VL6180x class |
| 13 | +
|
| 14 | +Resources: |
| 15 | +This library uses the Arduino Wire.h to complete I2C transactions. |
| 16 | +
|
| 17 | +Development environment specifics: |
| 18 | + IDE: Arduino 1.0.5 |
| 19 | + Hardware Platform: Arduino Pro 3.3V/8MHz |
| 20 | + VL6180x Breakout Version: 1.0 |
| 21 | +
|
| 22 | +
|
| 23 | +This code is beerware. If you see me (or any other SparkFun employee) at the |
| 24 | +local pub, and you've found our code helpful, please buy us a round! |
| 25 | +
|
| 26 | +Distributed as-is; no warranty is given. |
| 27 | +******************************************************************************/ |
| 28 | + |
| 29 | +#include <Wire.h> |
| 30 | +#include "SFE_VL6180X.h" |
| 31 | + |
| 32 | +VL6180x::VL6180x(uint8_t address) |
| 33 | +// Initialize the Library |
| 34 | +{ |
| 35 | + Wire.begin(); // Arduino Wire library initializer |
| 36 | + _i2caddress = address; //set default address for communication |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +uint8_t VL6180x::VL6180xInit(void){ |
| 41 | + uint8_t data; //for temp data storage |
| 42 | + |
| 43 | + data = VL6180x_getRegister(VL6180X_SYSTEM_FRESH_OUT_OF_RESET); |
| 44 | + |
| 45 | + if(data != 1) return VL6180x_FAILURE_RESET; |
| 46 | + |
| 47 | + //Required by datasheet |
| 48 | + //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf |
| 49 | + VL6180x_setRegister(0x0207, 0x01); |
| 50 | + VL6180x_setRegister(0x0208, 0x01); |
| 51 | + VL6180x_setRegister(0x0096, 0x00); |
| 52 | + VL6180x_setRegister(0x0097, 0xfd); |
| 53 | + VL6180x_setRegister(0x00e3, 0x00); |
| 54 | + VL6180x_setRegister(0x00e4, 0x04); |
| 55 | + VL6180x_setRegister(0x00e5, 0x02); |
| 56 | + VL6180x_setRegister(0x00e6, 0x01); |
| 57 | + VL6180x_setRegister(0x00e7, 0x03); |
| 58 | + VL6180x_setRegister(0x00f5, 0x02); |
| 59 | + VL6180x_setRegister(0x00d9, 0x05); |
| 60 | + VL6180x_setRegister(0x00db, 0xce); |
| 61 | + VL6180x_setRegister(0x00dc, 0x03); |
| 62 | + VL6180x_setRegister(0x00dd, 0xf8); |
| 63 | + VL6180x_setRegister(0x009f, 0x00); |
| 64 | + VL6180x_setRegister(0x00a3, 0x3c); |
| 65 | + VL6180x_setRegister(0x00b7, 0x00); |
| 66 | + VL6180x_setRegister(0x00bb, 0x3c); |
| 67 | + VL6180x_setRegister(0x00b2, 0x09); |
| 68 | + VL6180x_setRegister(0x00ca, 0x09); |
| 69 | + VL6180x_setRegister(0x0198, 0x01); |
| 70 | + VL6180x_setRegister(0x01b0, 0x17); |
| 71 | + VL6180x_setRegister(0x01ad, 0x00); |
| 72 | + VL6180x_setRegister(0x00ff, 0x05); |
| 73 | + VL6180x_setRegister(0x0100, 0x05); |
| 74 | + VL6180x_setRegister(0x0199, 0x05); |
| 75 | + VL6180x_setRegister(0x01a6, 0x1b); |
| 76 | + VL6180x_setRegister(0x01ac, 0x3e); |
| 77 | + VL6180x_setRegister(0x01a7, 0x1f); |
| 78 | + VL6180x_setRegister(0x0030, 0x00); |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +void VL6180x::VL6180xDefautSettings(void){ |
| 84 | + //Recommended settings from datasheet |
| 85 | + //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf |
| 86 | + |
| 87 | +//Enable Interrupts on Conversion Complete (any source) |
| 88 | + VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, (4 << 3)|(4) ); // Set GPIO1 high when sample complete |
| 89 | + |
| 90 | + |
| 91 | + VL6180x_setRegister(VL6180X_SYSTEM_MODE_GPIO1, 0x10); // Set GPIO1 high when sample complete |
| 92 | + VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD, 0x30); //Set Avg sample period |
| 93 | + VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, 0x46); // Set the ALS gain |
| 94 | + VL6180x_setRegister(VL6180X_SYSRANGE_VHV_REPEAT_RATE, 0xFF); // Set auto calibration period (Max = 255)/(OFF = 0) |
| 95 | + VL6180x_setRegister(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x63); // Set ALS integration time to 100ms |
| 96 | + VL6180x_setRegister(VL6180X_SYSRANGE_VHV_RECALIBRATE, 0x01); // perform a single temperature calibration |
| 97 | + //Optional settings from datasheet |
| 98 | + //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf |
| 99 | + VL6180x_setRegister(VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); // Set default ranging inter-measurement period to 100ms |
| 100 | + VL6180x_setRegister(VL6180X_SYSALS_INTERMEASUREMENT_PERIOD, 0x31); // Set default ALS inter-measurement period to 500ms |
| 101 | + VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24); // Configures interrupt on ‘New Sample Ready threshold event’ |
| 102 | + //Additional settings defaults from community |
| 103 | + VL6180x_setRegister(VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME, 0x32); |
| 104 | + VL6180x_setRegister(VL6180X_SYSRANGE_RANGE_CHECK_ENABLES, 0x10 | 0x01); |
| 105 | + VL6180x_setRegister16bit(VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, 0x7B ); |
| 106 | + VL6180x_setRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x63); |
| 107 | + |
| 108 | + VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD,0x30); |
| 109 | + VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN,0x40); |
| 110 | + VL6180x_setRegister(VL6180X_FIRMWARE_RESULT_SCALER,0x01); |
| 111 | +} |
| 112 | +void VL6180x::getIdentification(struct VL6180xIdentification *temp){ |
| 113 | + |
| 114 | + temp->idModel = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_ID); |
| 115 | + temp->idModelRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MAJOR); |
| 116 | + temp->idModelRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MINOR); |
| 117 | + temp->idModuleRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MAJOR); |
| 118 | + temp->idModuleRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MINOR); |
| 119 | + |
| 120 | + temp->idDate = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_DATE); |
| 121 | + temp->idTime = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_TIME); |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +uint8_t VL6180x::getDistance() |
| 126 | +{ |
| 127 | + VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); //Start Single shot mode |
| 128 | + delay(10); |
| 129 | + return VL6180x_getRegister(VL6180X_RESULT_RANGE_VAL); |
| 130 | +VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07); |
| 131 | +// return distance; |
| 132 | +} |
| 133 | +uint8_t VL6180x::getAmbientLight() |
| 134 | +{ |
| 135 | + VL6180x_setRegister(VL6180X_SYSALS_START, 0x01); |
| 136 | + delay(100); |
| 137 | +VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07); |
| 138 | + return VL6180x_getRegister(VL6180X_RESULT_ALS_VAL); |
| 139 | +} |
| 140 | + |
| 141 | +// --- Private Functions --- // |
| 142 | + |
| 143 | +uint8_t VL6180x::VL6180x_getRegister(uint16_t registerAddr) |
| 144 | +{ |
| 145 | + uint8_t data; |
| 146 | + |
| 147 | + Wire.beginTransmission( _i2caddress ); // Address set on class instantiation |
| 148 | + Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address |
| 149 | + Wire.write(registerAddr & 0xFF); //LSB of register address |
| 150 | + Wire.endTransmission(false); //Send address and register address bytes |
| 151 | + Wire.requestFrom( _i2caddress , 1); |
| 152 | + data = Wire.read(); //Read Data from selected register |
| 153 | + |
| 154 | + return data; |
| 155 | +} |
| 156 | + |
| 157 | +uint16_t VL6180x::VL6180x_getRegister16bit(uint16_t registerAddr) |
| 158 | +{ |
| 159 | + uint8_t data_low; |
| 160 | + uint8_t data_high; |
| 161 | + uint16_t data; |
| 162 | + |
| 163 | + Wire.beginTransmission( _i2caddress ); // Address set on class instantiation |
| 164 | + Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address |
| 165 | + Wire.write(registerAddr & 0xFF); //LSB of register address |
| 166 | + Wire.endTransmission(false); //Send address and register address bytes |
| 167 | + |
| 168 | + Wire.requestFrom( _i2caddress, 2); |
| 169 | + data_high = Wire.read(); //Read Data from selected register |
| 170 | + data_low = Wire.read(); //Read Data from selected register |
| 171 | + data = (data_high << 8)|data_low; |
| 172 | + |
| 173 | + return data; |
| 174 | +} |
| 175 | + |
| 176 | +void VL6180x::VL6180x_setRegister(uint16_t registerAddr, uint8_t data) |
| 177 | +{ |
| 178 | + Wire.beginTransmission( _i2caddress ); // Address set on class instantiation |
| 179 | + Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address |
| 180 | + Wire.write(registerAddr & 0xFF); //LSB of register address |
| 181 | + Wire.write(data); // Data/setting to be sent to device. |
| 182 | + Wire.endTransmission(); //Send address and register address bytes |
| 183 | +} |
| 184 | + |
| 185 | +void VL6180x::VL6180x_setRegister16bit(uint16_t registerAddr, uint16_t data) |
| 186 | +{ |
| 187 | + Wire.beginTransmission( _i2caddress ); // Address set on class instantiation |
| 188 | + Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address |
| 189 | + Wire.write(registerAddr & 0xFF); //LSB of register address |
| 190 | + uint8_t temp; |
| 191 | + temp = (data >> 8) & 0xff; |
| 192 | + Wire.write(temp); // Data/setting to be sent to device |
| 193 | + temp = data & 0xff; |
| 194 | + Wire.write(temp); // Data/setting to be sent to device |
| 195 | + Wire.endTransmission(); //Send address and register address bytes |
| 196 | +} |
| 197 | + |
| 198 | + |
0 commit comments