Advertisement
Guest User

bh1750

a guest
Nov 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #define BH1750_ADDR_L 0x23
  2. #define BH1750_ON 0x01
  3. #define BH1750_MODE_1 0x10
  4. #define BH1750_RESET 0x07
  5.  
  6. uint16_t lux=0;
  7. uint8_t time=120;
  8.  
  9.  
  10. void BH1750_Cmd_Write(uint8_t cmd)
  11. {
  12. uint8_t data[1];
  13. data[0] = cmd;
  14. while(HAL_I2C_Master_Transmit_IT(&hi2c1,BH1750_ADDR_L << 1,(uint8_t*)data,1)!=HAL_OK);
  15. __HAL_I2C_CLEAR_FLAG(&hi2c1,I2C_FLAG_STOPF);
  16. HAL_Delay(100);
  17. }
  18.  
  19. void BH1750_Start(void){
  20. BH1750_Cmd_Write(BH1750_ON);
  21. BH1750_Cmd_Write(BH1750_RESET);
  22. BH1750_Cmd_Write(BH1750_MODE_1);
  23.  
  24.  
  25. }
  26.  
  27.  
  28.  
  29. uint16_t BH1750_Read(){
  30. uint8_t data_re[2];
  31. uint8_t res;
  32. while(HAL_I2C_Master_Receive(&hi2c1,BH1750_ADDR_L << 1,(uint8_t*) data_re,2,time*2)!=HAL_OK);
  33. HAL_Delay(100);
  34. res=data_re[0];
  35. res=(data_re[0] << 8)+data_re[1];
  36. res=res/1.2;
  37. HAL_Delay(100);
  38. return res;
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement