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: C12832 Sht31 vl53l0x_api
Fork of mbed-cloud-connect-sensor-accelerometer by
main.cpp
- Committer:
- andcor02
- Date:
- 2017-11-02
- Revision:
- 5:1fca2683ae6f
- Parent:
- 4:ef7abafa9884
File content as of revision 5:1fca2683ae6f:
#include "mbed.h"
#include "C12832.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
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("[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);
}
}
