栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

cubemx stm32 bh1750模块 检测光强度 驱动代码

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

cubemx stm32 bh1750模块 检测光强度 驱动代码

CubeMX配置
  1. 打开i2c
  2. 打开与电脑通信的串口
硬件连接

使用的是野火的指南者,型号是stm32f103ve

ADDR引脚接地

代码 bh1750.c
#include "bh1750.h"
#include "i2c.h"



#define bh1750_i2c hi2c1


static uint8_t	BH1750_Send_Cmd(BH1750_MODE cmd);
static uint8_t BH1750_Read_Dat(uint8_t* dat);
static uint16_t BH1750_Dat_To_Lux(uint8_t* dat);




static uint8_t	BH1750_Send_Cmd(BH1750_MODE cmd)
{
	return HAL_I2C_Master_Transmit(&bh1750_i2c, BH1750_ADDR_WRITE, (uint8_t*)&cmd, 1, 0xFFFF);
}



static uint8_t BH1750_Read_Dat(uint8_t* dat)
{
	return HAL_I2C_Master_Receive(&bh1750_i2c, BH1750_ADDR_READ, dat, 2, 0xFFFF);
}



static uint16_t BH1750_Dat_To_Lux(uint8_t* dat)
{
	uint16_t lux = 0;
	lux = dat[0];
	lux <<= 8;
	lux |= dat[1];
	lux = (int)(lux / 1.2);
	
	return lux;
}



uint16_t Get_BH1750_Value(void)
{
	uint8_t dat[2] = {0};		//dat[0]是高字节,dat[1]是低字节
	uint16_t lux;

	if(HAL_OK != BH1750_Send_Cmd(ONCE_H_MODE))
	{
		return 0;
	}
	HAL_Delay(120);
	if(HAL_OK != BH1750_Read_Dat(dat))
	{
		return 0;
	}
	
	lux = BH1750_Dat_To_Lux(dat);
	return lux;
}

bh1750.h
#ifndef __BH1750_H_
#define __BH1750_H_

#include "stdint.h"



uint16_t Get_BH1750_Value(void);






#define	BH1750_ADDR_WRITE	0x46	//01000110
#define	BH1750_ADDR_READ	0x47	//01000111


typedef enum
{
	POWER_OFF_CMD	=	0x00,	//断电:无激活状态
	POWER_ON_CMD	=	0x01,	//通电:等待测量指令
	RESET_REGISTER	=	0x07,	//重置数字寄存器(在断电状态下不起作用)
	CONT_H_MODE		=	0x10,	//连续H分辨率模式:在11x分辨率下开始测量,测量时间120ms
	CONT_H_MODE2	=	0x11,	//连续H分辨率模式2:在0.51x分辨率下开始测量,测量时间120ms
	CONT_L_MODE		=	0x13,	//连续L分辨率模式:在411分辨率下开始测量,测量时间16ms
	ONCE_H_MODE		=	0x20,	//一次高分辨率模式:在11x分辨率下开始测量,测量时间120ms,测量后自动设置为断电模式
	ONCE_H_MODE2	=	0x21,	//一次高分辨率模式2:在0.51x分辨率下开始测量,测量时间120ms,测量后自动设置为断电模式
	ONCE_L_MODE		=	0x23	//一次低分辨率模式:在411x分辨率下开始测量,测量时间16ms,测量后自动设置为断电模式
} BH1750_MODE;




#endif 

main.c 头文件
#include "bh1750.h"
#include "stdio.h"
初始化
uint16_t bh1750_lux;
while循环
	bh1750_lux = Get_BH1750_Value();
	PrintfDebug("current: %05d luxrn", bh1750_lux);
	HAL_Delay(100);
演示现象

注:当手电筒照射时才会有较高值。

参考资料

【STM32Cube_14】使用硬件I2C读写环境光强度传感器(BH1750)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/925517.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号