In the previous article, we introduced IIC driving OLED display. This article is about IIC driving BH1750FVI light intensity sensor. In fact, I also wrote about BH1750FVI light intensity sensor in the previous Raspberry Pi series 17. Raspberry Pi 3B+ driving BH1750FVI light intensity sensor
Here is an IIC bus with two IIC slave devices mounted on it.
product description
BH1750FVI is a digital light intensity sensor IC for two-wire serial bus interface. This IC can adjust the brightness of LCD or keyboard backlight according to the collected light intensity data. Its high resolution can detect a wide range of light intensity changes. (1lx-65535lx)
Features
1. Support I2C BUS interface (f/s Mode Support).
2. Spectral sensitivity characteristics close to visual sensitivity (peak sensitivity wavelength typical value: 560nm).
3. Output the digital value corresponding to the brightness.
4. Corresponds to a wide range of input light (equivalent to 1-65535lx).
5. Realize low current by reducing power function.
6. Stable measurement is achieved through 50Hz/60Hz optical noise removal function. 7. Supports 1.8V logic input interface.
8. No other external parts are required.
9. Weak dependence on light sources (incandescent lamp, fluorescent lamp, halogen lamp, white light LED, fluorescent lamp).
10. There are two optional I2C slave addresses.
11. The factor that has the greatest impact on the adjustable measurement results is the size of the light entrance.
12. Using this function, the range from 1.1 lx to 100000 lx max/min can be calculated.
13. The minimum error variation is ±20%.
14. Little affected by infrared rays.
Applications
Mobile phones, LCD TVs, laptops, portable game consoles, digital cameras, digital video cameras, car positioning systems, LCD monitors.
Chip Block Diagram
describe:
・PD Photo diode with approximately human eye response.
・AMP Integrated operational amplifier: Converts PD current to PD voltage.
・ADC analog-to-digital conversion obtains 16-bit digital data.
・Logic + IC Interface Light intensity calculation and I2C bus interface, including the following registers: Data register → light intensity data storage. The initial value is: "0000_0000_0000_0000". Measurement time register → time measurement data storage. The initial value is: "0100_0101".
・OSC Internal oscillator (clock frequency typical value: 320kHz). This clock is the internal logic clock.
It can also be seen from this block diagram that the internal modules of this chip are relatively simple and the actual use is not complicated.
Measurement procedure steps
Instruction set structure
Example of measurement sequence from "writing instruction" to "reading measurement results"
#define IIC_SCL P10 // Clock
#define IIC_SDA P11 // data
This is the connection between IIC and microcontroller IO port.
Driver code
#include "bh1750fvi.h"
#include "iic.h"
#define BH1750FVI_SlaveAddress 0x46 // Device address
#define BH1750FVI_DOWN 0x00 // Power off instruction
#define BH1750FVI_ON 0x01 // Power-on command
#define BH1750FVI_RESEET 0x07 // Reset
#define BH1750FVI_CON_H 0x10 // Continuous resolution mode, 1lx, 120ms
#define BH1750FVI_CON_H2 0x11 // Continuous resolution mode, 0.5lx, 120ms
#define BH1750FVI_CON_L 0x13 // Continuous low resolution mode, 4lx, 16ms
#define BH1750FVI_ONE_H 0x20 // One time high resolution mode, 1lx, 120ms
#define BH1750FVI_ONE_H2 0x21 // One time high resolution mode, 0.5lx, 120ms
#define BH1750FVI_ONE_L 0x23 // One time low resolution mode, 4lx, 16ms
// Write data to BH1750FVI
void BH1750FVI_Write_Reg(u8 reg)
{
IIC_Start();
IIC_Write_Byte(BH1750FVI_SlaveAddress);
IIC_Write_Byte(reg);
IIC_Stop();
}
// Read 1 byte of data
u8 BH1750FVI_Read_Reg(u8 reg)
{
u8 that;
IIC_Start();
IIC_Write_Byte(BH1750FVI_SlaveAddress);
IIC_Write_Byte(reg);
IIC_Start();
IIC_Write_Byte(BH1750FVI_SlaveAddress + 1);
dat = IIC_Read_Byte();
IIC_Stop();
return that;
}
u16 BH1750FVI_Read_all()
{
u8 i;
u8 BUF[8];
u16 that;
IIC_Start();
IIC_Write_Byte(BH1750FVI_SlaveAddress + 1);
for(i = 0; i < 2; i++)
{
BUF[i] = IIC_Read_Byte();
if(i == 2){
IIC_Send_Ack(1);
}else{
IIC_Send_Ack(0);
}
}
IIC_Stop();
that = BUF[0]<<8 + BUF[1];
return that;
}
u16 BH1750FVI_Measure()
{
BH1750FVI_Write_Reg(BH1750FVI_ON);
BH1750FVI_Write_Reg(BH1750FVI_CON_H);
return BH1750FVI_Read_all();
}
void BH1750FVI_Init()
{
BH1750FVI_Write_Reg(BH1750FVI_DOWN);
BH1750FVI_Write_Reg(BH1750FVI_ON);
}
main.c
#include "stc15.h"
#include "delay.h"
#include "bh1750fvi.h"
#include "oled.h"
u8 num[] = {'0','1','2','3','4','5','6','7','8','9'};
void main()
{
u16 result;
BH1750FVI_Heat();
OLED_Init(); //Initialize OLED
OLED_Clear() ;
while (1)
{
result = BH1750FVI_Measure();
OLED_ShowChar(0,0,num[result/10000],1); // 10,000
OLED_ShowChar(10,0,num[result%10000/1000],1); // Thousands
OLED_ShowChar(20,0,num[result%10000%1000/100],1); // Hundred
OLED_ShowChar(30,0,num[result%10000%1000%100/10],1);// ten
OLED_ShowChar(40,0,num[result%10],1); //
delayms(2000);
}
}
Display effect diagram
Previous article:16.STC15W408AS MCU obtains DS18B20 temperature
Next article:14. STC15W408AS MCU IIC drives OLED
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- I made a board and the rectifier bridge was broken. I would like some advice.
- When using Keil to compile, address space overflow may occur when a function segment is removed
- 【Recruitment】Ability, Dream, and FUTURE
- Source code for reading and writing RC522 RFID module with MSP430F149
- I am looking for a manufacturer that produces resistors. Used for temperature compensation
- [Qinheng RISC-V core CH582] 8-CH582 realizes temperature and humidity data collection, display, upload to mobile phone, and message function
- Chengdu is recruiting embedded software engineers for automotive MCU
- Thanks to EEworld for the Children's Day gift!
- [SC8905 EVM Evaluation] Stress test of +5V power output
- [RVB2601 Creative Application Development] Unboxing + Detailed Picture Sharing