功能:
0.本系统采用STC89C52作为单片机
1.LCD1602液晶实时显示当前雨滴/光强/温湿度/晾衣架状态
2.支持手动/自动两种模式
3.自动模式下,当雨滴<5/光强<80同时满足时,晾衣架将自动晾晒
4.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB :
主程序:
#include <reg52.h> //头文件
#include "stdio.h"
#include "delay.h"
#include "lcd1602.h"
#include "28BYJ48.h"
#include "tlc0832.h"
#include "dht11.h"
#define uchar unsigned char //宏定义
#define uint unsigned int
/*******************引脚定义*********************/
sbit KEY_MODE = P3^6; //设置键
sbit KEY_OUT = P3^5; //晾晒键
sbit KEY_IN = P3^4; //回收键
/*******************变量定义*********************/
bit modeFlag = 0; //模式标记。=0手动,=1自动
uint light; //光强
uint water; //雨滴
uint humidity; //湿度
uint temp; //温度
uint motorCnt = 0; //记录晾衣架位置
uchar motorFlag = 2; //标记当前控制状态,=0已经晾晒,=1过程中,=2已经回收
bit motorDir = 0; //方向
bit dispFlag = 1;
uchar dis0[16];
/************************* 宏定义 *************************/
#define AUTO 0
#define MANUAL 1
/************************* 函数声明 *************************/
void Timer0_Init(void); //初始化定时器0
/********************************************************
函数名称:void mian()
函数作用:主函数
参数说明:
********************************************************/
void main()
{
modeFlag = MANUAL;
Timer0_Init(); //初始化定时器0
LCD_Init(); //初始化液晶
DelayMs(200); //延时有助于稳定
LCD_Clear(); //清屏
while (1) //死循环
{
if (dispFlag == 1)
{
dispFlag = 0;
DHT11_ReadData(); //读取温湿度值
humidity = U8RH_data_H;
temp = U8T_data_H;
light = 100 - 100 * ReadADC(AIN1_GND) / 255; //读取光强
water = 100 - 100 * ReadADC(AIN0_GND) / 255; //读取雨滴
sprintf(dis0, "L:%3d%% H:%3d%%", light, humidity);
LCD_DispStr(0, 0, dis0);
sprintf(dis0, "W:%3d%% T:%3d", water, temp);
LCD_DispStr(0, 1, dis0);
LCD_DispOneChar(12, 1, 0xdf);
LCD_DispOneChar(13, 1, 'C');
}
if (KEY_MODE == 0)
{
DelayMs(50);
if (KEY_MODE == 0)
{
modeFlag = ~modeFlag;
}
while (KEY_MODE == 0)
;
}
if (modeFlag == AUTO)
{
LCD_DispOneChar(15, 0, 'A');
if (motorFlag == 2) //已经回收状态
{
LCD_DispOneChar(15, 1, 'C');
if (light < 80 && water < 5) //当光强满足条件且没下雨时,晾晒衣服
{
motorFlag = 1;
motorDir = 1;
motorCnt = 0;
}
}
else if (motorFlag == 0) //已经晾晒状态
{
LCD_DispOneChar(15, 1, 'O');
if (light < 80 && water < 5) //当光强满足条件且没下雨时,晾晒衣服
{
;
}
else //否则回收
{
motorFlag = 1;
motorDir = 0;
motorCnt = 128;
}
}
}
else
{
LCD_DispOneChar(15, 0, 'M');
if (motorFlag == 2) //已经回收状态
{
LCD_DispOneChar(15, 1, 'C');
if (KEY_OUT == 0) //晾晒
{
DelayMs(50);
if (KEY_OUT == 0)
{
motorFlag = 1;
motorDir = 1;
motorCnt = 0;
}
while (KEY_OUT == 0)
;
}
}
else if (motorFlag == 0) //回收
{
LCD_DispOneChar(15, 1, 'O');
if (KEY_IN == 0)
{
DelayMs(50);
if (KEY_IN == 0)
{
motorFlag = 1;
motorDir = 0;
motorCnt = 128;
}
while (KEY_IN == 0)
;
}
}
}
if (motorFlag == 1)
{
LCD_DispOneChar(15, 1, 'D');
if (motorDir == 1)
{
motorCnt++;
motor_z();
if (motorCnt == 128)
{
motorFlag = 0;
}
}
else
{
motorCnt--;
motor_f();
if (motorCnt == 0)
{
motorFlag = 2;
}
}
}
}
}
/*------------------------------------------------
定时器初始化子程序
------------------------------------------------*/
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_Interrupt(void) interrupt 1
{
static unsigned char time20ms = 0;
TH0 = (65536 - 18432) / 256; //重新赋值 20ms
TL0 = (65536 - 18432) % 256;
time20ms++;
if (time20ms > 50)
{
time20ms = 0;
dispFlag = 1; //读标志位置1
}
}