Based on STM32CUBEMX driver TOF module VL53l0x(2)----modify device address

Based on STM32CUBEMX driver TOF module VL53l0x----2. Modify the device address

Overview

This chapter mainly introduces how to modify the I2C address of the VL53L0X sensor and successfully drive the device to use the new address. The VL53L0X is a multifunctional, high-performance proximity and ambient light sensor that is commonly used in applications such as measuring the distance between an object and the sensor, detecting the proximity of an object, and measuring ambient light intensity.
In some cases, it may be necessary to use multiple VL53L0X sensors on the same I2C bus, or to share bus resources with other I2C devices. Since each VL53L0X sensor has a default fixed I2C address, this can cause address conflicts, preventing simultaneous use of multiple sensors. To solve this problem, you can modify the sensor's I2C address to avoid conflicts and enable it to coexist with other devices on the same bus.
Modify the I2C address of the VL53L0X sensor by configuring the sensor's internal register. Specific register configuration values ​​and steps are provided to ensure successful changes to the sensor's address. Then drive the VL53L0X sensor with the new address and use the new I2C address to communicate with the VL53L0X sensor.
I am currently taking ST courses. If you need samples, you can join the group and apply: 615061293.
Insert image description here

Video teaching

https://www.bilibili.com/video/BV1cm4y1N7HN/

Based on STM32CUBEMX driver TOF module VL53l0x(2)----modify device address

sample application

https://www.wjx.top/vm/OhcKxJk.aspx#

Source code download

https://download.csdn.net/download/qq_24312945/88332774

Modify device address

The VL53L0X uses the I²C bus for communication. By default, the 7-bit address of the VL53L0X is 0x29. However, to avoid address conflicts and allow multiple sensors to share the same I2C bus, the user can modify the sensor's address by writing a custom 7-bit address to the register I2C_SLAVE_DEVICE_ADDRESS (register address 0x8A).
Insert image description here

The modified version is as follows.

// Public Methods //

void VL53L0X_setAddress(uint8_t add,uint8_t new_addr)
{
    
    
  VL53L0X_WriteByte(add,I2C_SLAVE_DEVICE_ADDRESS, new_addr & 0x7F);

}

In the VL53L0X.h file, this file contains the relevant definitions and configuration of the VL53L0X sensor.
In the VL53L0X.h file, you can define the address constants of two devices, representing different sensors respectively. For example, you can define an address constant using:

#define VL53L0X_DEFAULT_I2C_ADDR1 0x29  ///< The fixed I2C addres
#define VL53L0X_DEFAULT_I2C_ADDR2 0x30  ///< The fixed I2C addres

The address value here is selected based on actual needs and hardware connection conditions to ensure that each device has a unique address.
Multiple VL53L0X sensors can be easily managed and operated by using defined device address constants.
First use the VL53L0X_Init function to initialize the sensor, and then modify the device address by calling the VL53L0X_setAddress function. This function takes two parameters: the old device address (the initial device address) and the new device address (the value you want to change the sensor's address to).

  /* USER CODE BEGIN 2 */
	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_RESET);	
	
			HAL_Delay(200);	

	if (!VL53L0X_Init(VL53L0X_DEFAULT_I2C_ADDR1,true))
  {
    
    
    printf("Failed to detect and initialize sensor!");
    while (1) {
    
    }
  }
/************修改VL53L0X_2地址为VL53L0X_DEFAULT_I2C_ADDR2(0x30)*********************/
	VL53L0X_setAddress(VL53L0X_DEFAULT_I2C_ADDR1,VL53L0X_DEFAULT_I2C_ADDR2);	
	
  /* USER CODE END 2 */

main program

In the while loop of the main program, use the VL53L0X_readRangeSingleMillimeters function to read distance data from the sensor. This function requires the new address of the device, VL53L0X_DEFAULT_I2C_ADDR2, to be passed in as a parameter.

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
		L=VL53L0X_readRangeSingleMillimeters(VL53L0X_DEFAULT_I2C_ADDR2);
    	printf("L=%d",L);

		HAL_Delay(1000);			
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

Test Results

The test distance results are shown below.

Insert image description here

Guess you like

Origin blog.csdn.net/qq_24312945/article/details/132843804