Use STM32CUBEMX to set up i2c interface. You can check this topic. In my code I reference from this site.
After generated add in main.c file
#include “stm32f7xx_hal.h”
#include “stm32f7xx_hal_uart.h”
#include “stm32f7xx_hal_sdram.h”
#include “stm32f7xx_hal_ltdc.h”
#include “stm32746g_discovery.h”
#include “stm32746g_discovery_lcd.h”
#include “stm32746g_discovery_sdram.h”
#include “stm32f7xx_ll_fmc.h”
/* USER CODE BEGIN Includes */
#define LIDAR_ADD 0x62<<1
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ———————————————————*/
I2C_HandleTypeDef hi2c1;
/* USER CODE BEGIN PV */
/* Private variables ———————————————————*/
/* USER CODE END PV */
/* Private function prototypes ———————————————–*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
void configure(int configuration)
{
uint8_t cmd[1];
cmd[0]=0x04;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x00, 1, cmd,1,0×100);
switch (configuration)
{
case 0: // Default mode, balanced performance
cmd[0]=0x80;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0×1000);
cmd[0]=0x08;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0×1000);
cmd[0]=0x00;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0×1000);
break;
case 1: // Short range, high speed
cmd[0]=0x1d;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0×1000);
cmd[0]=0x08;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0×1000);
cmd[0]=0x00;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0×1000);
break;
case 2: // Default range, higher speed short range
cmd[0]=0x80;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0×1000);
cmd[0]=0x00;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0×1000);
cmd[0]=0x00;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0×1000);
break;
case 3: // Maximum range
cmd[0]=0xff;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0×1000);
cmd[0]=0x08;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0×1000);
cmd[0]=0x00;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0×1000);
break;
case 4: // High sensitivity detection, high erroneous measurements
cmd[0]=0x80;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0×1000);
cmd[0]=0x08;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0×1000);
cmd[0]=0x80;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0×1000);
break;
case 5: // Low sensitivity detection, low erroneous measurements
cmd[0]=0x80;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x02, 1, cmd,1,0×1000);
cmd[0]=0x08;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x04, 1, cmd,1,0×1000);
cmd[0]=0xb0;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x1c, 1, cmd,1,0×1000);
break;
}
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration———————————————————-*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
BSP_LCD_Init();
BSP_LCD_LayerDefaultInit(0, LCD_FB_START_ADDRESS);
BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS+(BSP_LCD_GetXSize()*BSP_LCD_GetYSize()*4));
BSP_LCD_DisplayOn();
BSP_LCD_SelectLayer(0);
BSP_LCD_Clear(LCD_COLOR_BLACK);
BSP_LCD_SelectLayer(1);
BSP_LCD_Clear(LCD_COLOR_BLACK);
BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
//configure(0,LIDAR_ADD);
uint8_t data[2]={10};
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
configure(3,LIDAR_ADD);
/* Infinite loop */
/* USER CODE BEGIN WHILE */
uint8_t cmd[0];
int distance;
char buffer[30];
while (1)
{
cmd[0]=0x04;
HAL_I2C_Mem_Write(&hi2c1, LIDAR_ADD,0x00, 1, cmd,1,0×100);
cmd[0]=0x8f;
HAL_I2C_Master_Transmit(&hi2c1,LIDAR_ADD,cmd, 1,0×100);
HAL_I2C_Master_Receive(&hi2c1, LIDAR_ADD, data,2,0×100);
distance =(data[0]<<8)|(data[1]);
sprintf(buffer,”Distance=%d [cm]”,distance);
BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)buffer, CENTER_MODE);
HAL_Delay(50);
BSP_LCD_Clear(LCD_COLOR_WHITE);
}
/* USER CODE END 3 */
}
………………………………………………………………………..