si1145 library

Dependents:   FRDM_ApplicationShield_GroveSensors

Committer:
brdarji
Date:
Sun Jul 03 08:01:08 2016 +0000
Revision:
10:ba32b0098ee8
Parent:
9:daf5ed7c7c3e
si1145 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ngood 0:0490a77adbc5 1
ngood 0:0490a77adbc5 2 #include "mbed.h"
ngood 0:0490a77adbc5 3 #include "SI1145.h"
ngood 0:0490a77adbc5 4
ngood 0:0490a77adbc5 5 SI1145::SI1145(PinName sda, PinName scl, char slave_adr)
ngood 0:0490a77adbc5 6 :
ngood 0:0490a77adbc5 7 i2c_p(new I2C(sda, scl)),
ngood 0:0490a77adbc5 8 i2c(*i2c_p),
ngood 0:0490a77adbc5 9 address(slave_adr),
ngood 0:0490a77adbc5 10 t_fine(0)
ngood 0:0490a77adbc5 11 {
ngood 0:0490a77adbc5 12 initalize();
ngood 0:0490a77adbc5 13 }
ngood 0:0490a77adbc5 14
ngood 0:0490a77adbc5 15 SI1145::SI1145(I2C &i2c_obj, char slave_adr)
ngood 0:0490a77adbc5 16 :
ngood 0:0490a77adbc5 17 i2c_p(NULL),
ngood 0:0490a77adbc5 18 i2c(i2c_obj),
ngood 0:0490a77adbc5 19 address(slave_adr),
ngood 0:0490a77adbc5 20 t_fine(0)
ngood 0:0490a77adbc5 21 {
ngood 0:0490a77adbc5 22 initalize();
ngood 0:0490a77adbc5 23 }
ngood 0:0490a77adbc5 24
ngood 0:0490a77adbc5 25 void SI1145::initalize(void) {
ngood 0:0490a77adbc5 26 uint16_t idu;
ngood 0:0490a77adbc5 27 //i2c.frequency(100000);
ngood 0:0490a77adbc5 28 char id[1] = {0x00};
ngood 0:0490a77adbc5 29 i2c.write(address,id,1);
ngood 0:0490a77adbc5 30 char data[1] = {0};
ngood 0:0490a77adbc5 31 i2c.read(address,data,1);
ngood 0:0490a77adbc5 32 float idf;
ngood 0:0490a77adbc5 33 idu = data[0];
ngood 0:0490a77adbc5 34 idf = (float)idu;
jelord 9:daf5ed7c7c3e 35 // pc.printf("%4.0f ID\n",idf); COMMENTED OUT B/C only one Serial object can exist in main...???
ngood 0:0490a77adbc5 36 // reset______________________________________________
ngood 0:0490a77adbc5 37 //write MEASRATE0
ngood 0:0490a77adbc5 38 char MEASRATE0[2] = {0x08,0x0};
ngood 0:0490a77adbc5 39 i2c.write(address,MEASRATE0,2);
ngood 0:0490a77adbc5 40 //write MEASRATE1
ngood 0:0490a77adbc5 41 char MEASRATE1[2] = {0x09,0x0};
ngood 0:0490a77adbc5 42 i2c.write(address,MEASRATE1,2);
ngood 0:0490a77adbc5 43 //write visQEN
ngood 0:0490a77adbc5 44 char visQEN[2] = {0x04,0x0};
ngood 0:0490a77adbc5 45 i2c.write(address,visQEN,2);
ngood 0:0490a77adbc5 46 // visQMODE1
ngood 0:0490a77adbc5 47 char visQMODE1[2] = {0x05,0x0};
ngood 0:0490a77adbc5 48 i2c.write(address,visQMODE1,2);
ngood 0:0490a77adbc5 49 //write visQMODE2
ngood 0:0490a77adbc5 50 char visQMODE2[2] = {0x06,0x0};
ngood 0:0490a77adbc5 51 i2c.write(address,visQMODE2,2);
ngood 0:0490a77adbc5 52 //write INTCFG
ngood 0:0490a77adbc5 53 char INTCFG[2] = {0x03,0x0};
ngood 0:0490a77adbc5 54 i2c.write(address,INTCFG,2);
ngood 0:0490a77adbc5 55 //write visQSTAT
ngood 0:0490a77adbc5 56 char visQSTAT[2] = {0x21,0xFF};
ngood 0:0490a77adbc5 57 i2c.write(address,visQSTAT,2);
ngood 0:0490a77adbc5 58 //write COMMAND
ngood 0:0490a77adbc5 59 char COMMAND[2] = {0x18,0x01};
ngood 0:0490a77adbc5 60 i2c.write(address,COMMAND,2);
ngood 0:0490a77adbc5 61 // wait
brdarji 10:ba32b0098ee8 62 wait_ms(10);
ngood 0:0490a77adbc5 63 //write HWKEY
ngood 0:0490a77adbc5 64 char HWKEY[2] = {0x07, 0x17};
ngood 0:0490a77adbc5 65 i2c.write(address,HWKEY,2);
ngood 0:0490a77adbc5 66 // wait
brdarji 10:ba32b0098ee8 67 wait_ms(10);
ngood 0:0490a77adbc5 68 //__________________________________________________________
ngood 0:0490a77adbc5 69 // enable UVindex measurement coefficients!
ngood 0:0490a77adbc5 70 char REG_UCOEFF0[2] = {0x13,0x29};
ngood 0:0490a77adbc5 71 i2c.write(address, REG_UCOEFF0, 2);
ngood 0:0490a77adbc5 72 char REG_UCOEFF1[2] = {0x14,0x89};
ngood 0:0490a77adbc5 73 i2c.write(address, REG_UCOEFF1, 2);
ngood 0:0490a77adbc5 74 char REG_UCOEFF2[2] = {0x15,0x02};
ngood 0:0490a77adbc5 75 i2c.write(address, REG_UCOEFF2, 2);
ngood 0:0490a77adbc5 76 char REG_UCOEFF3[2] = {0x16,0x00};
ngood 0:0490a77adbc5 77 i2c.write(address, REG_UCOEFF3, 2);
ngood 0:0490a77adbc5 78 //__________________________________________________________
ngood 0:0490a77adbc5 79
ngood 0:0490a77adbc5 80 // enable UV sensor
ngood 0:0490a77adbc5 81 char PARAM_CHLIST[2] = {0x17, 0x80 | 0x20 | 0x10 | 0x01};
ngood 0:0490a77adbc5 82 i2c.write(address, PARAM_CHLIST, 2);
ngood 0:0490a77adbc5 83 char COMMD_CHLIST[2] = {0x18, 0x01 | 0xA0};
ngood 0:0490a77adbc5 84 i2c.write(address, COMMD_CHLIST, 2);
ngood 0:0490a77adbc5 85 // enable interrupt on every sample
ngood 0:0490a77adbc5 86 char REG_INTCFG[2] = {0x03,0x01};
ngood 0:0490a77adbc5 87 i2c.write(address, REG_INTCFG, 2);
ngood 0:0490a77adbc5 88 char REG_visQEN[2] = {0x04,0x01};
ngood 0:0490a77adbc5 89 i2c.write(address, REG_visQEN, 2);
ngood 0:0490a77adbc5 90 // program proximity sensor LED current
ngood 0:0490a77adbc5 91 char REG_PSLED21[2] = {0x0F,0x03};
ngood 0:0490a77adbc5 92 i2c.write(address, REG_PSLED21, 2); // 20mA for LED 1 only
ngood 0:0490a77adbc5 93 char PARAM_ADCMUX_LARGEvis[2] = {0x17, 0x03};
ngood 0:0490a77adbc5 94 i2c.write(address, PARAM_ADCMUX_LARGEvis, 2);
ngood 0:0490a77adbc5 95 char PARAM_PS1ADCMUX[2] = {0x18, 0x07 | 0xA0};
ngood 0:0490a77adbc5 96 i2c.write(address, PARAM_PS1ADCMUX, 2);
ngood 0:0490a77adbc5 97 // prox sensor #1 uses LED #1
ngood 0:0490a77adbc5 98 char PARAM_PSLED12SEL_PS1LED1[2] = {0x17, 0x01};
ngood 0:0490a77adbc5 99 i2c.write(address, PARAM_PSLED12SEL_PS1LED1, 2);
ngood 0:0490a77adbc5 100 char PARAM_PSLED12SEL[2] = {0x18, 0x02 | 0xA0};
ngood 0:0490a77adbc5 101 i2c.write(address, PARAM_PSLED12SEL, 2);
ngood 0:0490a77adbc5 102 // fastest clocks, clock div 1
ngood 0:0490a77adbc5 103 char PARAM_0[2] = {0x17, 0x0 | 0xA0};
ngood 0:0490a77adbc5 104 i2c.write(address, PARAM_0, 2);
ngood 0:0490a77adbc5 105 char PARAM_PSADCGAIN[2] = {0x18, 0x0B};
ngood 0:0490a77adbc5 106 i2c.write(address, PARAM_PSADCGAIN, 2);
ngood 0:0490a77adbc5 107 // take 511 clocks to measure
ngood 0:0490a77adbc5 108 char PARAM_ADCCOUNTER_511CLK[2] = {0x17, 0x70};
ngood 0:0490a77adbc5 109 i2c.write(address, PARAM_ADCCOUNTER_511CLK, 2);
ngood 0:0490a77adbc5 110 char PARAM_PSADCOUNTER[2] = {0x18, 0x0A | 0xA0};
ngood 0:0490a77adbc5 111 i2c.write(address, PARAM_PSADCOUNTER, 2);
ngood 0:0490a77adbc5 112 // in prox mode, high range
ngood 0:0490a77adbc5 113 char PARAM_PSADCMISC_RANGE_PARAM_PSADCMISC_PSMODE[2] = {0x17, 0x20 | 0x04};
ngood 0:0490a77adbc5 114 i2c.write(address, PARAM_PSADCMISC_RANGE_PARAM_PSADCMISC_PSMODE, 2);
ngood 0:0490a77adbc5 115 char PARAM_PSADCMISC[2] = {0x18, 0x0C | 0xA0};
ngood 0:0490a77adbc5 116 i2c.write(address, PARAM_PSADCMISC, 2);
ngood 0:0490a77adbc5 117 //
ngood 0:0490a77adbc5 118 char PARAM_ADCMUX_SMALLvis[2] = {0x17, 0x00};
ngood 0:0490a77adbc5 119 i2c.write(address, PARAM_ADCMUX_SMALLvis, 2);
ngood 0:0490a77adbc5 120 char PARAM_ALSvisADCMUX[2] = {0x18, 0x0E | 0xA0};
ngood 0:0490a77adbc5 121 i2c.write(address, PARAM_ALSvisADCMUX, 2);
ngood 0:0490a77adbc5 122 // fastest clocks, clock div 1
ngood 0:0490a77adbc5 123 i2c.write(address, PARAM_0, 2);
ngood 0:0490a77adbc5 124 char PARAM_ALSvisADCGAIN[2] = {0x18, 0x1E | 0xA0};
ngood 0:0490a77adbc5 125 i2c.write(address, PARAM_ALSvisADCGAIN, 2);
ngood 0:0490a77adbc5 126 // take 511 clocks to measure
ngood 0:0490a77adbc5 127 i2c.write(address, PARAM_ADCCOUNTER_511CLK, 2);
ngood 0:0490a77adbc5 128 char PARAM_ALSvisADCOUNTER[2] = {0x18, 0x1D | 0xA0};
ngood 0:0490a77adbc5 129 i2c.write(address, PARAM_ALSvisADCOUNTER, 2);
ngood 0:0490a77adbc5 130 // in high range mode
ngood 0:0490a77adbc5 131 char PARAM_ALSvisADCMISC_RANGE[2] = {0x17, 0x20};
ngood 0:0490a77adbc5 132 i2c.write(address, PARAM_ALSvisADCMISC_RANGE, 2);
ngood 0:0490a77adbc5 133 char PARAM_ALSvisADCMISC[2] = {0x18, 0x1F | 0xA0};
ngood 0:0490a77adbc5 134 i2c.write(address, PARAM_ALSvisADCMISC, 2);
ngood 0:0490a77adbc5 135 // fastest clocks, clock div 1
ngood 0:0490a77adbc5 136 char PARAM_000[2] = {0x17, 0x0};
ngood 0:0490a77adbc5 137 i2c.write(address, PARAM_000, 2);
ngood 0:0490a77adbc5 138 char PARAM_ALSuvADCGAIN[2] = {0x18, 0x11 | 0xA0};
ngood 0:0490a77adbc5 139 i2c.write(address, PARAM_ALSuvADCGAIN, 2);
ngood 0:0490a77adbc5 140 // take 511 clocks to measure
ngood 0:0490a77adbc5 141 i2c.write(address, PARAM_ADCCOUNTER_511CLK, 2);
ngood 0:0490a77adbc5 142 char PARAM_ALSuvADCOUNTER[2] = {0x18, 0x10 | 0xA0};
ngood 0:0490a77adbc5 143 i2c.write(address, PARAM_ALSuvADCOUNTER, 2);
ngood 0:0490a77adbc5 144 // in high range mode (not normal signal)
ngood 0:0490a77adbc5 145 char PARAM_ALSuvADCMISC_uvRANGE[2] = {0x17, 0x20};
ngood 0:0490a77adbc5 146 i2c.write(address, PARAM_ALSuvADCMISC_uvRANGE, 2);
ngood 0:0490a77adbc5 147 char PARAM_ALSuvADCMISC[2] = {0x18, 0x12 | 0xA0};
ngood 0:0490a77adbc5 148 i2c.write(address, PARAM_ALSuvADCMISC, 2);
ngood 0:0490a77adbc5 149 // measurement rate for auto
ngood 0:0490a77adbc5 150 char REG_MEASRATE0[2] = {0x08,0xFF};
ngood 0:0490a77adbc5 151 i2c.write(address, REG_MEASRATE0, 2);// 255 * 31.25uS = 8ms
ngood 0:0490a77adbc5 152 // auto run
ngood 0:0490a77adbc5 153 char REG_COMMAND[2] = {0x18,0x0F};
ngood 0:0490a77adbc5 154 i2c.write(address, REG_COMMAND, 2);
ngood 0:0490a77adbc5 155 }
ngood 0:0490a77adbc5 156
caseyquinn 8:4511725f06b2 157 uint16_t SI1145::getUV()
ngood 0:0490a77adbc5 158 {
ngood 0:0490a77adbc5 159 // Variables
caseyquinn 1:8587b5583343 160 //float valf; // uv value
ngood 0:0490a77adbc5 161 char reg[1]; // register
ngood 0:0490a77adbc5 162 char data[2] = {0,0}; // data 2 x 8 bits
ngood 0:0490a77adbc5 163 uint16_t vali; // uv value
ngood 0:0490a77adbc5 164 // Set register
ngood 0:0490a77adbc5 165 reg[0] = 0x2C;
ngood 0:0490a77adbc5 166 // Read registers
ngood 0:0490a77adbc5 167 i2c.write(address, reg, 1);
ngood 0:0490a77adbc5 168 i2c.read(address, data, 2);
ngood 0:0490a77adbc5 169 // Merge bytes
ngood 0:0490a77adbc5 170 vali = data[0] | (data[1] << 8); // int
caseyquinn 1:8587b5583343 171 //valf = (float)vali; // convert to float
ngood 0:0490a77adbc5 172 // Return value
caseyquinn 1:8587b5583343 173 return vali;
ngood 0:0490a77adbc5 174 }
ngood 0:0490a77adbc5 175
caseyquinn 8:4511725f06b2 176 uint16_t SI1145::getVIS()
ngood 0:0490a77adbc5 177 {
ngood 0:0490a77adbc5 178 // Variables
caseyquinn 6:642ece0fad39 179 //float valf; // vis (IR+UV) value
ngood 0:0490a77adbc5 180 char reg[1]; // register
ngood 0:0490a77adbc5 181 char data[2] = {0,0}; // data 2 x 8 bits
caseyquinn 8:4511725f06b2 182 uint16_t vali; // vis value
ngood 0:0490a77adbc5 183 // Set register
ngood 0:0490a77adbc5 184 reg[0] = 0x22;
ngood 0:0490a77adbc5 185 // Read registers
ngood 0:0490a77adbc5 186 i2c.write(address, reg, 1);
ngood 0:0490a77adbc5 187 i2c.read(address, data, 2);
ngood 0:0490a77adbc5 188 // Merge bytes
ngood 0:0490a77adbc5 189 vali = data[0] | (data[1] << 8); // int
caseyquinn 6:642ece0fad39 190 //valf = (float)vali; // convert to float
ngood 0:0490a77adbc5 191 // Return value
caseyquinn 6:642ece0fad39 192 return vali;
ngood 0:0490a77adbc5 193 }
ngood 0:0490a77adbc5 194
caseyquinn 8:4511725f06b2 195 uint16_t SI1145::getIR()
ngood 0:0490a77adbc5 196 {
ngood 0:0490a77adbc5 197 // Variables
caseyquinn 1:8587b5583343 198 //float valf; // ir value
ngood 0:0490a77adbc5 199 char reg[1]; // register
ngood 0:0490a77adbc5 200 char data[2] = {0,0}; // data 2 x 8 bits
caseyquinn 8:4511725f06b2 201 uint16_t vali; // ir value
ngood 0:0490a77adbc5 202 // Set register
ngood 0:0490a77adbc5 203 reg[0] = 0x24;
ngood 0:0490a77adbc5 204 // Read registers
ngood 0:0490a77adbc5 205 i2c.write(address, reg, 1);
ngood 0:0490a77adbc5 206 i2c.read(address, data, 2);
ngood 0:0490a77adbc5 207 // Merge bytes
ngood 0:0490a77adbc5 208 vali = data[0] | (data[1] << 8); // int
caseyquinn 1:8587b5583343 209 //valf = (float)vali; // convert to float
ngood 0:0490a77adbc5 210 // Return value
caseyquinn 1:8587b5583343 211 return vali;
ngood 0:0490a77adbc5 212 }
ngood 0:0490a77adbc5 213
caseyquinn 8:4511725f06b2 214 uint16_t SI1145::getPROX()
ngood 0:0490a77adbc5 215 {
ngood 0:0490a77adbc5 216 // Variables
caseyquinn 1:8587b5583343 217 //float valf; // prox value
ngood 0:0490a77adbc5 218 char reg[1]; // register
ngood 0:0490a77adbc5 219 char data[2] = {0,0}; // data 2 x 8 bits
caseyquinn 8:4511725f06b2 220 uint16_t vali; // prox value
ngood 0:0490a77adbc5 221 // Set register
ngood 0:0490a77adbc5 222 reg[0] = 0x26;
ngood 0:0490a77adbc5 223 // Read registers
ngood 0:0490a77adbc5 224 i2c.write(address, reg, 1);
ngood 0:0490a77adbc5 225 i2c.read(address, data, 2);
ngood 0:0490a77adbc5 226 // Merge bytes
ngood 0:0490a77adbc5 227 vali = data[0] | (data[1] << 8); // int
caseyquinn 1:8587b5583343 228 //valf = (float)vali; // convert to float
ngood 0:0490a77adbc5 229 // Return value
caseyquinn 1:8587b5583343 230 return vali;
ngood 0:0490a77adbc5 231 }
ngood 0:0490a77adbc5 232