功能:
0.本项目采用STC89C52作为单片机系统的控制MCU
1.WIFI实时上传当前水位状态
2.通过液位传感器检测液位,检测到最低液位和最高液位时,超过设定阈值将声光报警
3.通过状态指示灯显示当前液位情况,三种颜色,红色代表低于下限,黄色代表高于上限,绿色代表正常
4.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB :
主程序:
#include "reg52.h"
#include <intrins.h>
#include <stdio.h>
#include "delay.h"
sbit LOW_WATER_LEVEL = P1^1; //水位下限
sbit HIGH_WATER_LEVEL = P1^0; //水位上限
sbit LED_NORMAL = P3^5;
sbit LED_LOW = P3^3;
sbit LED_HIGH = P3^4;
sbit BUZZER = P2^2;
unsigned char sendDataFlag = 0;
bit messageCnt = 0;
void Timer0_Init(); //函数声明
void UART_Init();
void UART_SendByte(unsigned char dat);
void UART_SendStr(unsigned char *s, unsigned char length);
void main()
{
Timer0_Init(); //定时器0初始化
UART_Init();
DelayS(5);
UART_SendStr("AT+CIPMUX=1\r\n", 13); //打开多连接
DelayS(1);
UART_SendStr("AT+CIPSERVER=1,8080\r\n", 21); //建立服务 端口号为8080
DelayS(1);
while (1) //主循环
{
if (sendDataFlag == 1) //发送数据标志
{
if (messageCnt == 0) //区分at命令还是数据内容
{
messageCnt = 1;
UART_SendStr("AT+CIPSEND=0,8\r\n", 16); //发送32位数据
}
else
{
if ((HIGH_WATER_LEVEL == 1) && (LOW_WATER_LEVEL == 1)) //高低都有水
{
LED_LOW = 1;
LED_HIGH = 0;
LED_NORMAL = 1;
BUZZER = 0;
UART_SendStr("*High* ", 8); //发送内容
}
else if ((HIGH_WATER_LEVEL == 0) && (LOW_WATER_LEVEL == 0)) //高低都没有水
{
LED_LOW = 0;
LED_HIGH = 1;
LED_NORMAL = 1;
BUZZER = 0;
UART_SendStr("*Low* ", 8); //发送内容
}
else if ((HIGH_WATER_LEVEL == 0) && (LOW_WATER_LEVEL == 1)) //正常水位
{
LED_LOW = 1;
LED_HIGH = 1;
LED_NORMAL = 0;
BUZZER = 1;
UART_SendStr("*Normal*", 8); //发送内容
}
else
{
LED_LOW = 0;
LED_HIGH = 0;
LED_NORMAL = 0;
BUZZER = ~BUZZER;
UART_SendStr("*Error* ", 8); //发送内容
}
UART_SendStr("\r\n", 2);
messageCnt = 0; //发送数据
}
sendDataFlag = 0; //清除发送数据标志
}
}
}
void Timer0_Init()
{
TMOD &= 0xF0;
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TH0 = (65536 - 18432) / 256; //重新赋值 20ms
TL0 = (65536 - 18432) % 256;
EA = 1; //总中断打开
ET0 = 1; //定时器中断打开
TR0 = 1; //定时器开关打开
}
void UART_Init()
{
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 = 0; //关闭串口中断
EA = 1; //打开总中断
}
void UART_SendByte(unsigned char dat) //串口发送单字节数据
{
unsigned char time_out;
time_out = 0x00;
SBUF = dat; //将数据放入SBUF中
while ((!TI) && (time_out < 100)) //检测是否发送出去
{
time_out++;
DelayUs10x(2);
} //未发送出去 进行短暂延时
TI = 0; //清除ti标志
}
void UART_SendStr(unsigned char *s, unsigned char length) //发送定长度字符串
{
unsigned char num;
num = 0x00;
while (num < length) //发送长度对比
{
UART_SendByte(*s); //放松单字节数据
s++; //指针++
num++; //下一个++
}
}
void Timer0_Interrupt() interrupt 1
{
static unsigned char times20ms = 0;
TH0 = (65536 - 18432) / 256; //重新赋值 20ms
TL0 = (65536 - 18432) % 256;
times20ms++;
if (times20ms > 50) //定时到1s
{
sendDataFlag = 1; //发送数据变量
times20ms = 0;
}
}
// void UART_Interrupt(void) interrupt 4 //串行中断服务程序
// {
// if (RI) //判断是接收中断产生
// {
// RI = 0; //标志位清零
// }
// }
仿真演示视频:
https://www.bilibili.com/video/BV1Uv4y127q3/
实物演示视频:
https://www.bilibili.com/video/BV1Bd4y1t7un/