From 2e77947dd284360a11d084fe772a78e546407c1e Mon Sep 17 00:00:00 2001 From: "lxbpxylps@126.com" Date: Thu, 14 Oct 2021 00:48:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90=20BH1750?= =?UTF-8?q?=20=E5=85=89=E7=85=A7=E5=BA=A6=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Hardware/bh1750/bh1750.c | 85 +++++++++++++++++++++++++++++++++++++++ Hardware/bh1750/bh1750.h | 22 ++++++++++ Project/SlaveNode.uvprojx | 7 +++- User/Main/main.c | 10 ++++- 4 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 Hardware/bh1750/bh1750.c create mode 100644 Hardware/bh1750/bh1750.h diff --git a/Hardware/bh1750/bh1750.c b/Hardware/bh1750/bh1750.c new file mode 100644 index 0000000..0e5ecaf --- /dev/null +++ b/Hardware/bh1750/bh1750.c @@ -0,0 +1,85 @@ +/** + * @file bh1750.c + * @author Myth + * @version 0.1 + * @date 2021.10.14 + * @brief BH1750 驱动 + */ + +#include "systick.h" + +#include "softi2c.h" + +#include "bh1750.h" + +#define SLAVE_ADDR_WR 0x46 +#define SLAVE_ADDR_RD 0x47 + +#define I2C_Start SoftI2C_Start(&bh1750_i2c) +#define I2C_Stop SoftI2C_Stop(&bh1750_i2c) +#define I2C_WriteByte(__byte__) SoftI2C_WriteByte(&bh1750_i2c, __byte__) +#define I2C_ReadByte SoftI2C_ReadByte(&bh1750_i2c) +#define I2C_Ack SoftI2C_Ack(&bh1750_i2c) +#define I2C_NAck SoftI2C_NAck(&bh1750_i2c) +#define I2C_WaitAck SoftI2C_WaitAck(&bh1750_i2c) + +SoftI2C_TypeDef bh1750_i2c; + +/** + * @brief 初始化 BH1750 + */ +void BH1750_Init(void) +{ + bh1750_i2c.SDA_GPIO = BH1750_GPIO; + bh1750_i2c.SDA_Pin = BH1750_SDA_PIN; + + bh1750_i2c.SCL_GPIO = BH1750_GPIO; + bh1750_i2c.SCL_Pin = BH1750_SCL_PIN; + + bh1750_i2c.Delay_Time = 5; + + SoftI2C_Init(&bh1750_i2c); + + Delay_ms(80); + + I2C_Start; + I2C_WriteByte(SLAVE_ADDR_WR); + I2C_WaitAck; + I2C_WriteByte(0x01); + I2C_WaitAck; + I2C_Stop; +} + +/** + * @brief 读取 BH1750 数据 + * @retval 读数 + */ +float BH1750_Read(void) +{ + uint8_t i; + uint8_t byte1; + uint8_t byte2; + + I2C_Start; + I2C_WriteByte(SLAVE_ADDR_WR); + I2C_WaitAck; + I2C_WriteByte(0x10); + I2C_WaitAck; + I2C_Stop; + + Delay_ms(180); + + I2C_Start; + I2C_WriteByte(SLAVE_ADDR_RD); + I2C_WaitAck; + + byte1 = I2C_ReadByte; + I2C_Ack; + + byte2 = I2C_ReadByte; + I2C_NAck; + + I2C_Stop; + + return (float)((byte1 << 8) + byte2) / 1.2; +} diff --git a/Hardware/bh1750/bh1750.h b/Hardware/bh1750/bh1750.h new file mode 100644 index 0000000..aad5dd5 --- /dev/null +++ b/Hardware/bh1750/bh1750.h @@ -0,0 +1,22 @@ +/** + * @file bh1750.h + * @author Myth + * @version 0.1 + * @date 2021.10.14 + * @brief BH1750 驱动 + */ + +#ifndef __BH1750_H +#define __BH1750_H + +#include "sys.h" + +//BH1750 引脚设置 +#define BH1750_GPIO GPIOB +#define BH1750_SDA_PIN GPIO_PIN_10 +#define BH1750_SCL_PIN GPIO_PIN_11 + +void BH1750_Init(void); +float BH1750_Read(void); + +#endif diff --git a/Project/SlaveNode.uvprojx b/Project/SlaveNode.uvprojx index 90dc4f3..45f0db2 100644 --- a/Project/SlaveNode.uvprojx +++ b/Project/SlaveNode.uvprojx @@ -339,7 +339,7 @@ USE_HAL_DRIVER, STM32F103x6 - ..\Core;..\Libraries\HAL_Lib\Inc;..\Libraries\SoftSPI_HAL_Lib;..\Libraries\SoftI2C_HAL_Lib;..\User\Main;..\System\sys;..\System\systick;..\System\uart;..\Hardware\led;..\Hardware\aht20 + ..\Core;..\Libraries\HAL_Lib\Inc;..\Libraries\SoftSPI_HAL_Lib;..\Libraries\SoftI2C_HAL_Lib;..\User\Main;..\System\sys;..\System\systick;..\System\uart;..\Hardware\led;..\Hardware\aht20;..\Hardware\bh1750 @@ -698,6 +698,11 @@ 1 ..\Hardware\aht20\aht20.c + + bh1750.c + 1 + ..\Hardware\bh1750\bh1750.c + diff --git a/User/Main/main.c b/User/Main/main.c index 2e6dced..d1f7afe 100644 --- a/User/Main/main.c +++ b/User/Main/main.c @@ -1,14 +1,15 @@ /** * @file main.c * @author Myth - * @version 0.3 - * @date 2021.10.12 + * @version 0.5 + * @date 2021.10.14 * @brief 工程主函数文件 * @details 初始化及主循环 * @note 此版本实现功能: * 串口回显,回显时 PC13 上的 LED 闪烁 * 软件 I2C * AHT20 温度湿度读取 + * BH1750 光照度读取 * JTAG 已禁用,请使用 SWD 调试 */ @@ -20,6 +21,7 @@ #include "led.h" #include "aht20.h" +#include "bh1750.h" void Echo(uint8_t byte); @@ -41,6 +43,8 @@ int main(void) AHT20_Init(); //初始化 AHT20 float humi, temp; + BH1750_Init(); //初始化 BH1750 + while (1) { //程序主循环 @@ -49,6 +53,8 @@ int main(void) else printf("fail to read AHT20.\n"); + printf("light: %f\n", BH1750_Read()); //读取 BH1750 + Delay_ms(500); }