stm32模块使用之vl53测距模块

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

本文主要做一下如何使用vl53的说明倾向于使用轮询方式。

一、vl53通信方式?

vl53模块和stm32可以通过管脚模拟I2C方式进行通信。
I2C具体方式为四根线,两根电源线接5v、GND,剩下两根一根是时序线SCL,另一根为数据线SDA.
这里我们通常选取两个本来不用的管脚去模拟SDL、SDA两根线。

二、具体使用方式

1. 初始化

VL53L1_Error VL53L1Init(VL53L1_Dev_t* pDev)
{

			VL53L1_Error Status = VL53L1_ERROR_NONE;
			pDev->I2cDevAddr=0x52;//默认地址
			pDev->comms_type=1;//默认通信模式
			pDev->comms_speed_khz = 400;//通信速率(可到400hz)
			pDev->Devnumber = 0;
	    VL53L1_GetDeviceInfo(pDev,&info_X);
			Status = VL53L1_WaitDeviceBooted(pDev);//启动设备,如在这一步出错,请检查线路连接是否正常,特别是电源线是否连接
	   // times_X = 0;//设置读取错误次数为0
			if(Status!=VL53L1_ERROR_NONE)
		{
			printf("Wait device Boot failed!\r\n");
			return Status;
		}
		else printf("device Boot success!");
		 HAL_Delay(2);

		Status = VL53L1_DataInit(pDev);//device init
		if(Status!=VL53L1_ERROR_NONE) 
		{
			printf("datainit failed!\r\n");
			return Status;
		}
  	else printf("device datainit success!");
		HAL_Delay(2);
		Status = VL53L1_StaticInit(pDev);
		if(Status!=VL53L1_ERROR_NONE) 
		{
			printf("static init failed!\r\n");
			return Status;
		}
		HAL_Delay(2);
		Status = VL53L1_SetDistanceMode(pDev, VL53L1_DISTANCEMODE_LONG);	//short,medium,long
		if(Status!=VL53L1_ERROR_NONE) 
		{
			printf("set discance mode failed!\r\n");
			return Status;
		}
		Status = VL53L1_StartMeasurement(pDev);
	if(Status!=VL53L1_ERROR_NONE) 
	{
		printf("start measurement failed!\r\n");
		return Status;
	}
	else printf("measurement success!");

		HAL_Delay(2);
		return Status;

}

2. 设置使用模式

VL53L1_Error VL53InitParam(VL53L1_Dev_t* pDev,uint8_t mode)
{

		VL53L1_Error status = VL53L1_ERROR_NONE;
    //4个限制
    status = VL53L1_SetLimitCheckEnable(pDev,VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,1);//sigma--standard deviation, enable SIGMA limit check
	if(status!=VL53L1_ERROR_NONE) 
		return status;
    HAL_Delay(2);
	status = VL53L1_SetLimitCheckEnable(pDev,VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,1);//signal--amplitude of the signal-
																																												//-reflected. enable signal rate limit check
	if(status!=VL53L1_ERROR_NONE) 
		return status;
	
	
	HAL_Delay(2);
	status = VL53L1_SetLimitCheckValue(pDev,VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,Mode_data[mode].sigmaLimit);//set SIGMA limit
	if(status!=VL53L1_ERROR_NONE) 
		return status;
	
	
	HAL_Delay(2);
	status = VL53L1_SetLimitCheckValue(pDev,VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,Mode_data[mode].signalLimit);//set signal rate limit
	if(status!=VL53L1_ERROR_NONE) 
		return status;
    //4个限制值设置完成
    
    
    status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(pDev,Mode_data[mode].timingBudget);//set the max interval for a whole diatance test
	if(status!=VL53L1_ERROR_NONE) 
		return status;
	HAL_Delay(2);
	status = VL53L1_SetInterMeasurementPeriodMilliSeconds(pDev, 300);
	if(status!=VL53L1_ERROR_NONE) 
	{
		printf("SetInterMeasurementPeriodMilliSeconds failed!\r\n");
		return status;
	}
	HAL_Delay(2);
	status = VL53L1_StartMeasurement(pDev);//更关键的是这一步,启动测量模式
	if(status!=VL53L1_ERROR_NONE) 
	{
		printf("start measurement failed!\r\n");
		return status;
	}
	else printf("measurement success!");
    return status; 

}

3. 获取距离

VL53L1_Error getDistance(VL53L1_Dev_t* pDev)
{
   
		VL53L1_Error status = VL53L1_ERROR_NONE;
    uint8_t isDataReady=1;
//    status = VL53L1_WaitMeasurementDataReady(pDev);
	 const Drive_times* chassis_time = get_chassis_times_Point(); 

    status = VL53L1_GetMeasurementDataReady(pDev,&isDataReady);//等待测量的数据准备好
    if(status!=VL53L1_ERROR_NONE) 
	{
		printf("Wait too long!\r\n");
//		times_X++;
		return status;
	}
//	else printf("Wait success!");
    if(!isDataReady)
    {
        flag++;
        return -7;
    }
    status = VL53L1_GetRangingMeasurementData(pDev, &result_data_X);
    distance = result_data_X.RangeMilliMeter;//得到距离测量值
		
    status = VL53L1_ClearInterruptAndStartMeasurement(pDev);
    return status;
	
}

4.问题

1.vl53是测量一个面,要搞清楚自己对于测距的真正需求
2.使用I2C就要保证vl53和板子要能在同时序下进行信息交互,所以要做好I2C_delay设置,即以下代码中的i的初值设定。

static void IIC_delay(void)
{
    uint16_t i=7;//针对168Mhz主频运行速率的延时
   while(i)
   {
     i--;
   }
}

总结

这里的vl53只是粗浅的使用,希望大家指正。

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值