用单片机做的,在电机测试了可以用。就是开机的时候,会最大转速输出,有点噪音
单片机源程序如下:
- void Pid_regulate(void)
- {
- u8 TempError;
- u8 TempAngle;
- if(Pid_Ts != 0)
- {
- return; //采样时间未到,退出
- }
- Pid_ts = 0x1a; //重置采样时间
- TempError = Temp_Setting - TempOp; //温度误差
- Integral = Integral + Ki * TempError; //误差* 积分增益并累加
- if(Integral > 1024)
- Integral = 1024;
- else if(Integral < -1024)
- Integral = -1024;
- TempAngle = FeedFwd_agnle(Templn,Voltage,WaterFlow);
- TempAngle = Kp * TempError;
- TempAngle = TempAngle + (Integral >> 4);//累加
- if(TempAngle < 2)
- TempAngle = 2; //下限
- else if(TempAngle > 200)
- TempAngle = 200; //上限
- Fire_angle = 200 - (u8)TempAngle; //得到触发角
- }
- void PID(void)
- {
- float ek,ek1,ek2,ek_sum;
- uint a;
- ek=K-temp; //计算误差
- while(1)
- {
- if(ON_OFF==1) //温控开关开
- {
- if(ek>=15)
- {
- outflag=1;
- OUT=0;
- Time_on=100;
- break;
- } //温差大于15度,不用PID调节
- else if(ek<0)
- {
- ek=0;
- outflag=0;
- OUT=1;
- Time_on=0;
- break;
- } //温差小于等于0不调节
- else
- {
- if(ek>5)
- a=0;
- if(ek<5)
- a=1;
- ek2=ek1;
- ek1=ek;
- ek_sum+=ek;
- uk=kp*ek+ki*ek_sum+kd*(ek1-ek2);
- //计算PID输出uk
- if(start_flag==0) //PID运算标志位
- {
- uk0=uk; //uk初值,用于参考以后uk加热时间
- start_flag=1;
- div=100/uk0;
- } //开始PID计算
- else if(start_flag==1)//加热片加热时间
- {
- Time_on=uk*div;
- OUT=0;
- outflag=1;
- }
- break;
- }
- }
- else
- {
- outflag=0;
- OUT=1;
- break;
- } //不加热
- }
- }
复制代码
所有资料51hei提供下载:
Pid.rar
(861 Bytes, 下载次数: 99)
|