重新组织时序,实现了 AHT20 和 BH1750 的最高速读取

This commit is contained in:
lxbpxylps@126.com 2021-10-15 00:21:49 +08:00
parent 2e77947dd2
commit 633d780c8e
6 changed files with 96 additions and 55 deletions

View File

@ -1,8 +1,8 @@
/** /**
* @file aht20.c * @file aht20.c
* @author Myth * @author Myth
* @version 0.1 * @version 0.2
* @date 2021.10.13 * @date 2021.10.15
* @brief AHT20 * @brief AHT20
*/ */
@ -25,8 +25,8 @@ SoftI2C_TypeDef aht20_i2c;
uint8_t AHT20_Read_Status(void); uint8_t AHT20_Read_Status(void);
/** /**
* @brief AHT20 * @brief AHT20
*/ */
void AHT20_Init(void) void AHT20_Init(void)
{ {
aht20_i2c.SDA_GPIO = AHT20_GPIO; aht20_i2c.SDA_GPIO = AHT20_GPIO;
@ -72,9 +72,9 @@ void AHT20_Init(void)
} }
/** /**
* @brief AHT20 * @brief AHT20
* @retval * @retval
*/ */
uint8_t AHT20_Read_Status(void) uint8_t AHT20_Read_Status(void)
{ {
uint8_t data; uint8_t data;
@ -93,21 +93,10 @@ uint8_t AHT20_Read_Status(void)
} }
/** /**
* @brief AHT20 * @brief AHT20 80ms
* @param humi: 湿 */
* @param temp: void AHT20_Start(void)
* @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;
//发送 AC 指令请求数据 //发送 AC 指令请求数据
I2C_Start; I2C_Start;
I2C_WriteByte(0x70); I2C_WriteByte(0x70);
@ -119,8 +108,23 @@ uint8_t AHT20_Read(float *humi, float *temp)
I2C_WriteByte(0x00); I2C_WriteByte(0x00);
I2C_WaitAck; I2C_WaitAck;
I2C_Stop; 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; uint16_t count = 0;
while (((AHT20_Read_Status() & 0x80) == 0x80)) //等待状态字最高位变为 1说明数据准备完成 while (((AHT20_Read_Status() & 0x80) == 0x80)) //等待状态字最高位变为 1说明数据准备完成
@ -161,3 +165,16 @@ uint8_t AHT20_Read(float *humi, float *temp)
return 1; 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);
}

View File

@ -1,8 +1,8 @@
/** /**
* @file aht20.h * @file aht20.h
* @author Myth * @author Myth
* @version 0.1 * @version 0.2
* @date 2021.10.13 * @date 2021.10.15
* @brief AHT20 * @brief AHT20
*/ */
@ -16,6 +16,8 @@
#define AHT20_SCL_PIN GPIO_PIN_4 #define AHT20_SCL_PIN GPIO_PIN_4
void AHT20_Init(void); void AHT20_Init(void);
void AHT20_Start(void);
uint8_t AHT20_Read(float *humi, float *temp); uint8_t AHT20_Read(float *humi, float *temp);
uint8_t AHT20_StartAndRead(float *humi, float *temp);
#endif #endif

View File

@ -1,8 +1,8 @@
/** /**
* @file bh1750.c * @file bh1750.c
* @author Myth * @author Myth
* @version 0.1 * @version 0.2
* @date 2021.10.14 * @date 2021.10.15
* @brief BH1750 * @brief BH1750
*/ */
@ -26,8 +26,8 @@
SoftI2C_TypeDef bh1750_i2c; SoftI2C_TypeDef bh1750_i2c;
/** /**
* @brief BH1750 * @brief BH1750
*/ */
void BH1750_Init(void) void BH1750_Init(void)
{ {
bh1750_i2c.SDA_GPIO = BH1750_GPIO; bh1750_i2c.SDA_GPIO = BH1750_GPIO;
@ -51,23 +51,27 @@ void BH1750_Init(void)
} }
/** /**
* @brief BH1750 * @brief BH1750 180ms
* @retval */
*/ void BH1750_Start(void)
float BH1750_Read(void)
{ {
uint8_t i;
uint8_t byte1;
uint8_t byte2;
I2C_Start; I2C_Start;
I2C_WriteByte(SLAVE_ADDR_WR); I2C_WriteByte(SLAVE_ADDR_WR);
I2C_WaitAck; I2C_WaitAck;
I2C_WriteByte(0x10); I2C_WriteByte(0x10);
I2C_WaitAck; I2C_WaitAck;
I2C_Stop; 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_Start;
I2C_WriteByte(SLAVE_ADDR_RD); I2C_WriteByte(SLAVE_ADDR_RD);
@ -83,3 +87,14 @@ float BH1750_Read(void)
return (float)((byte1 << 8) + byte2) / 1.2; return (float)((byte1 << 8) + byte2) / 1.2;
} }
/**
* @brief 180ms BH1750
* @retval
*/
float BH1750_StartAndRead(void)
{
BH1750_Start();
Delay_ms(180);
return BH1750_Read();
}

View File

@ -1,8 +1,8 @@
/** /**
* @file bh1750.h * @file bh1750.h
* @author Myth * @author Myth
* @version 0.1 * @version 0.2
* @date 2021.10.14 * @date 2021.10.15
* @brief BH1750 * @brief BH1750
*/ */
@ -11,12 +11,14 @@
#include "sys.h" #include "sys.h"
//BH1750 引脚设置 // BH1750 引脚设置
#define BH1750_GPIO GPIOB #define BH1750_GPIO GPIOB
#define BH1750_SDA_PIN GPIO_PIN_10 #define BH1750_SDA_PIN GPIO_PIN_10
#define BH1750_SCL_PIN GPIO_PIN_11 #define BH1750_SCL_PIN GPIO_PIN_11
void BH1750_Init(void); void BH1750_Init(void);
void BH1750_Start(void);
float BH1750_Read(void); float BH1750_Read(void);
float BH1750_StartAndRead(void);
#endif #endif

View File

@ -332,7 +332,7 @@
<v6LangP>3</v6LangP> <v6LangP>3</v6LangP>
<vShortEn>1</vShortEn> <vShortEn>1</vShortEn>
<vShortWch>1</vShortWch> <vShortWch>1</vShortWch>
<v6Lto>0</v6Lto> <v6Lto>1</v6Lto>
<v6WtE>0</v6WtE> <v6WtE>0</v6WtE>
<v6Rtti>0</v6Rtti> <v6Rtti>0</v6Rtti>
<VariousControls> <VariousControls>

View File

@ -1,15 +1,14 @@
/** /**
* @file main.c * @file main.c
* @author Myth * @author Myth
* @version 0.5 * @version 0.6
* @date 2021.10.14 * @date 2021.10.15
* @brief * @brief
* @details * @details
* @note * @note
* PC13 LED * PC13 LED
* I2C * AHT20 湿 BH1750
* AHT20 湿 * AHT20 BH1750 LED
* BH1750
* JTAG 使 SWD * JTAG 使 SWD
*/ */
@ -44,27 +43,33 @@ int main(void)
float humi, temp; float humi, temp;
BH1750_Init(); //初始化 BH1750 BH1750_Init(); //初始化 BH1750
float light;
while (1) while (1)
{ {
//程序主循环 //程序主循环
if (AHT20_Read(&humi, &temp)) //读取 AHT20 AHT20_Start(); // AHT20 开始测量
printf("humi: %.1f temp: %.1f\n", humi, temp); BH1750_Start(); // BH1750 开始测量
else
printf("fail to read AHT20.\n");
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; return 1;
} }
/** /**
* @brief * @brief
* @param byte: * @param byte:
*/ */
void Echo(uint8_t byte) void Echo(uint8_t byte)
{ {
LED1_Slow_Toggle; LED1_Slow_Toggle;