功能:
0.本项目采用STC89C52作为单片机系统的控制MCU
1.LCD1602液晶实时显示当前光照/土壤湿度/温度信息,以及光照/土壤湿度/温度设定阈值
2.按键可调整光照/土壤湿度/温度设定阈值
3.超过设定阈值,将控制相应的外设,如浇水、降温等
4.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB :
主程序:
#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
#include "tlc0832.h"
#include "lcd1602.h"
#include "delay.h"
#include "18b20.h"
sbit LED_WHITE = P2^2; //补光灯
sbit LED_YELLOW = P2^3; //补温灯
sbit FAN = P2^1;
sbit RELAY = P2^0;
sbit KEY_SET = P3^3;
sbit KEY_ADD = P3^4;
sbit KEY_SUB = P3^6;
bit refreshFlag = 0; //定义读时间标志
int temp; //温度读取值
float temperature = 0; //温度值
char disdat[14]; //打印数组初始化
char disset[16];
unsigned int Lval = 0; //光照强度
unsigned int Rval = 0; //土壤湿度
unsigned int dispTemp; //显示温度
unsigned char setIndex = 0; //设置值
unsigned char setLval = 50;
unsigned char setRval = 50;
unsigned char setTval = 35;
void Timer0_Init(void); //函数声明
void KeyProcess(void); //按键检测
void main(void)
{
Timer0_Init();
LCD_Init(); //初始化液晶
DelayMs(20); //延时有助于稳定
LCD_Clear(); //清屏
LED_WHITE = 0;
RELAY = 0;
LED_YELLOW = 0;
FAN = 0; //上电检测下 方便检测硬件
DelayMs(200);
LED_WHITE = 1;
RELAY = 1;
LED_YELLOW = 1;
FAN = 1;
DS18B20_Start();
DS18B20_GetTemp(&temp);
temperature = (float)temp * 0.0625; //温度处理
DelayS(1);
sprintf(disdat, "L:%2d R:%2d T:%2d", Lval, Rval, dispTemp); //打印电压电流值
LCD_DispStr(0, 0, disdat);
LCD_DispOneChar(14, 0, 0XDF);
LCD_DispOneChar(15, 0, 'C'); //显示
sprintf(disset, " L:%2d R:%2d T:%2d", (unsigned int)setLval, (unsigned int)setRval, (unsigned int)setTval); //打印电压电流值
LCD_DispStr(0, 1, disset);
while (1) //主循环
{
if (refreshFlag == 1) //定时读取
{
EA = 0;
refreshFlag = 0;
DS18B20_Start();
DS18B20_GetTemp(&temp);
temperature = (float)temp * 0.0625; //温度处理
if (temperature > 1) //防止温度读错
{
dispTemp = (unsigned int)temperature;
} //温度处理
Lval = 100 - 100 * ReadADC(AIN1_GND) / 255;
DelayMs(10); //延时有助于稳定
Rval = 99 * (255-ReadADC(AIN0_GND)) / 210;
sprintf(disdat, "L:%2d R:%2d T:%2d", Lval, Rval, dispTemp); //打印电压电流值
LCD_DispStr(0, 0, disdat); //显示
LCD_DispOneChar(14, 0, 0xDF);
LCD_DispOneChar(15, 0, 'C');
if (setIndex == 1) //进入设置
{
sprintf(disset, "*L:%2d R:%2d T:%2d ", (unsigned int)setLval, (unsigned int)setRval, (unsigned int)setTval); //打印电压电流值
}
else if (setIndex == 2)
{
sprintf(disset, " L:%2d*R:%2d T:%2d ", (unsigned int)setLval, (unsigned int)setRval, (unsigned int)setTval); //打印电压电流值
}
else if (setIndex == 3)
{
sprintf(disset, " L:%2d R:%2d*T:%2d ", (unsigned int)setLval, (unsigned int)setRval, (unsigned int)setTval); //打印电压电流值
}
else
{
sprintf(disset, " L:%2d R:%2d T:%2d ", (unsigned int)setLval, (unsigned int)setRval, (unsigned int)setTval); //打印电压电流值
}
LCD_DispStr(0, 1, disset);
if (Lval <= setLval) //光照对比
{
LED_WHITE = 0; //打开补光灯
}
else
{
LED_WHITE = 1; //关闭补光灯
}
if (Rval <= setRval) //土壤对比
{
RELAY = 0; //打开水泵继电器
}
else
{
RELAY = 1; //关闭水泵继电器
}
if (dispTemp < setTval) //温度对比
{
LED_YELLOW = 0; //打开补温灯
FAN = 1; //关闭风扇
}
else if (dispTemp > setTval)
{
LED_YELLOW = 1; //关闭补温灯
FAN = 0; //打开风扇
}
else
{
LED_YELLOW = 1; //关闭补温灯
FAN = 1; //关闭风扇
}
EA = 1;
}
KeyProcess();
}
}
void KeyProcess(void)
{
if (!KEY_SET) //设置键
{
DelayMs(20); //延时去抖
if(!KEY_SET) //再次确认按键按下
{
setIndex++;
if (setIndex > 3)
{
setIndex = 0; //取消设置
}
}
while(!KEY_SET);
}
if (!KEY_ADD) //加键
{
DelayMs(180); //延时去抖
if(!KEY_ADD) //再次确认按键按下
{
if (setIndex == 1) //设光照阈值
{
if (setLval < 99) //不超过99
{
setLval++;
}
}
else if (setIndex == 2) //设土壤阈值
{
if (setRval < 99) //不超过99
{
setRval++;
}
}
else if (setIndex == 3) //温度设置
{
if (setTval < 99) //不超过99
{
setTval++;
}
}
}
// while(!KEY_ADD);
}
if (!KEY_SUB) //减键
{
DelayMs(180); //延时去抖
if(!KEY_SUB) //再次确认按键按下
{
if (setIndex == 1) //设光照阈值
{
if (setLval > 0) //最小为0
{
setLval--;
}
}
else if (setIndex == 2) //设土壤阈值
{
if (setRval > 0) //最小为0
{
setRval--;
}
}
else if (setIndex == 3) //温度设置
{
if (setTval > 0) //最小为0
{
setTval--;
}
}
}
// while(!KEY_SUB);
}
}
void Timer0_Init(void)
{
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TH0 = (65536 - 18432) / 256; //重新赋值 20ms
TL0 = (65536 - 18432) % 256;
EA = 1; //总中断打开
ET0 = 1; //定时器中断打开
TR0 = 1; //定时器开关打开
}
void Timer0_isr(void) interrupt 1
{
static unsigned char time_20ms = 0; //定时器计数
TH0 = (65536 - 18432) / 256; //重新赋值 20ms
TL0 = (65536 - 18432) % 256;
time_20ms++;
if (time_20ms > 10) //定时读取温度
{
refreshFlag = 1; //读取温度 标志
time_20ms = 0;
}
}