📄 matlab温度控制.txt
字号:
(2)单片机C51源程序
#include <reg52.h> //库函数
#include <absacc.h>
#include <stdio.h>
#define uint unsigned int
#define uchar unsigned char
#define LEN 5 //接收数据长度
sbit flag=P1^6;
sbit receive=P1^5;
sbit send=P1^4;
sbit dis0=P2^7; //键盘列
sbit dis1=P2^6;
sbit dis2=P2^5;
sbit dis3=P2^4;
sbit key1=P2^3; //键盘控制口1
sbit key0=P2^2; //键盘控制口0
sbit PWM=P3^7; //PWM控制端口
sbit ADAT=P3^5; //数据控制端口
sbit ACLK=P3^4; //时钟控制端口
it ADCS=P3^3; //片选控制端口
uchar count; //接收数据计数器
uchar disp[4],dis; //显示缓冲区及指针
uchar key,keyx; //键盘
uchar out; //输出控制量
int para[8],ptr; //参数数组及指针
int r,kp,ki,kd,ek,sumpid,e2,e1,e0,u; //PID控制参数
uchar code numtab[4]={0x80,0x40,0x20,0x10};
uchar code distab[16]={0x28,0xee,0x32,0xa2,0xe4,0xa1,0x21,0xea,
0x20,0xa0,0x60,0x25,0x39,0x26,0x31,0x71};
uchar code ledtab[8]={0x08,0x9f,0x8f,0x10,0xd9,0x07,0x04,0xd0}; //参数提示符
int code param[9]={2,100,999,999,999,500,500,255}; //参数最大值
void time(uint t) //延时程序
{
uint i;
for(i=0;i<t;i++);
}
void serial() interrupt 4 using 1 //串行中断
{
if(RI) //RI=1,请求中断,CPU响应中断,从SBUF取出数据
{
RI=0;
control=SBUF;
if(control==211) {sendok=1;}
else if(control==212) {sendok=0;}
else
{
para[count]=control;
receive=~receive;
count++;
if(count>LEN){count=1;}
}
}
else
{
TI=0;
send=~send;
}
}
uint ADC0832() //ADC0832 的驱动程序
{
uchar x=0,i;
ADAT=1;ACLK=0;ADCS=0; //数据口赋初值
for(i=0;i<4;i++) //4位数码显示
{
ACLK=0;
ADAT=1;
ACLK=1;
};
ACLK=0;
for(i=0;i<8;i++) //8位AD转换
{
ACLK=1;
x=x<<1;
if(ADAT) x++;
ACLK=0;
}
ACLK=1;ADCS=1;
return x; //数据返回
}
void distran() //显示的数据位处理
{
disp[0]=distab[para[ptr]%10];
disp[1]=distab[(para[ptr]/10)%10];
disp[2]=distab[(para[ptr]/100)%10];
disp[3]=~ledtab[ptr];
}
void display() //键盘及数码管扫描
{
dis=(dis+1)%4;
P2=0xff;
P0=disp[dis];
P2=~numtab[dis];
key0=1; key1=1;
if(!key0) keyx=dis+1;
if(!key1) keyx=dis+5;
}
void keyproc() //键盘处理程序
{
if(key>0)
{
switch(key)
{
case 1: para[ptr]--; break; //减1
case 5: para[ptr]++; break; //加1
case 2: para[ptr]-=10; break; //减10
case 6: para[ptr]+=10; break; //加10
case 3: para[ptr]-=100; break; //减100
case 7: para[ptr]+=100; break; //加100
case 4: ptr--; break; //上一个参数
case 8: ptr++; break; //下一个参数
}
if(ptr>7) ptr=0;
if(ptr<0) ptr=7;
if(para[ptr]<0) para[ptr]=0;
if(para[ptr]>param[ptr]) para[ptr]=param[ptr];
}
key=0;
}
void PID0() //常规PID控制
{
r=para[1];
e2=e1;e1=e0;e0=r-para[0];
kp=para[2];ki=para[3]; kd=para[4];
u+=ki*e0+kp*(e0-e1)+kd*(e0-2*e1+e2);
if(u<0) u=0;
if(u>255) u=255;
para[7]=u;
out=(uchar)para[7];
}
void PID1() //微分先行PID
{
r=para[1]; ek=para[1]-para[0];
sumpid+=ek;
kp=para[2];ki=para[3]; kd=para[4];
u=kp*ek+ki*sumpid+kd*(para[0]-e1)-kp*ki*kd*para[0];
e1=para[0];
para[7]=u;
out=(uchar)para[7];
}
void PID2() //带死区PID
{
r=para[1]; ek=para[1]-para[0];
sumpid+=ek;
kp=para[2];ki=para[3]; kd=para[4];
if(ek<=5) ek=0;
u=kp*ek+ki*sumpid+kd*(para[0]-e1)
e1=e0;
para[7]=u;
out=(uchar)para[7];
}
void PID()
{
switch (para[0])
{
case 0: PID0() ; break;
case 1: PID1() ; break;
case 2: PID2() ; break;
}
}
void timer0() interrupt 1 //PWM脉冲输出
{
uchar tm; //计数器
TH0=0XE3; TL0=0X33; // 8 ms
if (tm++>out) PWM=0; //停止加热
else PWM=1; //加热
}
void timer1() interrupt 3 //处理程序
{
uint adc,wendu;
uint ta;
TH1=0X4C; TL1=0X00; // 50 mS
ta++;
adc=ADC0832(); //采样温度
sum+=adc;
if(ta>39) //控制周期为2S
{
ta=0;
adc=sum/40
para[6]=adc; //送给显示缓冲区
wendu=(adc-51)*300/204; //线性转换
if(sendok=1) SBUF=wendu; //向PC机发送温度数据
para[0]=wendu; //反馈值
PID(); //进行PID控制
}
distran();
display();
}
void main(void) //主程序
{
int i;
SCON=0X50;
TMOD=0x11;
T2CON=0X34; // RCLK=1; TCLK=1; TR2=1;
IE=0X9a;
TH0=0XE3; TL0=0X33; TR0=1; // 8 ms
TH1=0X4C; TL1=0X00; TR1=1; // 50 ms
RCAP2H=0XFF; RCAP2L=0XDB; TR2=1; // 9600 b/s
dis=0;ptr=0;P1=0xff;
count=1;
out=0;
sum=0;
receive=0;
send=0;
sendok=0;
flag=1;
for(i=0;i<7;i++) para[i]=0;
para[1]=50;
para[2]=6; para[3]=1; para[4]=10;
para[5]=0;
for(;;)
{
if(keyx>0) //有按键按下
{
key=keyx;
while(keyx!=0){ keyx=0; time(2500); }
keyproc();
time(4000);
}
display();
distran();
}
}
// 参数说明
// para[0] 反馈值
// para[1] 设定温度
// para[2] 比例系数
// para[3] 积分系数
// para[4] 微分系数
// para[5] 算法选择 0—2
// para[6] 采样结果
// para[7] 控制量输出 0-255
2.MATLAB6.5程序
function example(op)
%*************************** 温度控制系统 **************************** ***
%*************基于 MATLAB 的 PC 机与 MCS-52 串行通信 *************
global s %****** 定义全局变量 ******
global h
global shedingerror
global bilierror
global jifenerror
global weifenerror
global stopjieshou
global wendudata %******* 温度数据 *********
global fasongqueding %***** 确定要发送数据 *****
if nargin == 0 %******* 如果 op=0 则绘制 GUI ********
op=0;
end
switch op %****** 条件选择语句 ******
%***************************** 绘制 GUI **************************************
case 0
h.fig = figure('Position',[100 100 850 550],... %******* 创建图形窗口 ******
'Color',[0 0.82 0 ],...
'NumberTitle','off',...
'CreateFcn','example(5)',...
'DeleteFcn','example(9)',...
'Name','温度控制系统');
%************************* 标题 + 当前温度 + 状态 *************************
uicontrol('Style','text',... %**** 标题:温度控制系统 ****
'Units','normalized',...
'Position',[0.343 0.881 0.314 0.091],...
'BackgroundColor',[0 0.82 0],...
'String','温度控制系统',...
'HorizontalAlignment','center',...
'FontWeight','bold',...
'FontSize',25.0);
uicontrol('Style','frame',... %****“ 当前温度+状态 ”****
'Units','normalized',...
'BackgroundColor',[0.8 0.8 0.8],...
'Position',[0.071 0.764 0.857 0.106]);
uicontrol('Style','text',... %***** 提示:当前温度 *****
'Units','normalized',...
'Position',[0.099 0.795 0.121 0.047],...
'BackgroundColor',[0.8 0.8 0.8],...
'String','当前温度',...
'HorizontalAlignment','center',...
'FontSize',13.5);
h.dangqian = uicontrol('Style','edit',... %***** 显示:当前温度 *****
'Units','normalized',...
'Position',[0.25 0.785 0.104 0.06],...
'ForegroundColor',[1.0 0 0],...
'BackgroundColor',[1.0 1.0 1.0],...
'HorizontalAlignment','center',...
'String','10',...
'FontSize',13.5,...
'Enable','inactive',...
'CallBack','');
h.zhuangtai=uicontrol('Style','text',... %******* 提示:状态 ******
'Units','normalized',...
'Position',[0.501 0.788 0.41 0.05],...
'ForegroundColor',[1.0 0 0],...
'BackgroundColor',[0.8 0.8 0.8],...
'String','温度控制系统已经打开',...
'HorizontalAlignment','center',...
'FontSize',13.5);
%****************************** 参数设定 ******************************
uicontrol('Style','frame',... %****** 参数设定界面 *******
'Units','normalized',...
'ForegroundColor',[0.251 0 0],...
'BackgroundColor',[0.8 0.8 0.8],...
'Position',[0.071 0.368 0.311 0.36]);
uicontrol('Style','text',... %***** 提示:设定温度 ******
'Units','normalized',...
'Position',[0.099 0.650 0.121 0.047],...
'BackgroundColor',[0.8 0.8 0.8],...
'String','设定温度',...
'HorizontalAlignment','center',...
'FontSize',13.5);
h.sheding = uicontrol('Style','edit',... %****** 显示:设定温度 ******
'Units','normalized',...
'Position',[0.25 0.643 0.104 0.06],...
'ForegroundColor',[0 0 0],...
'BackgroundColor',[1.0 1.0 1.0],...
'HorizontalAlignment','center',...
'String','10',...
'FontSize',13.5,...
'CallBack','example(5)');
uicontrol('Style','text',... %****** 提示:比例系数 ******
'Units','normalized',...
'Position',[0.099 0.574 0.121 0.047],...
'BackgroundColor',[0.8 0.8 0.8],...
'String','比例系数',...
'HorizontalAlignment','center',...
'FontSize',13.5);
h.bili = uicontrol('Style','edit',... %***** 显示:比例系数 ******
'Units','normalized',...
'Position',[0.25 0.564 0.104 0.06],...
'ForegroundColor',[0 0 0],...
'BackgroundColor',[1.0 1.0 1.0],...
'HorizontalAlignment','center',...
'String','20',..
'FontSize',13.5,...
'CallBack','');
uicontrol('Style','text',... %****** 提示:积分系数 ******
'Units','normalized',...
'Position',[0.099 0.485 0.121 0.047],...
'BackgroundColor',[0.8 0.8 0.8],...
'String','积分系数',...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -