GETTING CLOSER

Dependencies:   NeoStrip_mod VCNL4020_1 mbed

Files at this revision

API Documentation at this revision

Comitter:
ghostaudio
Date:
Sun Jun 01 19:21:57 2014 +0000
Parent:
0:5db0fea04a3d
Commit message:
think this is working, depending on hardware!

Changed in this revision

VCNL40x0.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/VCNL40x0.lib	Fri May 30 16:42:55 2014 +0000
+++ b/VCNL40x0.lib	Sun Jun 01 19:21:57 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/ghostaudio/code/VCNL40x0_1/#5300841280d9
+http://mbed.org/users/ghostaudio/code/VCNL4020_1/#f79965668595
--- a/main.cpp	Fri May 30 16:42:55 2014 +0000
+++ b/main.cpp	Sun Jun 01 19:21:57 2014 +0000
@@ -26,10 +26,11 @@
 const uint8_t chan[] = {chan0,chan1,chan2,chan3};
 int mleds[] = {0,0,0,0};
 
-void ReadOne(uint8_t channel);
 void Setup(void);
+void SetupAllChannels(void);
 void Setmleds(void);
 void SwitchChannel(uint8_t chan);
+void ReadOne(uint8_t channel);
 void ReadAllSensors(void);
 void Setmleds(void);
 void SetProximityRate (unsigned char ProximityRate);
@@ -39,17 +40,19 @@
 void ReadCurrent (unsigned char *Current);
 void ReadProxiValue (unsigned int *ProxiValue);
 void SetProximityRate (unsigned char ProximityRate);
+void SetInterruptControl (unsigned char InterruptControl);
+void ReadInterruptControl (unsigned char *InterruptControl);
 void ReadProxiOnDemand (unsigned int *ProxiValue);
-void SetupAll(void);
+
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 int main() {
-    
+    wait_ms(5);
     //SwitchChannel(chan1);            // set channel
     //wait_ms(50);
     //Setup();
-    SetupAll();
+    SetupAllChannels();
     
     while (1) {
         
@@ -58,20 +61,35 @@
     }
 }
 
-void ReadOne(uint8_t channel){
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+ 
+// setup for VCNL4020. 
+    
+void Setup(void) {
+    unsigned char Current=0;
+    
     
-    unsigned int sensor = 0; 
-    SwitchChannel(channel);            // set channel
-        mleds[0] = 1;                      // LED on
-        Setmleds();
-        ReadProxiOnDemand(&sensor);    // read prox value on demand
-        mleds[0] = 0;                      // LED off
-        Setmleds();   
-        pc.printf("\r %i \n", sensor);
+ 
+    SetCurrent(20);                              // Set current to 200mA
+    ReadCurrent(&Current);
+    pc.printf("\n IR LED Current: %d\n", Current);
+    SetCommandRegister (COMMAND_ALL_DISABLE);    // ready for prox rate change
+    SetProximityRate (PROX_MEASUREMENT_RATE_31); // 31 measurements/s
+    SetInterruptControl (INTERRUPT_THRES_SEL_PROX |INTERRUPT_THRES_ENABLE |INTERRUPT_COUNT_EXCEED_1);
+    SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_SELFTIMED_MODE_ENABLE);
+    pc.printf("\r done\n");
+    wait(0.2);
 }
+
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-void SetupAll(void){
+
+// Setup all channels sequentially. 
+
+void SetupAllChannels(void){
     
+    
+    pc.baud(115200);
+    i2c.frequency(1000000);   
     SwitchChannel(chan0);            // set channel
     wait_ms(20);
     Setup();
@@ -87,22 +105,9 @@
     pc.printf("\r setup ok \n ");
     }
     
-    
-void Setup(void) {
-    unsigned char Current=0;
-    
-    pc.baud(115200);    
-    SetCurrent(20);                              // Set current to 200mA
-    ReadCurrent(&Current);
-    pc.printf("\n IR LED Current: %d\n", Current);
-    SetCommandRegister (COMMAND_ALL_DISABLE);    // ready for prox rate change
-    SetProximityRate (PROX_MEASUREMENT_RATE_31); // 31 measurements/s
-    SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_SELFTIMED_MODE_ENABLE);
-    pc.printf("\r done\n");
-    wait(0.2);
-}
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// mbed leds for some visual recognition.
 
 void Setmleds(void){
     mled0 = mleds[0];
@@ -113,17 +118,41 @@
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
+// reset MUX and choose channel.
+
 void SwitchChannel(uint8_t chan){
+        //i2c.start();
         reset = 0;
         wait_ms(10);
         reset = 1;
         wait_ms(10);    
         port[0] = chan;
         i2c.write(MUX,port, 1);
+        //i2c.stop();
     }
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
+// Choose on channel and read proximity from that channel.
+
+void ReadOne(uint8_t channel){
+    
+    unsigned int sensor = 0; 
+    SwitchChannel(channel);            // set channel
+        i2c.start();
+        mleds[0] = 1;                      // LED on
+        Setmleds();
+        ReadProxiOnDemand(&sensor);    // read prox value on demand
+        mleds[0] = 0;                      // LED off
+        Setmleds();   
+        pc.printf("\r %i \n", sensor);
+        i2c.stop();
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+// Read all sensors sequentially
+
 void ReadAllSensors(void){
     
     for (int i = 0; i < 4; i++){
@@ -131,14 +160,17 @@
         unsigned int sensor = 0; 
         
         SwitchChannel(chan[i]);            // set channel
+        //i2c.start();
         mleds[i] = 1;                      // LED on
         Setmleds();
-        ReadProxiOnDemand(&sensor);    // read prox value on demand
+        //ReadProxiValue(&sensor);
+        ReadProxiOnDemand(&sensor);        // read prox value on demand
         wait_ms(10); 
         mleds[i] = 0;                      // LED off
         Setmleds();   
         sensors[i] = sensor;
         pc.printf("\r %i ", sensors[i]);
+        //i2c.stop();
         }
         
     pc.printf("\r \n ");
@@ -149,23 +181,18 @@
 
 void SetCommandRegister (unsigned char Command) {
     
-    //unsigned char send[2];
-    
-    _send[0] = REGISTER_COMMAND;                   // VCNL40x0 Configuration reister
+    _send[0] = REGISTER_COMMAND;            // VCNL40x0 Configuration reister
     _send[1] = Command;
-    i2c.write(VCNL4020,_send, 2);                  // Write 2 bytes on I2C
+    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
+    _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]);
 }
@@ -212,11 +239,33 @@
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
+void SetInterruptControl (unsigned char InterruptControl) {
+
+    _send[0] = REGISTER_INTERRUPT_CONTROL;                  // VCNL40x0 Interrupt Control register
+    _send[1] = InterruptControl;
+    i2c.write(VCNL4020,_send, 2);                  // Write 2 bytes on I2C
+
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+void ReadInterruptControl (unsigned char *InterruptControl) {
+
+    _send[0] = REGISTER_INTERRUPT_CONTROL;                  // VCNL40x0 Interrupt Control register
+    i2c.write(VCNL4020,_send, 1);                  // Write 1 byte on I2C
+    i2c.read(VCNL4020+1,_receive, 1);              // Read 1 byte on I2C
+
+    *InterruptControl = (unsigned char)(_receive[0]);
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
 void ReadProxiOnDemand (unsigned int *ProxiValue) {
 
     unsigned char Command=0;
-
-    // enable prox value on demand
+    
+    // SetCommandRegister (COMMAND_ALL_DISABLE);
+    
     SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND);
  
     // wait on prox data ready bit