初步完成 BH1750 光照度读取
This commit is contained in:
parent
1390298d0d
commit
2e77947dd2
85
Hardware/bh1750/bh1750.c
Normal file
85
Hardware/bh1750/bh1750.c
Normal 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
22
Hardware/bh1750/bh1750.h
Normal 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
|
||||
@ -339,7 +339,7 @@
|
||||
<MiscControls></MiscControls>
|
||||
<Define>USE_HAL_DRIVER, STM32F103x6</Define>
|
||||
<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>
|
||||
</Cads>
|
||||
<Aads>
|
||||
@ -698,6 +698,11 @@
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\Hardware\aht20\aht20.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>bh1750.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\Hardware\bh1750\bh1750.c</FilePath>
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
</Groups>
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user