初步完成 BH1750 光照度读取

This commit is contained in:
lxbpxylps@126.com 2021-10-14 00:48:38 +08:00
parent 1390298d0d
commit 2e77947dd2
4 changed files with 121 additions and 3 deletions

85
Hardware/bh1750/bh1750.c Normal file
View File

@ -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;
}

22
Hardware/bh1750/bh1750.h Normal file
View File

@ -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

View File

@ -339,7 +339,7 @@
<MiscControls></MiscControls> <MiscControls></MiscControls>
<Define>USE_HAL_DRIVER, STM32F103x6</Define> <Define>USE_HAL_DRIVER, STM32F103x6</Define>
<Undefine></Undefine> <Undefine></Undefine>
<IncludePath>..\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</IncludePath> <IncludePath>..\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</IncludePath>
</VariousControls> </VariousControls>
</Cads> </Cads>
<Aads> <Aads>
@ -698,6 +698,11 @@
<FileType>1</FileType> <FileType>1</FileType>
<FilePath>..\Hardware\aht20\aht20.c</FilePath> <FilePath>..\Hardware\aht20\aht20.c</FilePath>
</File> </File>
<File>
<FileName>bh1750.c</FileName>
<FileType>1</FileType>
<FilePath>..\Hardware\bh1750\bh1750.c</FilePath>
</File>
</Files> </Files>
</Group> </Group>
</Groups> </Groups>

View File

@ -1,14 +1,15 @@
/** /**
* @file main.c * @file main.c
* @author Myth * @author Myth
* @version 0.3 * @version 0.5
* @date 2021.10.12 * @date 2021.10.14
* @brief * @brief
* @details * @details
* @note * @note
* PC13 LED * PC13 LED
* I2C * I2C
* AHT20 湿 * AHT20 湿
* BH1750
* JTAG 使 SWD * JTAG 使 SWD
*/ */
@ -20,6 +21,7 @@
#include "led.h" #include "led.h"
#include "aht20.h" #include "aht20.h"
#include "bh1750.h"
void Echo(uint8_t byte); void Echo(uint8_t byte);
@ -41,6 +43,8 @@ int main(void)
AHT20_Init(); //初始化 AHT20 AHT20_Init(); //初始化 AHT20
float humi, temp; float humi, temp;
BH1750_Init(); //初始化 BH1750
while (1) while (1)
{ {
//程序主循环 //程序主循环
@ -49,6 +53,8 @@ int main(void)
else else
printf("fail to read AHT20.\n"); printf("fail to read AHT20.\n");
printf("light: %f\n", BH1750_Read()); //读取 BH1750
Delay_ms(500); Delay_ms(500);
} }