void PID_out(void) { int temp; pid_t1=i_set_data; pid_t2=current_adc; pid_t3=v_set_data; pid_t4=voltage_adc; if(channel_sel&vi_selection==0) //闭环,电流 errpres=(float)(pid_t1-pid_t2); //电流偏差 else if(channel_sel&(vi_selection))//闭环,电压 errpres=(float)(pid_t3-pid_t4); //电压偏差 ppro=0.5*errpres; //Kp pint=0.5*(errpres+pintprev); //Ki pdif=0.5*(errpres-errprev); //Di pid_pwm=(float)(ppro+pint+pdif); errprev=errpres; pintprev=pint; //**********限幅最大输出值&最小输出值 // pwm=pid_pwm; if(pid_pwm>1020) //upperlimit { d_pwm=1020; //upperlimit; pwm=pid_pwm; } if(pid_pwm<1) //lowerlimit { pid_pwm=0; pwm=pid_pwm; //lowerlimit; } if((pid_pwm>1)&pid_pwm<1020)) //lowerlimit { pwm=pid_pwm; //lowerlimit; } } |