2021-10-12 14:23:45 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @file main.c
|
|
|
|
|
|
* @author Myth
|
2021-10-15 00:21:49 +08:00
|
|
|
|
* @version 0.6
|
|
|
|
|
|
* @date 2021.10.15
|
2021-10-12 14:23:45 +08:00
|
|
|
|
* @brief 工程主函数文件
|
|
|
|
|
|
* @details 初始化及主循环
|
|
|
|
|
|
* @note 此版本实现功能:
|
|
|
|
|
|
* 串口回显,回显时 PC13 上的 LED 闪烁
|
2021-10-15 00:21:49 +08:00
|
|
|
|
* AHT20 温度湿度读取及 BH1750 光照度读取
|
|
|
|
|
|
* 此版本实现了 AHT20 和 BH1750 的最高速读取。每一次读取结束后 LED 闪烁
|
2021-10-12 23:38:04 +08:00
|
|
|
|
* JTAG 已禁用,请使用 SWD 调试
|
2021-10-12 14:23:45 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "sys.h"
|
|
|
|
|
|
#include "systick.h"
|
|
|
|
|
|
#include "uart.h"
|
|
|
|
|
|
|
2021-10-12 23:38:04 +08:00
|
|
|
|
#include "softi2c.h"
|
2021-10-12 14:23:45 +08:00
|
|
|
|
|
|
|
|
|
|
#include "led.h"
|
2021-10-13 18:00:53 +08:00
|
|
|
|
#include "aht20.h"
|
2021-10-14 00:48:38 +08:00
|
|
|
|
#include "bh1750.h"
|
2021-10-12 14:23:45 +08:00
|
|
|
|
|
|
|
|
|
|
void Echo(uint8_t byte);
|
|
|
|
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (HAL_Init() != HAL_OK) //初始化 HAL 库
|
|
|
|
|
|
{
|
|
|
|
|
|
Error_Handler(__FILE__, __LINE__); //错误处理
|
|
|
|
|
|
}
|
|
|
|
|
|
SystemClock_Config(); //初始化系统时钟为 72MHz
|
2021-10-12 23:38:04 +08:00
|
|
|
|
DisableJTAG(); //禁用 JTAG
|
2021-10-12 14:23:45 +08:00
|
|
|
|
SysTick_Init(); //初始化 SysTick 和软件定时器
|
|
|
|
|
|
UART_Init(); //初始化串口
|
|
|
|
|
|
|
|
|
|
|
|
LED_Init(); //初始化 LED
|
|
|
|
|
|
|
|
|
|
|
|
UART_BindReceiveHandle(COM1, Echo); //绑定 COM1 串口接收中断至 Echo 函数
|
|
|
|
|
|
|
2021-10-13 18:00:53 +08:00
|
|
|
|
AHT20_Init(); //初始化 AHT20
|
|
|
|
|
|
float humi, temp;
|
2021-10-12 18:08:20 +08:00
|
|
|
|
|
2021-10-14 00:48:38 +08:00
|
|
|
|
BH1750_Init(); //初始化 BH1750
|
2021-10-15 00:21:49 +08:00
|
|
|
|
float light;
|
2021-10-14 00:48:38 +08:00
|
|
|
|
|
2021-10-12 14:23:45 +08:00
|
|
|
|
while (1)
|
|
|
|
|
|
{
|
|
|
|
|
|
//程序主循环
|
2021-10-15 00:21:49 +08:00
|
|
|
|
AHT20_Start(); // AHT20 开始测量
|
|
|
|
|
|
BH1750_Start(); // BH1750 开始测量
|
2021-10-12 23:38:04 +08:00
|
|
|
|
|
2021-10-15 00:21:49 +08:00
|
|
|
|
Delay_ms(80);
|
2021-10-14 00:48:38 +08:00
|
|
|
|
|
2021-10-15 00:21:49 +08:00
|
|
|
|
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;
|
2021-10-12 14:23:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-15 00:21:49 +08:00
|
|
|
|
* @brief 串口回显函数
|
|
|
|
|
|
* @param byte: 本次中断接收到的字节
|
|
|
|
|
|
*/
|
2021-10-12 14:23:45 +08:00
|
|
|
|
void Echo(uint8_t byte)
|
|
|
|
|
|
{
|
|
|
|
|
|
LED1_Slow_Toggle;
|
|
|
|
|
|
UART_SendChar(COM1, byte);
|
|
|
|
|
|
}
|