Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BH1750_ADDR_L 0x23
- #define BH1750_ON 0x01
- #define BH1750_MODE_1 0x10
- #define BH1750_RESET 0x07
- uint16_t lux=0;
- uint8_t time=120;
- void BH1750_Cmd_Write(uint8_t cmd)
- {
- uint8_t data[1];
- data[0] = cmd;
- while(HAL_I2C_Master_Transmit_IT(&hi2c1,BH1750_ADDR_L << 1,(uint8_t*)data,1)!=HAL_OK);
- __HAL_I2C_CLEAR_FLAG(&hi2c1,I2C_FLAG_STOPF);
- HAL_Delay(100);
- }
- void BH1750_Start(void){
- BH1750_Cmd_Write(BH1750_ON);
- BH1750_Cmd_Write(BH1750_RESET);
- BH1750_Cmd_Write(BH1750_MODE_1);
- }
- uint16_t BH1750_Read(){
- uint8_t data_re[2];
- uint8_t res;
- while(HAL_I2C_Master_Receive(&hi2c1,BH1750_ADDR_L << 1,(uint8_t*) data_re,2,time*2)!=HAL_OK);
- HAL_Delay(100);
- res=data_re[0];
- res=(data_re[0] << 8)+data_re[1];
- res=res/1.2;
- HAL_Delay(100);
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement