Driver for Proximity / Ambient Light Sensor VCNL40xx from Vishay

Dependencies:   mbed

Committer:
kokisan2000
Date:
Fri Jul 04 21:31:19 2014 +0000
Revision:
1:cf336a5443e3
Parent:
0:1f3d09f0060a
insert pins for second I2C bus

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kokisan2000 0:1f3d09f0060a 1 /*
kokisan2000 0:1f3d09f0060a 2 Copyright (c) 2012 Vishay GmbH, www.vishay.com
kokisan2000 0:1f3d09f0060a 3 author: DS, version 1.2
kokisan2000 0:1f3d09f0060a 4
kokisan2000 0:1f3d09f0060a 5 Permission is hereby granted, free of charge, to any person obtaining a copy
kokisan2000 0:1f3d09f0060a 6 of this software and associated documentation files (the "Software"), to deal
kokisan2000 0:1f3d09f0060a 7 in the Software without restriction, including without limitation the rights
kokisan2000 0:1f3d09f0060a 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
kokisan2000 0:1f3d09f0060a 9 copies of the Software, and to permit persons to whom the Software is
kokisan2000 0:1f3d09f0060a 10 furnished to do so, subject to the following conditions:
kokisan2000 0:1f3d09f0060a 11
kokisan2000 0:1f3d09f0060a 12 The above copyright notice and this permission notice shall be included in
kokisan2000 0:1f3d09f0060a 13 all copies or substantial portions of the Software.
kokisan2000 0:1f3d09f0060a 14
kokisan2000 0:1f3d09f0060a 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
kokisan2000 0:1f3d09f0060a 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
kokisan2000 0:1f3d09f0060a 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
kokisan2000 0:1f3d09f0060a 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
kokisan2000 0:1f3d09f0060a 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kokisan2000 0:1f3d09f0060a 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
kokisan2000 0:1f3d09f0060a 21 THE SOFTWARE.
kokisan2000 0:1f3d09f0060a 22 */
kokisan2000 0:1f3d09f0060a 23
kokisan2000 0:1f3d09f0060a 24 #define VERSION "\n Version: 1.2 01/2012\n"
kokisan2000 0:1f3d09f0060a 25 #define MAIN1 // select MAIN1, MAIN2, MAIN3 or MIAN4
kokisan2000 0:1f3d09f0060a 26 #define BAUD 115200 // increase up to 921600 for high speed communication (depends on terminal programm and USB mode)
kokisan2000 0:1f3d09f0060a 27
kokisan2000 0:1f3d09f0060a 28 #include "mbed.h"
kokisan2000 0:1f3d09f0060a 29 #include "VCNL40x0.h"
kokisan2000 0:1f3d09f0060a 30
kokisan2000 1:cf336a5443e3 31 //VCNL40x0 VCNL40x0_Device (p28, p27, VCNL40x0_ADDRESS); // Define SDA, SCL pin and I2C address
kokisan2000 1:cf336a5443e3 32 VCNL40x0 VCNL40x0_Device (p9, p10, VCNL40x0_ADDRESS); // Define SDA, SCL pin and I2C address
kokisan2000 0:1f3d09f0060a 33 DigitalOut mled0(LED1); // LED #1
kokisan2000 0:1f3d09f0060a 34 DigitalOut mled1(LED2); // LED #2
kokisan2000 0:1f3d09f0060a 35 DigitalOut mled2(LED3); // LED #3
kokisan2000 0:1f3d09f0060a 36 Serial pc(USBTX, USBRX); // Tx, Rx USB transmission
kokisan2000 0:1f3d09f0060a 37
kokisan2000 0:1f3d09f0060a 38 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 39 // main #1
kokisan2000 0:1f3d09f0060a 40 // Read Proximity on demand and Ambillight on demand in endless loop
kokisan2000 0:1f3d09f0060a 41 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 42 # ifdef MAIN1
kokisan2000 0:1f3d09f0060a 43
kokisan2000 0:1f3d09f0060a 44 int main() {
kokisan2000 0:1f3d09f0060a 45 unsigned char ID=0;
kokisan2000 0:1f3d09f0060a 46 unsigned char Current=0;
kokisan2000 0:1f3d09f0060a 47 unsigned int ProxiValue=0;
kokisan2000 0:1f3d09f0060a 48 unsigned int AmbiValue=0;
kokisan2000 0:1f3d09f0060a 49
kokisan2000 0:1f3d09f0060a 50 pc.baud(BAUD); // set USB speed (virtual COM port)
kokisan2000 0:1f3d09f0060a 51
kokisan2000 0:1f3d09f0060a 52 // print information on screen
kokisan2000 0:1f3d09f0060a 53 pc.printf("\n\n VCNL4010/4020 Proximity/Ambient Light Sensor");
kokisan2000 0:1f3d09f0060a 54 pc.printf("\n library tested with mbed LPC1768 (ARM Cortex-M3 core) on www.mbed.org");
kokisan2000 0:1f3d09f0060a 55 pc.printf(VERSION);
kokisan2000 0:1f3d09f0060a 56 pc.printf("\n Demonstration #1:");
kokisan2000 0:1f3d09f0060a 57 pc.printf("\n Read Proximity on demand and Ambillight on demand in endless loop");
kokisan2000 0:1f3d09f0060a 58
kokisan2000 0:1f3d09f0060a 59 VCNL40x0_Device.ReadID (&ID); // Read VCNL40x0 product ID revision register
kokisan2000 0:1f3d09f0060a 60 pc.printf("\n\n Product ID Revision Register: %d", ID);
kokisan2000 0:1f3d09f0060a 61
kokisan2000 0:1f3d09f0060a 62 VCNL40x0_Device.SetCurrent (20); // Set current to 200mA
kokisan2000 0:1f3d09f0060a 63 VCNL40x0_Device.ReadCurrent (&Current); // Read back IR LED current
kokisan2000 0:1f3d09f0060a 64 pc.printf("\n IR LED Current: %d\n\n", Current);
kokisan2000 0:1f3d09f0060a 65
kokisan2000 0:1f3d09f0060a 66 wait_ms(3000); // wait 3s (only for display)
kokisan2000 0:1f3d09f0060a 67
kokisan2000 0:1f3d09f0060a 68 // endless loop /////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 69
kokisan2000 0:1f3d09f0060a 70 while (1) {
kokisan2000 0:1f3d09f0060a 71 mled0 = 1; // LED on
kokisan2000 0:1f3d09f0060a 72 VCNL40x0_Device.ReadProxiOnDemand (&ProxiValue); // read prox value on demand
kokisan2000 0:1f3d09f0060a 73 VCNL40x0_Device.ReadAmbiOnDemand (&AmbiValue); // read ambi value on demand
kokisan2000 0:1f3d09f0060a 74 mled0 = 0; // LED off
kokisan2000 0:1f3d09f0060a 75
kokisan2000 0:1f3d09f0060a 76 // pront values on screen
kokisan2000 0:1f3d09f0060a 77 pc.printf("\nProxi: %5.0i cts \tAmbi: %5.0i cts \tIlluminance: %7.2f lx", ProxiValue, AmbiValue, AmbiValue/4.0);
kokisan2000 0:1f3d09f0060a 78 }
kokisan2000 0:1f3d09f0060a 79 }
kokisan2000 0:1f3d09f0060a 80 # endif
kokisan2000 0:1f3d09f0060a 81
kokisan2000 0:1f3d09f0060a 82 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 83 // main #2
kokisan2000 0:1f3d09f0060a 84 // Proximity Measurement in selfetimed mode with 4 measurements/s
kokisan2000 0:1f3d09f0060a 85 // Read prox value if ready with conversion, endless loop
kokisan2000 0:1f3d09f0060a 86 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 87 # ifdef MAIN2
kokisan2000 0:1f3d09f0060a 88
kokisan2000 0:1f3d09f0060a 89 int main() {
kokisan2000 0:1f3d09f0060a 90 unsigned char ID=0;
kokisan2000 0:1f3d09f0060a 91 unsigned char Command=0;
kokisan2000 0:1f3d09f0060a 92 unsigned char Current=0;
kokisan2000 0:1f3d09f0060a 93 unsigned int ProxiValue=0;
kokisan2000 0:1f3d09f0060a 94 unsigned int AmbiValue=0;
kokisan2000 0:1f3d09f0060a 95
kokisan2000 0:1f3d09f0060a 96 pc.baud(BAUD);
kokisan2000 0:1f3d09f0060a 97
kokisan2000 0:1f3d09f0060a 98 // print information on screen
kokisan2000 0:1f3d09f0060a 99 pc.printf("\n\n VCNL4010/4020 Proximity/Ambient Light Sensor");
kokisan2000 0:1f3d09f0060a 100 pc.printf("\n library tested with mbed LPC1768 (ARM Cortex-M3 core) on www.mbed.org");
kokisan2000 0:1f3d09f0060a 101 pc.printf(VERSION);
kokisan2000 0:1f3d09f0060a 102 pc.printf("\n Demonstration #2:");
kokisan2000 0:1f3d09f0060a 103 pc.printf("\n Proximity Measurement in selftimed mode with 4 measurements/s");
kokisan2000 0:1f3d09f0060a 104 pc.printf("\n Read prox value if ready with conversion, endless loop");
kokisan2000 0:1f3d09f0060a 105
kokisan2000 0:1f3d09f0060a 106 VCNL40x0_Device.ReadID (&ID); // Read VCNL40x0 product ID revision register
kokisan2000 0:1f3d09f0060a 107 pc.printf("\n\n Product ID Revision Register: %d", ID);
kokisan2000 0:1f3d09f0060a 108
kokisan2000 0:1f3d09f0060a 109 VCNL40x0_Device.SetCurrent (20); // Set current to 200mA
kokisan2000 0:1f3d09f0060a 110 VCNL40x0_Device.ReadCurrent (&Current); // Read back IR LED current
kokisan2000 0:1f3d09f0060a 111 pc.printf("\n IR LED Current: %d\n\n", Current);
kokisan2000 0:1f3d09f0060a 112
kokisan2000 0:1f3d09f0060a 113 VCNL40x0_Device.SetProximityRate (PROX_MEASUREMENT_RATE_4); // set proximity rate to 4/s
kokisan2000 0:1f3d09f0060a 114
kokisan2000 0:1f3d09f0060a 115 // enable prox in selftimed mode
kokisan2000 0:1f3d09f0060a 116 VCNL40x0_Device.SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_SELFTIMED_MODE_ENABLE);
kokisan2000 0:1f3d09f0060a 117
kokisan2000 0:1f3d09f0060a 118 wait_ms(3000); // wait 3s (only for display)
kokisan2000 0:1f3d09f0060a 119
kokisan2000 0:1f3d09f0060a 120 // endless loop /////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 121
kokisan2000 0:1f3d09f0060a 122 while (1) {
kokisan2000 0:1f3d09f0060a 123
kokisan2000 0:1f3d09f0060a 124 // wait on prox data ready bit
kokisan2000 0:1f3d09f0060a 125 do {
kokisan2000 0:1f3d09f0060a 126 VCNL40x0_Device.ReadCommandRegister (&Command); // read command register
kokisan2000 0:1f3d09f0060a 127 } while (!(Command & COMMAND_MASK_PROX_DATA_READY));// prox data ready ?
kokisan2000 0:1f3d09f0060a 128
kokisan2000 0:1f3d09f0060a 129 mled0 = 1; // LED on
kokisan2000 0:1f3d09f0060a 130 VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value
kokisan2000 0:1f3d09f0060a 131 mled0 = 0; // LED off
kokisan2000 0:1f3d09f0060a 132
kokisan2000 0:1f3d09f0060a 133 // print values on screen
kokisan2000 0:1f3d09f0060a 134 pc.printf("\nProxi: %5.0i cts", ProxiValue);
kokisan2000 0:1f3d09f0060a 135 }
kokisan2000 0:1f3d09f0060a 136 }
kokisan2000 0:1f3d09f0060a 137 # endif
kokisan2000 0:1f3d09f0060a 138
kokisan2000 0:1f3d09f0060a 139 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 140 // main #3
kokisan2000 0:1f3d09f0060a 141 // Proximity Measurement in selfetimed mode with 31 measurements/s
kokisan2000 0:1f3d09f0060a 142 // Interrupt waiting on proximity value > upper threshold limit
kokisan2000 0:1f3d09f0060a 143 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 144 # ifdef MAIN3
kokisan2000 0:1f3d09f0060a 145
kokisan2000 0:1f3d09f0060a 146 int main() {
kokisan2000 0:1f3d09f0060a 147 unsigned char ID=0;
kokisan2000 0:1f3d09f0060a 148 unsigned char Command=0;
kokisan2000 0:1f3d09f0060a 149 unsigned char Current=0;
kokisan2000 0:1f3d09f0060a 150 unsigned int ProxiValue=0;
kokisan2000 0:1f3d09f0060a 151 unsigned int AmbiValue=0;
kokisan2000 0:1f3d09f0060a 152 unsigned char InterruptStatus=0;
kokisan2000 0:1f3d09f0060a 153 unsigned char InterruptControl=0;
kokisan2000 0:1f3d09f0060a 154
kokisan2000 0:1f3d09f0060a 155 pc.baud(BAUD);
kokisan2000 0:1f3d09f0060a 156
kokisan2000 0:1f3d09f0060a 157 // print information on screen
kokisan2000 0:1f3d09f0060a 158 pc.printf("\n\n VCNL4010/4020 Proximity/Ambient Light Sensor");
kokisan2000 0:1f3d09f0060a 159 pc.printf("\n library tested with mbed LPC1768 (ARM Cortex-M3 core) on www.mbed.org");
kokisan2000 0:1f3d09f0060a 160 pc.printf(VERSION);
kokisan2000 0:1f3d09f0060a 161 pc.printf("\n Demonstration #3:");
kokisan2000 0:1f3d09f0060a 162 pc.printf("\n Proximity Measurement in selfetimed mode with 31 measurements/s");
kokisan2000 0:1f3d09f0060a 163 pc.printf("\n Interrupt waiting on proximity value > upper threshold limit");
kokisan2000 0:1f3d09f0060a 164
kokisan2000 0:1f3d09f0060a 165 VCNL40x0_Device.ReadID (&ID); // Read VCNL40x0 product ID revision register
kokisan2000 0:1f3d09f0060a 166 pc.printf("\n\n Product ID Revision Register: %d", ID);
kokisan2000 0:1f3d09f0060a 167
kokisan2000 0:1f3d09f0060a 168 VCNL40x0_Device.SetCurrent (20); // Set current to 200mA
kokisan2000 0:1f3d09f0060a 169 VCNL40x0_Device.ReadCurrent (&Current); // Read back IR LED current
kokisan2000 0:1f3d09f0060a 170 pc.printf("\n IR LED Current: %d\n\n", Current);
kokisan2000 0:1f3d09f0060a 171
kokisan2000 0:1f3d09f0060a 172 // stop all activities (necessary for changing proximity rate, see datasheet)
kokisan2000 0:1f3d09f0060a 173 VCNL40x0_Device.SetCommandRegister (COMMAND_ALL_DISABLE);
kokisan2000 0:1f3d09f0060a 174
kokisan2000 0:1f3d09f0060a 175 // set proximity rate to 31/s
kokisan2000 0:1f3d09f0060a 176 VCNL40x0_Device.SetProximityRate (PROX_MEASUREMENT_RATE_31);
kokisan2000 0:1f3d09f0060a 177
kokisan2000 0:1f3d09f0060a 178 // enable prox in selftimed mode
kokisan2000 0:1f3d09f0060a 179 VCNL40x0_Device.SetCommandRegister (COMMAND_PROX_ENABLE |
kokisan2000 0:1f3d09f0060a 180 COMMAND_SELFTIMED_MODE_ENABLE);
kokisan2000 0:1f3d09f0060a 181
kokisan2000 0:1f3d09f0060a 182 // set interrupt control register
kokisan2000 0:1f3d09f0060a 183 VCNL40x0_Device.SetInterruptControl (INTERRUPT_THRES_SEL_PROX |
kokisan2000 0:1f3d09f0060a 184 INTERRUPT_THRES_ENABLE |
kokisan2000 0:1f3d09f0060a 185 INTERRUPT_COUNT_EXCEED_1);
kokisan2000 0:1f3d09f0060a 186
kokisan2000 0:1f3d09f0060a 187 VCNL40x0_Device.ReadInterruptControl (&InterruptControl); // Read back Interrupt Control register
kokisan2000 0:1f3d09f0060a 188 pc.printf("\n Interrupt Control Register: %i\n\n", InterruptControl);
kokisan2000 0:1f3d09f0060a 189
kokisan2000 0:1f3d09f0060a 190
kokisan2000 0:1f3d09f0060a 191 // wait on prox data ready bit
kokisan2000 0:1f3d09f0060a 192 do {
kokisan2000 0:1f3d09f0060a 193 VCNL40x0_Device.ReadCommandRegister (&Command); // read command register
kokisan2000 0:1f3d09f0060a 194 } while (!(Command & COMMAND_MASK_PROX_DATA_READY)); // prox data ready ?
kokisan2000 0:1f3d09f0060a 195
kokisan2000 0:1f3d09f0060a 196 VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value
kokisan2000 0:1f3d09f0060a 197 VCNL40x0_Device.SetHighThreshold (ProxiValue+100); // set high threshold for interrupt
kokisan2000 0:1f3d09f0060a 198
kokisan2000 0:1f3d09f0060a 199 wait_ms(3000); // wait 3s (only for display)
kokisan2000 0:1f3d09f0060a 200
kokisan2000 0:1f3d09f0060a 201 // endless loop /////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 202
kokisan2000 0:1f3d09f0060a 203 while (1) {
kokisan2000 0:1f3d09f0060a 204
kokisan2000 0:1f3d09f0060a 205 // wait on prox data ready bit
kokisan2000 0:1f3d09f0060a 206 do {
kokisan2000 0:1f3d09f0060a 207 VCNL40x0_Device.ReadCommandRegister (&Command); // read command register
kokisan2000 0:1f3d09f0060a 208 } while (!(Command & COMMAND_MASK_PROX_DATA_READY));// prox data ready ?
kokisan2000 0:1f3d09f0060a 209
kokisan2000 0:1f3d09f0060a 210 mled0 = 1; // LED on
kokisan2000 0:1f3d09f0060a 211 VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value
kokisan2000 0:1f3d09f0060a 212 mled0 = 0; // LED off
kokisan2000 0:1f3d09f0060a 213
kokisan2000 0:1f3d09f0060a 214 // read interrupt status register
kokisan2000 0:1f3d09f0060a 215 VCNL40x0_Device.ReadInterruptStatus (&InterruptStatus);
kokisan2000 0:1f3d09f0060a 216
kokisan2000 0:1f3d09f0060a 217 // print prox value and interrupt status on screen
kokisan2000 0:1f3d09f0060a 218 pc.printf("\nProxi: %5.0i cts \tInterruptStatus: %i", ProxiValue, InterruptStatus);
kokisan2000 0:1f3d09f0060a 219
kokisan2000 0:1f3d09f0060a 220 // check interrupt status for High Threshold
kokisan2000 0:1f3d09f0060a 221 if (InterruptStatus & INTERRUPT_MASK_STATUS_THRES_HI) {
kokisan2000 0:1f3d09f0060a 222 mled1 = 1; // LED on
kokisan2000 0:1f3d09f0060a 223 VCNL40x0_Device.SetInterruptStatus (InterruptStatus); // clear Interrupt Status
kokisan2000 0:1f3d09f0060a 224 mled1 = 0; // LED on
kokisan2000 0:1f3d09f0060a 225 }
kokisan2000 0:1f3d09f0060a 226 }
kokisan2000 0:1f3d09f0060a 227 }
kokisan2000 0:1f3d09f0060a 228 # endif
kokisan2000 0:1f3d09f0060a 229
kokisan2000 0:1f3d09f0060a 230 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 231 // main #4
kokisan2000 0:1f3d09f0060a 232 // Proximity Measurement and Ambientlight Measurement in selftimed mode
kokisan2000 0:1f3d09f0060a 233 // Proximity with 31 measurements/s, Ambientlight with 2 measurement/s
kokisan2000 0:1f3d09f0060a 234 // Interrupt waiting on proximity value > upper threshold limit
kokisan2000 0:1f3d09f0060a 235 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 236 # ifdef MAIN4
kokisan2000 0:1f3d09f0060a 237
kokisan2000 0:1f3d09f0060a 238 int main() {
kokisan2000 0:1f3d09f0060a 239 unsigned int i=0;
kokisan2000 0:1f3d09f0060a 240 unsigned char ID=0;
kokisan2000 0:1f3d09f0060a 241 unsigned char Command=0;
kokisan2000 0:1f3d09f0060a 242 unsigned char Current=0;
kokisan2000 0:1f3d09f0060a 243 unsigned int ProxiValue=0;
kokisan2000 0:1f3d09f0060a 244 unsigned int SummeProxiValue=0;
kokisan2000 0:1f3d09f0060a 245 unsigned int AverageProxiValue=0;
kokisan2000 0:1f3d09f0060a 246 unsigned int AmbiValue=0;
kokisan2000 0:1f3d09f0060a 247 unsigned char InterruptStatus=0;
kokisan2000 0:1f3d09f0060a 248 unsigned char InterruptControl=0;
kokisan2000 0:1f3d09f0060a 249
kokisan2000 0:1f3d09f0060a 250 pc.baud(BAUD);
kokisan2000 0:1f3d09f0060a 251
kokisan2000 0:1f3d09f0060a 252 // print information on screen
kokisan2000 0:1f3d09f0060a 253 pc.printf("\n\n VCNL4010/4020 Proximity/Ambient Light Sensor");
kokisan2000 0:1f3d09f0060a 254 pc.printf("\n library tested with mbed LPC1768 (ARM Cortex-M3 core) on www.mbed.org");
kokisan2000 0:1f3d09f0060a 255 pc.printf(VERSION);
kokisan2000 0:1f3d09f0060a 256 pc.printf("\n Demonstration #4:");
kokisan2000 0:1f3d09f0060a 257 pc.printf("\n Proximity Measurement and Ambient light Measurement in selftimed mode");
kokisan2000 0:1f3d09f0060a 258 pc.printf("\n Proximity with 31 measurements/s, Ambient light with 2 measurement/s");
kokisan2000 0:1f3d09f0060a 259 pc.printf("\n Interrupt waiting on proximity value > upper threshold limit");
kokisan2000 0:1f3d09f0060a 260
kokisan2000 0:1f3d09f0060a 261 VCNL40x0_Device.ReadID (&ID); // Read VCNL40x0 product ID revision register
kokisan2000 0:1f3d09f0060a 262 pc.printf("\n\n Product ID Revision Register: %d", ID);
kokisan2000 0:1f3d09f0060a 263
kokisan2000 0:1f3d09f0060a 264 VCNL40x0_Device.SetCurrent (20); // Set current to 200mA
kokisan2000 0:1f3d09f0060a 265 VCNL40x0_Device.ReadCurrent (&Current); // Read back IR LED current
kokisan2000 0:1f3d09f0060a 266 pc.printf("\n IR LED Current: %d", Current);
kokisan2000 0:1f3d09f0060a 267
kokisan2000 0:1f3d09f0060a 268 // stop all activities (necessary for changing proximity rate, see datasheet)
kokisan2000 0:1f3d09f0060a 269 VCNL40x0_Device.SetCommandRegister (COMMAND_ALL_DISABLE);
kokisan2000 0:1f3d09f0060a 270
kokisan2000 0:1f3d09f0060a 271 // set proximity rate to 31/s
kokisan2000 0:1f3d09f0060a 272 VCNL40x0_Device.SetProximityRate (PROX_MEASUREMENT_RATE_31);
kokisan2000 0:1f3d09f0060a 273
kokisan2000 0:1f3d09f0060a 274 // enable prox and ambi in selftimed mode
kokisan2000 0:1f3d09f0060a 275 VCNL40x0_Device.SetCommandRegister (COMMAND_PROX_ENABLE |
kokisan2000 0:1f3d09f0060a 276 COMMAND_AMBI_ENABLE |
kokisan2000 0:1f3d09f0060a 277 COMMAND_SELFTIMED_MODE_ENABLE);
kokisan2000 0:1f3d09f0060a 278
kokisan2000 0:1f3d09f0060a 279 // set interrupt control for threshold
kokisan2000 0:1f3d09f0060a 280 VCNL40x0_Device.SetInterruptControl (INTERRUPT_THRES_SEL_PROX |
kokisan2000 0:1f3d09f0060a 281 INTERRUPT_THRES_ENABLE |
kokisan2000 0:1f3d09f0060a 282 INTERRUPT_COUNT_EXCEED_1);
kokisan2000 0:1f3d09f0060a 283
kokisan2000 0:1f3d09f0060a 284 // set ambient light measurement parameter
kokisan2000 0:1f3d09f0060a 285 VCNL40x0_Device.SetAmbiConfiguration (AMBI_PARA_AVERAGE_32 |
kokisan2000 0:1f3d09f0060a 286 AMBI_PARA_AUTO_OFFSET_ENABLE |
kokisan2000 0:1f3d09f0060a 287 AMBI_PARA_MEAS_RATE_2);
kokisan2000 0:1f3d09f0060a 288
kokisan2000 0:1f3d09f0060a 289 // measure average of prox value
kokisan2000 0:1f3d09f0060a 290 SummeProxiValue = 0;
kokisan2000 0:1f3d09f0060a 291
kokisan2000 0:1f3d09f0060a 292 for (i=0; i<30; i++) {
kokisan2000 0:1f3d09f0060a 293 do { // wait on prox data ready bit
kokisan2000 0:1f3d09f0060a 294 VCNL40x0_Device.ReadCommandRegister (&Command); // read command register
kokisan2000 0:1f3d09f0060a 295 } while (!(Command & COMMAND_MASK_PROX_DATA_READY)); // prox data ready ?
kokisan2000 0:1f3d09f0060a 296
kokisan2000 0:1f3d09f0060a 297 VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value
kokisan2000 0:1f3d09f0060a 298
kokisan2000 0:1f3d09f0060a 299 SummeProxiValue += ProxiValue; // Summary of all measured prox values
kokisan2000 0:1f3d09f0060a 300 }
kokisan2000 0:1f3d09f0060a 301
kokisan2000 0:1f3d09f0060a 302 AverageProxiValue = SummeProxiValue/30; // calculate average
kokisan2000 0:1f3d09f0060a 303
kokisan2000 0:1f3d09f0060a 304 VCNL40x0_Device.SetHighThreshold (AverageProxiValue+100); // set upper threshold for interrupt
kokisan2000 0:1f3d09f0060a 305 pc.printf("\n Upper Threshold Value: %i cts\n\n", AverageProxiValue+100);
kokisan2000 0:1f3d09f0060a 306
kokisan2000 0:1f3d09f0060a 307 wait_ms(2000); // wait 2s (only for display)
kokisan2000 0:1f3d09f0060a 308
kokisan2000 0:1f3d09f0060a 309 // endless loop /////////////////////////////////////////////////////////////////////////////////////
kokisan2000 0:1f3d09f0060a 310
kokisan2000 0:1f3d09f0060a 311 while (1) {
kokisan2000 0:1f3d09f0060a 312
kokisan2000 0:1f3d09f0060a 313 // wait on data ready bit
kokisan2000 0:1f3d09f0060a 314 do {
kokisan2000 0:1f3d09f0060a 315 VCNL40x0_Device.ReadCommandRegister (&Command); // read command register
kokisan2000 0:1f3d09f0060a 316 } while (!(Command & (COMMAND_MASK_PROX_DATA_READY | COMMAND_MASK_AMBI_DATA_READY))); // data ready ?
kokisan2000 0:1f3d09f0060a 317
kokisan2000 0:1f3d09f0060a 318 // read interrupt status register
kokisan2000 0:1f3d09f0060a 319 VCNL40x0_Device.ReadInterruptStatus (&InterruptStatus);
kokisan2000 0:1f3d09f0060a 320
kokisan2000 0:1f3d09f0060a 321 // check interrupt status for High Threshold
kokisan2000 0:1f3d09f0060a 322 if (InterruptStatus & INTERRUPT_MASK_STATUS_THRES_HI) {
kokisan2000 0:1f3d09f0060a 323 mled2 = 1; // LED on, Interrupt
kokisan2000 0:1f3d09f0060a 324 VCNL40x0_Device.SetInterruptStatus (InterruptStatus); // clear Interrupt Status
kokisan2000 0:1f3d09f0060a 325 mled2 = 0; // LED off, Interrupt
kokisan2000 0:1f3d09f0060a 326 }
kokisan2000 0:1f3d09f0060a 327
kokisan2000 0:1f3d09f0060a 328 // prox value ready for using
kokisan2000 0:1f3d09f0060a 329 if (Command & COMMAND_MASK_PROX_DATA_READY) {
kokisan2000 0:1f3d09f0060a 330 mled0 = 1; // LED on, Prox Data Ready
kokisan2000 0:1f3d09f0060a 331 VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value
kokisan2000 0:1f3d09f0060a 332
kokisan2000 0:1f3d09f0060a 333 // print prox value and interrupt status on screen
kokisan2000 0:1f3d09f0060a 334 pc.printf("\nProxi: %5.0i cts \tInterruptStatus: %i", ProxiValue, InterruptStatus);
kokisan2000 0:1f3d09f0060a 335
kokisan2000 0:1f3d09f0060a 336 mled0 = 0; // LED off, Prox data Ready
kokisan2000 0:1f3d09f0060a 337 }
kokisan2000 0:1f3d09f0060a 338
kokisan2000 0:1f3d09f0060a 339 // ambi value ready for using
kokisan2000 0:1f3d09f0060a 340 if (Command & COMMAND_MASK_AMBI_DATA_READY) {
kokisan2000 0:1f3d09f0060a 341 mled1 = 1; // LED on, Ambi Data Ready
kokisan2000 0:1f3d09f0060a 342 VCNL40x0_Device.ReadAmbiValue (&AmbiValue); // read ambi value
kokisan2000 0:1f3d09f0060a 343
kokisan2000 0:1f3d09f0060a 344 // print ambi value and interrupt status on screen
kokisan2000 0:1f3d09f0060a 345 pc.printf("\n Ambi: %i", AmbiValue);
kokisan2000 0:1f3d09f0060a 346
kokisan2000 0:1f3d09f0060a 347 mled1 = 0; // LED off, Ambi Data Ready
kokisan2000 0:1f3d09f0060a 348 }
kokisan2000 0:1f3d09f0060a 349 }
kokisan2000 0:1f3d09f0060a 350 }
kokisan2000 0:1f3d09f0060a 351 # endif
kokisan2000 0:1f3d09f0060a 352