功能介绍:
0.本系统采用STM32F103C8T6作为单片机
1.采用0.96寸OLED作为显示器实时显示二氧化碳温湿度等相关相关参数
2.通过4个功能按键可设置二氧化碳浓度等参数的阈值
3.一旦环境参数超过设置的阈值,蜂鸣器和LED即声光报警
4.设计有风扇和补光灯电路,可实现降温和补光功能
5.二氧化碳传感器采用的是高精度SGP30芯片
原理图:
PCB:
主程序:
#define __MAIN_C
#include "main.h"
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "bsp_gpio.h"
#include "bsp_clkconfig.h"
#include "bsp_delay.h"
#include "bsp_GeneralTim.h"
#include "bsp_dht11.h"
#include "bsp_key.h"
#include "bsp_oled_iic.h"
#include "bsp_sgp30.h"
enum MODE_DF dispMode;
volatile CreatByte Flag;
int temperature = 0;
volatile int temp_min = 15;
volatile int temp_max = 40;
int humidity = 0;
volatile int humidity_min = 35;
volatile int humidity_max = 75;
u32 CO2_CONC = 0;
volatile int CO2_CONC_max = 1000;
char dis0[16];
volatile unsigned char setIndex = 0;
int main(void)
{
u32 sgp30_dat;
// 使用HSI,SYSCLK = 4M * RCC_PLLMul_x, x:[2,3,...16],最高是64MH
HSI_SetSysClock(RCC_PLLMul_2); //使用内部8MHz晶振,并设置PLL输出为8MHz
GENERAL_TIM_Init();
DelayMs(50);
GPIO_Config();
DelayMs(50);
Key_GPIO_Config();
DelayMs(50);
DHT11_GPIO_Init();
DelayMs(50);
SGP30_Init(); //初始化SGP30
DelayMs(50);
SGP30_Write(0x20,0x08);
sgp30_dat = SGP30_Read();//读取SGP30的值
CO2_CONC = (sgp30_dat & 0xffff0000) >> 16;
OLED_I2C_GPIO_Init();
OLED_Init();
DelayMs(250);
OLED_Display_Off();
OLED_Clear();
sprintf(dis0, "Temp:%3d", 0);
OLED_ShowString(0, 0, dis0, FONT_1608);
OLED_ShowWord(8*8, 0, 0); //显示摄氏度
sprintf(dis0, "Humidity:%3d%%", 0);
OLED_ShowString(0, 2, dis0, FONT_1608);
sprintf(dis0, "CO2:%5dppm", CO2_CONC);
OLED_ShowString(0, 4, dis0, FONT_1608);
OLED_Display_On();
while (1) //主循环
{
if (readFlag == 1)
{
readFlag = 0;
if (dispMode == NORMAL)
{
DHT11_ReadData(); //读取模块数据周期不易小于 2S
temperature = U8T_data_H;
humidity = U8RH_data_H;
sprintf(dis0, "Temp:%3d", temperature);
OLED_ShowString(0, 0, dis0, FONT_1608);
OLED_ShowWord(8*8, 0, 0); //显示摄氏度
sprintf(dis0, "Humidity:%3d%%", humidity);
OLED_ShowString(0, 2, dis0, FONT_1608);
SGP30_Write(0x20,0x08);
sgp30_dat = SGP30_Read();//读取SGP30的值
CO2_CONC = (sgp30_dat & 0xffff0000) >> 16;
sprintf(dis0, "CO2:%5dppm", CO2_CONC);
OLED_ShowString(0, 4, dis0, FONT_1608);
if (temperature <= temp_max && temperature >= temp_min && \
humidity <= humidity_max && humidity >= humidity_min && \
CO2_CONC <= CO2_CONC_max) //处于阈值范围内
{
BUZZER_OFF;
}
else
{
BUZZER_ON;
}
if (temperature < temp_min) //温度低于设定值
{
LIGHT_ON; //开启补温灯
}
else
{
LIGHT_OFF; //关闭补温灯
}
if (CO2_CONC > CO2_CONC_max) //CO2浓度超过设定值
{
FAN_ON; //开启风扇
}
else
{
FAN_OFF; //关闭风扇
}
}
}
TIM_ITConfig(GENERAL_TIM2, TIM_IT_Update, DISABLE);
KeyProcess();
TIM_ITConfig(GENERAL_TIM2, TIM_IT_Update, ENABLE);
}
}
void dispSet(void)
{
sprintf(dis0, "TH:%2d", temp_max);
OLED_ShowString(8, 0, dis0, FONT_1608);
sprintf(dis0, "TL:%2d", temp_min);
OLED_ShowString(8*8, 0, dis0, FONT_1608);
sprintf(dis0, "HH:%2d", humidity_max);
OLED_ShowString(8, 2, dis0, FONT_1608);
sprintf(dis0, "HL:%2d", humidity_min);
OLED_ShowString(8*8, 2, dis0, FONT_1608);
sprintf(dis0, "CO2_MAX:%5d", CO2_CONC_max);
OLED_ShowString(8, 4, dis0, FONT_1608);
if (setIndex == 1)
{
OLED_ShowChar(0, 4, ' ', FONT_1608);
OLED_ShowChar(0, 0, '>', FONT_1608);
}
else if (setIndex == 2)
{
OLED_ShowChar(0, 0, ' ', FONT_1608);
OLED_ShowChar(8*7, 0, '>', FONT_1608);
}
else if (setIndex == 3)
{
OLED_ShowChar(8*7, 0, ' ', FONT_1608);
OLED_ShowChar(0, 2, '>', FONT_1608);
}
else if (setIndex == 4)
{
OLED_ShowChar(0, 2, ' ', FONT_1608);
OLED_ShowChar(8*7, 2, '>', FONT_1608);
}
else if (setIndex == 5)
{
OLED_ShowChar(8*7, 2, ' ', FONT_1608);
OLED_ShowChar(0, 4, '>', FONT_1608);
}
}
/*********************************************END OF FILE**********************/