EasyManua.ls Logo

STMicroelectronics STM8 User Manual

STMicroelectronics STM8
126 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #117 background imageLoading...
Page #117 background image
unsigned int BH1750_read_word(void)
{
unsigned long value = 0x0000;
unsigned char num_of_bytes = 0x02;
unsigned char bytes[2] = {0x00, 0x00};
while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
I2C_GenerateSTART(ENABLE);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(BH1750_addr, I2C_DIRECTION_RX);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
while(num_of_bytes)
{
if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))
{
if(num_of_bytes == 0)
{
I2C_AcknowledgeConfig(I2C_ACK_NONE);
I2C_GenerateSTOP(ENABLE);
}
bytes[(num_of_bytes - 1)] = I2C_ReceiveData();
num_of_bytes--;
}
};
value = ((bytes[1] << 8) | bytes[0]);
return value;
}
As with SPI, we need to check first if I2C hardware is free. We, then, initiate a I2C start condition and
check master/slave mode selection. Next, we send out slave device’s ID or address with read
command, signalling that we wish to read from the slave. Again, a flag is checked before continuing.
Here the sensor gives 16-bit light output data and so we need to extract two 8-bit data values. This is
done in the while loop. At the end of data extraction process, we must generate stop as well as take
care of acknowledgement. Finally, the two bytes are joined to form a word value representing light
output.
The following function simplifies the task of determining lux value from the sensor. It selects mode of
operation and latency. We will call this function in the main loop to extract average light value in lux.
unsigned int get_lux_value(unsigned char mode, unsigned int delay_time)
{
unsigned long lux_value = 0x00;
unsigned char dly = 0x00;
unsigned char s = 0x08;
while(s)
{
BH1750_write(power_up);
BH1750_write(mode);
lux_value += BH1750_read_word();
for(dly = 0; dly < delay_time; dly += 1)
{
delay_ms(1);
}
BH1750_write(power_down);
Question and Answer IconNeed help?

Do you have a question about the STMicroelectronics STM8 and is the answer not in the manual?

STMicroelectronics STM8 Specifications

General IconGeneral
ADC Resolution10-bit
CoreSTM8
Maximum Clock FrequencyUp to 24 MHz
Flash MemoryUp to 128 KB
RAMUp to 6 KB
EEPROMUp to 2 KB
Operating Voltage2.95 V to 5.5 V
Operating Temperature-40°C to +85°C or +125°C
Communication InterfacesUART, SPI, I2C
Package OptionsLQFP, TSSOP, UFQFPN, SO

Summary

Hardware Tools

Software Tools

Creating a New Code Project

Uploading Code

General Purpose Input Output (GPIO)

Clock System (CLK)

External Interrupt (EXTI)

Alphanumerical LCD

Analog-to-Digital Converter (ADC)

Analog Watchdog (AWD)

Independent Watchdog (IWDG)

Window Watchdog (WWDG)

Time Base Generation (TIM2)

Timer Interrupt (TIM4)

General Purpose Pulse Width Modulation (TIM2 PWM)

Advanced Pulse Width Modulation (TIM1 PWM)

Timer Input Capture (TIM1 & TIM2)

Serial Communication (UART)

Serial Peripheral Interface (SPI)

Inter-Integrated Circuit (I2C)

Peripheral Clock Configurations

Unlocking a Locked STM8 Chip

Related product manuals