bh1750光照传感器硬件iic
时间: 2023-09-28 21:11:26 浏览: 34
bh1750光照传感器的硬件I2C接口包含两个引脚:SCL和SDA。其中,SCL是时钟线,SDA是数据线。通过这两条线,主控器可以向传感器发送读写命令,并接收传感器返回的数据。在使用bh1750传感器时,需要先将其连接到主控器的I2C总线上,然后通过相应的I2C库函数进行初始化和读写操作。具体的连接和使用方法可以参考bh1750传感器的数据手册和相应的开发文档。
相关问题
基于51单片机BH1750光照传感器,使用四脚OLED显示代码
好的,以下是基于51单片机BH1750光照传感器,使用四脚OLED显示的代码:
```c
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define SCL P3_6
#define SDA P3_7
sbit OLED_SCL = P1^0;
sbit OLED_SDA = P1^1;
void delay(uint t)
{
uint i;
while(t--)
{
for(i=0; i<125; i++);
}
}
void IIC_Start()
{
SDA = 1;
SCL = 1;
_nop_();
_nop_();
_nop_();
SDA = 0;
_nop_();
_nop_();
_nop_();
SCL = 0;
}
void IIC_Stop()
{
SDA = 0;
_nop_();
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
SDA = 1;
}
void IIC_SendByte(uchar dat)
{
uchar i;
for(i=0; i<8; i++)
{
SDA = dat & 0x80;
dat <<= 1;
_nop_();
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
SCL = 0;
}
}
uchar IIC_ReadByte()
{
uchar i, dat = 0;
SDA = 1;
for(i=0; i<8; i++)
{
_nop_();
_nop_();
_nop_();
SCL = 1;
dat <<= 1;
dat |= SDA;
_nop_();
_nop_();
_nop_();
SCL = 0;
}
return dat;
}
void OLED_Init()
{
OLED_SCL = 1;
OLED_SDA = 1;
delay(100);
OLED_SCL = 0;
delay(200);
OLED_SCL = 1;
OLED_SDA = 0;
delay(200);
OLED_SDA = 1;
delay(200);
}
void OLED_WriteCmd(uchar cmd)
{
IIC_Start();
IIC_SendByte(0x78);
IIC_SendByte(0x00);
IIC_SendByte(cmd);
IIC_Stop();
}
void OLED_WriteData(uchar dat)
{
IIC_Start();
IIC_SendByte(0x78);
IIC_SendByte(0x40);
IIC_SendByte(dat);
IIC_Stop();
}
void OLED_SetPos(uchar x, uchar y)
{
OLED_WriteCmd(0xb0+y);
OLED_WriteCmd(((x&0xf0)>>4)|0x10);
OLED_WriteCmd((x&0x0f)|0x01);
}
void OLED_Fill(uchar dat)
{
uchar i, j;
for(i=0; i<8; i++)
{
OLED_WriteCmd(0xb0+i);
OLED_WriteCmd(0x00);
OLED_WriteCmd(0x10);
for(j=0; j<128; j++)
{
OLED_WriteData(dat);
}
}
}
void OLED_ShowChar(uchar x, uchar y, uchar chr)
{
uchar i;
chr -= 32;
OLED_SetPos(x, y);
for(i=0; i<6; i++)
{
OLED_WriteData(font6x8[chr][i]);
}
}
void OLED_ShowString(uchar x, uchar y, uchar *str)
{
while(*str != '\0')
{
OLED_ShowChar(x, y, *str);
x += 6;
str++;
}
}
void BH1750_Init()
{
IIC_Start();
IIC_SendByte(0x46);
IIC_Stop();
}
uchar BH1750_ReadData()
{
uchar dat1, dat2;
IIC_Start();
IIC_SendByte(0x46);
IIC_Stop();
delay(20);
IIC_Start();
IIC_SendByte(0x47);
dat1 = IIC_ReadByte();
dat2 = IIC_ReadByte();
IIC_Stop();
return dat2;
}
void main()
{
uchar dat;
OLED_Init();
BH1750_Init();
OLED_Fill(0x00);
OLED_ShowString(0, 0, "Light:");
while(1)
{
dat = BH1750_ReadData();
OLED_SetPos(48, 0);
OLED_ShowChar(48, 0, '0'+dat/100);
OLED_ShowChar(54, 0, '0'+dat%100/10);
OLED_ShowChar(60, 0, '0'+dat%10);
delay(500);
}
}
```
需要注意的是,此代码中引用了一个字库,需要在代码中自己添加字库的定义。
树莓派5 iic控制BH1750
树莓派是一款基于Linux系统的微型计算机,具有较强的扩展性和应用性。IIC(Inter-Integrated Circuit)是一种串行通信协议,可用于芯片之间的通信。BH1750是一种数字式光强度传感器,可以通过IIC总线与树莓派进行通信。
要使用树莓派控制BH1750,首先需要确认已经将BH1750连接到树莓派的IIC总线上。然后,在树莓派上安装必要的库和驱动程序,例如python-smbus库和i2c-tools工具。安装完成后,可以使用Python编写程序来控制BH1750。
以下是一个简单的Python程序示例,用于读取BH1750传感器的光照强度值:
```
import smbus
# BH1750地址,可通过i2cdetect命令获取
DEVICE_ADDRESS = 0x23
# 控制命令
POWER_ON = 0x01
RESET = 0x07
CONTINUOUS_HIGH_RES_MODE = 0x10
# 打开I2C总线
bus = smbus.SMBus(1)
# 初始化传感器
bus.write_byte(DEVICE_ADDRESS, POWER_ON)
bus.write_byte(DEVICE_ADDRESS, RESET)
bus.write_byte(DEVICE_ADDRESS, CONTINUOUS_HIGH_RES_MODE)
# 读取光照强度值
data = bus.read_i2c_block_data(DEVICE_ADDRESS, 0x00)
# 转换光照强度值
lux = (data + (256 * data)) / 1.2
# 输出结果
print("光照强度值为:%.2f lux" %lux)
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)