Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NeoStrip_mod VCNL40x0_1 mbed
Fork of XYZ_PROX_01_1 by
main.cpp@3:91e3eb00df03, 2014-05-30 (annotated)
- Committer:
- tomfleet
- Date:
- Fri May 30 12:52:11 2014 +0000
- Revision:
- 3:91e3eb00df03
- Parent:
- 2:9252c8c48272
fracking commit suystem...
;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ghostaudio | 0:09dd83d78a0a | 1 | #include "mbed.h" |
| ghostaudio | 0:09dd83d78a0a | 2 | #include "VCNL40x0.h" |
| ghostaudio | 0:09dd83d78a0a | 3 | #include "NeoStrip.h" |
| tomfleet | 2:9252c8c48272 | 4 | #define NumPixels 4 |
| tomfleet | 2:9252c8c48272 | 5 | |
| tomfleet | 2:9252c8c48272 | 6 | #define PIN_OUTPUT 6 |
| ghostaudio | 0:09dd83d78a0a | 7 | |
| ghostaudio | 0:09dd83d78a0a | 8 | I2C i2c(p28, p27); |
| ghostaudio | 0:09dd83d78a0a | 9 | DigitalOut mled0(LED1); |
| ghostaudio | 0:09dd83d78a0a | 10 | DigitalOut mled1(LED2); |
| ghostaudio | 0:09dd83d78a0a | 11 | DigitalOut mled2(LED3); |
| ghostaudio | 0:09dd83d78a0a | 12 | DigitalOut mled3(LED4); |
| ghostaudio | 0:09dd83d78a0a | 13 | Serial pc(USBTX, USBRX); |
| ghostaudio | 0:09dd83d78a0a | 14 | DigitalOut reset(p29); |
| ghostaudio | 0:09dd83d78a0a | 15 | NeoStrip strip(p26, NumPixels); |
| ghostaudio | 0:09dd83d78a0a | 16 | |
| ghostaudio | 0:09dd83d78a0a | 17 | const uint8_t MUX = 0xE0; // 11100000 |
| ghostaudio | 0:09dd83d78a0a | 18 | const uint8_t VCNL4020 = 0x26; |
| ghostaudio | 0:09dd83d78a0a | 19 | unsigned int sensors[4]; |
| ghostaudio | 0:09dd83d78a0a | 20 | char _send[3]; |
| ghostaudio | 0:09dd83d78a0a | 21 | char _receive[2]; |
| ghostaudio | 0:09dd83d78a0a | 22 | char port[2]; |
| ghostaudio | 0:09dd83d78a0a | 23 | const uint8_t chan[] = {0x01,0x02,0x04,0x08}; |
| ghostaudio | 0:09dd83d78a0a | 24 | int mleds[] = {0,0,0,0}; |
| ghostaudio | 0:09dd83d78a0a | 25 | |
| ghostaudio | 0:09dd83d78a0a | 26 | void Setup(void); |
| ghostaudio | 0:09dd83d78a0a | 27 | void Setmleds(void); |
| ghostaudio | 0:09dd83d78a0a | 28 | void SwitchChannel(uint8_t chan); |
| ghostaudio | 0:09dd83d78a0a | 29 | void ReadAllSensors(void); |
| ghostaudio | 0:09dd83d78a0a | 30 | void Setmleds(void); |
| ghostaudio | 0:09dd83d78a0a | 31 | void SetProximityRate (unsigned char ProximityRate); |
| ghostaudio | 0:09dd83d78a0a | 32 | void SetCommandRegister (unsigned char Command); |
| ghostaudio | 0:09dd83d78a0a | 33 | void ReadCommandRegister (unsigned char *Command); |
| ghostaudio | 0:09dd83d78a0a | 34 | void SetCurrent (unsigned char Current); |
| ghostaudio | 0:09dd83d78a0a | 35 | void ReadProxiValue (unsigned int *ProxiValue); |
| ghostaudio | 0:09dd83d78a0a | 36 | void SetProximityRate (unsigned char ProximityRate); |
| ghostaudio | 0:09dd83d78a0a | 37 | void ReadProxiOnDemand (unsigned int *ProxiValue); |
| ghostaudio | 0:09dd83d78a0a | 38 | void Setup(void); |
| ghostaudio | 0:09dd83d78a0a | 39 | |
| ghostaudio | 0:09dd83d78a0a | 40 | |
| ghostaudio | 0:09dd83d78a0a | 41 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ghostaudio | 0:09dd83d78a0a | 42 | |
| ghostaudio | 0:09dd83d78a0a | 43 | int main() { |
| ghostaudio | 0:09dd83d78a0a | 44 | |
| ghostaudio | 0:09dd83d78a0a | 45 | Setup(); |
| ghostaudio | 0:09dd83d78a0a | 46 | |
| ghostaudio | 0:09dd83d78a0a | 47 | while (1) { |
| ghostaudio | 0:09dd83d78a0a | 48 | |
| ghostaudio | 0:09dd83d78a0a | 49 | ReadAllSensors(); |
| ghostaudio | 0:09dd83d78a0a | 50 | |
| ghostaudio | 0:09dd83d78a0a | 51 | } |
| ghostaudio | 0:09dd83d78a0a | 52 | } |
| ghostaudio | 0:09dd83d78a0a | 53 | |
| ghostaudio | 0:09dd83d78a0a | 54 | |
| ghostaudio | 0:09dd83d78a0a | 55 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ghostaudio | 0:09dd83d78a0a | 56 | |
| ghostaudio | 0:09dd83d78a0a | 57 | void Setup(void) { |
| ghostaudio | 0:09dd83d78a0a | 58 | |
| ghostaudio | 0:09dd83d78a0a | 59 | pc.baud(115200); |
| ghostaudio | 0:09dd83d78a0a | 60 | // set BAUD |
| ghostaudio | 0:09dd83d78a0a | 61 | for (int i = 0; i < 4; i++){ |
| ghostaudio | 0:09dd83d78a0a | 62 | |
| ghostaudio | 0:09dd83d78a0a | 63 | reset = 0; |
| ghostaudio | 0:09dd83d78a0a | 64 | wait(0.001); |
| ghostaudio | 0:09dd83d78a0a | 65 | reset = 1; |
| ghostaudio | 0:09dd83d78a0a | 66 | wait(0.001); |
| ghostaudio | 0:09dd83d78a0a | 67 | port[0] = chan[i]; |
| ghostaudio | 0:09dd83d78a0a | 68 | i2c.write(MUX,port, 1); |
| ghostaudio | 0:09dd83d78a0a | 69 | SetCurrent(20); // Set current to 200mA |
| ghostaudio | 0:09dd83d78a0a | 70 | SetCommandRegister (COMMAND_ALL_DISABLE); // ready for prox rate change |
| ghostaudio | 0:09dd83d78a0a | 71 | SetProximityRate (PROX_MEASUREMENT_RATE_31); // 31 measurements/s |
| ghostaudio | 0:09dd83d78a0a | 72 | wait(0.2); |
| ghostaudio | 0:09dd83d78a0a | 73 | |
| ghostaudio | 0:09dd83d78a0a | 74 | } |
| ghostaudio | 0:09dd83d78a0a | 75 | } |
| ghostaudio | 0:09dd83d78a0a | 76 | |
| ghostaudio | 0:09dd83d78a0a | 77 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ghostaudio | 0:09dd83d78a0a | 78 | |
| ghostaudio | 0:09dd83d78a0a | 79 | void Setmleds(void){ |
| ghostaudio | 0:09dd83d78a0a | 80 | mled0 = mleds[0]; |
| ghostaudio | 0:09dd83d78a0a | 81 | mled1 = mleds[1]; |
| ghostaudio | 0:09dd83d78a0a | 82 | mled2 = mleds[2]; |
| ghostaudio | 0:09dd83d78a0a | 83 | mled3 = mleds[3]; |
| ghostaudio | 0:09dd83d78a0a | 84 | } |
| ghostaudio | 0:09dd83d78a0a | 85 | |
| ghostaudio | 0:09dd83d78a0a | 86 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ghostaudio | 0:09dd83d78a0a | 87 | |
| ghostaudio | 0:09dd83d78a0a | 88 | void SwitchChannel(uint8_t chan){ |
| ghostaudio | 0:09dd83d78a0a | 89 | reset = 0; |
| ghostaudio | 0:09dd83d78a0a | 90 | wait(0.001); |
| ghostaudio | 0:09dd83d78a0a | 91 | reset = 1; |
| ghostaudio | 0:09dd83d78a0a | 92 | wait(0.001); |
| ghostaudio | 0:09dd83d78a0a | 93 | port[0] = chan; |
| ghostaudio | 0:09dd83d78a0a | 94 | i2c.write(MUX,port, 1); |
| ghostaudio | 0:09dd83d78a0a | 95 | } |
| ghostaudio | 0:09dd83d78a0a | 96 | |
| ghostaudio | 0:09dd83d78a0a | 97 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ghostaudio | 0:09dd83d78a0a | 98 | |
| ghostaudio | 0:09dd83d78a0a | 99 | void ReadAllSensors(void){ |
| ghostaudio | 0:09dd83d78a0a | 100 | |
| ghostaudio | 0:09dd83d78a0a | 101 | for (int i = 0; i < 4; i++){ |
| ghostaudio | 0:09dd83d78a0a | 102 | |
| ghostaudio | 0:09dd83d78a0a | 103 | SwitchChannel(chan[i]); // set channel |
| ghostaudio | 0:09dd83d78a0a | 104 | mleds[i] = 1; // LED on |
| ghostaudio | 0:09dd83d78a0a | 105 | Setmleds(); |
| ghostaudio | 0:09dd83d78a0a | 106 | ReadProxiOnDemand(&sensors[i]); // read prox value on demand |
| ghostaudio | 0:09dd83d78a0a | 107 | mleds[i] = 0; // LED off |
| ghostaudio | 0:09dd83d78a0a | 108 | Setmleds(); |
| ghostaudio | 0:09dd83d78a0a | 109 | } |
| ghostaudio | 0:09dd83d78a0a | 110 | |
| ghostaudio | 0:09dd83d78a0a | 111 | for (int i = 0; i < 4; i++){ |
| ghostaudio | 0:09dd83d78a0a | 112 | pc.printf("\r %i ", sensors[i]); |
| ghostaudio | 0:09dd83d78a0a | 113 | } |
| ghostaudio | 0:09dd83d78a0a | 114 | pc.printf("\r \n ");}; |
| tomfleet | 2:9252c8c48272 | 115 | //} |
| ghostaudio | 0:09dd83d78a0a | 116 | |
| ghostaudio | 0:09dd83d78a0a | 117 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ghostaudio | 0:09dd83d78a0a | 118 | |
| ghostaudio | 0:09dd83d78a0a | 119 | void SetCommandRegister (unsigned char Command) { |
| ghostaudio | 0:09dd83d78a0a | 120 | |
| ghostaudio | 0:09dd83d78a0a | 121 | //unsigned char send[2]; |
| ghostaudio | 0:09dd83d78a0a | 122 | |
| ghostaudio | 0:09dd83d78a0a | 123 | _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration reister |
| ghostaudio | 0:09dd83d78a0a | 124 | _send[1] = Command; |
| ghostaudio | 0:09dd83d78a0a | 125 | i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C |
| ghostaudio | 0:09dd83d78a0a | 126 | } |
| ghostaudio | 0:09dd83d78a0a | 127 | |
| ghostaudio | 0:09dd83d78a0a | 128 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ghostaudio | 0:09dd83d78a0a | 129 | |
| ghostaudio | 0:09dd83d78a0a | 130 | void ReadCommandRegister (unsigned char *Command) { |
| ghostaudio | 0:09dd83d78a0a | 131 | |
| ghostaudio | 0:09dd83d78a0a | 132 | //unsigned char send[2]; |
| ghostaudio | 0:09dd83d78a0a | 133 | //unsigned char receive[2]; |
| ghostaudio | 0:09dd83d78a0a | 134 | |
| ghostaudio | 0:09dd83d78a0a | 135 | _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration register |
| ghostaudio | 0:09dd83d78a0a | 136 | i2c.write(VCNL4020,_send, 1); // Write 1 byte on I2C |
| ghostaudio | 0:09dd83d78a0a | 137 | i2c.read(VCNL4020+1,_receive, 1); // Read 1 byte on I2C |
| ghostaudio | 0:09dd83d78a0a | 138 | |
| ghostaudio | 0:09dd83d78a0a | 139 | *Command = (unsigned char)(_receive[0]); |
| ghostaudio | 0:09dd83d78a0a | 140 | } |
| ghostaudio | 0:09dd83d78a0a | 141 | |
| ghostaudio | 0:09dd83d78a0a | 142 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ghostaudio | 0:09dd83d78a0a | 143 | |
| ghostaudio | 0:09dd83d78a0a | 144 | void SetCurrent (unsigned char Current) { |
| ghostaudio | 0:09dd83d78a0a | 145 | |
| ghostaudio | 0:09dd83d78a0a | 146 | _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED Current register |
| ghostaudio | 0:09dd83d78a0a | 147 | _send[1] = Current; |
| ghostaudio | 0:09dd83d78a0a | 148 | i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C |
| ghostaudio | 0:09dd83d78a0a | 149 | |
| ghostaudio | 0:09dd83d78a0a | 150 | } |
| ghostaudio | 0:09dd83d78a0a | 151 | |
| ghostaudio | 0:09dd83d78a0a | 152 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ghostaudio | 0:09dd83d78a0a | 153 | |
| ghostaudio | 0:09dd83d78a0a | 154 | void ReadProxiValue (unsigned int *ProxiValue) { |
| ghostaudio | 0:09dd83d78a0a | 155 | |
| ghostaudio | 0:09dd83d78a0a | 156 | _send[0] = REGISTER_PROX_VALUE; // VCNL40x0 Proximity Value register |
| ghostaudio | 0:09dd83d78a0a | 157 | i2c.write(VCNL4020, _send, 1); // Write 1 byte on I2C |
| ghostaudio | 0:09dd83d78a0a | 158 | i2c.read(VCNL4020+1, _receive, 2); // Read 2 bytes on I2C |
| ghostaudio | 0:09dd83d78a0a | 159 | *ProxiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]); |
| ghostaudio | 0:09dd83d78a0a | 160 | } |
| ghostaudio | 0:09dd83d78a0a | 161 | |
| ghostaudio | 0:09dd83d78a0a | 162 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ghostaudio | 0:09dd83d78a0a | 163 | |
| ghostaudio | 0:09dd83d78a0a | 164 | void SetProximityRate (unsigned char ProximityRate) { |
| ghostaudio | 0:09dd83d78a0a | 165 | |
| ghostaudio | 0:09dd83d78a0a | 166 | _send[0] = REGISTER_PROX_RATE; // VCNL40x0 Proximity rate register |
| ghostaudio | 0:09dd83d78a0a | 167 | _send[1] = ProximityRate; |
| ghostaudio | 0:09dd83d78a0a | 168 | i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C |
| ghostaudio | 0:09dd83d78a0a | 169 | } |
| ghostaudio | 0:09dd83d78a0a | 170 | |
| ghostaudio | 0:09dd83d78a0a | 171 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ghostaudio | 0:09dd83d78a0a | 172 | |
| ghostaudio | 0:09dd83d78a0a | 173 | void ReadProxiOnDemand (unsigned int *ProxiValue) { |
| ghostaudio | 0:09dd83d78a0a | 174 | |
| ghostaudio | 0:09dd83d78a0a | 175 | unsigned char Command=0; |
| ghostaudio | 0:09dd83d78a0a | 176 | |
| ghostaudio | 0:09dd83d78a0a | 177 | // enable prox value on demand |
| ghostaudio | 0:09dd83d78a0a | 178 | SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND); |
| ghostaudio | 0:09dd83d78a0a | 179 | |
| ghostaudio | 0:09dd83d78a0a | 180 | // wait on prox data ready bit |
| ghostaudio | 0:09dd83d78a0a | 181 | do { |
| ghostaudio | 0:09dd83d78a0a | 182 | ReadCommandRegister (&Command); // read command register |
| ghostaudio | 0:09dd83d78a0a | 183 | } while (!(Command & COMMAND_MASK_PROX_DATA_READY)); |
| ghostaudio | 0:09dd83d78a0a | 184 | |
| ghostaudio | 0:09dd83d78a0a | 185 | ReadProxiValue (ProxiValue); // read prox value |
| ghostaudio | 0:09dd83d78a0a | 186 | |
| ghostaudio | 0:09dd83d78a0a | 187 | SetCommandRegister (COMMAND_ALL_DISABLE); // stop prox value on demand |
| ghostaudio | 0:09dd83d78a0a | 188 | |
| ghostaudio | 0:09dd83d78a0a | 189 | } |
