【Cypress FM4 EVK 开发板试用体验】+单片机PID控制
-
-
yufeng1021
- LV3工程师
-
| 2017-01-20 10:40:40
- 浏览量 719
- 回复:0
算是以前算法的植入,不过运行起来各种的不稳定,还在调试吧,代码先分享与大家
#include /*******************************************************************************/
int idata p,ti,td,t,gd;
/*p--比例度, ti--积分时间, td--微分时间,t--采样时间,gd--给定值*/
/*时间的单位均为0.1S*/
int idata cel,cel1,shchshx,shchxx,u0;
/*clshx--满度对应的上限值,clxx--满度对应的下限值*/
/*cel--用clshx,clxx,jzhshx,jzhxx校准后的测量值,用来计算偏差*/
/*shchshx--D/A输出对应5V时的数字量,shchxx--D/A输出对应1V时的数字量*/
/*u0--输出的位置信号*/
int idata jzhshx,jzhxx,celiang;
/*jzhshx--输入5V对应的A/D数字量,jzhxx--jzhshx--输入5V对应的A/D数字量*/
/*celiang--实际测量的A/D值 */
int idata de,de1; /*本次偏差(测量值-给定值)和上次偏差*/
int idata df1; /*pid运算中要保留的中间量*/
bit zdbit,dshbit,zhfzy;
/*zdbit--手动自动切换标志,dshbit--等分采样时间到标志*/
/*******************************************************************************/
void d_a(unsigned int x)
{
}
/*******************************************************************************/
void pid()
{
int de2,dpv;
float tdf,tf,tif,uf;
if(dshbit==1)
{
dshbit=0;
de2=de1;
de1=de;
de=cel-gd;
dpv=cel-cel1;
cel1=cel;
if(zdbit==1)
{
tf=t;
tdf=td;
tif=ti;
uf=tdf*(de+de2-2*de1)/(tdf+10*tf)+tf*df1/(10*(tf+tdf));
df1=uf;
uf=uf+dpv+tf*de/tif;
uf=uf*1000;
uf=uf/p;
uf=uf/1000;
uf=uf*(shchshx-shchxx);
if(zhfzy==0)
{
u0=u0+uf;
}
if(zhfzy==1)
{
u0=u0-uf;
}
}
if(u0>shchshx+0.05*(shchshx-shchxx)){u0=shchshx+0.05*(shchshx-shchxx);}
if(u0
d_a(u0);
}
}
/*******************************************************************************/
void shjchl()
{
float x;
x=celiang-jzhxx;
x=x/(jzhshx-jzhxx);
cel=1000*x;
}
/*******************************************************************************/
void timer0(void) interrupt 1
{
TH0=-(2252/256);
TL0=-(2252%256);
}
/*=============================================================================*/
void main()
{
TMOD=0x11;
TH0 =-(2240/256);
TL0 =-(2240%256);
ET0 =1;
TR0=1;
EA=1;
p=1000;
ti=600;
td=0;
t=10;
gd=500;
celiang=2400;
jzhshx=4000;
jzhxx=1000;
shchshx=4000;
shchxx=800;
u0=2400;
df1=0;
zhfzy=0;
cel1=466;
zdbit=1;
dshbit=1;
while(1)
{
dshbit=1;
shjchl();
pid();
}
}
算是以前算法的植入,不过运行起来各种的不稳定,还在调试吧,代码先分享与大家
#include /*******************************************************************************/
int idata p,ti,td,t,gd;
/*p--比例度, ti--积分时间, td--微分时间,t--采样时间,gd--给定值*/
/*时间的单位均为0.1S*/
int idata cel,cel1,shchshx,shchxx,u0;
/*clshx--满度对应的上限值,clxx--满度对应的下限值*/
/*cel--用clshx,clxx,jzhshx,jzhxx校准后的测量值,用来计算偏差*/
/*shchshx--D/A输出对应5V时的数字量,shchxx--D/A输出对应1V时的数字量*/
/*u0--输出的位置信号*/
int idata jzhshx,jzhxx,celiang;
/*jzhshx--输入5V对应的A/D数字量,jzhxx--jzhshx--输入5V对应的A/D数字量*/
/*celiang--实际测量的A/D值 */
int idata de,de1; /*本次偏差(测量值-给定值)和上次偏差*/
int idata df1; /*pid运算中要保留的中间量*/
bit zdbit,dshbit,zhfzy;
/*zdbit--手动自动切换标志,dshbit--等分采样时间到标志*/
/*******************************************************************************/
void d_a(unsigned int x)
{
}
/*******************************************************************************/
void pid()
{
int de2,dpv;
float tdf,tf,tif,uf;
if(dshbit==1)
{
dshbit=0;
de2=de1;
de1=de;
de=cel-gd;
dpv=cel-cel1;
cel1=cel;
if(zdbit==1)
{
tf=t;
tdf=td;
tif=ti;
uf=tdf*(de+de2-2*de1)/(tdf+10*tf)+tf*df1/(10*(tf+tdf));
df1=uf;
uf=uf+dpv+tf*de/tif;
uf=uf*1000;
uf=uf/p;
uf=uf/1000;
uf=uf*(shchshx-shchxx);
if(zhfzy==0)
{
u0=u0+uf;
}
if(zhfzy==1)
{
u0=u0-uf;
}
}
if(u0>shchshx+0.05*(shchshx-shchxx)){u0=shchshx+0.05*(shchshx-shchxx);}
if(u0
d_a(u0);
}
}
/*******************************************************************************/
void shjchl()
{
float x;
x=celiang-jzhxx;
x=x/(jzhshx-jzhxx);
cel=1000*x;
}
/*******************************************************************************/
void timer0(void) interrupt 1
{
TH0=-(2252/256);
TL0=-(2252%256);
}
/*=============================================================================*/
void main()
{
TMOD=0x11;
TH0 =-(2240/256);
TL0 =-(2240%256);
ET0 =1;
TR0=1;
EA=1;
p=1000;
ti=600;
td=0;
t=10;
gd=500;
celiang=2400;
jzhshx=4000;
jzhxx=1000;
shchshx=4000;
shchxx=800;
u0=2400;
df1=0;
zhfzy=0;
cel1=466;
zdbit=1;
dshbit=1;
while(1)
{
dshbit=1;
shjchl();
pid();
}
}