1/27/16 updated JJ

Committer:
j_j205
Date:
Wed Jan 27 20:48:49 2016 +0000
Revision:
0:375445d76687
1/27/16 updated JJ

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j_j205 0:375445d76687 1 #include "vl6180x.h"
j_j205 0:375445d76687 2 #include "mbed.h"
j_j205 0:375445d76687 3
j_j205 0:375445d76687 4 VL6180x::VL6180x(I2C &_i2c, char _address): i2c(_i2c), address(_address)
j_j205 0:375445d76687 5 {
j_j205 0:375445d76687 6 address = I2C_SLAVE_DEFAULT_ADDRESS;
j_j205 0:375445d76687 7 }
j_j205 0:375445d76687 8
j_j205 0:375445d76687 9 VL6180x::VL6180x(PinName sda, PinName scl, char _address): i2c(sda, scl), address(_address)
j_j205 0:375445d76687 10 {
j_j205 0:375445d76687 11 i2c.frequency(400000);
j_j205 0:375445d76687 12 address = I2C_SLAVE_DEFAULT_ADDRESS;
j_j205 0:375445d76687 13 }
j_j205 0:375445d76687 14
j_j205 0:375445d76687 15 void VL6180x::initialize()
j_j205 0:375445d76687 16 {
j_j205 0:375445d76687 17
j_j205 0:375445d76687 18 // Mandatory : private registers
j_j205 0:375445d76687 19 setRegister(0x0207, 0x01);
j_j205 0:375445d76687 20 setRegister(0x0208, 0x01);
j_j205 0:375445d76687 21 setRegister(0x0096, 0x00);
j_j205 0:375445d76687 22 setRegister(0x0097, 0xfd);
j_j205 0:375445d76687 23 setRegister(0x00e3, 0x00);
j_j205 0:375445d76687 24 setRegister(0x00e4, 0x04);
j_j205 0:375445d76687 25 setRegister(0x00e5, 0x02);
j_j205 0:375445d76687 26 setRegister(0x00e6, 0x01);
j_j205 0:375445d76687 27 setRegister(0x00e7, 0x03);
j_j205 0:375445d76687 28 setRegister(0x00f5, 0x02);
j_j205 0:375445d76687 29 setRegister(0x00d9, 0x05);
j_j205 0:375445d76687 30 setRegister(0x00db, 0xce);
j_j205 0:375445d76687 31 setRegister(0x00dc, 0x03);
j_j205 0:375445d76687 32 setRegister(0x00dd, 0xf8);
j_j205 0:375445d76687 33 setRegister(0x009f, 0x00);
j_j205 0:375445d76687 34 setRegister(0x00a3, 0x3c);
j_j205 0:375445d76687 35 setRegister(0x00b7, 0x00);
j_j205 0:375445d76687 36 setRegister(0x00bb, 0x3c);
j_j205 0:375445d76687 37 setRegister(0x00b2, 0x09);
j_j205 0:375445d76687 38 setRegister(0x00ca, 0x09);
j_j205 0:375445d76687 39 setRegister(0x0198, 0x01);
j_j205 0:375445d76687 40 setRegister(0x01b0, 0x17);
j_j205 0:375445d76687 41 setRegister(0x01ad, 0x00);
j_j205 0:375445d76687 42 setRegister(0x00ff, 0x05);
j_j205 0:375445d76687 43 setRegister(0x0100, 0x05);
j_j205 0:375445d76687 44 setRegister(0x0199, 0x05);
j_j205 0:375445d76687 45 setRegister(0x01a6, 0x1b);
j_j205 0:375445d76687 46 setRegister(0x01ac, 0x3e);
j_j205 0:375445d76687 47 setRegister(0x01a7, 0x1f);
j_j205 0:375445d76687 48 setRegister(0x0030, 0x00);
j_j205 0:375445d76687 49
j_j205 0:375445d76687 50 // Recommended : Public registers - See data sheet for more detail
j_j205 0:375445d76687 51 setRegister(SYSTEM_MODE_GPIO1, 0x10); // Enables polling for ‘New Sample ready’
j_j205 0:375445d76687 52 // when measurement completes
j_j205 0:375445d76687 53 setRegister(READOUT_AVERAGING_SAMPLE_PERIOD, 0x30); // Set the averaging sample period
j_j205 0:375445d76687 54 // (compromise between lower noise and
j_j205 0:375445d76687 55 // increased execution time)
j_j205 0:375445d76687 56 setRegister(SYSALS_ANALOGUE_GAIN, 0x46); // Sets the light and dark gain (upper
j_j205 0:375445d76687 57 // nibble). Dark gain should not be
j_j205 0:375445d76687 58 // changed.
j_j205 0:375445d76687 59 setRegister(SYSRANGE_VHV_REPEAT_RATE, 0xFF); // sets the # of range measurements after
j_j205 0:375445d76687 60 // which auto calibration of system is
j_j205 0:375445d76687 61 // performed
j_j205 0:375445d76687 62 setRegister(SYSALS_INTEGRATION_PERIOD, 0x63); // Set ALS integration time to 100ms
j_j205 0:375445d76687 63 setRegister(SYSRANGE_VHV_RECALIBRATE, 0x01); // perform a single temperature calibration
j_j205 0:375445d76687 64 // of the ranging sensor
j_j205 0:375445d76687 65
j_j205 0:375445d76687 66
j_j205 0:375445d76687 67 setRegister(SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); // Set default ranging inter-measurement
j_j205 0:375445d76687 68 // period to 100ms
j_j205 0:375445d76687 69 setRegister(SYSALS_INTERMEASUREMENT_PERIOD, 0x31); // Set default ALS inter-measurement period
j_j205 0:375445d76687 70 // to 500ms
j_j205 0:375445d76687 71 setRegister(SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24); // Configures interrupt on ‘New Sample
j_j205 0:375445d76687 72 // Ready threshold event’
j_j205 0:375445d76687 73 }
j_j205 0:375445d76687 74
j_j205 0:375445d76687 75 void VL6180x::startContinuousOperation()
j_j205 0:375445d76687 76 {
j_j205 0:375445d76687 77 setRegister(SYSRANGE_START, 0x03);
j_j205 0:375445d76687 78
j_j205 0:375445d76687 79 }
j_j205 0:375445d76687 80
j_j205 0:375445d76687 81 int VL6180x::getDistance()
j_j205 0:375445d76687 82 {
j_j205 0:375445d76687 83 int distance = 0;
j_j205 0:375445d76687 84 // should test bit 2 of RESULT_INTERRUPT_STATUS_GPIO ?
j_j205 0:375445d76687 85 distance = getRegister(RESULT_RANGE_VAL);
j_j205 0:375445d76687 86 // should clear interrupt ?
j_j205 0:375445d76687 87 return distance;
j_j205 0:375445d76687 88 }
j_j205 0:375445d76687 89
j_j205 0:375445d76687 90 int VL6180x::getSingleDistance()
j_j205 0:375445d76687 91 {
j_j205 0:375445d76687 92 int distance = 0;
j_j205 0:375445d76687 93 while(getRegister(RESULT_RANGE_STATUS) & 0x01 != 0x01);
j_j205 0:375445d76687 94 setRegister(SYSRANGE_START, 0x01); // start measurement
j_j205 0:375445d76687 95 while(getRegister(RESULT_INTERRUPT_STATUS_GPIO) & 0x04 != 0x04);
j_j205 0:375445d76687 96 distance = getRegister(RESULT_RANGE_VAL);
j_j205 0:375445d76687 97 setRegister(SYSTEM_INTERRUPT_CLEAR, 0x07);
j_j205 0:375445d76687 98 return distance;
j_j205 0:375445d76687 99 }
j_j205 0:375445d76687 100
j_j205 0:375445d76687 101 float VL6180x::getAmbientLight()
j_j205 0:375445d76687 102 {
j_j205 0:375445d76687 103 return 0.0f;
j_j205 0:375445d76687 104 }
j_j205 0:375445d76687 105
j_j205 0:375445d76687 106 void VL6180x::setRegister(int reg, int value)
j_j205 0:375445d76687 107 {
j_j205 0:375445d76687 108 char data_write[3];
j_j205 0:375445d76687 109 data_write[0] = ((reg &0xff00 ) >> 8);
j_j205 0:375445d76687 110 data_write[1] = (reg &0x00ff );
j_j205 0:375445d76687 111 data_write[2] = value;
j_j205 0:375445d76687 112 i2c.write(address, data_write, 3);
j_j205 0:375445d76687 113 }
j_j205 0:375445d76687 114
j_j205 0:375445d76687 115 int VL6180x::getRegister(int reg)
j_j205 0:375445d76687 116 {
j_j205 0:375445d76687 117 char data_write[2];
j_j205 0:375445d76687 118 char data_read[1];
j_j205 0:375445d76687 119 data_write[0] = ((reg &0xff00 ) >> 8);
j_j205 0:375445d76687 120 data_write[1] = (reg &0x00ff ) ;
j_j205 0:375445d76687 121
j_j205 0:375445d76687 122 i2c.write(address, data_write, 2, 1); //no stop
j_j205 0:375445d76687 123 i2c.read(address, data_read, 1, 0);
j_j205 0:375445d76687 124
j_j205 0:375445d76687 125 return data_read[0];
j_j205 0:375445d76687 126 }
j_j205 0:375445d76687 127
j_j205 0:375445d76687 128 VL6180x::Identification VL6180x::getIdentification()
j_j205 0:375445d76687 129 {
j_j205 0:375445d76687 130
j_j205 0:375445d76687 131 char data_write[2];
j_j205 0:375445d76687 132 char data_read[10];
j_j205 0:375445d76687 133 data_write[0] = ( (0x00 &0xff00 ) >> 8);
j_j205 0:375445d76687 134 data_write[1] = (0x00 &0x00ff ) ;
j_j205 0:375445d76687 135
j_j205 0:375445d76687 136 i2c.write(address, data_write, 2, 1); //no stop
j_j205 0:375445d76687 137 i2c.read(address, data_read, 10, 0);
j_j205 0:375445d76687 138
j_j205 0:375445d76687 139 Identification id;
j_j205 0:375445d76687 140 id.model = data_read[0];
j_j205 0:375445d76687 141 id.modelRevMajor = data_read[1];
j_j205 0:375445d76687 142 id.modelRevMinor = data_read[2];
j_j205 0:375445d76687 143 id.moduleRevMajor = data_read[3];
j_j205 0:375445d76687 144 id.moduleRevMinor = data_read[4];
j_j205 0:375445d76687 145 id.date = data_read[6] << 8 | data_read[7];
j_j205 0:375445d76687 146 id.time = data_read[8] << 8 | data_read[9];
j_j205 0:375445d76687 147 return id;
j_j205 0:375445d76687 148 }
j_j205 0:375445d76687 149
j_j205 0:375445d76687 150 void VL6180x::printIdentification(VL6180x::Identification id)
j_j205 0:375445d76687 151 {
j_j205 0:375445d76687 152 printf("VL6180x Identification:\r\n");
j_j205 0:375445d76687 153 printf("Model id: 0x%02X\r\n", id.model);
j_j205 0:375445d76687 154 printf("Model rev: %d.%d\r\n", id.modelRevMajor, id.modelRevMinor);
j_j205 0:375445d76687 155 printf("Module rev: %d.%d\r\n", id.moduleRevMajor, id.modelRevMajor);
j_j205 0:375445d76687 156 printf("Date: %04d-%02d-%02d (%d)\r\n", ((id.date >> 12) & 0x0F) + 2010, (id.date >> 8) & 0x0F, (id.date >> 3) & 0x1F, id.date & 0x07);
j_j205 0:375445d76687 157 printf("Time: %02d:%02d:%02d\r\n", (id.time / 60*60) % 24, (id.time / 60) % 60, id.time % 60);
j_j205 0:375445d76687 158 }
j_j205 0:375445d76687 159
j_j205 0:375445d76687 160 void VL6180x::setAddress(int address)
j_j205 0:375445d76687 161 {
j_j205 0:375445d76687 162 char data[3];
j_j205 0:375445d76687 163 data[0] = I2C_SLAVE_DEVICE_ADDRESS / 256;
j_j205 0:375445d76687 164 data[1] = I2C_SLAVE_DEVICE_ADDRESS % 256;
j_j205 0:375445d76687 165 data[2] = address >> 1;
j_j205 0:375445d76687 166 i2c.write(address, data, 3); //no stop
j_j205 0:375445d76687 167 }