Code-Basis für Programm zum Taster-Entprellen

Dependencies:   SB1602E mbed

Fork of Laser-Distance by Stefan Kummer

Revision:
5:1fca2683ae6f
Parent:
4:ef7abafa9884
Child:
8:0f74264cc38a
diff -r ef7abafa9884 -r 1fca2683ae6f main.cpp
--- a/main.cpp	Thu Nov 02 10:55:17 2017 +0000
+++ b/main.cpp	Thu Nov 02 11:26:33 2017 +0000
@@ -1,20 +1,115 @@
 #include "mbed.h"
 #include "C12832.h"
-#include "MMA7660.h"
+#include "vl53l0x_api.h"
+#include "vl53l0x_platform.h"
+#include "vl53l0x_i2c_platform.h"
+
+#define USE_I2C_2V8
 
 /* Sets up LCD and prints sensor data value of Indoor Air Quality sensor to LCD */
+C12832 lcd(PE_14, PE_12, PD_12, PD_11, PE_9); //LCD: MOSI, SCK, RESET, A0, nCS
 
-C12832 lcd(PE_14, PE_12, PD_12, PD_11, PE_9); //LCD: MOSI, SCK, RESET, A0, nCS
-MMA7660 MMA(PF_0, PF_1); //ACCELEROMETER: I2C_SDA, I2C_SCL 
+VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) {
+    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
+    uint8_t NewDatReady=0;
+    uint32_t LoopNb;
+    
+    if (Status == VL53L0X_ERROR_NONE) {
+        LoopNb = 0;
+        do {
+            Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady);
+            if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) {
+                break;
+            }
+            LoopNb = LoopNb + 1;
+            VL53L0X_PollingDelay(Dev);
+        } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
+
+        if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
+            Status = VL53L0X_ERROR_TIME_OUT;
+        }
+    }
+
+    return Status;
+}
+
+VL53L0X_Error WaitStopCompleted(VL53L0X_DEV Dev) {
+    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
+    uint32_t StopCompleted=0;
+    uint32_t LoopNb;
+
+    if (Status == VL53L0X_ERROR_NONE) {
+        LoopNb = 0;
+        do {
+            Status = VL53L0X_GetStopCompletedStatus(Dev, &StopCompleted);
+            if ((StopCompleted == 0x00) || Status != VL53L0X_ERROR_NONE) {
+                break;
+            }
+            LoopNb = LoopNb + 1;
+            VL53L0X_PollingDelay(Dev);
+        } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
+
+        if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
+            Status = VL53L0X_ERROR_TIME_OUT;
+        }
+
+    }
+
+    return Status;
+}
 
 int main()
 {
+    lcd.cls();
+    
+    //Setup laser
+    int var=1, measure=0;
+    int ave=0, sum=0;
+    VL53L0X_Dev_t MyDevice;
+    VL53L0X_Dev_t *pMyDevice = &MyDevice;
+    VL53L0X_RangingMeasurementData_t    RangingMeasurementData;
+    VL53L0X_RangingMeasurementData_t   *pRangingMeasurementData    = &RangingMeasurementData;
+    
+    // Initialize Comms laster
+    pMyDevice->I2cDevAddr      = 0x52;
+    pMyDevice->comms_type      =  1;
+    pMyDevice->comms_speed_khz =  400;
+    
+    
+    VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0);
+    VL53L0X_DataInit(&MyDevice); 
+    uint32_t refSpadCount;
+    uint8_t isApertureSpads;
+    uint8_t VhvSettings;
+    uint8_t PhaseCal;
+    
+    VL53L0X_StaticInit(pMyDevice); 
+    VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization
+    VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization
+    VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode
+    VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.25*65536)); //High Accuracy mode, see API PDF
+    VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(18*65536)); //High Accuracy mode, see API PDF
+    VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 200000); //High Accuracy mode, see API PDF
+    VL53L0X_StartMeasurement(pMyDevice);
+    
     while(1) {
         lcd.cls();
         lcd.locate(0,3);
-        lcd.printf("[ACCEL]");
-        lcd.locate(0,14);
-        lcd.printf("x=%.2f y=%.2f z=%.2f",MMA.x(), MMA.y(), MMA.z()); // Print to LCD values
+        lcd.printf("[DISTANCE]");
+            while(var<=10){
+                WaitMeasurementDataReady(pMyDevice);
+                VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData);
+                measure=pRangingMeasurementData->RangeMilliMeter;
+                sum=sum+measure;
+                VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
+                VL53L0X_PollingDelay(pMyDevice);
+                var++;
+                }
+        ave=sum/var;
+        var=1;
+        sum=0; 
+        lcd.locate(0,15);
+        lcd.printf("%dmm", ave); // Print to LCD values
         wait(1);
     }
 }
\ No newline at end of file