转载
PID_Parameters.P_gain_Cold= (float)(1.0f);
PID_Parameters.I_gain_Cold= (float)(0.1f);
PID_Parameters.D_gain_Cold= (float)(0.1f);
PID_Parameters.Deadband_Cold= 0.2f;
PID_Parameters.Integral_Cold= 5.0f;
void Temperature_Pwm_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* -----------------------------------------------------------------------
TIM4 Configuration: generate 4 PWM signals with 4 different duty cycles:
TIM4CLK = 72 MHz, Prescaler = 35999, TIM3 counter clock = 2 KHz
TIM4 ARR Register = 15999 => TIM4 Frequency = TIM4 counter clock/(ARR + 1)
TIM4 Frequency = 0.125Hz.
4000为基本单位保证开关频率大于2秒
----------------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = Tim4_Arr;
TIM_TimeBaseStructure.TIM_Prescaler = 35999;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* PWM4 Mode configuration: Channel1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Tim4_Arr;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);*/
/* PWM4 Mode configuration: Channel2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Tim4_Arr;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);*/
/* PWM4 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Tim4_Arr;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
/* PWM4 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Tim4_Arr;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM4,TIM_IT_Update, ENABLE);
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
TIM_CtrlPWMOutputs(TIM4,ENABLE);
Fans_Stop;
}
float PID_Cold(void)
{
static float err=0,ferror=0;
static float Temprature_Setting_Old=0.0f,Temprature_Setting=0.0f;
static u8 Setting_Refresh_Flag=0;
Temprature_Setting = PID_Parameters.Control_Temprature_Set;
if(Temprature_Setting_Old!=Temprature_Setting)
{
Temprature_Setting_Old = Temprature_Setting;
Setting_Refresh_Flag =1 ;
}
//计算偏差
err=(PID_Parameters.Control_Temprature_Set)-(PID_Parameters.Control_Temprature_Measure);
if(abs(err)>PID_Parameters.Deadband_Heat)
{
ferror=(float)err; //数据类型转换
//计算比例项
PID_Parameters.proportion_Cold = PID_Parameters.P_gain_Cold* ferror;
//计算积分项
if(PID_Parameters.proportion_Cold<-5.0f || PID_Parameters.proportion_Cold>5.0f)
{
PID_Parameters.Integral_Cold= 5.0f;
}
else
{
if(Setting_Refresh_Flag)
{
Setting_Refresh_Flag = 0 ;
PID_Parameters.Integral_Cold = 5.0f;
}
else
{
PID_Parameters.Integral_Cold += PID_Parameters.I_gain_Cold* ferror;
}
if(PID_Parameters.Integral_Cold>5.0f)
{
PID_Parameters.Integral_Cold= 5.0f;
Hot_Cold_Flag = 0 ;
}
else if(PID_Parameters.Integral_Cold<0.0f)
{
PID_Parameters.Integral_Cold=0.0f;
}
}
//输出结果
PID_Parameters.Result_Cold = PID_Parameters.proportion_Cold + PID_Parameters.Integral_Cold;
}
else
{
PID_Parameters.Result_Cold = PID_Parameters.Integral_Cold;
}
if(PID_Parameters.Result_Cold>5.0f)
{
PID_Parameters.Result_Cold= 5.0f;
Hot_Cold_Flag = 0 ;
}
else if(PID_Parameters.Result_Cold<0.0)
{
PID_Parameters.Result_Cold=0.0;
}
if(PID_Parameters.Result_Cold <=1.0f)
PID_Parameters.Result_Cold_PWM= 0.0f;
else if(PID_Parameters.Result_Cold <=2.0f)
PID_Parameters.Result_Cold_PWM = 4000.0f;
else if(PID_Parameters.Result_Cold <=3.0f)
PID_Parameters.Result_Cold_PWM = 8000.0f;
else if(PID_Parameters.Result_Cold <=4.0f)
PID_Parameters.Result_Cold_PWM = 12000.0f;
else
PID_Parameters.Result_Cold_PWM = 15999.0f;
return PID_Parameters.Result_Cold_PWM;
}
转载
|