diff --git a/Hardware/aht20/aht20.c b/Hardware/aht20/aht20.c
index 5bfab3a..40fc0ba 100644
--- a/Hardware/aht20/aht20.c
+++ b/Hardware/aht20/aht20.c
@@ -1,8 +1,8 @@
/**
* @file aht20.c
* @author Myth
- * @version 0.1
- * @date 2021.10.13
+ * @version 0.2
+ * @date 2021.10.15
* @brief AHT20 驱动
*/
@@ -25,8 +25,8 @@ SoftI2C_TypeDef aht20_i2c;
uint8_t AHT20_Read_Status(void);
/**
- * @brief 初始化 AHT20
- */
+ * @brief 初始化 AHT20
+ */
void AHT20_Init(void)
{
aht20_i2c.SDA_GPIO = AHT20_GPIO;
@@ -72,9 +72,9 @@ void AHT20_Init(void)
}
/**
- * @brief 读取 AHT20 状态字
- * @retval 状态字
- */
+ * @brief 读取 AHT20 状态字
+ * @retval 状态字
+ */
uint8_t AHT20_Read_Status(void)
{
uint8_t data;
@@ -93,21 +93,10 @@ uint8_t AHT20_Read_Status(void)
}
/**
- * @brief 读取 AHT20 数据
- * @param humi: 浮点数指针,储存湿度
- * @param temp: 浮点数指针,储存温度
- * @retval 读取成功返回 1,读取失败返回 0
- */
-uint8_t AHT20_Read(float *humi, float *temp)
+ * @brief 向 AHT20 发送测量命令。需等待 80ms 后才能读取数据
+ */
+void AHT20_Start(void)
{
- uint8_t byte1 = 0;
- uint8_t byte2 = 0;
- uint8_t byte3 = 0;
- uint8_t byte4 = 0;
- uint8_t byte5 = 0;
- uint8_t byte6 = 0;
- uint32_t data32 = 0;
-
//发送 AC 指令请求数据
I2C_Start;
I2C_WriteByte(0x70);
@@ -119,8 +108,23 @@ uint8_t AHT20_Read(float *humi, float *temp)
I2C_WriteByte(0x00);
I2C_WaitAck;
I2C_Stop;
+}
- Delay_ms(80); //延时 80ms,等待 AHT20 生成数据
+/**
+ * @brief 读取 AHT20 数据。在发送测量命令至少 80ms 后调用
+ * @param humi: 浮点数指针,储存湿度
+ * @param temp: 浮点数指针,储存温度
+ * @retval 读取成功返回 1,读取失败返回 0
+ */
+uint8_t AHT20_Read(float *humi, float *temp)
+{
+ uint8_t byte1 = 0;
+ uint8_t byte2 = 0;
+ uint8_t byte3 = 0;
+ uint8_t byte4 = 0;
+ uint8_t byte5 = 0;
+ uint8_t byte6 = 0;
+ uint32_t data32 = 0;
uint16_t count = 0;
while (((AHT20_Read_Status() & 0x80) == 0x80)) //等待状态字最高位变为 1,说明数据准备完成
@@ -161,3 +165,16 @@ uint8_t AHT20_Read(float *humi, float *temp)
return 1;
}
+
+/**
+ * @brief 发送测量命令,等待 80ms 并读取 AHT20 数据
+ * @param humi: 浮点数指针,储存湿度
+ * @param temp: 浮点数指针,储存温度
+ * @retval 读取成功返回 1,读取失败返回 0
+ */
+uint8_t AHT20_StartAndRead(float *humi, float *temp)
+{
+ AHT20_Start();
+ Delay_ms(80); //延时 80ms,等待 AHT20 生成数据
+ return AHT20_Read(humi, temp);
+}
diff --git a/Hardware/aht20/aht20.h b/Hardware/aht20/aht20.h
index 649a521..7da96e4 100644
--- a/Hardware/aht20/aht20.h
+++ b/Hardware/aht20/aht20.h
@@ -1,8 +1,8 @@
/**
* @file aht20.h
* @author Myth
- * @version 0.1
- * @date 2021.10.13
+ * @version 0.2
+ * @date 2021.10.15
* @brief AHT20 驱动
*/
@@ -16,6 +16,8 @@
#define AHT20_SCL_PIN GPIO_PIN_4
void AHT20_Init(void);
+void AHT20_Start(void);
uint8_t AHT20_Read(float *humi, float *temp);
+uint8_t AHT20_StartAndRead(float *humi, float *temp);
#endif
diff --git a/Hardware/bh1750/bh1750.c b/Hardware/bh1750/bh1750.c
index 0e5ecaf..da51298 100644
--- a/Hardware/bh1750/bh1750.c
+++ b/Hardware/bh1750/bh1750.c
@@ -1,8 +1,8 @@
/**
* @file bh1750.c
* @author Myth
- * @version 0.1
- * @date 2021.10.14
+ * @version 0.2
+ * @date 2021.10.15
* @brief BH1750 驱动
*/
@@ -26,8 +26,8 @@
SoftI2C_TypeDef bh1750_i2c;
/**
- * @brief 初始化 BH1750
- */
+ * @brief 初始化 BH1750
+ */
void BH1750_Init(void)
{
bh1750_i2c.SDA_GPIO = BH1750_GPIO;
@@ -51,23 +51,27 @@ void BH1750_Init(void)
}
/**
- * @brief 读取 BH1750 数据
- * @retval 读数
- */
-float BH1750_Read(void)
+ * @brief 向 BH1750 发送测量命令。需等待 180ms 后才能读取数据
+ */
+void BH1750_Start(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);
+/**
+ * @brief 读取 BH1750 数据。在发送测量命令至少 180ms 后调用
+ * @retval 读数
+ */
+float BH1750_Read(void)
+{
+ uint8_t i;
+ uint8_t byte1;
+ uint8_t byte2;
I2C_Start;
I2C_WriteByte(SLAVE_ADDR_RD);
@@ -83,3 +87,14 @@ float BH1750_Read(void)
return (float)((byte1 << 8) + byte2) / 1.2;
}
+
+/**
+ * @brief 发送测量命令,等待 180ms 并读取 BH1750 数据
+ * @retval 读数
+ */
+float BH1750_StartAndRead(void)
+{
+ BH1750_Start();
+ Delay_ms(180);
+ return BH1750_Read();
+}
diff --git a/Hardware/bh1750/bh1750.h b/Hardware/bh1750/bh1750.h
index aad5dd5..4d8a5db 100644
--- a/Hardware/bh1750/bh1750.h
+++ b/Hardware/bh1750/bh1750.h
@@ -1,8 +1,8 @@
/**
* @file bh1750.h
* @author Myth
- * @version 0.1
- * @date 2021.10.14
+ * @version 0.2
+ * @date 2021.10.15
* @brief BH1750 驱动
*/
@@ -11,12 +11,14 @@
#include "sys.h"
-//BH1750 引脚设置
+// BH1750 引脚设置
#define BH1750_GPIO GPIOB
#define BH1750_SDA_PIN GPIO_PIN_10
#define BH1750_SCL_PIN GPIO_PIN_11
void BH1750_Init(void);
+void BH1750_Start(void);
float BH1750_Read(void);
+float BH1750_StartAndRead(void);
#endif
diff --git a/Project/SlaveNode.uvprojx b/Project/SlaveNode.uvprojx
index 45f0db2..4f71a92 100644
--- a/Project/SlaveNode.uvprojx
+++ b/Project/SlaveNode.uvprojx
@@ -332,7 +332,7 @@
3
1
1
- 0
+ 1
0
0
diff --git a/User/Main/main.c b/User/Main/main.c
index d1f7afe..a9c1512 100644
--- a/User/Main/main.c
+++ b/User/Main/main.c
@@ -1,15 +1,14 @@
/**
* @file main.c
* @author Myth
- * @version 0.5
- * @date 2021.10.14
+ * @version 0.6
+ * @date 2021.10.15
* @brief 工程主函数文件
* @details 初始化及主循环
* @note 此版本实现功能:
* 串口回显,回显时 PC13 上的 LED 闪烁
- * 软件 I2C
- * AHT20 温度湿度读取
- * BH1750 光照度读取
+ * AHT20 温度湿度读取及 BH1750 光照度读取
+ * 此版本实现了 AHT20 和 BH1750 的最高速读取。每一次读取结束后 LED 闪烁
* JTAG 已禁用,请使用 SWD 调试
*/
@@ -44,27 +43,33 @@ int main(void)
float humi, temp;
BH1750_Init(); //初始化 BH1750
+ float light;
while (1)
{
//程序主循环
- if (AHT20_Read(&humi, &temp)) //读取 AHT20
- printf("humi: %.1f temp: %.1f\n", humi, temp);
- else
- printf("fail to read AHT20.\n");
+ AHT20_Start(); // AHT20 开始测量
+ BH1750_Start(); // BH1750 开始测量
- printf("light: %f\n", BH1750_Read()); //读取 BH1750
+ Delay_ms(80);
- Delay_ms(500);
+ AHT20_Read(&humi, &temp); //读取 AHT20
+
+ Delay_ms(100);
+
+ light = BH1750_Read(); //读取 BH1750
+
+ printf("humi: %.1f temp: %.1f light: %.1f\n", humi, temp, light);
+ LED1_Toggle;
}
return 1;
}
/**
- * @brief 串口回显函数
- * @param byte: 本次中断接收到的字节
- */
+ * @brief 串口回显函数
+ * @param byte: 本次中断接收到的字节
+ */
void Echo(uint8_t byte)
{
LED1_Slow_Toggle;