📄 pid_con.c
字号:
/*====================================================================================================
其中包含了读温度的子程序
=====================================================================================================*/
#include<at89x52.h>
#include <string.h>
#include <stdio.h>
#define uint unsigned int
#define uchar unsigned char
#define PID_CAL_TIME 3 //执行pid运算的时间常数
extern uchar xdata pid_control_time;//执行pid运算的时间变量
extern uint ad_convert(uchar four_in_one);
extern uint xdata set_temper1,set_temper2; /*设定温度*/
extern uint xdata kp1,kp2, /*比例常数*/
ki1,ki2, /*积分常数*/
kd1,kd2, /*微分常数*/
kz1,kz2; /* 自定义常数*/
uint PIDCalc( struct PID *pp, uint NextPoint );
void PIDInit (struct PID *pp);
uint read_temperature (uchar sel_tunnel) ;
void PID_control(void);
uchar xdata temper1_ready,temper2_ready;
int xdata heat1_time,heat2_time; /*每个时间片内的加热时间*//*heat*_time的初始值设为100*/
/*====================================================================================================
PID Function
The PID (比例、积分、微分) function is used in mainly
control applications. PIDCalc performs one iteration of the PID
algorithm.
While the PID function works, main is just a dummy program showing
a typical usage.
=====================================================================================================*/
xdata struct PID {
uint SetPoint; // 设定目标 Desired Value
uint Proportion; // 比例常数 Proportional Const
uint Integral; // 积分常数 Integral Const
uint Derivative; // 微分常数 Derivative Const
uint LastError; // Error[-1]
uint PrevError; // Error[-2]
int SumError; // Sums of Errors
}PID;
/*====================================================================================================
PID计算部分
=====================================================================================================*/
uint PIDCalc( struct PID *pp, uint NextPoint )
{
int dError,
Error;
Error = pp->SetPoint - NextPoint; // 偏差
pp->SumError += Error; // 积分
dError = pp->LastError - pp->PrevError; // 当前微分
pp->PrevError = pp->LastError;
pp->LastError = Error;
return (pp->Proportion * Error // 比例项
+ pp->Integral * pp->SumError // 积分项
+ pp->Derivative * dError // 微分项
);
}
/*====================================================================================================
Initialize PID Structure
=====================================================================================================*/
void PIDInit (struct PID *pp)
{
memset ( pp,0,sizeof(PID));
}
/*====================================================================================================
进行温度值的采样,输出值为温度值的10倍
=====================================================================================================*/
uint read_temperature (uchar sel_tunnel)
{
uchar i;
uint ad_data,ad_sum,ad_max,ad_min;
uint temperature;
ad_sum=0;
ad_max=0;
ad_min=4096;
for(i=0;i<7;i++)
{
ad_data=ad_convert(sel_tunnel-1);
ad_sum+=ad_data;
if(ad_max<ad_data)
ad_max=ad_data;
if(ad_min>ad_data)
ad_min=ad_data;
}
ad_sum-=ad_max;
ad_sum-=ad_min;
ad_data=ad_sum/5;
temperature=((float)ad_data-2458)*1000/3276; /*temperature的单位为0.1摄氏度*/
return(temperature);
}
void PID_control(void)
{
xdata struct PID PID_T1,PID_T2; // PID Control Structure
uint xdata temper1,temper2; // PID Feedback (Input),当前的温度值
if(pid_control_time>=PID_CAL_TIME)
{
pid_control_time=0;
PIDInit ( &PID_T1 ); // Initialize Structure
PIDInit ( &PID_T2 ); // Initialize Structure
PID_T1.Proportion = kp1; // Set PID Coefficients
PID_T1.Integral = ki1;
PID_T1.Derivative = kd1;
PID_T1.SetPoint = set_temper1; // Set PID Setpoint
PID_T2.Proportion = kp2; // Set PID Coefficients
PID_T2.Integral = ki2;
PID_T2.Derivative = kd2;
PID_T2.SetPoint = set_temper2; // Set PID Setpoint
temper1 = read_temperature(1); // Read Input
temper2 = read_temperature(2);
//temper1=380;
//temper2=380;
heat1_time = PIDCalc ( &PID_T1,temper1 ); // Perform PID Interation
if(heat1_time<=0)
heat1_time=0;
heat2_time = PIDCalc ( &PID_T2,temper2 );
if(heat2_time<=0)
heat2_time=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -