stm32+增量式pid+max6675 PWM温度控制
本文采用的芯片为STM32F103RCT6
温度芯片为MAX6675
之前spi通讯的max6675代码:
https://blog.csdn.net/answerMack/article/details/83310370
模拟io通讯的max6675代码:
https://blog.csdn.net/answerMack/article/details/83270562
代码写了,具体还没试验过
用电表打过结果,感觉还可以,就粘上来了。
因为选用的热电偶线测温变化太快,所以在中间加了报警,但是里面没有蜂鸣器。后续代码会改进。会加上CAN总线等设置。只是简单实现温控。
pid.h
#ifndef __PID_H
#define __PID_H
#include "sys.h"
typedef struct {
int Set_temperature; //期望值
int Current_temperature; //当前值
float proportion; //比例系数P
float integral; //积分系数I
float differential; //微分系数D
int T; //采样周期
float error_current; //当前误差:浮点数
int error_last; //上一步误差
int error_sum; //误差累计
float pid_proportion_out; //比例项
float pid_integral_out; //积分项
float pid_differential_out; //微分项
float pid_out; //PID控制输出
}PID;
//PID *Pid;//存放PID算法需要的数据
//void PID_Init(int SETtemp);
float pid_control(PID *PP,float current_temp);
void PWM_CONTROL(float SUM); //占空比计算的
void PWM_Out(u8 m); //按功率输出的
//struct PID *PP;
#endif
可能有错误,请评论帮忙指正。谢谢!!
//
pid.c
#include "pid.h"
#include "timer.h"
#include "usart.h"
//void PID_Init(int SETtemp) //启动时,PID的参数值应该从保存上次值的存储器空间读取出来。在此直接设定
//{
// Pid->Set_temperature=SETtemp; //用户设定值
// Pid->proportion=20;
// Pid->integral=5000; //积分系数
// Pid->differential=1200; //微分系数
// Pid->T=1000; //Pid计算周期(采样周期)
// Pid->error_current=0.0;
// Pid->error_last=0;
// Pid->error_sum=0;
// Pid->pid_proportion_out=0;
// Pid->pid_integral_out=0;
// Pid->pid_differential_out=0;
// Pid->pid_out=0;
//}
//struct PID *PP,
float pid_control( PID *PP,float current_temp){
static float PID_ZL=0.0;
float result=0.0;
float A0,A1,A2;
PP->error_current = PP->Set_temperature - current_temp;
printf("the PP->error_current is:%.2f\n",PP->error_current);
printf("\n");
if(PP->error_current>20)PWM_Out(0);
else if(PP->error_current<-20)PWM_Out(1);
else if(PP->error_current>10 && PP->error_current<20)PWM_Out(2);
else if(PP->error_current>-20 && PP->error_current<-10)PWM_Out(3);
else if(PP->error_current>-20 && PP->error_current