本文主要是介绍ESP8266驱动外设之光照模块BH1750,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- BH1750中文参考手册分析
BH1750中文参考手册分析
详细资料请参考BH1750中文参考手册分析
- 指令集合说明
- 指令集合程序
/** BH1750.h** Created on: 2021年4月9日* Author: Administrator*/#ifndef APP_INCLUDE_BH1750_H_
#define APP_INCLUDE_BH1750_H_#define BH1750_ADDR 0x46 // 设备地址 ADDRESS引脚接地时地址为0x46,接电源时地址为0xB8
#define BS1750_DOWN 0x00 // 断电指令
#define BH1750_ON 0x01 // 通电指令
#define BH1750_RSET 0x07 // 重置指令
#define BH1750_CON_H 0x10 // 连续高分辨率模式,1lx,120ms
#define BH1750_CON_H2 0x11 // 连续高分辨率模式,0.5lx,120ms
#define BH1750_CON_L 0x13 // 连续低分辨率模式,4lx,16ms
#define BH1750_ONE_H 0x20 // 一次高分辨率模式,1lx,120ms
#define BH1750_ONE_H2 0x21 // 一次高分辨率模式,0.5lx,120ms
#define BH1750_ONE_L 0x23 // 一次低分辨率模式,4lx,16ms#define RESOLUTION BH1750_CON_H // 连续高分辨率模式,0.5lx#if RESOLUTION == BH1750_CON_H || RESOLUTION == BH1750_ONE_H#define SCALE_INTERVAL 1
#elif RESOLUTION == BH1750_CON_H2 || RESOLUTION == BH1750_ONE_H2#define SCALE_INTERVAL 0.5f
#elif RESOLUTION == BH1750_CON_L || RESOLUTION == BH1750_ONE_L#define SCALE_INTERVAL 4
#endifvoid BH1750_init(void);#endif /* APP_INCLUDE_BH1750_H_ */
- 检测流程
- 函数实现
#include "c_types.h"
#include "osapi.h"
#include "BH1750.h"#include "driver/i2c_master.h"
uint8 BH1750_Data[2];
os_timer_t os_timer_BH1750; /*定义软件定时器句柄*/
/******************************************************************************* FunctionName :* Description :* Parameters : NONE* Returns :
*******************************************************************************/
bool ICACHE_FLASH_ATTR
BH1750_single_write(uint8 reg_addr)
{uint8 ack;i2c_master_start();i2c_master_writeByte(BH1750_ADDR);ack = i2c_master_getAck();if (ack) {os_printf("BH1750 not ack when BH1750 write addr\n");i2c_master_stop();return false;}i2c_master_writeByte(reg_addr);ack = i2c_master_getAck();if (ack) {os_printf("BH1750 not ack when BH1750 write reg addr\n");i2c_master_stop();return false;}i2c_master_stop();return true;
}/******************************************************************************* FunctionName :* Description :* Parameters : NONE* Returns :
*******************************************************************************/
bool ICACHE_FLASH_ATTR
BH1750_multiple_read(void)
{uint8 i;uint8 ack;i2c_master_start();i2c_master_writeByte(BH1750_ADDR|1);ack = i2c_master_getAck();if (ack) {os_printf("BH1750 not ack when BH1750 write read cmd\n");i2c_master_stop();return false;}for(i = 0; i < 2; i++){BH1750_Data[i] = i2c_master_readByte();if(i < 1) i2c_master_send_ack();}i2c_master_send_nack();i2c_master_stop();return true;
}LOCAL void ICACHE_FLASH_ATTR
os_timer_BH1750_cb(void)
{uint16 bh1750_data = 0;float bh1750_data_f = 0;BH1750_multiple_read();bh1750_data = (((uint16_t)BH1750_Data[0]<<8) + BH1750_Data[1]);bh1750_data_f = (float)bh1750_data / 1.2f * SCALE_INTERVAL;os_printf("BH1750_Data[0] = %d \r\n",BH1750_Data[0]);os_printf("BH1750_Data[1] = %d \r\n",BH1750_Data[1]);os_printf("bh1750_data = %d \r\n",bh1750_data);os_printf("Light is : %d lx\r\n",(uint16)bh1750_data_f);
}/******************************************************************************* FunctionName :* Description :* Parameters : NONE* Returns :
*******************************************************************************/
void ICACHE_FLASH_ATTR
BH1750_init(void)
{i2c_master_gpio_init();//初始化引脚BH1750_single_write(BH1750_ON);BH1750_single_write(RESOLUTION);os_timer_disarm(&os_timer_BH1750);os_timer_setfn(&os_timer_BH1750,(os_timer_func_t *)os_timer_BH1750_cb,NULL);os_timer_arm(&os_timer_BH1750,2000,1);
}
这篇关于ESP8266驱动外设之光照模块BH1750的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!