我想将相机ov5640
与stm32l4r5 nucleo-144
套件一起使用。我们使用i2c
来设置相机。我正在使用以下代码进行i2c
通信。
#define OV5640_ADDR 0x78
int ov7670_init(I2C_HandleTypeDef *p_hi2c)
{
sp_hi2c = p_hi2c;
HAL_Delay(100);
uint8_t reg;
uint8_t buffer;
reg = ov7670_read(OV5640_CHIPIDH, &buffer);
if (buffer == 0x56)
{
reg = ov7670_read(OV5640_CHIPIDL, &buffer);
if (buffer == 0x40)
{
return 0;
}
else
{
return 1;
}
}
else
{
return 1;
}
}
static int ov5640_write(uint16_t regAddr, uint8_t data)
{
HAL_StatusTypeDef ret;
do {
ret = HAL_I2C_Mem_Write(sp_hi2c, OV5640_ADDR, regAddr, I2C_MEMADD_SIZE_8BIT, &data, 1, 100);
} while (ret != HAL_OK && 0);
return ret;
}
static int ov7670_read(uint16_t regAddr, uint8_t *data)
{
HAL_StatusTypeDef ret;
uint8_t regAddrMSB = regAddr >> 8;
uint8_t regAddrLSB = regAddr & 0x00FF;
do {
ret = HAL_I2C_Master_Transmit(sp_hi2c, OV5640_ADDR, ®AddrMSB, 1, 100);
ret |= HAL_I2C_Master_Transmit(sp_hi2c, OV5640_ADDR, ®AddrLSB, 1, 100);
ret |= HAL_I2C_Master_Receive(sp_hi2c, OV5640_ADDR, data, 1, 100);
} while (ret != HAL_OK && 0);
return ret;
}
我使用STM32CUBEMX
软件来设置i2c
引脚,并生成用于初始化i2c
引脚的相关代码。相机有i2c
个寄存器,地址为16位。但是i2c
通讯无法正常工作。我无法读取寄存器。