各位高手,本人这个程序调了一天了,他的占空比好像一直不变化!求助!!!
#ifndef pid
#define pid
uchar set; //温度初始值
uint rout; // PID Response (Output)
uchar high_time,low_time,count=0; //占空比调节参数
struct PID {
unsigned int SetPoint; // 设定目标 Desired Value
unsigned int Proportion; // 比例常数 Proportional Const
unsigned int Integral; // 积分常数 Integral Const
unsigned int Derivative; // 微分常数 Derivative Const
unsigned int LastError; // Error[-1]
unsigned int PrevError; // Error[-2]
unsigned int SumError; // Sums of Errors
};
struct PID spid; // PID Control Structure
/****************pid初始化*********************/
void init_pid()
{
high_time=50;
spid.LastError = 20;
spid.PrevError = 20;
spid.SumError= 20;
spid.Proportion = 20; // Set PID Coefficients
spid.Integral = 8;
spid.Derivative =6;
spid.SetPoint = set; // Set PID Setpoint
}
/***************************PID算法**************************/
unsigned int PIDCalc( struct PID *pp, unsigned int NextPoint )
{
unsigned int dError=0,Error=0;
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);
}
/********************PID控制占空比*************************/
//high_time表示高电平数
void duty_cycle(uint t) // 占空比
{
/* uchar s;
s=set;
if(s>t)
{
if(s-t>2)
high_time=100;
else
{
rout = PIDCalc ( &spid,t ); // Perform PID Interation
if(high_time<=100)
high_time=(uchar)(rout/600);
else
high_time=100;
}
}
else
{
high_time=0;
}
*/
uchar i;
if(set>t)
{
if(set-t>3)
{
high_time=100;
low_time=0;
}
else
{
for(i=0;i<10;i++)
{
rout=PIDCalc(&spid,t);
}
if(high_time<=100)
high_time=(uchar)(rout/800);
else
{ high_time=100;
low_time=(100-high_time);
}
}
}
else if(set<=t)
{
if(t-set>0)
{
high_time=0;
low_time=100;
}
else
{
for(i=0;i<10;i++)
{
rout=PIDCalc(&spid,t);
}
if(high_time<=100)
high_time=(uchar)(rout/10000);
else
high_time=100;
low_time=(100-high_time);
}
}
}
#endif
void main ()
{
uint t;
TMOD=0x01;
TH0 =0x10;
TL0 =0x10;
EA = 1;
ET0 = 1;
TR0 = 1;
set=70;
init_pid();
view();
while(1)
{
t=gettemp;
duty_cycle(t);
}
}
/*void t0_int(void) interrupt 1 //PWM波输出
{
if(++count<=(high_time))
SWH=0;
else if(count<=100)
SWH=1;
else
count=0;
TH0=0x10; //定时器初始化
TL0=0x10;
} */
void t0() interrupt 1
{
TF0=0;
TR0=0;
if(++count<=(high_time))
SWH=0;
else if((high_time)<count<=100)
{
SWH=1;
}
else
//SWH=0;
count=0;
TH0=0x10;
TL0=0x10;
TR0=1;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>