#1

Dependencies:   mbed SB1602E

Fork of mbed-cloud-connect-sensor-laser-distance by Andrea Corrado

Committer:
andcor02
Date:
Thu Nov 02 11:26:33 2017 +0000
Revision:
5:1fca2683ae6f
Parent:
4:ef7abafa9884
Child:
8:0f74264cc38a
Sets up LCD and prints sensor data value of VL53L0X laser distance sensor to LCD

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andcor02 0:80032665d37e 1 #include "mbed.h"
andcor02 0:80032665d37e 2 #include "C12832.h"
andcor02 5:1fca2683ae6f 3 #include "vl53l0x_api.h"
andcor02 5:1fca2683ae6f 4 #include "vl53l0x_platform.h"
andcor02 5:1fca2683ae6f 5 #include "vl53l0x_i2c_platform.h"
andcor02 5:1fca2683ae6f 6
andcor02 5:1fca2683ae6f 7 #define USE_I2C_2V8
andcor02 0:80032665d37e 8
andcor02 0:80032665d37e 9 /* Sets up LCD and prints sensor data value of Indoor Air Quality sensor to LCD */
andcor02 5:1fca2683ae6f 10 C12832 lcd(PE_14, PE_12, PD_12, PD_11, PE_9); //LCD: MOSI, SCK, RESET, A0, nCS
andcor02 0:80032665d37e 11
andcor02 5:1fca2683ae6f 12 VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) {
andcor02 5:1fca2683ae6f 13 VL53L0X_Error Status = VL53L0X_ERROR_NONE;
andcor02 5:1fca2683ae6f 14 uint8_t NewDatReady=0;
andcor02 5:1fca2683ae6f 15 uint32_t LoopNb;
andcor02 5:1fca2683ae6f 16
andcor02 5:1fca2683ae6f 17 if (Status == VL53L0X_ERROR_NONE) {
andcor02 5:1fca2683ae6f 18 LoopNb = 0;
andcor02 5:1fca2683ae6f 19 do {
andcor02 5:1fca2683ae6f 20 Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady);
andcor02 5:1fca2683ae6f 21 if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) {
andcor02 5:1fca2683ae6f 22 break;
andcor02 5:1fca2683ae6f 23 }
andcor02 5:1fca2683ae6f 24 LoopNb = LoopNb + 1;
andcor02 5:1fca2683ae6f 25 VL53L0X_PollingDelay(Dev);
andcor02 5:1fca2683ae6f 26 } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
andcor02 5:1fca2683ae6f 27
andcor02 5:1fca2683ae6f 28 if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
andcor02 5:1fca2683ae6f 29 Status = VL53L0X_ERROR_TIME_OUT;
andcor02 5:1fca2683ae6f 30 }
andcor02 5:1fca2683ae6f 31 }
andcor02 5:1fca2683ae6f 32
andcor02 5:1fca2683ae6f 33 return Status;
andcor02 5:1fca2683ae6f 34 }
andcor02 5:1fca2683ae6f 35
andcor02 5:1fca2683ae6f 36 VL53L0X_Error WaitStopCompleted(VL53L0X_DEV Dev) {
andcor02 5:1fca2683ae6f 37 VL53L0X_Error Status = VL53L0X_ERROR_NONE;
andcor02 5:1fca2683ae6f 38 uint32_t StopCompleted=0;
andcor02 5:1fca2683ae6f 39 uint32_t LoopNb;
andcor02 5:1fca2683ae6f 40
andcor02 5:1fca2683ae6f 41 if (Status == VL53L0X_ERROR_NONE) {
andcor02 5:1fca2683ae6f 42 LoopNb = 0;
andcor02 5:1fca2683ae6f 43 do {
andcor02 5:1fca2683ae6f 44 Status = VL53L0X_GetStopCompletedStatus(Dev, &StopCompleted);
andcor02 5:1fca2683ae6f 45 if ((StopCompleted == 0x00) || Status != VL53L0X_ERROR_NONE) {
andcor02 5:1fca2683ae6f 46 break;
andcor02 5:1fca2683ae6f 47 }
andcor02 5:1fca2683ae6f 48 LoopNb = LoopNb + 1;
andcor02 5:1fca2683ae6f 49 VL53L0X_PollingDelay(Dev);
andcor02 5:1fca2683ae6f 50 } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
andcor02 5:1fca2683ae6f 51
andcor02 5:1fca2683ae6f 52 if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
andcor02 5:1fca2683ae6f 53 Status = VL53L0X_ERROR_TIME_OUT;
andcor02 5:1fca2683ae6f 54 }
andcor02 5:1fca2683ae6f 55
andcor02 5:1fca2683ae6f 56 }
andcor02 5:1fca2683ae6f 57
andcor02 5:1fca2683ae6f 58 return Status;
andcor02 5:1fca2683ae6f 59 }
andcor02 0:80032665d37e 60
andcor02 0:80032665d37e 61 int main()
andcor02 0:80032665d37e 62 {
andcor02 5:1fca2683ae6f 63 lcd.cls();
andcor02 5:1fca2683ae6f 64
andcor02 5:1fca2683ae6f 65 //Setup laser
andcor02 5:1fca2683ae6f 66 int var=1, measure=0;
andcor02 5:1fca2683ae6f 67 int ave=0, sum=0;
andcor02 5:1fca2683ae6f 68 VL53L0X_Dev_t MyDevice;
andcor02 5:1fca2683ae6f 69 VL53L0X_Dev_t *pMyDevice = &MyDevice;
andcor02 5:1fca2683ae6f 70 VL53L0X_RangingMeasurementData_t RangingMeasurementData;
andcor02 5:1fca2683ae6f 71 VL53L0X_RangingMeasurementData_t *pRangingMeasurementData = &RangingMeasurementData;
andcor02 5:1fca2683ae6f 72
andcor02 5:1fca2683ae6f 73 // Initialize Comms laster
andcor02 5:1fca2683ae6f 74 pMyDevice->I2cDevAddr = 0x52;
andcor02 5:1fca2683ae6f 75 pMyDevice->comms_type = 1;
andcor02 5:1fca2683ae6f 76 pMyDevice->comms_speed_khz = 400;
andcor02 5:1fca2683ae6f 77
andcor02 5:1fca2683ae6f 78
andcor02 5:1fca2683ae6f 79 VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0);
andcor02 5:1fca2683ae6f 80 VL53L0X_DataInit(&MyDevice);
andcor02 5:1fca2683ae6f 81 uint32_t refSpadCount;
andcor02 5:1fca2683ae6f 82 uint8_t isApertureSpads;
andcor02 5:1fca2683ae6f 83 uint8_t VhvSettings;
andcor02 5:1fca2683ae6f 84 uint8_t PhaseCal;
andcor02 5:1fca2683ae6f 85
andcor02 5:1fca2683ae6f 86 VL53L0X_StaticInit(pMyDevice);
andcor02 5:1fca2683ae6f 87 VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization
andcor02 5:1fca2683ae6f 88 VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization
andcor02 5:1fca2683ae6f 89 VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode
andcor02 5:1fca2683ae6f 90 VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.25*65536)); //High Accuracy mode, see API PDF
andcor02 5:1fca2683ae6f 91 VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(18*65536)); //High Accuracy mode, see API PDF
andcor02 5:1fca2683ae6f 92 VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 200000); //High Accuracy mode, see API PDF
andcor02 5:1fca2683ae6f 93 VL53L0X_StartMeasurement(pMyDevice);
andcor02 5:1fca2683ae6f 94
andcor02 0:80032665d37e 95 while(1) {
andcor02 3:1ed9495c9158 96 lcd.cls();
andcor02 0:80032665d37e 97 lcd.locate(0,3);
andcor02 5:1fca2683ae6f 98 lcd.printf("[DISTANCE]");
andcor02 5:1fca2683ae6f 99 while(var<=10){
andcor02 5:1fca2683ae6f 100 WaitMeasurementDataReady(pMyDevice);
andcor02 5:1fca2683ae6f 101 VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData);
andcor02 5:1fca2683ae6f 102 measure=pRangingMeasurementData->RangeMilliMeter;
andcor02 5:1fca2683ae6f 103 sum=sum+measure;
andcor02 5:1fca2683ae6f 104 VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
andcor02 5:1fca2683ae6f 105 VL53L0X_PollingDelay(pMyDevice);
andcor02 5:1fca2683ae6f 106 var++;
andcor02 5:1fca2683ae6f 107 }
andcor02 5:1fca2683ae6f 108 ave=sum/var;
andcor02 5:1fca2683ae6f 109 var=1;
andcor02 5:1fca2683ae6f 110 sum=0;
andcor02 5:1fca2683ae6f 111 lcd.locate(0,15);
andcor02 5:1fca2683ae6f 112 lcd.printf("%dmm", ave); // Print to LCD values
andcor02 4:ef7abafa9884 113 wait(1);
andcor02 0:80032665d37e 114 }
andcor02 2:587b4d7444d1 115 }