功能介绍:
0.本系统采用STC89C52作为单片机
1.LCD1602液晶实时显示温度/PID参数/设定的温度上下限/PWM输出/通信地址
2.当温度超过上下限阈值时,蜂鸣器报警
3.采用增量式PID算法控制当前温度。
4.定时向串口发送当前温度
5.可通过串口发送命令更改配置参数
6.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB :
主程序:
#include <reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include <stdio.h>
#include "delay.h"
#include "pid.h"
#include "tlc0832.h"
#include "oled_iic.h"
#include "math.h"
/************************* 宏定义 *************************/
#define ON 0
#define OFF 1
#define T25 (273.15+25) //T25
#define Bx (4050.0) //B值
#define Ka (273.15) //绝对零度
#define Rp (10000.0) //ntc串联电阻
#define ntcR25 (10000.0) //25度时电阻
#define FRAME_HEADER 0x1a
#define FRAME_FUCTION_TEMP 0x00 //功能位,温度
#define FRAME_FUCTION_TARGET 0x01 //功能位,目标温度
#define FRAME_FUCTION_KP 0x02 //功能位,Kp
#define FRAME_FUCTION_KI 0x03 //功能位,Ki
#define FRAME_FUCTION_KD 0x04 //功能位,Kd
#define FRAME_FUCTION_TEMP_MIN 0x05 //功能位,温度下限
#define FRAME_FUCTION_TEMP_MAX 0x06 //功能位,温度上限
#define FRAME_FUCTION_ADDR 0x07 //功能位,通信地址
#define FRAME_END 0x3f
/************************* 引脚定义 *************************/
sbit BUZZER = P3^7;
sbit L9110_A = P1^3;
sbit L9110_B = P1^4;
/************************* 变量定义 *************************/
PID_Calibration_Def xdata PID_Calibration; //PID结构体
PID_State_Def xdata PID_State; //PID结构体
float temperature; //实际温度
float f_tempVolt = 0; //温度对应电压
float current = 0;
float Rt = 0;
int xdata tempMin = 20; //温度下限
int xdata tempMax = 50; //温度上限
char xdata dis0[16]; //定义显示区域临时存储数组
bit refreshFlag = 1; //刷新标志
unsigned char setIndex = 0;
unsigned char PWMCnt = 0;
unsigned char address = 0; //通信地址
/************************* 函数声明 *************************/
void Timer0_Init(void); //函数声明
void KeyProcess(void); //按键处理
void Set_PID_Parameter(void); //PID参数初始化
void UART_Init(void); //串口初始化
void UART_SendByte(unsigned char dat); //串口发送单字节数据
// void UART_SendStr(unsigned char *s, unsigned char length); //发送定长度字符串
void SendData(unsigned char address, float temperature); //串口发送数据
void main(void)
{
/************************* 初始化 *************************/
Timer0_Init();
OLED_Init();
OLED_Clear();
UART_Init();
Set_PID_Parameter();
/************************* 主循环 *************************/
while (1)
{
ET0 = 0;
f_tempVolt = 5 * (float)ReadADC(AIN0_GND) / 255; //读取电压
ET0 = 1;
current = (5 - f_tempVolt) / Rp; //计算电流值
Rt = f_tempVolt / current; //计算电阻值
temperature = ((Bx * T25) / (T25 * (log(Rt) - log(ntcR25)) + Bx)) - Ka;
PID_State.actual = temperature; //当前温度
// PID
PID_State = PID_Increament(PID_Calibration, PID_State);
if (PWMCnt <= PID_State.output) //占空比调节
{
L9110_A = 1;
L9110_B = 0;
}
else
{
L9110_A = 0;
L9110_B = 1;
}
if (refreshFlag == 1) //定时刷新屏幕
{
refreshFlag = 0;
sprintf(dis0,"T:%5.1f", temperature);
OLED_ShowString(8, 0, dis0, FONT_1608); //显示温度
OLED_ShowWord(8*8, 0, 0); //显示摄氏度
SendData(address, temperature*10);
sprintf(dis0," S:%3d", (int)PID_State.target); //显示目标温度
OLED_ShowString(10*8, 0, dis0, FONT_1608);
sprintf(dis0," MIN:%3d MAX:%3d", tempMin, tempMax); //显示温度下限,温度上限
OLED_ShowString(0, 2, dis0, FONT_1608);
sprintf(dis0," P:%3d I:%3d %3d", (int)PID_Calibration.kp, (int)PID_Calibration.ki, (int)PID_State.output); //显示Kd,Ki,PWM输出
OLED_ShowString(0, 4, dis0, FONT_1608);
sprintf(dis0," D:%3d ADDR:%2d", (int)PID_Calibration.kd, (int)address); //显示Kd,通信地址
OLED_ShowString(0, 6, dis0, FONT_1608);
//显示设定位置
if (setIndex == 0)
{
OLED_ShowChar(10*8, 0, ' ', FONT_1608);
OLED_ShowChar(0, 2, ' ', FONT_1608);
OLED_ShowChar(8*8, 2, ' ', FONT_1608);
OLED_ShowChar(0, 4, ' ', FONT_1608);
OLED_ShowChar(6*8, 4, ' ', FONT_1608);
OLED_ShowChar(0, 6, ' ', FONT_1608);
OLED_ShowChar(6*8, 6, ' ', FONT_1608);
}
else if (setIndex == 1)
{
OLED_ShowChar(0, 0, ' ', FONT_1608);
OLED_ShowChar(10*8, 0, '>', FONT_1608);
}
else if (setIndex == 2)
{
OLED_ShowChar(10*8, 0, ' ', FONT_1608);
OLED_ShowChar(0, 2, '>', FONT_1608);
}
else if (setIndex == 3)
{
OLED_ShowChar(0, 2, ' ', FONT_1608);
OLED_ShowChar(8*8, 2, '>', FONT_1608);
}
else if (setIndex == 4)
{
OLED_ShowChar(8*8, 2, ' ', FONT_1608);
OLED_ShowChar(0, 4, '>', FONT_1608);
}
else if (setIndex == 5)
{
OLED_ShowChar(0, 4, ' ', FONT_1608);
OLED_ShowChar(6*8, 4, '>', FONT_1608);
}
else if (setIndex == 6)
{
OLED_ShowChar(6*8, 4, ' ', FONT_1608);
OLED_ShowChar(0, 6, '>', FONT_1608);
}
else if (setIndex == 7)
{
OLED_ShowChar(0, 6, ' ', FONT_1608);
OLED_ShowChar(6*8, 6, '>', FONT_1608);
}
if (temperature < tempMin || temperature > tempMax) //高于温度上限,或低于温度下限
{
BUZZER = 0; //打开蜂鸣器
}
else
{
BUZZER = 1; //关闭蜂鸣器
}
}
KeyProcess();
DelayMs(100);
}
}
void Set_PID_Parameter(void)
{
PID_Calibration.kp = 100;
PID_Calibration.ki = 20;
PID_Calibration.kd = 10;
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 Timer0_Init(void)
{
TMOD &= 0xF0; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TL0 = 0x00; //设置定时初值
TH0 = 0xEE; //设置定时初值 5ms
PT0 = 1; //设置高优先级
EA = 1; //总中断打开
ET0 = 1; //定时器中断打开
TR0 = 1; //定时器开关打开
}
void Timer0_isr(void) interrupt 1
{
static unsigned int numCount = 0;
TL0 = 0x66; //设置定时初始值
TH0 = 0xFC; //设置定时初始值 1ms
numCount++;
if (numCount > 200)
{
numCount = 0;
refreshFlag = 1;
}
if (PWMCnt < 100)
{
PWMCnt++;
}
else
{
PWMCnt = 1; //一个周期结束
}
}
void SendData(unsigned char address, float temperature)
{
UART_SendByte(FRAME_HEADER); //发送帧头
UART_SendByte(address); //发送地址
UART_SendByte(FRAME_FUCTION_TEMP); //发送功能位,温度
UART_SendByte(0x02); //发送数据长度位
UART_SendByte((unsigned char)((((int)temperature)>>8) & 0x00ff)); //发送数据内容高字节
UART_SendByte((unsigned char)(((int)temperature) & 0x00ff)); //发送数据内容低字节
UART_SendByte(FRAME_END); //发送帧尾
}
/************************* 串口配置 *************************/
void UART_Init(void)
{
SCON = 0x50;
TH2 = 0xFF;
TL2 = 0xFD;
RCAP2H = 0xFF; //(65536-(FOSC/32/BAUD)) BAUD = 9600 FOSC = 11059200
RCAP2L = 0xDC;
// 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; //打开总中断
}
/************************* 串口发送字节 *************************/
void UART_SendByte(unsigned char dat) //串口发送单字节数据
{
unsigned char time_out;
time_out = 0;
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 UART_Interrupt(void) interrupt 4 //串行中断服务程序
{
static unsigned char i = 0;
static unsigned char firstBit = 0;
static unsigned char R_buf[6];
if (RI)//判断是接收中断产生
{
RI = 0; //标志位清零
TR0 = 0;
//SBUF = SBUF;
if (SBUF == FRAME_HEADER) //检查到帧头
{
firstBit = 1; //接收标志成功
i = 0;
R_buf[1] = 0;
R_buf[2] = 0;
R_buf[3] = 0;
R_buf[4] = 0;
R_buf[5] = 0;
}
if (firstBit == 1)
{
R_buf[i] = SBUF;
i++;
if (i == 6)
{
i = 0;
if (R_buf[0] == FRAME_HEADER && R_buf[5] == FRAME_END) //检测到帧头和帧尾
{
if (R_buf[1] == address) //地址对应
{
if (R_buf[2] == FRAME_FUCTION_TARGET && R_buf[3] == 0x01) //功能位和数据长度
{
if (R_buf[4] <= 100 && R_buf[4] >= 10)
{
PID_State.target = R_buf[4];
}
}
else if (R_buf[2] == FRAME_FUCTION_KP && R_buf[3] == 0x01)
{
if (R_buf[4] <= 100)
{
PID_Calibration.kp = R_buf[4];
}
}
else if (R_buf[2] == FRAME_FUCTION_KI && R_buf[3] == 0x01)
{
if (R_buf[4] <= 100)
{
PID_Calibration.ki = R_buf[4];
}
}
else if (R_buf[2] == FRAME_FUCTION_KD && R_buf[3] == 0x01)
{
if (R_buf[4] <= 100)
{
PID_Calibration.kd = R_buf[4];
}
}
else if (R_buf[2] == FRAME_FUCTION_TEMP_MIN && R_buf[3] == 0x01)
{
if (R_buf[4] < tempMax && R_buf[4] >= 0)
{
tempMin = R_buf[4];
}
}
else if (R_buf[2] == FRAME_FUCTION_TEMP_MAX && R_buf[3] == 0x01)
{
if (R_buf[4] <= 100 && R_buf[4] > tempMin)
{
tempMax = R_buf[4];
}
}
else if (R_buf[2] == FRAME_FUCTION_ADDR && R_buf[3] == 0x01)
{
if (R_buf[4] <= 16)
{
address = R_buf[4];
}
}
}
}
firstBit = 0;
}
}
TR0 = 1;
}
if (TI)//判断是发送中断产生
{
TI = 0; //标志位清零
}
}