//四个VL53L0挂载在同一个IIC总线下,所以使用四个片选信号--2019/10/30
//!!!!!!!注意:重新使能设备后,设备iic的地址会恢复为默认值0x52--2019/10/30
//VL53L0 0
//片选使能--2019/10/30
void i2c_init(void);
uint8_t i2c_write(uint8_t addr, uint8_t reg, uint32_t len, uint8_t * data);
uint8_t i2c_read(uint8_t addr, uint8_t reg, uint32_t len, uint8_t *buf);
void i2c_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
//模拟iic配置
GPIO_InitStructure.GPIO_Pin = I2C_PIN_SCL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(I2C_SCL_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = I2C_PIN_SDA;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(I2C_SDA_GPIO, &GPIO_InitStructure);
//片选使能配置
GPIO_InitStructure.GPIO_Pin = I2C_PIN_X0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(I2C_X_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = I2C_PIN_X1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(I2C_X_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = I2C_PIN_X2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(I2C_X_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = I2C_PIN_X3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(I2C_X_GPIO, &GPIO_InitStructure);
I2C_X0_LOW();
I2C_X1_LOW();
I2C_X2_LOW();
I2C_X3_LOW();
delay_ms(20);
}
VL53L0X_Error vl53l0x_init(void)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE; //初始值赋值为0
//初始化一定按照这个顺序执行,否则不成功
VL53L0X_i2c_init();
vl53l0x_initX(&vl53l0x_dev0,0);
vl53l0x_initX(&vl53l0x_dev1,1);
vl53l0x_initX(&vl53l0x_dev2,2);
vl53l0x_initX(&vl53l0x_dev3,3);
return Status; //返回0
}
//单个VL53L0初始化
VL53L0X_Error vl53l0x_initX( VL53L0X_Dev_t *pMyDevice ,u8 vl53l0_x_id)
{
VL53L0X_Error Status = VL53L0X_ERROR_NONE; //初始值赋值为0
pMyDevice->I2cDevAddr = 0x52; //iic地址 0x52是默认地址,要初始化必须先写0x52,才能初始化,之后再通过软件修改
pMyDevice->comms_type = 1; //选择IIC还是SPI iic=1;SPI=0
pMyDevice->comms_speed_khz = 400; //iic速率
//正点原子的VL53L0用户手册上写明了再次使能时地址会恢复为0x52,所以只能使能一次,设置好地址即可,这里是核心
switch(vl53l0_x_id)
{
case 0:
I2C_X0_HIGH();
delay_ms(20);
vl53l0x_Addr_set(pMyDevice,0x60);//设置第一个VL53L0X传感器I2C地址
break;
case 1:
I2C_X1_HIGH();
delay_ms(20);
vl53l0x_Addr_set(pMyDevice,0x62);//设置第一个VL53L0X传感器I2C地址
break;
case 2:
I2C_X2_HIGH();
delay_ms(20);
vl53l0x_Addr_set(pMyDevice,0x64);
break;
case 3:
I2C_X3_HIGH();
delay_ms(20);
vl53l0x_Addr_set(pMyDevice,0x66);
break;
}
Status = VL53L0X_DataInit(pMyDevice); // Data initialization //VL53L0X_DataInit:一次设备的初始化,初始化成功返回0
if(Status != VL53L0X_ERROR_NONE){ //判断如果状态不为0 打印错误信息
print_pal_error(Status);
return Status; // 返回错误值 可通过此值DEBUG查找错误位置
}
Status = VL53L0X_GetDeviceInfo(pMyDevice, &vl53l0x_dev_info); //读取给定设备的设备信息
if(Status != VL53L0X_ERROR_NONE){
print_pal_error(Status);
return Status;
}
printf("VL53L0X_GetDeviceInfo:\n");
printf("Device Name : %s\n", vl53l0x_dev_info.Name); //设备名
printf("Device Type : %s\n", vl53l0x_dev_info.Type); //产品类型VL53L0X = 1, VL53L1 = 2
printf("Device ID : %s\n", vl53l0x_dev_info.ProductId); // 设备ID
printf("ProductRevisionMajor : %d\n", vl53l0x_dev_info.ProductRevisionMajor);
printf("ProductRevisionMinor : %d\n", vl53l0x_dev_info.ProductRevisionMinor);
if ((vl53l0x_dev_info.ProductRevisionMajor != 1) && (vl53l0x_dev_info.ProductRevisionMinor != 1)){
printf("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){ //判断如果不为0打印错误信息
print_pal_error(Status);
return Status;
}
}
模块的初始化顺序是:使用默认地址初始化设备---修改传感器IIC地址---再次初始化---测量配置
所以在这个传感器的初始化中我们先用默认的0X52地址将VL53L0X模块初始化,初始化完成后方可修改其地址,这里使用SWITCH函数判断用户配置的地址,避免函数重写,减小代码尺寸。修改完地址调用VL53L0X_DataInit()函数进行模块的再次初始化,使修改生效。
注意:VL53L0X不能保存地址,如果掉电后地址会恢复为默认的0X52,同时修改完地址后只能执行一次初始化,更多的初始化次数会也会导致地址复位。这在硬件的处理上要加倍注意。
VL53L0X_Error vl53l0x_start_single_test(VL53L0X_Dev_t *pdev, \
VL53L0X_RangingMeasurementData_t *pdata)
{
int i=0,j=0,sum=0;
VL53L0X_Error status = VL53L0X_ERROR_NONE;
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;
}
for(i=0;i<5;i++)
sum+=pdata->RangeMilliMeter;
pdata->RangeMilliMeter=sum/5;
printf("%d\r\n",pdata->RangeMilliMeter);
return status;
}