#include "iom16v.h"
#include "macros.h"
struct _PID
{
float PVn; //反馈信号变量
float SPn; //设定值
float Mn; //PID运算结果
float Kc; //比例系数
float Ts; //采样时间(ms)
float Ti; //积分时间(ms)
float Td; //微分时间(ms)
float Mx; //积分项调整参数
float PVn_1;//前一次反馈变量
float MPn; //比例项的结果值
float MIn; //积分项的结果值
float MDn; //微分项的结果值
};
int tmp;
//int result;
struct _PID *myPID;
void init_myPID(void);
void init_ports(void);
void init_device(void);
float MPn_value(struct _PID *PID);
float MIn_value(struct _PID *PID);
float MDn_value(struct _PID *PID);
float Mx_value(struct _PID *PID);
float Mn_value(struct _PID *PID);
void PID (void)
{
init_device();
init_myPID();
init_ports();
//myPID->PVn=result;
myPID->SPn = 98;
myPID->Kc = 1.5;//1.5
myPID->Ts = 0.002;//0.002
myPID->Ti = 0.015;//0.015???
myPID->Td = 0.0;
myPID->PVn = 90;
myPID->MPn = MPn_value(myPID);
myPID->MIn = MIn_value(myPID);
myPID->MDn = MDn_value(myPID);
myPID->Mn = Mn_value(myPID);
myPID->Mx = Mx_value(myPID);
tmp=myPID->Mn;
myPID->PVn_1 = myPID->PVn;
}
/******************************************************************************/
void init_myPID(void)
{
myPID->PVn = 0.0;
myPID->SPn = 0.0;
myPID->Mn = 0.0;
myPID->Kc = 0.0;
myPID->Ts = 0.0;
myPID->Ti = 0.0;
myPID->Td = 0.0;
myPID->Mx = 0.0;
myPID->PVn_1 = 0.0;
myPID->MPn = 0.0;
myPID->MIn = 0.0;
myPID->MDn = 0.0;
}
//------------------------------------------------------------------------------
void init_ports(void)
{
PORTA = 0x00; //If ADC Function was be used,the PORTA could`t set bit 1
DDRA = 0x00; //the port set input mode.
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//------------------------------------------------------------------------------
void init_device(void)
{
CLI();//中断清零,不允许中断
init_ports();
MCUCR = 0x00; //Set Power control(State:Close)//用来确定,外部中断的触发方式,包括上升沿,下降沿,低电平等触发方式
GICR = 0x00; //Set boot guide(State:Close).//通用中断控制寄存器GICR的高3位为INT0,INT1,INT2的中断允许位
SEI(); //re-enable interrupts//打开全局中断(使能)
//all peripherals are now initialized
}
// 计算 比例项的值
//------------------------------------------------------------------------------
float MPn_value(struct _PID *PID)
{
float myMPn = 0.0;
myMPn = PID->Kc *( PID->SPn - PID->PVn);
return myMPn;
}
//计算积分项的值
//------------------------------------------------------------------------------
float MIn_value(struct _PID *PID)
{
float myMIn = 0.0;
myMIn = PID->Kc*(PID->Ts/PID->Ti)*(PID->SPn - PID->PVn) + PID->Mx;
return myMIn;
}
//计算微分项的值
//------------------------------------------------------------------------------
float MDn_value(struct _PID *PID)
{
float myMDn = 0.0;
myMDn = PID->Kc * (PID->Td/PID->Ts) * (PID->PVn_1 - PID->PVn);
return myMDn;
}
//计算PID的结果
//------------------------------------------------------------------------------
float Mn_value(struct _PID *PID)
{
float myMn = 0.0;
myMn = PID->MPn + PID->MIn + PID->MDn;
return myMn;
}
//计算积分项的调整值
//------------------------------------------------------------------------------
float Mx_value(struct _PID *PID)
{
float myMx = 0.0;
if(PID->Mn > 1.0)
{
myMx = 1.0 - (PID->MPn + PID->MDn);
}
else if(PID->Mn < 0.0)
{
myMx = -(PID->MPn + PID->MDn);
}
return myMx;
}
void zkb()//这是输出PWM波的程序
{
DDRD|=0X80;
TCCR2=0X6A;
OCR2=tmp;//tmp
}
void main()
{
int tmp;
init_myPID();
init_ports();
init_device();
PID();
zkb();
while(1)
{
PID();
zkb();
}
}
|