功能介绍:
0.本系统采用STC89C52作为单片机
1.LCD1602液晶实时显示温度和设定的温度上下限阈值
2.当温度超过上下限阈值时,对应控制继电器启动,采用PID算法控制。当温度达到阈值均值时,关闭继电器
3.当温度超过阈值1分钟以上,蜂鸣器报警
4.加热器件采用功率电阻,降温器件采用4010小风扇
原理图:
PCB:
主程序:
#include <reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include <stdio.h>
#include "18b20.h"
#include "lcd1602.h"
#include "delay.h"
#include "pid.h"
#define ON 0
#define OFF 1
/************************* 引脚定义 *************************/
sbit BUZZER = P2^3;
sbit KEY_ADD = P3^4; //按键引脚定义
sbit KEY_SUB = P3^6;
sbit KEY_SET = P3^3;
sbit KEY_ENTER = P3^5;
sbit RELAY_HEAT = P2^1; //控制升温继电器
sbit RELAY_COOL = P2^0; //控制降温继电器
/************************* 变量定义 *************************/
PID_Calibration_Def xdata PID_Calibration;
PID_State_Def PID_State;
int temp; //温度读取
float temperature; //实际温度
int xdata tempMin = 30; //温度下限
int xdata tempMax = 34; //最高上限
char dis0[16]; //定义显示区域临时存储数组
bit refreshFlag = 1; //刷新标志
bit heatFlag = 0; //加热启动标志
bit coolFlag = 0; //制冷启动标志
unsigned char setIndex = 0;
unsigned char time500ms = 0;
unsigned char PWMCnt = 0;
/************************* 函数声明 *************************/
void Timer0_Init(void); //函数声明
void KeyProcess(void); //按键处理
void Set_PID_Parameter(void); //PID参数初始化
void main(void)
{
/************************* 初始化 *************************/
Timer0_Init();
LCD_Init(); //初始化液晶
DelayMs(20); //延时有助于稳定
LCD_Clear(); //清屏
Set_PID_Parameter();
/************************* 主循环 *************************/
while (1)
{
if (refreshFlag == 1)
{
refreshFlag = 0;
TR0 = 0;
while(!DS18B20_Start());
DS18B20_GetTemp(&temp); //读取温度
temperature = (float)temp * 0.0625; //温度转换
if (temperature >= -55)
{
sprintf(dis0," Temp:%5.1f", temperature);
LCD_DispStr(0, 0, dis0);
LCD_DispOneChar(12, 0, 0xdf);
LCD_DispOneChar(13, 0, 'C');
}
PID_State.actual = temperature; //当前温度
TR0 = 1;
sprintf(dis0,"Min:%3d", tempMin);
LCD_DispStr(1, 1, dis0);
sprintf(dis0,"Max:%3d", tempMax);
LCD_DispStr(9, 1, dis0);
if (temperature < tempMin || temperature > tempMax) //高于温度上限,或低于温度下限
{
if (time500ms >= 120) //计时1分钟
{
BUZZER = 0; //打开蜂鸣器
}
else
{
time500ms++;
}
}
else
{
time500ms = 0;
BUZZER = 1; //关闭蜂鸣器
}
if (temperature < tempMin)
{
heatFlag = 1;
coolFlag = 0;
}
else if (temperature > tempMax)
{
coolFlag = 1;
heatFlag = 0;
}
if (heatFlag == 1 || coolFlag == 1)
{
// 增量式PID
PID_State.target = (tempMin + tempMax) / 2; //加热至阈值均值
PID_State = PID_Increament(PID_Calibration, PID_State);
if (PID_State.actual <= PID_State.target + 0.5 && PID_State.actual >= PID_State.target - 0.5)
{
coolFlag = 0;
heatFlag = 0;
}
}
else
{
PID_State.last_error = 0;
PID_State.previous_error = 0;
PID_State.output = 0;
}
}
if (heatFlag == 1)
{
if (PWMCnt <= PID_State.output) //占空比调节
{
RELAY_HEAT = ON;
}
else if ((PWMCnt > PID_State.output)) //关闭时间段
{
RELAY_HEAT = OFF;
}
}
else
{
RELAY_HEAT = OFF;
}
if (coolFlag == 1)
{
if (PWMCnt <= PID_State.output) //占空比调节
{
RELAY_COOL = ON;
}
else if ((PWMCnt > PID_State.output)) //关闭时间段
{
RELAY_COOL = OFF;
}
}
else
{
RELAY_COOL = OFF;
}
KeyProcess();
}
}
void Set_PID_Parameter(void)
{
PID_Calibration.kp = 5;
PID_Calibration.ki = 1;
PID_Calibration.kd = 0.1;
PID_State.actual = 0;
PID_State.target = 35;
PID_State.integral = 0;
PID_State.last_error = 0;
PID_State.previous_error = 0;
PID_State.output = 0;
}
void KeyProcess(void)
{
if (!KEY_SET) //设置按键按下
{
DelayMs(5);
if (!KEY_SET)
{
setIndex++;
if (setIndex > 2)
{
setIndex = 0;
}
if (setIndex == 0)
{
LCD_DispOneChar(0, 1, ' ');
LCD_DispOneChar(8, 1, ' ');
}
else if (setIndex == 1)
{
LCD_DispOneChar(0, 1, '>');
LCD_DispOneChar(8, 1, ' ');
}
else if (setIndex == 2)
{
LCD_DispOneChar(0, 1, ' ');
LCD_DispOneChar(8, 1, '>');
}
}
while (!KEY_SET);
}
if (!KEY_ENTER) //确认键按下
{
DelayMs(5);
if (!KEY_ENTER)
{
setIndex = 0;
LCD_DispOneChar(0, 1, ' ');
LCD_DispOneChar(8, 1, ' ');
}
while (!KEY_ENTER);
}
if (!KEY_ADD) //加键按下
{
DelayMs(180);
if (!KEY_ADD)
{
if (setIndex == 1)
{
tempMin++;
if (tempMin >= tempMax)
{
tempMin = 0;
}
}
else if (setIndex == 2)
{
tempMax++;
if (tempMax >= 99)
{
tempMax = tempMin + 1;
}
}
}
//while (!KEY_ADD);
}
if (!KEY_SUB) //减键按下
{
DelayMs(180);
if (!KEY_SUB)
{
if (setIndex == 1)
{
tempMin--;
if (tempMin < 0)
{
tempMin = tempMax - 1;
}
}
else if (setIndex == 2)
{
tempMax--;
if (tempMax <= tempMin)
{
tempMax = 125;
}
}
}
//while (!KEY_SUB);
}
}
void Timer0_Init(void)
{
TMOD &= 0xF0; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TL0 = 0x00; //设置定时初值
TH0 = 0x4C; //设置定时初值 50ms
PT0 = 1; //设置高优先级
EA = 1; //总中断打开
ET0 = 1; //定时器中断打开
TR0 = 1; //定时器开关打开
}
void Timer0_isr(void) interrupt 1
{
static unsigned int numCount = 0;
TL0 = 0x00; //设置定时初值
TH0 = 0x4C; //设置定时初值 50ms
numCount++;
if (numCount > 10)
{
numCount = 0;
refreshFlag = 1;
}
if (heatFlag == 1)
{
if (PWMCnt < 100)
{
PWMCnt++;
}
else
{
PWMCnt = 1; //一个周期结束
}
}
if (coolFlag == 1)
{
if (PWMCnt < 100)
{
PWMCnt++;
}
else
{
PWMCnt = 1; //一个周期结束
}
}
}