STM32F 7508DK I2C BH1570 驱动开发和I2C使用
[复制链接]
本帖最后由 skyworth74 于 2022-10-3 09:41 编辑
万丈高楼平地起,先把要使用的外设驱动准备搭起来,这次需用光照强度传感器采集外面的光照强度,从网上选了BH1570,
BH1570FVI是一款常用的光照强度传感器,通过IIC接口协议和芯片通信,具有16位的转换精度,光照强度范围为(1-65535lx)。
关于芯片的引脚如上图所示:芯片供电电压为2.0-3.6V,其中DVI引脚是芯片的复位引脚,SCL为IIC时钟引脚,SDA为IIC数据引脚,ADDR为芯片的地址引脚,当接低电平时芯片的地址为0X46,当接低电平时芯片地址为0XB8,ADDR悬空时地址为0X46。
BH1570 网上很多驱动程序,都是通过GPIO模拟I2C通讯的,既然STM32F7508有集成I2C接口,我们还是要站在巨人的肩膀上不要自己在造轮子,下面附上外设设置,和读取命令
/**
* @brief Initializes I2C HAL.
* @param i2c_handler : I2C handler
* @retval None
*/
static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler)
{
if(HAL_I2C_GetState(i2c_handler) == HAL_I2C_STATE_RESET)
{
if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler))
{
/* Audio and LCD I2C configuration */
i2c_handler->Instance = DISCOVERY_AUDIO_I2Cx;
}
else
{
/* External, camera and Arduino connector I2C configuration */
i2c_handler->Instance = DISCOVERY_EXT_I2Cx;
}
i2c_handler->Init.Timing = DISCOVERY_I2Cx_TIMING;
i2c_handler->Init.OwnAddress1 = 0;
i2c_handler->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
i2c_handler->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
i2c_handler->Init.OwnAddress2 = 0;
i2c_handler->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
i2c_handler->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
/* Init the I2C */
I2Cx_MspInit(i2c_handler);
HAL_I2C_Init(i2c_handler);
}
}
void bt1570_IO_init(void)
{
//I2C_HandleTypeDef *hI2cExtHandler = getI2c_handleTypeDef();
I2Cx_Init(&hI2cExtHandler);
}
void bh1570_init(uint16_t flag)
{
uint8_t opecode =0x01;
int status =0;
TS_IO_Delay(200);
bh1570_I2C_InitializeIfRequired();
status = I2C_BH1570_Opecode_Write(&opecode,1);
//xlog_printf("status:%d line:%d",status,__LINE__);
bh1570_handle.i2cInitialized = BH1570_I2C_INITIALIZED;
}
uint16_t bh1570_ReadLight(uint16_t lightvalue)
{
uint16_t value = 0;
uint8_t opecode = 0x01;
uint8_t data_buf[8] = {0};
int status =0;
//BH1570_IO_Write(BH1570_ADDR, &opecode);
status = I2C_BH1570_Opecode_Write(&opecode,1);
//xlog_printf("status:%d line:%d",status,__LINE__);
TS_IO_Delay(200);
opecode = 0x10;
status = I2C_BH1570_Opecode_Write(&opecode,1);
// xlog_printf("status:%d line:%d",status,__LINE__);
TS_IO_Delay(200);
//value = BH1570_IO_Read(BH1570_ADDR, 0);
status = I2C_BH1570_Data_Read(data_buf,2);
//xlog_printf("status:%d line:%d",status,__LINE__);
value =(data_buf[0]<<8)+data_buf[1];
return value;
}
/**
* @brief Return the status of I2C was initialized or not.
* @param None.
* @retval : I2C initialization status.
*/
static uint8_t bh1570_Get_I2C_InitializedStatus(void)
{
return(bh1570_handle.i2cInitialized);
}
static void bh1570_Set_I2C_InitializedStatus(uint8_t status)
{
bh1570_handle.i2cInitialized = status ;
}
void bh1570_I2C_InitializeIfRequired(void)
{
if (BH1570_I2C_NOT_INITIALIZED==bh1570_Get_I2C_InitializedStatus())
{
bt1570_IO_init();
}
bh1570_Set_I2C_InitializedStatus(BH1570_I2C_INITIALIZED);
}
uint32_t I2C_BH1570_Opecode_Write(uint8_t* pData,uint16_t size)
{
int hal_status =HAL_OK;
hal_status =HAL_I2C_Master_Transmit(&hI2cExtHandler,0x46,pData,size,1);
return hal_status;
}
uint32_t I2C_BH1570_Data_Read(uint8_t* pData,uint16_t size)
{
int hal_status =HAL_OK;
hal_status =HAL_I2C_Master_Receive(&hI2cExtHandler,0x46+1,pData,size,1);
return hal_status;
}
经过初始化以后直接调用uint16_t bh1570_ReadLight(uint16_t lightvalue)就可以读取到环境光强度
|