ff
Dependencies: vl53l0x_api libxDot-mbed5
main.cpp@3:83ec4984a7af, 2019-04-23 (annotated)
- Committer:
- mohamedachour
- Date:
- Tue Apr 23 14:27:14 2019 +0000
- Revision:
- 3:83ec4984a7af
- Parent:
- 2:8ac50bd2c7a1
hh
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mjarvisal | 0:b6867e1a23fa | 1 | #include "mbed.h" |
mjarvisal | 0:b6867e1a23fa | 2 | #include "vl53l0x_api.h" |
mjarvisal | 0:b6867e1a23fa | 3 | #include "vl53l0x_platform.h" |
mjarvisal | 0:b6867e1a23fa | 4 | #include "vl53l0x_i2c_platform.h" |
mjarvisal | 0:b6867e1a23fa | 5 | |
mohamedachour | 3:83ec4984a7af | 6 | #define USE_I2C_2V8 |
mohamedachour | 3:83ec4984a7af | 7 | |
andcor02 | 2:8ac50bd2c7a1 | 8 | Serial pc(USBTX, USBRX); |
andcor02 | 2:8ac50bd2c7a1 | 9 | DigitalOut led(LED1); |
mjarvisal | 0:b6867e1a23fa | 10 | |
mjarvisal | 0:b6867e1a23fa | 11 | |
mjarvisal | 0:b6867e1a23fa | 12 | VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) { |
mjarvisal | 0:b6867e1a23fa | 13 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; |
mjarvisal | 0:b6867e1a23fa | 14 | uint8_t NewDatReady=0; |
mjarvisal | 0:b6867e1a23fa | 15 | uint32_t LoopNb; |
mohamedachour | 3:83ec4984a7af | 16 | |
mjarvisal | 0:b6867e1a23fa | 17 | if (Status == VL53L0X_ERROR_NONE) { |
mjarvisal | 0:b6867e1a23fa | 18 | LoopNb = 0; |
mjarvisal | 0:b6867e1a23fa | 19 | do { |
mjarvisal | 0:b6867e1a23fa | 20 | Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady); |
mjarvisal | 0:b6867e1a23fa | 21 | if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) { |
mjarvisal | 0:b6867e1a23fa | 22 | break; |
mjarvisal | 0:b6867e1a23fa | 23 | } |
mjarvisal | 0:b6867e1a23fa | 24 | LoopNb = LoopNb + 1; |
mjarvisal | 0:b6867e1a23fa | 25 | VL53L0X_PollingDelay(Dev); |
mjarvisal | 0:b6867e1a23fa | 26 | } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP); |
mjarvisal | 0:b6867e1a23fa | 27 | |
mjarvisal | 0:b6867e1a23fa | 28 | if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) { |
mjarvisal | 0:b6867e1a23fa | 29 | Status = VL53L0X_ERROR_TIME_OUT; |
mjarvisal | 0:b6867e1a23fa | 30 | } |
mjarvisal | 0:b6867e1a23fa | 31 | } |
mjarvisal | 0:b6867e1a23fa | 32 | |
mjarvisal | 0:b6867e1a23fa | 33 | return Status; |
mjarvisal | 0:b6867e1a23fa | 34 | } |
mjarvisal | 0:b6867e1a23fa | 35 | |
mjarvisal | 0:b6867e1a23fa | 36 | VL53L0X_Error WaitStopCompleted(VL53L0X_DEV Dev) { |
mjarvisal | 0:b6867e1a23fa | 37 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; |
mjarvisal | 0:b6867e1a23fa | 38 | uint32_t StopCompleted=0; |
mjarvisal | 0:b6867e1a23fa | 39 | uint32_t LoopNb; |
mjarvisal | 0:b6867e1a23fa | 40 | |
mjarvisal | 0:b6867e1a23fa | 41 | if (Status == VL53L0X_ERROR_NONE) { |
mjarvisal | 0:b6867e1a23fa | 42 | LoopNb = 0; |
mjarvisal | 0:b6867e1a23fa | 43 | do { |
mjarvisal | 0:b6867e1a23fa | 44 | Status = VL53L0X_GetStopCompletedStatus(Dev, &StopCompleted); |
mjarvisal | 0:b6867e1a23fa | 45 | if ((StopCompleted == 0x00) || Status != VL53L0X_ERROR_NONE) { |
mjarvisal | 0:b6867e1a23fa | 46 | break; |
mjarvisal | 0:b6867e1a23fa | 47 | } |
mjarvisal | 0:b6867e1a23fa | 48 | LoopNb = LoopNb + 1; |
mjarvisal | 0:b6867e1a23fa | 49 | VL53L0X_PollingDelay(Dev); |
mjarvisal | 0:b6867e1a23fa | 50 | } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP); |
mjarvisal | 0:b6867e1a23fa | 51 | |
mjarvisal | 0:b6867e1a23fa | 52 | if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) { |
mjarvisal | 0:b6867e1a23fa | 53 | Status = VL53L0X_ERROR_TIME_OUT; |
mjarvisal | 0:b6867e1a23fa | 54 | } |
mjarvisal | 0:b6867e1a23fa | 55 | |
mjarvisal | 0:b6867e1a23fa | 56 | } |
mjarvisal | 0:b6867e1a23fa | 57 | |
mjarvisal | 0:b6867e1a23fa | 58 | return Status; |
mjarvisal | 0:b6867e1a23fa | 59 | } |
mjarvisal | 0:b6867e1a23fa | 60 | |
mjarvisal | 0:b6867e1a23fa | 61 | |
mohamedachour | 3:83ec4984a7af | 62 | |
mjarvisal | 0:b6867e1a23fa | 63 | int main() |
mjarvisal | 0:b6867e1a23fa | 64 | { |
mohamedachour | 3:83ec4984a7af | 65 | int x=1, measure=0; |
mohamedachour | 3:83ec4984a7af | 66 | int ave=0, sum=0; |
mjarvisal | 0:b6867e1a23fa | 67 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; |
mjarvisal | 0:b6867e1a23fa | 68 | VL53L0X_Dev_t MyDevice; |
mjarvisal | 0:b6867e1a23fa | 69 | VL53L0X_Dev_t *pMyDevice = &MyDevice; |
mohamedachour | 3:83ec4984a7af | 70 | VL53L0X_RangingMeasurementData_t RangingMeasurementData; |
mohamedachour | 3:83ec4984a7af | 71 | VL53L0X_RangingMeasurementData_t *pRangingMeasurementData = &RangingMeasurementData; |
mjarvisal | 0:b6867e1a23fa | 72 | VL53L0X_Version_t Version; |
mjarvisal | 0:b6867e1a23fa | 73 | |
mjarvisal | 0:b6867e1a23fa | 74 | // Initialize Comms |
mjarvisal | 0:b6867e1a23fa | 75 | pMyDevice->I2cDevAddr = 0x52; |
mjarvisal | 0:b6867e1a23fa | 76 | pMyDevice->comms_type = 1; |
mjarvisal | 0:b6867e1a23fa | 77 | pMyDevice->comms_speed_khz = 400; |
mjarvisal | 0:b6867e1a23fa | 78 | |
mohamedachour | 3:83ec4984a7af | 79 | |
mohamedachour | 3:83ec4984a7af | 80 | VL53L0X_ERROR_CONTROL_INTERFACE; |
mohamedachour | 3:83ec4984a7af | 81 | VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0); |
mohamedachour | 3:83ec4984a7af | 82 | VL53L0X_DataInit(&MyDevice); // Data initialization |
mohamedachour | 3:83ec4984a7af | 83 | Status = VL53L0X_ERROR_NONE; |
mohamedachour | 3:83ec4984a7af | 84 | uint32_t refSpadCount; |
mohamedachour | 3:83ec4984a7af | 85 | uint8_t isApertureSpads; |
mohamedachour | 3:83ec4984a7af | 86 | uint8_t VhvSettings; |
mohamedachour | 3:83ec4984a7af | 87 | uint8_t PhaseCal; |
mjarvisal | 0:b6867e1a23fa | 88 | |
mohamedachour | 3:83ec4984a7af | 89 | VL53L0X_StaticInit(pMyDevice); // Device Initialization |
mohamedachour | 3:83ec4984a7af | 90 | VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization |
mohamedachour | 3:83ec4984a7af | 91 | VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization |
mohamedachour | 3:83ec4984a7af | 92 | VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode |
mohamedachour | 3:83ec4984a7af | 93 | VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.25*65536)); //High Accuracy mode, see API PDF |
mohamedachour | 3:83ec4984a7af | 94 | VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(18*65536)); //High Accuracy mode, see API PDF |
mohamedachour | 3:83ec4984a7af | 95 | VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 200000); //High Accuracy mode, see API PDF |
mohamedachour | 3:83ec4984a7af | 96 | VL53L0X_StartMeasurement(pMyDevice); |
mjarvisal | 0:b6867e1a23fa | 97 | |
mohamedachour | 3:83ec4984a7af | 98 | while (1){ |
mohamedachour | 3:83ec4984a7af | 99 | while(x<=50){ |
mohamedachour | 3:83ec4984a7af | 100 | WaitMeasurementDataReady(pMyDevice); |
mohamedachour | 3:83ec4984a7af | 101 | VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData); |
mohamedachour | 3:83ec4984a7af | 102 | measure=pRangingMeasurementData->RangeMilliMeter; |
mohamedachour | 3:83ec4984a7af | 103 | //printf("In loop measurement %d\n", mea); |
mohamedachour | 3:83ec4984a7af | 104 | sum=sum+measure; |
mohamedachour | 3:83ec4984a7af | 105 | led=!led; |
mohamedachour | 3:83ec4984a7af | 106 | // Clear the interrupt |
mohamedachour | 3:83ec4984a7af | 107 | VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY); |
mohamedachour | 3:83ec4984a7af | 108 | VL53L0X_PollingDelay(pMyDevice); |
mohamedachour | 3:83ec4984a7af | 109 | x++; |
mjarvisal | 0:b6867e1a23fa | 110 | } |
mohamedachour | 3:83ec4984a7af | 111 | ave=sum/x; |
mohamedachour | 3:83ec4984a7af | 112 | pc.printf("Average is %d\n", ave); |
mohamedachour | 3:83ec4984a7af | 113 | x=1; |
mohamedachour | 3:83ec4984a7af | 114 | sum=0; |
mohamedachour | 3:83ec4984a7af | 115 | wait(20); |
mjarvisal | 0:b6867e1a23fa | 116 | } |
mjarvisal | 0:b6867e1a23fa | 117 | |
mohamedachour | 3:83ec4984a7af | 118 | VL53L0X_StopMeasurement(pMyDevice); |
mohamedachour | 3:83ec4984a7af | 119 | WaitStopCompleted(pMyDevice); |
mohamedachour | 3:83ec4984a7af | 120 | VL53L0X_ClearInterruptMask(pMyDevice,VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY); |
mohamedachour | 3:83ec4984a7af | 121 | |
mjarvisal | 0:b6867e1a23fa | 122 | } |
mjarvisal | 0:b6867e1a23fa | 123 |