找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2944|回复: 0
打印 上一主题 下一主题
收起左侧

pid控温程序存在,测了输出端波形很乱,但变化有一定规律,pwm波不是方波吗?怎么....

[复制链接]
跳转到指定楼层
楼主
ID:95878 发表于 2015-11-15 17:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<intrins.h>
#include<math.h>
#include<string.h>
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
unsigned int rout ;
// PID Response (Output)
unsigned int rin ;
// PID Feedback (Input)
unsigned int set_temper,temper,s ;


sbit output=P3^4;
unsigned char high_time,low_time,count=0 ;
//占空比调节参数


void PIDInit(struct PID*pp)
{
    memset(pp,0,sizeof(struct PID)); //PID参数初始化全部设置为0
}

unsigned int PIDCalc(struct PID*pp,unsigned int NextPoint)
{
    unsigned 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);
    // 微分项
}

/***********************************************************
温度比较处理子程序
***********************************************************/
void compare_temper(unsigned int set_temper,unsigned int temper)
{
    unsigned char i ;
    //EA=0;
    if(set_temper>temper)
    {
        if(set_temper-temper>10)
        {
            high_time=100 ; //大于1°不进行PID运算
            low_time=0 ;
        }
        else
        {   //在1°范围内进行PID运算
            for(i=0;i<10;i++)
            {
                //get_temper();
                rin=temper;
                // Read Input
                rout=PIDCalc(&spid,rin); //执行PID运算
                // Perform PID Interation
            }
            if(high_time<=100) //限制最大值
            high_time=(unsigned char)(rout/800);
            else
            high_time=100;
            low_time=(100-high_time);
        }
    }
/****************************************/
    else if(set_temper<=temper) //当实际温度大于设置温度时
    {
        if(temper-set_temper>0)//如果实际温度大于设定温度
        {
            high_time=0 ;
            low_time=100 ;
        }
        else
        {
            for(i=0;i<10;i++)
            {
                //get_temper();
                rin=temper;
                // Read Input
                rout=PIDCalc(&spid,rin);
                // Perform PID Interation
            }
            if(high_time<100) //此变量是无符号字符型
            high_time=(unsigned char)(rout/10000);
            else
            high_time=0 ;//限制不输出负值
            low_time=(100-high_time);
            //EA=1;
        }
    }
}


/*****************************************************
T1中断服务子程序,用于控制电平的翻转 ,40us*100=4ms周期
******************************************************/
void serve_T1()interrupt 3 using 1
{
    if(++count<=(high_time))
         output=0 ;
    else if(count<=100)
    {
        output=1 ;
    }
    else
    count=0 ;
    TH1=0x2f ;
    TL1=0xe0 ;
}

void PIDBEGIN()
{


    TH1=0x2f ;
    TL1=0x40 ;


    ET1=1 ;
    TR1=1 ;

    high_time=50 ;
    low_time=50 ;
    PIDInit(&spid);
    // Initialize Structure
    spid.Proportion=10 ;
    // Set PID Coefficients
    spid.Integral=8 ;
    spid.Derivative=6 ;
    spid.SetPoint=100 ;
    // Set PID Setpoint

}
#endif
main
{
while(1){
PIDBEGIN();
if(hengwen==0)
{

  compare_temper(temp4,temp5);

}
}
}
temp4是设定温度和temp5时实际温度,求高人指点,急急急

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表