📄 pid.c
字号:
#include <msp430x44x.h>
#include "pid.h"
#include "cry1602.h" //包含液晶操作函数
//============================PID运算子程序=====================================
//函数说明:主要用于PI运算,通过给定和反馈计算出输出
//入口:ifin,give(反馈,给定)
//出口: out
//全局变量:lasterror,lastout;
//调用模块: 无
int lerror1,lastout;
int UPPER_LIMIT=4090; //输出上限,12位,范围为0~4096,这里设置为4090
//out_max,yout[5],lyrk,yrk,;
unsigned char xi,xp,xd,xp_local[3],xi_local[3],xd_local[3];
unsigned int fun1,fun2,fun3;
unsigned char PID_buffer[16];
char p;
unsigned int ypid(unsigned int give,unsigned int feedback)
{
int error1,error2,out;
error1=give-feedback; //计算积分偏差
error2=error1-lerror1; //计算比例偏差,计算两次偏差之差
lerror1=error1; //更新e(k-1)
out=lastout+error1/xi+error2*xp; //计算PI控制值
if(error1==0)out=out; //计算不灵敏区的运算补偿,如果偏差为0,输出不发生改变
else if(error1<8&&error1>-8)
{
if(error1>0)
out=out+1; //如果偏差大于0,输出加1
else
out=out-1; //如果偏差小于0,输出减1
}
else if(error1<16&&error1>-16) //如果偏差很大,则输出跟随着快速调节,步长改为2,以迅速的回到正常值
{
if(error1>0)
out=out+2; //如果偏差大于0,输出加2
else
out=out-2; //如果偏差小于0,输出减2
}
if(out>UPPER_LIMIT)
out=UPPER_LIMIT; //对输出值进行上限控制
if(out<0)out=0; //对输出值进行下限控制,如果输出小于0,则输出0.
lastout=out; //更新out(k-1)
if(error1<xd&&error1>-xd)out=out+error1;
return(out); //返回最终运算结果
}
//-----------------------------------------
/*----------------------------------------------------
函数说明:clr_lastout()
功 能:将PID计算模块的上次输出改为指定的值
全局变量:
调用模块: 无
---------------------------------------------*/
void clr_lastout(unsigned int value1)
{//PID运算从0开始
lastout=value1;
}
/*----------------------------------------------------
函数说明:initialize_pid_value()
功 能:初始化PID的比例系数,积分系数,微分参数
全局变量:
调用模块:
---------------------------------------------*/
void initialize_pid_value(void)
{
xp=1;
xi=8;
xd=4;
PID_buffer[0]='x';
PID_buffer[1]='p';
PID_buffer[2]='=';
PID_buffer[3]='0'+xp;
PID_buffer[4]=' ';
PID_buffer[5]='x';
PID_buffer[6]='i';
PID_buffer[7]='=';
PID_buffer[8]='0'+xi;
PID_buffer[9]=' ';
PID_buffer[10]='x';
PID_buffer[11]='d';
PID_buffer[12]='=';
PID_buffer[13]='0'+xd;
PID_buffer[14]=' ';
PID_buffer[15]=' ';
DispNChar(0,1,16,PID_buffer);//显示PID参数
Delay400ms(); //延迟一小段时间
}
unsigned int pid_xp_value_add(unsigned int step,unsigned int step_div) //xp+步长/分度
{
unsigned int xp10;
xp=xp+step/step_div;
xp10=xp*step_div;
return xp10;
}
unsigned int pid_xi_value_add(unsigned int step,unsigned int step_div) //xi+步长/分度
{
unsigned int xi10;
xi=xi+step/step_div;
xi10=xi*step_div;
return xi10;
}
unsigned int pid_xd_value_add(unsigned int step,unsigned int step_div) //xd+步长/分度
{
unsigned int xd10;
xd=xd+step/step_div;
xd10=xd*step_div;
return xd10;
}
unsigned int pid_xp_value_dec(unsigned int step,unsigned int step_div) //xp-步长/分度
{
unsigned int xp10;
xp=xp-step/step_div;
xp10=xp*step_div;
return xp10;
}
unsigned int pid_xi_value_dec(unsigned int step,unsigned int step_div) //xi-步长/分度
{
unsigned int xi10;
xi=xi-step/step_div;
xi10=xi*step_div;
return xi10;
}
unsigned int pid_xd_value_dec(unsigned int step,unsigned int step_div) //xd-步长/分度
{
unsigned int xd10;
xd=xd-step/step_div;
xd10=xd*step_div;
return xd10;
}
void xp_setup(void) //fun1
{
fun1=1; //按键功能标志
if(fun2==0&fun3==0) //如果其它功能键没有按下,则显示xp的参数
{
xp_local[0]='0'+xp%10;
xp_local[1]='.';
xp_local[2]='0'+xp/10;
//fun1=1; //进入设置界面
LcdClear();
DispNChar(0,0,15," PID xp setup "); //在第1行显示xd setup
DispNChar(0,1,6," xp="); //在第二行显示输出电压数值
//DispNChar(0,1,16,"ce shi dec"); //在第二行显示输出电压数值
DispNChar(10,1,3,xp_local);
//fun1=0; //如果其它键没有按下,则清除标志
Delay400ms();
Delay400ms();
}
else if(fun2==1) //如果按了xi键,再按xp键,则xp键是加法功能
{
pid_xi_value_add(1,1);
xi_local[0]='0'+xi%10;
xi_local[1]='.';
xi_local[2]='0'+xi/10;
DispNChar(0,0,15," PID xi setup "); //在第1行显示xi setup
DispNChar(0,1,6," xi="); //在第二行显示输出电压数值
DispNChar(10,1,3,xi_local); //更显xp显示
fun1=0;
fun2=0; //清除2个组合按键的标志
Delay400ms();
Delay400ms();
}
else if(fun3==1) //如果按了xi设置后,再按下xd键,则xp键是加法功能
{
pid_xd_value_add(1,1);
xd_local[0]='0'+xd%10;
xd_local[1]='.';
xd_local[2]='0'+xd/10;
DispNChar(0,0,15," PID xd setup "); //在第1行显示xi setup
DispNChar(0,1,6," xd="); //在第二行显示输出电压数值
DispNChar(10,1,3,xd_local);
fun1=0;
fun3=0; //清除按键标志
Delay400ms();
Delay400ms();
}
}
void xi_setup(void) //fun2
{
fun2=1; //按键功能标志
if(fun1==0&fun3==0)
{
xi_local[0]='0'+xi%10;
xi_local[1]='.';
xi_local[2]='0'+xi/10;
fun2=1;
LcdClear();
DispNChar(0,0,15," PID xi setup "); //在第1行显示xi setup
DispNChar(0,1,6," xi="); //在第二行显示输出电压数值
//DispNChar(0,1,16,"ce shi inc"); //在第二行显示输出电压数值
DispNChar(10,1,3,xi_local);
Delay400ms();
Delay400ms();
//fun2=0;
}
else if(fun1==1) //如果按了xp设置后,再按下xi键,则xi键是xp的加法功能
{
pid_xp_value_add(1,1);
xp_local[0]='0'+xp%10;
xp_local[1]='.';
xp_local[2]='0'+xp/10;
DispNChar(0,0,15," PID xp setup "); //在第1行显示xi setup
DispNChar(0,1,6," xp="); //在第二行显示输出电压数值
DispNChar(10,1,3,xp_local); //更显xp显示
fun1=0;
fun2=0;
Delay400ms();//清除按键标志
Delay400ms();
}
else if(fun3==1) //如果按了xd后,再按下xi键,则xi键是减法功能
{
pid_xd_value_dec(1,1);
xd_local[0]='0'+xd%10;
xd_local[1]='.';
xd_local[2]='0'+xd/10;
DispNChar(0,0,15," PID xd setup "); //在第1行显示xi setup
DispNChar(0,1,6," xd="); //在第二行显示输出电压数值
DispNChar(10,1,3,xd_local);
fun3=0;
fun2=0; //清除按键标志
Delay400ms();
Delay400ms();
}
}
void xd_setup(void) //fun3
{
fun3=1; //键按下标志
if(fun1==0&fun2==0)
{
xd_local[0]='0'+xd%10;
xd_local[1]='.';
xd_local[2]='0'+xd/10;
//fun2=1;
LcdClear();
DispNChar(0,0,15," PID xd setup "); //在第1行显示xi setup
DispNChar(0,1,6," xd="); //在第二行显示输出电压数值
//DispNChar(0,1,16,"ce shi inc"); //在第二行显示输出电压数值
DispNChar(10,1,3,xd_local);
//fun3=0;
Delay400ms();
Delay400ms();
}
else if(fun1==1) //如果按了xp设置后,再按下xd键,则xd键是减法功能
{
pid_xp_value_dec(1,1);
xp_local[0]='0'+xp%10;
xp_local[1]='.';
xp_local[2]='0'+xp/10;
DispNChar(0,0,15," PID xp setup "); //在第1行显示xi setup
DispNChar(0,1,6," xp="); //在第二行显示输出电压数值
DispNChar(10,1,3,xp_local); //更显xp显示
fun1=0;
fun3=0; //清除按键标志
Delay400ms();
Delay400ms();
}
else if(fun2==1) //如果按了xi设置后,再按下xd键,则xd键是减法功能
{
pid_xi_value_dec(1,1);
xi_local[0]='0'+xi%10;
xi_local[1]='.';
xi_local[2]='0'+xi/10;
DispNChar(0,0,15," PID xi setup "); //在第1行显示xi setup
DispNChar(0,1,6," xi="); //在第二行显示输出电压数值
DispNChar(10,1,3,xi_local);
fun2=0;
fun3=0; //清除按键标志
Delay400ms();
Delay400ms();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -