功能介绍:
0.本系统采用STC89C52作为单片机
1.系统实时监测当前的光照和温湿度
2.支持手动/自动两种模式
3.自动模式下,当光强超过阈值或温度超过阈值时,关闭遮阳伞,起到遮阳降温的作用
若光强和温度值适宜,但湿度较高,打开遮阳伞,让环境通风
4.按键可设定阈值,手动控制遮阳伞,切换工作模式
5.WIFI定时发送监测的光照和温湿度
6.WIFI可控制电机,切换工作模式。
*C# 关闭遮阳伞 *O# 打开遮阳伞 *M# 手动模式 *A# 自动模式
7.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB:
主程序:
/*************************************************************
智能多肉养殖系统
补充说明:
***************************************************************/
#include "main.h"
/*******************变量定义*********************/
enum _MODE_DF_ dispMode;
bit modeFlag = AUTO; //模式标记
uchar setIndex = 0;
uint humidity; //湿度
uint temperature; //温度
uint light; //光强
uint hmMin= 25;
uint hmMax= 35;
uint tempMin = 25;
uint tempMax = 40;
uint lightMin = 30;
uint lightMax = 60;
uint motorCnt = 0; //记录遮光棚位置
uchar motorFlag = 2; //标记当前控制状态,=0遮阳棚已关闭,=1过程中,=2遮阳棚已打开
bit motorDir = 0; //方向
bit dispFlag = 0;
bit sendFlag = 0;
char dis[32];
/********************************************************
函数名称:void mian()
函数作用:主函数
参数说明:
********************************************************/
void main()
{
modeFlag = MANUAL;
Timer0_Init(); //初始化定时器0
UART_Init(); //初始化串口
LCD_Init(); //初始化液晶
DelayMs(200); //延时有助于稳定
LCD_DispStr(4, 0, "Welcome!");
UART_SendStr("AT+CIPMUX=1\r\n", 13); //打开多连接
DelayS(1);
UART_SendStr("AT+CIPSERVER=1,8080\r\n", 21); //建立服务 端口号为8080
DelayS(1);
LCD_Clear(); //清屏
BYJ48 = ((BYJ48 & 0xF0) | REV[1]); //取数据
ULN_DQ3 = BIT3; ULN_DQ2 = BIT2; ULN_DQ1 = BIT1; ULN_DQ0 = BIT0;
while (1) //死循环
{
if (dispFlag == 1)
{
dispFlag = 0;
if (dispMode == NORMAL)
{
DispNormal();
}
}
if (sendFlag == 1)
{
sendFlag = 0;
SendData();
}
if (modeFlag == AUTO)
{
if (motorFlag == 2) //遮阳伞已打开
{
if (light > lightMax || temperature > tempMax) //光照太强或温度过高
{
//关闭遮阳伞
motorFlag = 1;
motorDir = 1;
motorCnt = MINCNT;
}
}
else if (motorFlag == 0) //遮阳伞已关闭
{
if (light <= lightMax && temperature <= tempMax) //光照温度适宜
{
if (humidity > hmMax) //湿度过高,打开遮阳伞通风
{
//打开遮阳伞
motorFlag = 1;
motorDir = 0;
motorCnt = MAXCNT;
}
}
}
}
KeyProcess();
}
}
/*------------------------------------------------
定时器初始化子程序
------------------------------------------------*/
void Timer0_Init(void)
{
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TH0 = (65536 - 9216) / 256; //重新赋值 1ms
TL0 = (65536 - 9216) % 256;
EA = 1; //总中断打开
ET0 = 1; //定时器中断打开
TR0 = 1; //定时器开关打开
}
/*------------------------------------------------
定时器中断子程序
------------------------------------------------*/
void Timer0_Interrupt(void) interrupt 1
{
static unsigned int time1ms = 0;
TL0 = 0x66; //设置定时初值
TH0 = 0xFC; //设置定时初值
time1ms++;
if (time1ms % 500 == 0)
{
dispFlag = 1; //显示标志
}
if (time1ms > 5000)
{
time1ms = 0;
sendFlag = 1; //WIFI发送信息标志
}
if (motorFlag == 1 && time1ms % 1 == 0)
{
if (motorDir == 1)
{
ES = 0;
motorCnt++;
BYJ48 = ((BYJ48 & 0xF0) | REV[motorCnt % 8]); //取数据
ULN_DQ3 = BIT3; ULN_DQ2 = BIT2; ULN_DQ1 = BIT1; ULN_DQ0 = BIT0;
if (motorCnt == MAXCNT)
{
motorFlag = 0;
ES = 1;
}
}
else
{
ES = 0;
motorCnt--;
BYJ48 = ((BYJ48 & 0xF0) | REV[motorCnt % 8]); //取数据
ULN_DQ3 = BIT3; ULN_DQ2 = BIT2; ULN_DQ1 = BIT1; ULN_DQ0 = BIT0;
if (motorCnt == MINCNT)
{
motorFlag = 2;
ES = 1;
}
}
}
}
void SendData(void)
{
UART_SendStr("AT+CIPSEND=0,30\r\n", 17); //发送数据
DelayMs(100);
sprintf(dis, "Hm:%3d%% Temp:%3d'C Light:%2d%%\r\n", humidity, temperature, light); //串口发送
UART_SendStr(dis, 30); //发送数据
DelayMs(100);
}
void DispNormal(void)
{
ET0 = 0;
DHT11_0_ReadData();
ET0 = 1;
temperature = U8T_data_H;
humidity = 100 - 100 * (ReadADC(AIN0_GND)-85) / 170; //读取土壤湿度
sprintf(dis, "Hm:%3d%% T:%3d", humidity, temperature);
LCD_DispStr(0, 0, dis);
LCD_DispOneChar(13, 0, 0xdf);
LCD_DispOneChar(14, 0, 'C');
LCD_DispOneChar(15, 0, ' ');
light = 100 - 100 * ReadADC(AIN1_GND) / 255; //读取光强
sprintf(dis, "L:%2d%% ", light);
LCD_DispStr(0, 1, dis);
if (modeFlag == AUTO)
{
LCD_DispStr(6, 1, "Md:A ");
}
else
{
LCD_DispStr(6, 1, "Md:M ");
}
if (motorFlag == 2) //遮阳伞已打开
{
LCD_DispStr(11, 1, "St:O ");
}
else if (motorFlag == 1)
{
LCD_DispStr(11, 1, "St:D ");
}
else if (motorFlag == 0) //遮阳伞已关闭
{
LCD_DispStr(11, 1, "St:C ");
}
}
void DispSetHm(unsigned char setIndex)
{
LCD_DispStr(0, 0, " Set Humidity ");
sprintf(dis, " H:%2d%% L:%2d%% ", hmMax, hmMin);
LCD_DispStr(0, 1, dis);
switch (setIndex)
{
case 1: LCD_SetCursor(6, 1, 1); break;
case 2: LCD_SetCursor(12, 1, 1); break;
default:;
}
}
void DispSetTemp(unsigned char setIndex)
{
LCD_DispStr(0, 0, "Set Temperature ");
sprintf(dis, " H:%3d L:%3d ", tempMax, tempMin);
LCD_DispStr(0, 1, dis);
switch (setIndex)
{
case 1: LCD_SetCursor(7, 1, 1); break;
case 2: LCD_SetCursor(13, 1, 1); break;
default:;
}
}
void DispSetLight(unsigned char setIndex)
{
LCD_DispStr(0, 0, " Set LightLimit ");
sprintf(dis, " H:%2d%% L:%2d%% ", lightMax, lightMin);
LCD_DispStr(0, 1, dis);
switch (setIndex)
{
case 1: LCD_SetCursor(6, 1, 1); break;
case 2: LCD_SetCursor(12, 1, 1); break;
default:;
}
}
void UART_Init(void)
{
SCON = 0x50;
TH2 = 0xFF;
TL2 = 0xFD;
RCAP2H = 0xFF; //(65536-(FOSC/32/BAUD)) BAUD = 115200 FOSC = 11059200
RCAP2L = 0xFD;
/*****************/
TCLK = 1;
RCLK = 1;
C_T2 = 0;
EXEN2 = 0;
/*****************/
TR2 = 1;
ES = 1; //打开串口中断
EA = 1; //打开总中断
}