重新组织时序,实现了 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
* @author Myth
* @version 0.1
* @date 2021.10.13
* @version 0.2
* @date 2021.10.15
* @brief AHT20
*/
@ -93,7 +93,25 @@ uint8_t AHT20_Read_Status(void)
}
/**
* @brief AHT20
* @brief AHT20 80ms
*/
void AHT20_Start(void)
{
//发送 AC 指令请求数据
I2C_Start;
I2C_WriteByte(0x70);
I2C_WaitAck;
I2C_WriteByte(0xac);
I2C_WaitAck;
I2C_WriteByte(0x33);
I2C_WaitAck;
I2C_WriteByte(0x00);
I2C_WaitAck;
I2C_Stop;
}
/**
* @brief AHT20 80ms
* @param humi: 湿
* @param temp:
* @retval 1 0
@ -108,20 +126,6 @@ uint8_t AHT20_Read(float *humi, float *temp)
uint8_t byte6 = 0;
uint32_t data32 = 0;
//发送 AC 指令请求数据
I2C_Start;
I2C_WriteByte(0x70);
I2C_WaitAck;
I2C_WriteByte(0xac);
I2C_WaitAck;
I2C_WriteByte(0x33);
I2C_WaitAck;
I2C_WriteByte(0x00);
I2C_WaitAck;
I2C_Stop;
Delay_ms(80); //延时 80ms等待 AHT20 生成数据
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);
}

View File

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

View File

@ -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
*/
@ -51,7 +51,20 @@ void BH1750_Init(void)
}
/**
* @brief BH1750
* @brief BH1750 180ms
*/
void BH1750_Start(void)
{
I2C_Start;
I2C_WriteByte(SLAVE_ADDR_WR);
I2C_WaitAck;
I2C_WriteByte(0x10);
I2C_WaitAck;
I2C_Stop;
}
/**
* @brief BH1750 180ms
* @retval
*/
float BH1750_Read(void)
@ -60,15 +73,6 @@ float BH1750_Read(void)
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;
@ -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();
}

View File

@ -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
*/
@ -17,6 +17,8 @@
#define BH1750_SCL_PIN GPIO_PIN_11
void BH1750_Init(void);
void BH1750_Start(void);
float BH1750_Read(void);
float BH1750_StartAndRead(void);
#endif

View File

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

View File

@ -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,18 +43,24 @@ 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;