需求
需要用 stm32 来驱动 bh1750 的光照模块
解决
使用硬件 I2C 来驱动,很简单
#include "i2c.h"
#define BH1750_ADDRESS 0x23 //BH1750
#define BH1750_MODE_OTH 0x20
int32_t Bh1750GetValue()
{
uint8_t cmd[] = {BH1750_MODE_OTH};
uint8_t buf[2];
buf[0] = 0x00;
HAL_I2C_Master_Transmit(&hi2c1, BH1750_ADDRESS << 1, cmd, 1, 1000);
HAL_I2C_Master_Receive(&hi2c1, BH1750_ADDRESS << 1, buf, 2, 1000);
int32_t value = (buf[0] << 8) | buf[1];
value = value/1.2;
return value;
}