#include "demo.h"
#include "atk_oled.h"
#include "./SYSTEM/sys/sys.h"
#include "./SYSTEM/usart/usart.h"
#include "./SYSTEM/delay/delay.h"
#include "./BSP/LED/led.h"
#include "atk_air.h"
#include "atk_aht20.h"
#include "Beep.h"
#include "atk_light.h"
#include <stdio.h>
/**
* @brief 例程演示入口函数
* @param 无
* @retval 无
*/
void demo_run(void)
{
uint8_t t = 0;
float ppm; // 有害气体浓度
float temp; // 温度
float humid; // 湿度
float intensity; // 光照强度
char ppm_str[16]; // 存储转换后的字符串(适当增加缓冲区)
char temp_str[16];
char humid_str[16];
char intensity_str[16];
atk_air_init(); // 初始化空气质量传感器
atk_oled_init(); // 初始化OLED(默认SSD1306 128x64)
atk_aht20_init();
atk_light_init();
// 初始显示静态文本(调整Y坐标,避免超出屏幕)
atk_oled_show_string(0, 0, "EIM", 16);
atk_oled_show_string(0, 15, "Air Quality:", 12);
atk_oled_show_string(0, 27, "Temp: C", 12);
atk_oled_show_string(0, 39, "Humidity: %", 12);
atk_oled_show_string(0, 51, "Intensity: lx", 12);
atk_oled_refresh_gram(); // 首次刷新
t = ' ';
while (1)
{
// 读取传感器数据
atk_aht20_read_data(&temp, &humid);
ppm = atk_air_get_ppm();
intensity = atk_light_get_val();
atk_aht20_read_data(&temp, &humid); /* 读取ATH20传感器数据 */
printf("空气质量: %.2f\r\n", ppm);
printf("\n");
printf("湿度: %.2f%%\r\n", humid);
printf("\n");
printf("温度: %.2f℃\r\n", temp);
printf("\n");
printf("光敏传感器光照强度:%d lx\r\n", atk_light_get_val());
printf("\n");
delay_ms(1000);
if(ppm > 50|| temp >30 || intensity > 100)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_RESET);
Beep_Alarm(5);
Beep_TurnOn();
delay_ms(5);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_SET);
Beep_TurnOff();
delay_ms(5);
}
else
{
Beep_TurnOff();
}
// 格式化字符串(限制小数点后1位,节省空间)
snprintf(ppm_str, sizeof(ppm_str), "%.2f", ppm);
snprintf(temp_str, sizeof(temp_str), "%.2f", temp);
snprintf(humid_str, sizeof(humid_str), "%.2f", humid);
snprintf(intensity_str, sizeof(intensity_str), "%.2f", intensity);
// 动态更新数值(覆盖之前的内容)
atk_oled_show_string(73, 15, ppm_str, 12);
atk_oled_show_string(38, 27, temp_str, 12);
atk_oled_show_string(55, 39, humid_str, 12);
atk_oled_show_string(65, 51, humid_str, 12);
atk_oled_refresh_gram(); // 刷新显示
t++;
if (t > '~') t = ' ';
delay_ms(1000); // 降低刷新频率(1秒1次)
}
}
请修改以上代码,使得STM32F103ZET6能够每隔5分钟,记录并存储一次环境信息,且可以通过按键查询历时信息。不需要使用LCD显示数据,我们已经在代码中实现了数据显示在OLED屏中。