单片机解密STC8G1K08-SOP8单片机制作定时控制程序
- 单片机解密#define Max_Cycle 290//限制幅度
- #define Min_Cycle -290
- typedef struct//结构体
- {
- int targetValue;//设定值
- int Error;//误差
- int prevError;//上次误差
- int Integral;//误差积分
- float Kp;//比例系数
- float Ki;//没用
- float Kd;//微分系数
- }PID;
- int Calc_PID(PID *pid,int Pos)//位置PID
- {
- int output;
- int Differential;//微分变量
-
- pid->Error = (pid->targetValue - Pos); // 误差有正负
-
- pid->Integral += pid->Error; // 误差累计
-
- Differential = pid->Error - pid->prevError; // 微分部分本次误差减去上次误差
-
- output = (pid->Kp * pid->Error + pid->Integral * pid->Ki + pid->Kd * Differential);//通过比例 积分 微分参数算出输出控制量 可省略积分部分
-
- pid->prevError = pid->Error; // 本次误差赋值为上次
-
- if(output>Max_Cycle) // 超过上限则输出恒定值
- output = Max_Cycle;
-
- if(output<Min_Cycle)
- output = Min_Cycle;
- return output;
- }
- void Init_PID_Parameter(void)
- {
- xPID.targetValue = 2300; //
- yPID.targetValue = 2300;
- xPID.Error = 0;
- yPID.Error = 0;
- xPID.prevError=0;
- yPID.prevError=0;
-
- xPID.Ki = 0.0f;
- yPID.Ki = 0.0f;
- xPID.Kp = 0.3f;
- yPID.Kp = 0.3f;
-
- xPID.Kd = 0.60f;
- yPID.Kd = 0.60f;
- }

芯片解密