NUCLEO OK. Need to add pull ups for SDA and SCL. (3)

Dependencies:   VCNL40x0 mbed

Committer:
mbedaddict
Date:
Tue Mar 24 12:58:56 2015 +0000
Revision:
2:5519099a3d51
Parent:
0:f43603955213
Recommit because it seems to now working. Compilation fails on another PC.

Who changed what in which revision?

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