打印

GY-VL53L0X 测试代码

[复制链接]
1185|8
手机看帖
扫描二维码
随时随地手机跟帖
沙发
god9987|  楼主 | 2017-8-26 21:08 | 只看该作者
//变量
VL53L0X_Dev_t vl53l0x_dev;
VL53L0X_DeviceInfo_t vl53l0x_dev_info;
VL53L0X_RangingMeasurementData_t vl53l0x_data;
VL53L0X_Error vl53l0x_status;

使用特权

评论回复
板凳
god9987|  楼主 | 2017-8-26 21:08 | 只看该作者
//函数
VL53L0X_Error vl53l0x_init(void);
VL53L0X_Error vl53l0x_measure_init(VL53L0X_Dev_t *pMyDevice);
VL53L0X_Error vl53l0x_start_single_test(VL53L0X_Dev_t *pdev,VL53L0X_RangingMeasurementData_t *pdata);

使用特权

评论回复
地板
god9987|  楼主 | 2017-8-26 21:09 | 只看该作者
//打印错误
void print_pal_error(VL53L0X_Error Status)
{
    char buf[VL53L0X_MAX_STRING_LENGTH];
    VL53L0X_GetPalErrorString(Status, buf);
    OLED_printf(0,0,font_6x8,"API Status: %i : %s\n", Status, buf);
}

VL53L0X_Error vl53l0x_init(void)
{
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
    VL53L0X_Dev_t *pMyDevice = &vl53l0x_dev;
    pMyDevice->I2cDevAddr      = 0x52;
    pMyDevice->comms_type      =  1;
    pMyDevice->comms_speed_khz =  400;
   
    VL53L0X_i2c_init();

    Status = VL53L0X_DataInit(pMyDevice); // Data initialization
    if(Status != VL53L0X_ERROR_NONE){
        print_pal_error(Status);
        return Status;
    }
    Status = VL53L0X_GetDeviceInfo(pMyDevice, &vl53l0x_dev_info);
    if(Status != VL53L0X_ERROR_NONE){
        print_pal_error(Status);
        return Status;
    }
    //OLED_printf(0,0,font_6x8,"VL53L0X_GetDeviceInfo:\n");
    //OLED_printf(0,1,font_6x8,"Device Name : %s\n", vl53l0x_dev_info.Name);
    //OLED_printf(0,2,font_6x8,"Device Type : %s\n", vl53l0x_dev_info.Type);
    //OLED_printf(0,3,font_6x8,"Device ID : %s\n", vl53l0x_dev_info.ProductId);
    //OLED_printf(0,4,font_6x8,"ProductRevisionMajor : %d\n", vl53l0x_dev_info.ProductRevisionMajor);
    //OLED_printf(0,5,font_6x8,"ProductRevisionMinor : %d\n", vl53l0x_dev_info.ProductRevisionMinor);

    if ((vl53l0x_dev_info.ProductRevisionMajor != 1) && (vl53l0x_dev_info.ProductRevisionMinor != 1)){
        OLED_printf(0,0,font_6x8,"Error expected cut 1.1 but found cut %d.%d\n",vl53l0x_dev_info.ProductRevisionMajor, vl53l0x_dev_info.ProductRevisionMinor);
        Status = VL53L0X_ERROR_NOT_SUPPORTED;
        print_pal_error(Status);
        return Status;
    }

    Status = vl53l0x_measure_init(pMyDevice);
    vl53l0x_status = Status;
    if(Status != VL53L0X_ERROR_NONE){
        print_pal_error(Status);
        return Status;
    }

使用特权

评论回复
5
god9987|  楼主 | 2017-8-26 21:10 | 只看该作者

    return Status;
}


VL53L0X_Error vl53l0x_measure_init(VL53L0X_Dev_t *pMyDevice)
{
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;

    //FixPoint1616_t LimitCheckCurrent;
    uint32_t refSpadCount;
    uint8_t isApertureSpads;
    uint8_t VhvSettings;
    uint8_t PhaseCal;
    // Device Initialization
    Status = VL53L0X_StaticInit(pMyDevice);
    if(Status != VL53L0X_ERROR_NONE){
        //printf ("Call of VL53L0X_StaticInit\n");
        print_pal_error(Status);
        return Status;
    }
    // Device Initialization
    Status = VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal);
    if(Status != VL53L0X_ERROR_NONE){
        //printf ("Call of VL53L0X_PerformRefCalibration\n");
        print_pal_error(Status);
        return Status;
    }

    // needed if a coverglass is used and no calibration has been performed
    Status = VL53L0X_PerformRefSpadManagement(pMyDevice,&refSpadCount, &isApertureSpads);
    if(Status != VL53L0X_ERROR_NONE){
        //printf ("Call of VL53L0X_PerformRefSpadManagement\n");
        //printf ("refSpadCount = %d, isApertureSpads = %d\n", refSpadCount, isApertureSpads);
        print_pal_error(Status);
        return Status;
    }

使用特权

评论回复
6
god9987|  楼主 | 2017-8-26 21:10 | 只看该作者
  // no need to do this when we use VL53L0X_PerformSingleRangingMeasurement
    Status = VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_SINGLE_RANGING); // Setup in single ranging mode
    if(Status != VL53L0X_ERROR_NONE){
        //printf ("Call of VL53L0X_SetDeviceMode\n");
        print_pal_error(Status);
        return Status;
    }

    // Enable/Disable Sigma and Signal check
    if(Status == VL53L0X_ERROR_NONE){
        Status = VL53L0X_SetLimitCheckEnable(pMyDevice,
                        VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1);
    }
    if(Status == VL53L0X_ERROR_NONE){
        Status = VL53L0X_SetLimitCheckEnable(pMyDevice,
                        VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1);
    }
                               
    if(Status == VL53L0X_ERROR_NONE){
        Status = VL53L0X_SetLimitCheckValue(pMyDevice,
                        VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
                        (FixPoint1616_t)(0.25*65536));
        }                       
    if(Status == VL53L0X_ERROR_NONE){
        Status = VL53L0X_SetLimitCheckValue(pMyDevice,
                        VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,
                        (FixPoint1616_t)(18*65536));                       
    }
    if(Status == VL53L0X_ERROR_NONE){
        Status = VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice,
                        200000);
    }
    if(Status != VL53L0X_ERROR_NONE){
        //printf ("Sigma and Signal check error\n");
        print_pal_error(Status);
        return Status;
    }

使用特权

评论回复
7
god9987|  楼主 | 2017-8-26 21:11 | 只看该作者

//传感器测试
VL53L0X_Error vl53l0x_start_single_test(VL53L0X_Dev_t *pdev,VL53L0X_RangingMeasurementData_t *pdata)
{
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
    uint8_t RangeStatus;
    char buf[VL53L0X_MAX_STRING_LENGTH];
   
    if(vl53l0x_status != VL53L0X_ERROR_NONE)
        return vl53l0x_status;

    status = VL53L0X_PerformSingleRangingMeasurement(pdev, pdata);
    if(status != VL53L0X_ERROR_NONE){
        //printf("error:Call of VL53L0X_PerformSingleRangingMeasurement\n");
        return status;
    }
   
    // New Range Status: data is valid when pdata->RangeStatus = 0
    RangeStatus = pdata->RangeStatus;
    VL53L0X_GetRangeStatusString(RangeStatus, buf);
    OLED_printf(0,0,font_6x8,"Rg:%i:%s\n", RangeStatus, buf);//打印状态
    OLED_printf(0,1,font_6x8,"%4i",vl53l0x_data.RangeMilliMeter);//打印距离
   
    return status;
}

int main(void)
{                
        //芯片初始化
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);       
        GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); /*使能SWD 禁用JTAG*/
   
    //外设初始化
        delay_init();
        led_init();
    OLED_init();
    iic_init();
    vl53l0x_init();
        while(1)
        {          
        Loop_Begin(100)

使用特权

评论回复
8
god9987|  楼主 | 2017-8-26 21:11 | 只看该作者
  //指示灯闪缩
            LED0=!LED0;
            
             if(vl53l0x_status == VL53L0X_ERROR_NONE)
             {
                vl53l0x_start_single_test(&vl53l0x_dev, &vl53l0x_data);
             }
            
                       
                }Loop_End()
        
    }
}


/******************* (C) COPYRIGHT 2017 lincongcong *****END OF FILE************/

使用特权

评论回复
9
stm32lover| | 2017-8-26 21:35 | 只看该作者
GY-VL53L0X 测试代码怎么样呀?谢谢分享

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

54

主题

1028

帖子

0

粉丝