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
Diff: main.cpp
- Revision:
- 0:09dd83d78a0a
- Child:
- 2:9252c8c48272
diff -r 000000000000 -r 09dd83d78a0a main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Fri May 30 12:39:24 2014 +0000
@@ -0,0 +1,187 @@
+#include "mbed.h"
+#include "VCNL40x0.h"
+#include "NeoStrip.h"
+#define #define NumPixels 4
+
+I2C i2c(p28, p27);
+DigitalOut mled0(LED1);
+DigitalOut mled1(LED2);
+DigitalOut mled2(LED3);
+DigitalOut mled3(LED4);
+Serial pc(USBTX, USBRX);
+DigitalOut reset(p29);
+NeoStrip strip(p26, NumPixels);
+
+const uint8_t MUX = 0xE0; // 11100000
+const uint8_t VCNL4020 = 0x26;
+unsigned int sensors[4];
+char _send[3];
+char _receive[2];
+char port[2];
+const uint8_t chan[] = {0x01,0x02,0x04,0x08};
+int mleds[] = {0,0,0,0};
+
+void Setup(void);
+void Setmleds(void);
+void SwitchChannel(uint8_t chan);
+void ReadAllSensors(void);
+void Setmleds(void);
+void SetProximityRate (unsigned char ProximityRate);
+void SetCommandRegister (unsigned char Command);
+void ReadCommandRegister (unsigned char *Command);
+void SetCurrent (unsigned char Current);
+void ReadProxiValue (unsigned int *ProxiValue);
+void SetProximityRate (unsigned char ProximityRate);
+void ReadProxiOnDemand (unsigned int *ProxiValue);
+void Setup(void);
+
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+int main() {
+
+ Setup();
+
+ while (1) {
+
+ ReadAllSensors();
+
+ }
+}
+
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void Setup(void) {
+
+ pc.baud(115200);
+ // set BAUD
+ for (int i = 0; i < 4; i++){
+
+ reset = 0;
+ wait(0.001);
+ reset = 1;
+ wait(0.001);
+ port[0] = chan[i];
+ i2c.write(MUX,port, 1);
+ SetCurrent(20); // Set current to 200mA
+ SetCommandRegister (COMMAND_ALL_DISABLE); // ready for prox rate change
+ SetProximityRate (PROX_MEASUREMENT_RATE_31); // 31 measurements/s
+ wait(0.2);
+
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void Setmleds(void){
+ mled0 = mleds[0];
+ mled1 = mleds[1];
+ mled2 = mleds[2];
+ mled3 = mleds[3];
+ }
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void SwitchChannel(uint8_t chan){
+ reset = 0;
+ wait(0.001);
+ reset = 1;
+ wait(0.001);
+ port[0] = chan;
+ i2c.write(MUX,port, 1);
+ }
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void ReadAllSensors(void){
+
+ for (int i = 0; i < 4; i++){
+
+ SwitchChannel(chan[i]); // set channel
+ mleds[i] = 1; // LED on
+ Setmleds();
+ ReadProxiOnDemand(&sensors[i]); // read prox value on demand
+ mleds[i] = 0; // LED off
+ Setmleds();
+ }
+
+ for (int i = 0; i < 4; i++){
+ pc.printf("\r %i ", sensors[i]);
+ }
+ pc.printf("\r \n ");};
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void SetCommandRegister (unsigned char Command) {
+
+ //unsigned char send[2];
+
+ _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration reister
+ _send[1] = Command;
+ i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void ReadCommandRegister (unsigned char *Command) {
+
+ //unsigned char send[2];
+ //unsigned char receive[2];
+
+ _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration register
+ i2c.write(VCNL4020,_send, 1); // Write 1 byte on I2C
+ i2c.read(VCNL4020+1,_receive, 1); // Read 1 byte on I2C
+
+ *Command = (unsigned char)(_receive[0]);
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void SetCurrent (unsigned char Current) {
+
+ _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED Current register
+ _send[1] = Current;
+ i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C
+
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void ReadProxiValue (unsigned int *ProxiValue) {
+
+ _send[0] = REGISTER_PROX_VALUE; // VCNL40x0 Proximity Value register
+ i2c.write(VCNL4020, _send, 1); // Write 1 byte on I2C
+ i2c.read(VCNL4020+1, _receive, 2); // Read 2 bytes on I2C
+ *ProxiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void SetProximityRate (unsigned char ProximityRate) {
+
+ _send[0] = REGISTER_PROX_RATE; // VCNL40x0 Proximity rate register
+ _send[1] = ProximityRate;
+ i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void ReadProxiOnDemand (unsigned int *ProxiValue) {
+
+ unsigned char Command=0;
+
+ // enable prox value on demand
+ SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND);
+
+ // wait on prox data ready bit
+ do {
+ ReadCommandRegister (&Command); // read command register
+ } while (!(Command & COMMAND_MASK_PROX_DATA_READY));
+
+ ReadProxiValue (ProxiValue); // read prox value
+
+ SetCommandRegister (COMMAND_ALL_DISABLE); // stop prox value on demand
+
+}
\ No newline at end of file
