GY-VL53L0X 测试代码

[复制链接]
2329|17
 楼主 | 2017-4-9 12:02 | 显示全部楼层 |阅读模式
GY-VL53L0X 测试代码      
 楼主 | 2017-4-9 12:04 | 显示全部楼层
#include "stm32f10x.h"
#include "sysTick.h"
#include "led.h"
#include "oled.h"
#include "iic.h"

#include "vl53l0x_api.h"
#include "vl53l0x_platform.h"
#include "vl53l0x_i2c_platform.h"
 楼主 | 2017-4-9 12:05 | 显示全部楼层
//变量
VL53L0X_Dev_t vl53l0x_dev;
VL53L0X_DeviceInfo_t vl53l0x_dev_info;
VL53L0X_RangingMeasurementData_t vl53l0x_data;
VL53L0X_Error vl53l0x_status;

//函数
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);

//打印错误
void print_pal_error(VL53L0X_Error Status)
 楼主 | 2017-4-9 12:06 | 显示全部楼层
{
    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;
    }
 楼主 | 2017-4-9 12:07 | 显示全部楼层
//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;
    }
 楼主 | 2017-4-9 12:11 | 显示全部楼层
    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;
    }

    // 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;
    }
 楼主 | 2017-4-9 12:11 | 显示全部楼层
// 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;
    }

    return Status;
}

//传感器测试
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;
    }
 楼主 | 2017-4-9 12:12 | 显示全部楼层
// 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)
                {
            //指示灯闪缩
            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************/
| 2017-4-9 12:32 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
| 2017-4-17 19:38 | 显示全部楼层
GY-VL53L0X,这是个什么外设呢?没有见过
| 2017-4-19 22:17 | 显示全部楼层
magicoctoy 发表于 2017-4-17 19:38
GY-VL53L0X,这是个什么外设呢?没有见过

这是一个激光的模块,应该是用来测距离的。
| 2017-4-19 22:24 | 显示全部楼层
这个出来是什么信号呢?可以测量多远的距离呢?
| 2017-5-18 14:54 | 显示全部楼层
叶覃 发表于 2017-4-19 22:24
这个出来是什么信号呢?可以测量多远的距离呢?

出来应该就是数字信号,官方标称是2m但是一般程序里好像都是1.2m
| 2017-7-25 22:33 | 显示全部楼层
楼主英明,请问如何在ST官网下载GY-VL53L0X的API
| 2017-8-5 20:21 | 显示全部楼层
赞赞赞,楼主大大的好人
| 2017-8-5 20:29 | 显示全部楼层
请问楼主大人可以把程序打包发一份么,那样方便些,麻烦楼主了
| 2017-8-13 23:47 | 显示全部楼层
god9987 发表于 2017-4-9 12:12
// New Range Status: data is valid when pdata->RangeStatus = 0
    RangeStatus = pdata->RangeStatus ...

楼主大大,请教你一个问题。就是我导入API库编译后,提示#include <Windows.h>找不到,这个/#include <Windows.h>是在哪里的?
| 2018-2-24 22:58 | 显示全部楼层
我试着调用这个程序还有加入了API的.h文件,但是编译出错  大神知道什么原因嘛
用的PIC单片机

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
扫描二维码,随时随地手机跟帖
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复

您需要登录后才可以回帖
登录 | 注册
高级模式
我要创建版块 申请成为版主

论坛热帖

关闭

热门推荐上一条 /4 下一条

分享 快速回复 返回顶部 返回列表