IUT CACHAN pour Yves GUINAND

Fork of VL6180 by Steven Burg

Committer:
sburg
Date:
Mon Oct 19 16:19:05 2015 +0000
Revision:
0:f23c00f688b2
Child:
3:5d61f202b1bd
VL6180 first revision.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sburg 0:f23c00f688b2 1 #include "VL6180.h"
sburg 0:f23c00f688b2 2 #include "mbed.h"
sburg 0:f23c00f688b2 3
sburg 0:f23c00f688b2 4 VL6180::VL6180(PinName sda, PinName scl) : _i2c(sda, scl) {
sburg 0:f23c00f688b2 5 char poweron;
sburg 0:f23c00f688b2 6 poweron = readI2C(0x16);
sburg 0:f23c00f688b2 7 if(poweron == 1) {
sburg 0:f23c00f688b2 8 //mandatory initialization
sburg 0:f23c00f688b2 9 writeI2C(0x0207,0x01);
sburg 0:f23c00f688b2 10 writeI2C(0x0208,0x01);
sburg 0:f23c00f688b2 11 writeI2C(0x0096,0x00);
sburg 0:f23c00f688b2 12 writeI2C(0x0097,0xfd);
sburg 0:f23c00f688b2 13 writeI2C(0x00e3,0x00);
sburg 0:f23c00f688b2 14 writeI2C(0x00e4,0x04);
sburg 0:f23c00f688b2 15 writeI2C(0x00e5,0x02);
sburg 0:f23c00f688b2 16 writeI2C(0x00e6,0x01);
sburg 0:f23c00f688b2 17 writeI2C(0x00e7,0x03);
sburg 0:f23c00f688b2 18 writeI2C(0x00f5,0x02);
sburg 0:f23c00f688b2 19 writeI2C(0x00d9,0x05);
sburg 0:f23c00f688b2 20 writeI2C(0x00db,0xce);
sburg 0:f23c00f688b2 21 writeI2C(0x00dc,0x03);
sburg 0:f23c00f688b2 22 writeI2C(0x00dd,0xf8);
sburg 0:f23c00f688b2 23 writeI2C(0x009f,0x00);
sburg 0:f23c00f688b2 24 writeI2C(0x00a3,0x3c);
sburg 0:f23c00f688b2 25 writeI2C(0x00b7,0x00);
sburg 0:f23c00f688b2 26 writeI2C(0x00bb,0x3c);
sburg 0:f23c00f688b2 27 writeI2C(0x00b2,0x09);
sburg 0:f23c00f688b2 28 writeI2C(0x00ca,0x09);
sburg 0:f23c00f688b2 29 writeI2C(0x0198,0x01);
sburg 0:f23c00f688b2 30 writeI2C(0x01b0,0x17);
sburg 0:f23c00f688b2 31 writeI2C(0x01ad,0x00);
sburg 0:f23c00f688b2 32 writeI2C(0x00ff,0x05);
sburg 0:f23c00f688b2 33 writeI2C(0x0100,0x05);
sburg 0:f23c00f688b2 34 writeI2C(0x0199,0x05);
sburg 0:f23c00f688b2 35 writeI2C(0x01a6,0x1b);
sburg 0:f23c00f688b2 36 writeI2C(0x01ac,0x3e);
sburg 0:f23c00f688b2 37 writeI2C(0x01a7,0x1f);
sburg 0:f23c00f688b2 38 writeI2C(0x0030,0x00);
sburg 0:f23c00f688b2 39 //recommended initialization
sburg 0:f23c00f688b2 40 writeI2C(0x0011,0x10);// Enables polling for ‘New Sample ready’ when measurement completes
sburg 0:f23c00f688b2 41 writeI2C(0x010a,0x30);// Set the averaging sample period (compromise between lower noise and increased execution time)
sburg 0:f23c00f688b2 42 writeI2C(0x003f,0x46);// Sets the light and dark gain (upper nibble). Dark gain should not be changed.
sburg 0:f23c00f688b2 43 writeI2C(0x0031,0xFF);// sets the # of range measurements after which auto calibration of system is performed
sburg 0:f23c00f688b2 44 writeI2C(0x0040,0x63);// Set ALS integration time to 100ms
sburg 0:f23c00f688b2 45 writeI2C(0x002e,0x01);// perform a single temperature calibration of the ranging sensor
sburg 0:f23c00f688b2 46 //optional initialization
sburg 0:f23c00f688b2 47 writeI2C(0x001b,0x09);// Set default ranging inter-measurement period to 100ms
sburg 0:f23c00f688b2 48 writeI2C(0x003e,0x31);// Set default ALS inter-measurement period to 500ms
sburg 0:f23c00f688b2 49 writeI2C(0x0014,0x24);// Configures interrupt on ‘New Sample Ready threshold event’
sburg 0:f23c00f688b2 50
sburg 0:f23c00f688b2 51 writeI2C(0x016,0x00);//change fresh out of set status to 0
sburg 0:f23c00f688b2 52 }
sburg 0:f23c00f688b2 53 }
sburg 0:f23c00f688b2 54
sburg 0:f23c00f688b2 55 float VL6180::read() {
sburg 0:f23c00f688b2 56 char status;
sburg 0:f23c00f688b2 57 char retn;
sburg 0:f23c00f688b2 58
sburg 0:f23c00f688b2 59 writeI2C(0x18, 0x01);
sburg 0:f23c00f688b2 60
sburg 0:f23c00f688b2 61 status = readI2C(0x4F);
sburg 0:f23c00f688b2 62 while((status & 0x2) != 4) {
sburg 0:f23c00f688b2 63 status = readI2C(0x4F);
sburg 0:f23c00f688b2 64 }
sburg 0:f23c00f688b2 65
sburg 0:f23c00f688b2 66 retn = readI2C(0x62);
sburg 0:f23c00f688b2 67
sburg 0:f23c00f688b2 68 writeI2C(0x15, 0x07);
sburg 0:f23c00f688b2 69
sburg 0:f23c00f688b2 70 return (float)retn / 10.0;
sburg 0:f23c00f688b2 71 }
sburg 0:f23c00f688b2 72
sburg 0:f23c00f688b2 73 VL6180::operator float() {
sburg 0:f23c00f688b2 74 return read();
sburg 0:f23c00f688b2 75 }
sburg 0:f23c00f688b2 76
sburg 0:f23c00f688b2 77
sburg 0:f23c00f688b2 78 void VL6180::writeI2C(int reg, char data) {
sburg 0:f23c00f688b2 79 char dataout[3];
sburg 0:f23c00f688b2 80
sburg 0:f23c00f688b2 81 dataout[0] = (reg >> 8) & 0xFF;
sburg 0:f23c00f688b2 82 dataout[1] = reg & 0xFF;
sburg 0:f23c00f688b2 83 dataout[2] = data & 0xFF;
sburg 0:f23c00f688b2 84
sburg 0:f23c00f688b2 85 _i2c.write(_addr, dataout, 3);
sburg 0:f23c00f688b2 86 }
sburg 0:f23c00f688b2 87
sburg 0:f23c00f688b2 88 char VL6180::readI2C(int reg) {
sburg 0:f23c00f688b2 89 char dataout[2];
sburg 0:f23c00f688b2 90 char datain[1];
sburg 0:f23c00f688b2 91
sburg 0:f23c00f688b2 92 dataout[0] = (reg >> 8) & 0xFF;
sburg 0:f23c00f688b2 93 dataout[1] = reg & 0xFF;
sburg 0:f23c00f688b2 94
sburg 0:f23c00f688b2 95 _i2c.write(_addr, dataout, 2);
sburg 0:f23c00f688b2 96 _i2c.read(_addr, datain, 1);
sburg 0:f23c00f688b2 97
sburg 0:f23c00f688b2 98 return datain[0];
sburg 0:f23c00f688b2 99 }
sburg 0:f23c00f688b2 100