📄 pid_51.c
字号:
#include"stdio.h"
struct PID
{
unsigned int PV; /* 设定目标 */
unsigned int SV; /* 经过值 */
unsigned char KP; /* 比例常数 */
unsigned char KI; /* 积分常数 */
unsigned char KD; /* 微分常数 */
unsigned int L_error; /* */
unsigned int Lastout; /* 误差和 */
unsigned int Out; /* 输出 */
} ;
#define UPPER_LIMIT 0xff
/* ========================================================= */
void pid(struct PID * pp)
{
int out;
unsigned int Error_1; /* Error_1 当前温差 */
unsigned int Error_2; /* Error_2 上次温差 */
Error_1=pp->PV - pp->SV; /* 计算积分偏差 */
Error_2=Error_1 - pp->L_error; /* 计算比例偏差 */
pp->L_error=Error_1; /* 更新e(k-1) */
out=pp->Lastout + Error_1 / pp->KI + Error_2 * pp->KP; /* 计算PI控制值 */
if(Error_1==0)
out=out; /* 计算不灵敏区的运算补偿 */
else
if(Error_1< 8 && Error_1> -8)
{
if(Error_1>0)out=out+1;
else
out=out-1;
}
else
if(Error_1<16 && Error_1> -16)
{
if(Error_1>0)
out=out+2;
else
out=out-2;
}
if(out>UPPER_LIMIT)
out=UPPER_LIMIT; /* 对输出值进行上限控制 */
if(out<0)
out=0; /* 对输出值进行下限控制 */
pp->Lastout=out; /* 更新out(k-1) */
if(Error_1<pp->KD&&Error_1>-pp->KD)
out=out+Error_1;
pp->Out=out;
/* return(out); //返回最终运算结果 */
}
/* ================================================*/
void clr_lastout(struct PID * pp , unsigned int value1)/* PID运算从0开始 */
{
pp->Lastout=value1;
}
/*=====================================================*/
void init(struct PID * pp,unsigned t){
pp->KP=1;
pp->KI=8;
pp->KD=4;
pp->PV=t;
}
/* ======================================================= */
void main(void)
{
struct PID spp ;
unsigned int t;
init(&spp,200) ;
clr_lastout(&spp,0) ;
while(1)
{
printf("输入经过值\n");
scanf("%d",&t);
spp.SV=t;
pid(&spp);
printf("输出结果===%d\n\n",spp.Out);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -