用PIC单片机的PWM驱动MOS,做一BUCK的电源;想加入PI算法,可程式总是出错,现把程式其它部分删掉;图为仿真部分电路(控制A/Dpin为1.2v)请大家帮忙看看:程式哪书写错误。
#include "pic.h
"typedef unsigned char unchar;
typedef unsigned int unint;
unint PWM_period=0,PWM_duty=0;
void main()
{typedef struct pid
{ float setvol;//设定值
float pcoe;//比例系数
float icoe;//积分系数
int lasterror;
int preerror;
float sumerror;
}pid;
pid volpid,*sptr=&volpid;
//pid *sptr;
//sptr=&volpid;
void initpid(pid *sp);
void crespid(pid *sp,int testpoint);
asm("clrwdt");
initpid(pid *sptr);//
sptr->setvol=1.2;
while(1){crespid(pid *sptr,4);}}
/*------------增量式PI ---------*/
void initpid(pid *sp)
{sp->setvol=0;
sp->pcoe=0.4;
sp->icoe=0.05;//
sp->lasterror=0;}
void crespid(pid *sp,int testpoint)
{static float temerror;
if(sp->setvol>testpoint)
temerror=sp->setvol-testpoint;
else temerror=testpoint-sp->setvol;
{PWM_duty+=(sp->pceo)*(temerror-sp->lasterror)+(sp->iceo)*temerror;}
sp->lasterror=temerror;//把PWM—duty--部分省略
}