Ken Parish
/
VCNL40x0
...
Fork of VCNL40x0 by
main.cpp@0:1f3d09f0060a, 2013-11-30 (annotated)
- Committer:
- kokisan2000
- Date:
- Sat Nov 30 19:44:13 2013 +0000
- Revision:
- 0:1f3d09f0060a
Library for Proximity / Ambient Light Sensor VCNL40xx from Vishay
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:1f3d09f0060a | 31 | VCNL40x0 VCNL40x0_Device (p28, p27, VCNL40x0_ADDRESS); // Define SDA, SCL pin and I2C address |
kokisan2000 | 0:1f3d09f0060a | 32 | DigitalOut mled0(LED1); // LED #1 |
kokisan2000 | 0:1f3d09f0060a | 33 | DigitalOut mled1(LED2); // LED #2 |
kokisan2000 | 0:1f3d09f0060a | 34 | DigitalOut mled2(LED3); // LED #3 |
kokisan2000 | 0:1f3d09f0060a | 35 | Serial pc(USBTX, USBRX); // Tx, Rx USB transmission |
kokisan2000 | 0:1f3d09f0060a | 36 | |
kokisan2000 | 0:1f3d09f0060a | 37 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 38 | // main #1 |
kokisan2000 | 0:1f3d09f0060a | 39 | // Read Proximity on demand and Ambillight on demand in endless loop |
kokisan2000 | 0:1f3d09f0060a | 40 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 41 | # ifdef MAIN1 |
kokisan2000 | 0:1f3d09f0060a | 42 | |
kokisan2000 | 0:1f3d09f0060a | 43 | int main() { |
kokisan2000 | 0:1f3d09f0060a | 44 | unsigned char ID=0; |
kokisan2000 | 0:1f3d09f0060a | 45 | unsigned char Current=0; |
kokisan2000 | 0:1f3d09f0060a | 46 | unsigned int ProxiValue=0; |
kokisan2000 | 0:1f3d09f0060a | 47 | unsigned int AmbiValue=0; |
kokisan2000 | 0:1f3d09f0060a | 48 | |
kokisan2000 | 0:1f3d09f0060a | 49 | pc.baud(BAUD); // set USB speed (virtual COM port) |
kokisan2000 | 0:1f3d09f0060a | 50 | |
kokisan2000 | 0:1f3d09f0060a | 51 | // print information on screen |
kokisan2000 | 0:1f3d09f0060a | 52 | pc.printf("\n\n VCNL4010/4020 Proximity/Ambient Light Sensor"); |
kokisan2000 | 0:1f3d09f0060a | 53 | pc.printf("\n library tested with mbed LPC1768 (ARM Cortex-M3 core) on www.mbed.org"); |
kokisan2000 | 0:1f3d09f0060a | 54 | pc.printf(VERSION); |
kokisan2000 | 0:1f3d09f0060a | 55 | pc.printf("\n Demonstration #1:"); |
kokisan2000 | 0:1f3d09f0060a | 56 | pc.printf("\n Read Proximity on demand and Ambillight on demand in endless loop"); |
kokisan2000 | 0:1f3d09f0060a | 57 | |
kokisan2000 | 0:1f3d09f0060a | 58 | VCNL40x0_Device.ReadID (&ID); // Read VCNL40x0 product ID revision register |
kokisan2000 | 0:1f3d09f0060a | 59 | pc.printf("\n\n Product ID Revision Register: %d", ID); |
kokisan2000 | 0:1f3d09f0060a | 60 | |
kokisan2000 | 0:1f3d09f0060a | 61 | VCNL40x0_Device.SetCurrent (20); // Set current to 200mA |
kokisan2000 | 0:1f3d09f0060a | 62 | VCNL40x0_Device.ReadCurrent (&Current); // Read back IR LED current |
kokisan2000 | 0:1f3d09f0060a | 63 | pc.printf("\n IR LED Current: %d\n\n", Current); |
kokisan2000 | 0:1f3d09f0060a | 64 | |
kokisan2000 | 0:1f3d09f0060a | 65 | wait_ms(3000); // wait 3s (only for display) |
kokisan2000 | 0:1f3d09f0060a | 66 | |
kokisan2000 | 0:1f3d09f0060a | 67 | // endless loop ///////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 68 | |
kokisan2000 | 0:1f3d09f0060a | 69 | while (1) { |
kokisan2000 | 0:1f3d09f0060a | 70 | mled0 = 1; // LED on |
kokisan2000 | 0:1f3d09f0060a | 71 | VCNL40x0_Device.ReadProxiOnDemand (&ProxiValue); // read prox value on demand |
kokisan2000 | 0:1f3d09f0060a | 72 | VCNL40x0_Device.ReadAmbiOnDemand (&AmbiValue); // read ambi value on demand |
kokisan2000 | 0:1f3d09f0060a | 73 | mled0 = 0; // LED off |
kokisan2000 | 0:1f3d09f0060a | 74 | |
kokisan2000 | 0:1f3d09f0060a | 75 | // pront values on screen |
kokisan2000 | 0:1f3d09f0060a | 76 | pc.printf("\nProxi: %5.0i cts \tAmbi: %5.0i cts \tIlluminance: %7.2f lx", ProxiValue, AmbiValue, AmbiValue/4.0); |
kokisan2000 | 0:1f3d09f0060a | 77 | } |
kokisan2000 | 0:1f3d09f0060a | 78 | } |
kokisan2000 | 0:1f3d09f0060a | 79 | # endif |
kokisan2000 | 0:1f3d09f0060a | 80 | |
kokisan2000 | 0:1f3d09f0060a | 81 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 82 | // main #2 |
kokisan2000 | 0:1f3d09f0060a | 83 | // Proximity Measurement in selfetimed mode with 4 measurements/s |
kokisan2000 | 0:1f3d09f0060a | 84 | // Read prox value if ready with conversion, endless loop |
kokisan2000 | 0:1f3d09f0060a | 85 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 86 | # ifdef MAIN2 |
kokisan2000 | 0:1f3d09f0060a | 87 | |
kokisan2000 | 0:1f3d09f0060a | 88 | int main() { |
kokisan2000 | 0:1f3d09f0060a | 89 | unsigned char ID=0; |
kokisan2000 | 0:1f3d09f0060a | 90 | unsigned char Command=0; |
kokisan2000 | 0:1f3d09f0060a | 91 | unsigned char Current=0; |
kokisan2000 | 0:1f3d09f0060a | 92 | unsigned int ProxiValue=0; |
kokisan2000 | 0:1f3d09f0060a | 93 | unsigned int AmbiValue=0; |
kokisan2000 | 0:1f3d09f0060a | 94 | |
kokisan2000 | 0:1f3d09f0060a | 95 | pc.baud(BAUD); |
kokisan2000 | 0:1f3d09f0060a | 96 | |
kokisan2000 | 0:1f3d09f0060a | 97 | // print information on screen |
kokisan2000 | 0:1f3d09f0060a | 98 | pc.printf("\n\n VCNL4010/4020 Proximity/Ambient Light Sensor"); |
kokisan2000 | 0:1f3d09f0060a | 99 | pc.printf("\n library tested with mbed LPC1768 (ARM Cortex-M3 core) on www.mbed.org"); |
kokisan2000 | 0:1f3d09f0060a | 100 | pc.printf(VERSION); |
kokisan2000 | 0:1f3d09f0060a | 101 | pc.printf("\n Demonstration #2:"); |
kokisan2000 | 0:1f3d09f0060a | 102 | pc.printf("\n Proximity Measurement in selftimed mode with 4 measurements/s"); |
kokisan2000 | 0:1f3d09f0060a | 103 | pc.printf("\n Read prox value if ready with conversion, endless loop"); |
kokisan2000 | 0:1f3d09f0060a | 104 | |
kokisan2000 | 0:1f3d09f0060a | 105 | VCNL40x0_Device.ReadID (&ID); // Read VCNL40x0 product ID revision register |
kokisan2000 | 0:1f3d09f0060a | 106 | pc.printf("\n\n Product ID Revision Register: %d", ID); |
kokisan2000 | 0:1f3d09f0060a | 107 | |
kokisan2000 | 0:1f3d09f0060a | 108 | VCNL40x0_Device.SetCurrent (20); // Set current to 200mA |
kokisan2000 | 0:1f3d09f0060a | 109 | VCNL40x0_Device.ReadCurrent (&Current); // Read back IR LED current |
kokisan2000 | 0:1f3d09f0060a | 110 | pc.printf("\n IR LED Current: %d\n\n", Current); |
kokisan2000 | 0:1f3d09f0060a | 111 | |
kokisan2000 | 0:1f3d09f0060a | 112 | VCNL40x0_Device.SetProximityRate (PROX_MEASUREMENT_RATE_4); // set proximity rate to 4/s |
kokisan2000 | 0:1f3d09f0060a | 113 | |
kokisan2000 | 0:1f3d09f0060a | 114 | // enable prox in selftimed mode |
kokisan2000 | 0:1f3d09f0060a | 115 | VCNL40x0_Device.SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_SELFTIMED_MODE_ENABLE); |
kokisan2000 | 0:1f3d09f0060a | 116 | |
kokisan2000 | 0:1f3d09f0060a | 117 | wait_ms(3000); // wait 3s (only for display) |
kokisan2000 | 0:1f3d09f0060a | 118 | |
kokisan2000 | 0:1f3d09f0060a | 119 | // endless loop ///////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 120 | |
kokisan2000 | 0:1f3d09f0060a | 121 | while (1) { |
kokisan2000 | 0:1f3d09f0060a | 122 | |
kokisan2000 | 0:1f3d09f0060a | 123 | // wait on prox data ready bit |
kokisan2000 | 0:1f3d09f0060a | 124 | do { |
kokisan2000 | 0:1f3d09f0060a | 125 | VCNL40x0_Device.ReadCommandRegister (&Command); // read command register |
kokisan2000 | 0:1f3d09f0060a | 126 | } while (!(Command & COMMAND_MASK_PROX_DATA_READY));// prox data ready ? |
kokisan2000 | 0:1f3d09f0060a | 127 | |
kokisan2000 | 0:1f3d09f0060a | 128 | mled0 = 1; // LED on |
kokisan2000 | 0:1f3d09f0060a | 129 | VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value |
kokisan2000 | 0:1f3d09f0060a | 130 | mled0 = 0; // LED off |
kokisan2000 | 0:1f3d09f0060a | 131 | |
kokisan2000 | 0:1f3d09f0060a | 132 | // print values on screen |
kokisan2000 | 0:1f3d09f0060a | 133 | pc.printf("\nProxi: %5.0i cts", ProxiValue); |
kokisan2000 | 0:1f3d09f0060a | 134 | } |
kokisan2000 | 0:1f3d09f0060a | 135 | } |
kokisan2000 | 0:1f3d09f0060a | 136 | # endif |
kokisan2000 | 0:1f3d09f0060a | 137 | |
kokisan2000 | 0:1f3d09f0060a | 138 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 139 | // main #3 |
kokisan2000 | 0:1f3d09f0060a | 140 | // Proximity Measurement in selfetimed mode with 31 measurements/s |
kokisan2000 | 0:1f3d09f0060a | 141 | // Interrupt waiting on proximity value > upper threshold limit |
kokisan2000 | 0:1f3d09f0060a | 142 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 143 | # ifdef MAIN3 |
kokisan2000 | 0:1f3d09f0060a | 144 | |
kokisan2000 | 0:1f3d09f0060a | 145 | int main() { |
kokisan2000 | 0:1f3d09f0060a | 146 | unsigned char ID=0; |
kokisan2000 | 0:1f3d09f0060a | 147 | unsigned char Command=0; |
kokisan2000 | 0:1f3d09f0060a | 148 | unsigned char Current=0; |
kokisan2000 | 0:1f3d09f0060a | 149 | unsigned int ProxiValue=0; |
kokisan2000 | 0:1f3d09f0060a | 150 | unsigned int AmbiValue=0; |
kokisan2000 | 0:1f3d09f0060a | 151 | unsigned char InterruptStatus=0; |
kokisan2000 | 0:1f3d09f0060a | 152 | unsigned char InterruptControl=0; |
kokisan2000 | 0:1f3d09f0060a | 153 | |
kokisan2000 | 0:1f3d09f0060a | 154 | pc.baud(BAUD); |
kokisan2000 | 0:1f3d09f0060a | 155 | |
kokisan2000 | 0:1f3d09f0060a | 156 | // print information on screen |
kokisan2000 | 0:1f3d09f0060a | 157 | pc.printf("\n\n VCNL4010/4020 Proximity/Ambient Light Sensor"); |
kokisan2000 | 0:1f3d09f0060a | 158 | pc.printf("\n library tested with mbed LPC1768 (ARM Cortex-M3 core) on www.mbed.org"); |
kokisan2000 | 0:1f3d09f0060a | 159 | pc.printf(VERSION); |
kokisan2000 | 0:1f3d09f0060a | 160 | pc.printf("\n Demonstration #3:"); |
kokisan2000 | 0:1f3d09f0060a | 161 | pc.printf("\n Proximity Measurement in selfetimed mode with 31 measurements/s"); |
kokisan2000 | 0:1f3d09f0060a | 162 | pc.printf("\n Interrupt waiting on proximity value > upper threshold limit"); |
kokisan2000 | 0:1f3d09f0060a | 163 | |
kokisan2000 | 0:1f3d09f0060a | 164 | VCNL40x0_Device.ReadID (&ID); // Read VCNL40x0 product ID revision register |
kokisan2000 | 0:1f3d09f0060a | 165 | pc.printf("\n\n Product ID Revision Register: %d", ID); |
kokisan2000 | 0:1f3d09f0060a | 166 | |
kokisan2000 | 0:1f3d09f0060a | 167 | VCNL40x0_Device.SetCurrent (20); // Set current to 200mA |
kokisan2000 | 0:1f3d09f0060a | 168 | VCNL40x0_Device.ReadCurrent (&Current); // Read back IR LED current |
kokisan2000 | 0:1f3d09f0060a | 169 | pc.printf("\n IR LED Current: %d\n\n", Current); |
kokisan2000 | 0:1f3d09f0060a | 170 | |
kokisan2000 | 0:1f3d09f0060a | 171 | // stop all activities (necessary for changing proximity rate, see datasheet) |
kokisan2000 | 0:1f3d09f0060a | 172 | VCNL40x0_Device.SetCommandRegister (COMMAND_ALL_DISABLE); |
kokisan2000 | 0:1f3d09f0060a | 173 | |
kokisan2000 | 0:1f3d09f0060a | 174 | // set proximity rate to 31/s |
kokisan2000 | 0:1f3d09f0060a | 175 | VCNL40x0_Device.SetProximityRate (PROX_MEASUREMENT_RATE_31); |
kokisan2000 | 0:1f3d09f0060a | 176 | |
kokisan2000 | 0:1f3d09f0060a | 177 | // enable prox in selftimed mode |
kokisan2000 | 0:1f3d09f0060a | 178 | VCNL40x0_Device.SetCommandRegister (COMMAND_PROX_ENABLE | |
kokisan2000 | 0:1f3d09f0060a | 179 | COMMAND_SELFTIMED_MODE_ENABLE); |
kokisan2000 | 0:1f3d09f0060a | 180 | |
kokisan2000 | 0:1f3d09f0060a | 181 | // set interrupt control register |
kokisan2000 | 0:1f3d09f0060a | 182 | VCNL40x0_Device.SetInterruptControl (INTERRUPT_THRES_SEL_PROX | |
kokisan2000 | 0:1f3d09f0060a | 183 | INTERRUPT_THRES_ENABLE | |
kokisan2000 | 0:1f3d09f0060a | 184 | INTERRUPT_COUNT_EXCEED_1); |
kokisan2000 | 0:1f3d09f0060a | 185 | |
kokisan2000 | 0:1f3d09f0060a | 186 | VCNL40x0_Device.ReadInterruptControl (&InterruptControl); // Read back Interrupt Control register |
kokisan2000 | 0:1f3d09f0060a | 187 | pc.printf("\n Interrupt Control Register: %i\n\n", InterruptControl); |
kokisan2000 | 0:1f3d09f0060a | 188 | |
kokisan2000 | 0:1f3d09f0060a | 189 | |
kokisan2000 | 0:1f3d09f0060a | 190 | // wait on prox data ready bit |
kokisan2000 | 0:1f3d09f0060a | 191 | do { |
kokisan2000 | 0:1f3d09f0060a | 192 | VCNL40x0_Device.ReadCommandRegister (&Command); // read command register |
kokisan2000 | 0:1f3d09f0060a | 193 | } while (!(Command & COMMAND_MASK_PROX_DATA_READY)); // prox data ready ? |
kokisan2000 | 0:1f3d09f0060a | 194 | |
kokisan2000 | 0:1f3d09f0060a | 195 | VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value |
kokisan2000 | 0:1f3d09f0060a | 196 | VCNL40x0_Device.SetHighThreshold (ProxiValue+100); // set high threshold for interrupt |
kokisan2000 | 0:1f3d09f0060a | 197 | |
kokisan2000 | 0:1f3d09f0060a | 198 | wait_ms(3000); // wait 3s (only for display) |
kokisan2000 | 0:1f3d09f0060a | 199 | |
kokisan2000 | 0:1f3d09f0060a | 200 | // endless loop ///////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 201 | |
kokisan2000 | 0:1f3d09f0060a | 202 | while (1) { |
kokisan2000 | 0:1f3d09f0060a | 203 | |
kokisan2000 | 0:1f3d09f0060a | 204 | // wait on prox data ready bit |
kokisan2000 | 0:1f3d09f0060a | 205 | do { |
kokisan2000 | 0:1f3d09f0060a | 206 | VCNL40x0_Device.ReadCommandRegister (&Command); // read command register |
kokisan2000 | 0:1f3d09f0060a | 207 | } while (!(Command & COMMAND_MASK_PROX_DATA_READY));// prox data ready ? |
kokisan2000 | 0:1f3d09f0060a | 208 | |
kokisan2000 | 0:1f3d09f0060a | 209 | mled0 = 1; // LED on |
kokisan2000 | 0:1f3d09f0060a | 210 | VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value |
kokisan2000 | 0:1f3d09f0060a | 211 | mled0 = 0; // LED off |
kokisan2000 | 0:1f3d09f0060a | 212 | |
kokisan2000 | 0:1f3d09f0060a | 213 | // read interrupt status register |
kokisan2000 | 0:1f3d09f0060a | 214 | VCNL40x0_Device.ReadInterruptStatus (&InterruptStatus); |
kokisan2000 | 0:1f3d09f0060a | 215 | |
kokisan2000 | 0:1f3d09f0060a | 216 | // print prox value and interrupt status on screen |
kokisan2000 | 0:1f3d09f0060a | 217 | pc.printf("\nProxi: %5.0i cts \tInterruptStatus: %i", ProxiValue, InterruptStatus); |
kokisan2000 | 0:1f3d09f0060a | 218 | |
kokisan2000 | 0:1f3d09f0060a | 219 | // check interrupt status for High Threshold |
kokisan2000 | 0:1f3d09f0060a | 220 | if (InterruptStatus & INTERRUPT_MASK_STATUS_THRES_HI) { |
kokisan2000 | 0:1f3d09f0060a | 221 | mled1 = 1; // LED on |
kokisan2000 | 0:1f3d09f0060a | 222 | VCNL40x0_Device.SetInterruptStatus (InterruptStatus); // clear Interrupt Status |
kokisan2000 | 0:1f3d09f0060a | 223 | mled1 = 0; // LED on |
kokisan2000 | 0:1f3d09f0060a | 224 | } |
kokisan2000 | 0:1f3d09f0060a | 225 | } |
kokisan2000 | 0:1f3d09f0060a | 226 | } |
kokisan2000 | 0:1f3d09f0060a | 227 | # endif |
kokisan2000 | 0:1f3d09f0060a | 228 | |
kokisan2000 | 0:1f3d09f0060a | 229 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 230 | // main #4 |
kokisan2000 | 0:1f3d09f0060a | 231 | // Proximity Measurement and Ambientlight Measurement in selftimed mode |
kokisan2000 | 0:1f3d09f0060a | 232 | // Proximity with 31 measurements/s, Ambientlight with 2 measurement/s |
kokisan2000 | 0:1f3d09f0060a | 233 | // Interrupt waiting on proximity value > upper threshold limit |
kokisan2000 | 0:1f3d09f0060a | 234 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 235 | # ifdef MAIN4 |
kokisan2000 | 0:1f3d09f0060a | 236 | |
kokisan2000 | 0:1f3d09f0060a | 237 | int main() { |
kokisan2000 | 0:1f3d09f0060a | 238 | unsigned int i=0; |
kokisan2000 | 0:1f3d09f0060a | 239 | unsigned char ID=0; |
kokisan2000 | 0:1f3d09f0060a | 240 | unsigned char Command=0; |
kokisan2000 | 0:1f3d09f0060a | 241 | unsigned char Current=0; |
kokisan2000 | 0:1f3d09f0060a | 242 | unsigned int ProxiValue=0; |
kokisan2000 | 0:1f3d09f0060a | 243 | unsigned int SummeProxiValue=0; |
kokisan2000 | 0:1f3d09f0060a | 244 | unsigned int AverageProxiValue=0; |
kokisan2000 | 0:1f3d09f0060a | 245 | unsigned int AmbiValue=0; |
kokisan2000 | 0:1f3d09f0060a | 246 | unsigned char InterruptStatus=0; |
kokisan2000 | 0:1f3d09f0060a | 247 | unsigned char InterruptControl=0; |
kokisan2000 | 0:1f3d09f0060a | 248 | |
kokisan2000 | 0:1f3d09f0060a | 249 | pc.baud(BAUD); |
kokisan2000 | 0:1f3d09f0060a | 250 | |
kokisan2000 | 0:1f3d09f0060a | 251 | // print information on screen |
kokisan2000 | 0:1f3d09f0060a | 252 | pc.printf("\n\n VCNL4010/4020 Proximity/Ambient Light Sensor"); |
kokisan2000 | 0:1f3d09f0060a | 253 | pc.printf("\n library tested with mbed LPC1768 (ARM Cortex-M3 core) on www.mbed.org"); |
kokisan2000 | 0:1f3d09f0060a | 254 | pc.printf(VERSION); |
kokisan2000 | 0:1f3d09f0060a | 255 | pc.printf("\n Demonstration #4:"); |
kokisan2000 | 0:1f3d09f0060a | 256 | pc.printf("\n Proximity Measurement and Ambient light Measurement in selftimed mode"); |
kokisan2000 | 0:1f3d09f0060a | 257 | pc.printf("\n Proximity with 31 measurements/s, Ambient light with 2 measurement/s"); |
kokisan2000 | 0:1f3d09f0060a | 258 | pc.printf("\n Interrupt waiting on proximity value > upper threshold limit"); |
kokisan2000 | 0:1f3d09f0060a | 259 | |
kokisan2000 | 0:1f3d09f0060a | 260 | VCNL40x0_Device.ReadID (&ID); // Read VCNL40x0 product ID revision register |
kokisan2000 | 0:1f3d09f0060a | 261 | pc.printf("\n\n Product ID Revision Register: %d", ID); |
kokisan2000 | 0:1f3d09f0060a | 262 | |
kokisan2000 | 0:1f3d09f0060a | 263 | VCNL40x0_Device.SetCurrent (20); // Set current to 200mA |
kokisan2000 | 0:1f3d09f0060a | 264 | VCNL40x0_Device.ReadCurrent (&Current); // Read back IR LED current |
kokisan2000 | 0:1f3d09f0060a | 265 | pc.printf("\n IR LED Current: %d", Current); |
kokisan2000 | 0:1f3d09f0060a | 266 | |
kokisan2000 | 0:1f3d09f0060a | 267 | // stop all activities (necessary for changing proximity rate, see datasheet) |
kokisan2000 | 0:1f3d09f0060a | 268 | VCNL40x0_Device.SetCommandRegister (COMMAND_ALL_DISABLE); |
kokisan2000 | 0:1f3d09f0060a | 269 | |
kokisan2000 | 0:1f3d09f0060a | 270 | // set proximity rate to 31/s |
kokisan2000 | 0:1f3d09f0060a | 271 | VCNL40x0_Device.SetProximityRate (PROX_MEASUREMENT_RATE_31); |
kokisan2000 | 0:1f3d09f0060a | 272 | |
kokisan2000 | 0:1f3d09f0060a | 273 | // enable prox and ambi in selftimed mode |
kokisan2000 | 0:1f3d09f0060a | 274 | VCNL40x0_Device.SetCommandRegister (COMMAND_PROX_ENABLE | |
kokisan2000 | 0:1f3d09f0060a | 275 | COMMAND_AMBI_ENABLE | |
kokisan2000 | 0:1f3d09f0060a | 276 | COMMAND_SELFTIMED_MODE_ENABLE); |
kokisan2000 | 0:1f3d09f0060a | 277 | |
kokisan2000 | 0:1f3d09f0060a | 278 | // set interrupt control for threshold |
kokisan2000 | 0:1f3d09f0060a | 279 | VCNL40x0_Device.SetInterruptControl (INTERRUPT_THRES_SEL_PROX | |
kokisan2000 | 0:1f3d09f0060a | 280 | INTERRUPT_THRES_ENABLE | |
kokisan2000 | 0:1f3d09f0060a | 281 | INTERRUPT_COUNT_EXCEED_1); |
kokisan2000 | 0:1f3d09f0060a | 282 | |
kokisan2000 | 0:1f3d09f0060a | 283 | // set ambient light measurement parameter |
kokisan2000 | 0:1f3d09f0060a | 284 | VCNL40x0_Device.SetAmbiConfiguration (AMBI_PARA_AVERAGE_32 | |
kokisan2000 | 0:1f3d09f0060a | 285 | AMBI_PARA_AUTO_OFFSET_ENABLE | |
kokisan2000 | 0:1f3d09f0060a | 286 | AMBI_PARA_MEAS_RATE_2); |
kokisan2000 | 0:1f3d09f0060a | 287 | |
kokisan2000 | 0:1f3d09f0060a | 288 | // measure average of prox value |
kokisan2000 | 0:1f3d09f0060a | 289 | SummeProxiValue = 0; |
kokisan2000 | 0:1f3d09f0060a | 290 | |
kokisan2000 | 0:1f3d09f0060a | 291 | for (i=0; i<30; i++) { |
kokisan2000 | 0:1f3d09f0060a | 292 | do { // wait on prox data ready bit |
kokisan2000 | 0:1f3d09f0060a | 293 | VCNL40x0_Device.ReadCommandRegister (&Command); // read command register |
kokisan2000 | 0:1f3d09f0060a | 294 | } while (!(Command & COMMAND_MASK_PROX_DATA_READY)); // prox data ready ? |
kokisan2000 | 0:1f3d09f0060a | 295 | |
kokisan2000 | 0:1f3d09f0060a | 296 | VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value |
kokisan2000 | 0:1f3d09f0060a | 297 | |
kokisan2000 | 0:1f3d09f0060a | 298 | SummeProxiValue += ProxiValue; // Summary of all measured prox values |
kokisan2000 | 0:1f3d09f0060a | 299 | } |
kokisan2000 | 0:1f3d09f0060a | 300 | |
kokisan2000 | 0:1f3d09f0060a | 301 | AverageProxiValue = SummeProxiValue/30; // calculate average |
kokisan2000 | 0:1f3d09f0060a | 302 | |
kokisan2000 | 0:1f3d09f0060a | 303 | VCNL40x0_Device.SetHighThreshold (AverageProxiValue+100); // set upper threshold for interrupt |
kokisan2000 | 0:1f3d09f0060a | 304 | pc.printf("\n Upper Threshold Value: %i cts\n\n", AverageProxiValue+100); |
kokisan2000 | 0:1f3d09f0060a | 305 | |
kokisan2000 | 0:1f3d09f0060a | 306 | wait_ms(2000); // wait 2s (only for display) |
kokisan2000 | 0:1f3d09f0060a | 307 | |
kokisan2000 | 0:1f3d09f0060a | 308 | // endless loop ///////////////////////////////////////////////////////////////////////////////////// |
kokisan2000 | 0:1f3d09f0060a | 309 | |
kokisan2000 | 0:1f3d09f0060a | 310 | while (1) { |
kokisan2000 | 0:1f3d09f0060a | 311 | |
kokisan2000 | 0:1f3d09f0060a | 312 | // wait on data ready bit |
kokisan2000 | 0:1f3d09f0060a | 313 | do { |
kokisan2000 | 0:1f3d09f0060a | 314 | VCNL40x0_Device.ReadCommandRegister (&Command); // read command register |
kokisan2000 | 0:1f3d09f0060a | 315 | } while (!(Command & (COMMAND_MASK_PROX_DATA_READY | COMMAND_MASK_AMBI_DATA_READY))); // data ready ? |
kokisan2000 | 0:1f3d09f0060a | 316 | |
kokisan2000 | 0:1f3d09f0060a | 317 | // read interrupt status register |
kokisan2000 | 0:1f3d09f0060a | 318 | VCNL40x0_Device.ReadInterruptStatus (&InterruptStatus); |
kokisan2000 | 0:1f3d09f0060a | 319 | |
kokisan2000 | 0:1f3d09f0060a | 320 | // check interrupt status for High Threshold |
kokisan2000 | 0:1f3d09f0060a | 321 | if (InterruptStatus & INTERRUPT_MASK_STATUS_THRES_HI) { |
kokisan2000 | 0:1f3d09f0060a | 322 | mled2 = 1; // LED on, Interrupt |
kokisan2000 | 0:1f3d09f0060a | 323 | VCNL40x0_Device.SetInterruptStatus (InterruptStatus); // clear Interrupt Status |
kokisan2000 | 0:1f3d09f0060a | 324 | mled2 = 0; // LED off, Interrupt |
kokisan2000 | 0:1f3d09f0060a | 325 | } |
kokisan2000 | 0:1f3d09f0060a | 326 | |
kokisan2000 | 0:1f3d09f0060a | 327 | // prox value ready for using |
kokisan2000 | 0:1f3d09f0060a | 328 | if (Command & COMMAND_MASK_PROX_DATA_READY) { |
kokisan2000 | 0:1f3d09f0060a | 329 | mled0 = 1; // LED on, Prox Data Ready |
kokisan2000 | 0:1f3d09f0060a | 330 | VCNL40x0_Device.ReadProxiValue (&ProxiValue); // read prox value |
kokisan2000 | 0:1f3d09f0060a | 331 | |
kokisan2000 | 0:1f3d09f0060a | 332 | // print prox value and interrupt status on screen |
kokisan2000 | 0:1f3d09f0060a | 333 | pc.printf("\nProxi: %5.0i cts \tInterruptStatus: %i", ProxiValue, InterruptStatus); |
kokisan2000 | 0:1f3d09f0060a | 334 | |
kokisan2000 | 0:1f3d09f0060a | 335 | mled0 = 0; // LED off, Prox data Ready |
kokisan2000 | 0:1f3d09f0060a | 336 | } |
kokisan2000 | 0:1f3d09f0060a | 337 | |
kokisan2000 | 0:1f3d09f0060a | 338 | // ambi value ready for using |
kokisan2000 | 0:1f3d09f0060a | 339 | if (Command & COMMAND_MASK_AMBI_DATA_READY) { |
kokisan2000 | 0:1f3d09f0060a | 340 | mled1 = 1; // LED on, Ambi Data Ready |
kokisan2000 | 0:1f3d09f0060a | 341 | VCNL40x0_Device.ReadAmbiValue (&AmbiValue); // read ambi value |
kokisan2000 | 0:1f3d09f0060a | 342 | |
kokisan2000 | 0:1f3d09f0060a | 343 | // print ambi value and interrupt status on screen |
kokisan2000 | 0:1f3d09f0060a | 344 | pc.printf("\n Ambi: %i", AmbiValue); |
kokisan2000 | 0:1f3d09f0060a | 345 | |
kokisan2000 | 0:1f3d09f0060a | 346 | mled1 = 0; // LED off, Ambi Data Ready |
kokisan2000 | 0:1f3d09f0060a | 347 | } |
kokisan2000 | 0:1f3d09f0060a | 348 | } |
kokisan2000 | 0:1f3d09f0060a | 349 | } |
kokisan2000 | 0:1f3d09f0060a | 350 | # endif |
kokisan2000 | 0:1f3d09f0060a | 351 |