導航:首頁 > 源碼編譯 > pid演算法控溫

pid演算法控溫

發布時間:2023-06-07 19:09:52

『壹』 如何用PID演算法編程,使單片機通過控制繼電器來實現恆溫功能。

/***********************************************************************
PID溫度控製程序
程序說明:
系統上電後顯示 「--溫度」
表示需要先設定溫度才開始進行溫度檢測
溫度設定完畢後程序才開始進行PID溫控
***********************************************************************/
#include <reg52.h>
#include <absacc.h>
#include"DS18B20.H"
#include"PID.H"
#define uchar unsigned char
#define uint unsigned int
unsigned char code tab[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF
}
;
/*個位0~9的數碼管段碼*/
unsigned char code sao[]=
{
0x7f,0xbf,0xdf,0xef
}
;
//掃描碼
uchar set=30,keyflag=1 ; //set初始化為30° keyflag為進入溫度設定的標志位
//4個按鍵使用說明
sbit key_out=P1^0 ; //用於溫度設定後的退出
sbit key_up=P1^1 ; //設定溫度加
sbit key_down=P1^2 ; //設定溫度減
sbit key_in=P1^3 ; //在程序的運行中如需要重新設定溫度 按下此鍵才能進入設置模式並且此時是停在溫度控制的,按下key_out鍵後才表示設定完畢
void Show_key();
/***********************************************************/
void delays(unsigned char k)
{
unsigned char i,j ;
for(i=0;i<k;i++)
for(j=0;j<50;j++);
}
/*********************************************************
//數碼管顯示函數
P0口 作為數據口
P2口的低四位作為掃描口
變數 x表示掃描
d表示是否要加小數點 為1是 為0不加
y表示傳遞的數值
*********************************************************/
LCD_disp_char(uchar x,bit d,uchar y)
{
P2=0XFF ;
P0=0xFF ;
if(d==0)
P0=tab[y];
else
P0=tab[y]&0x7f ; //與上0x7f表示是否要加小數點
P2=sao[x]; //打開掃描端號

}
/*********************************************************
按鍵掃描
*********************************************************/
void keyscan(void)
{
if(key_in==0) //按鍵進入函數
{
delays(10); //延時消抖 (以下同)
if(key_in==0)
{
while(key_in==0)
{
Show_key(); //如果一直按著鍵不放 就一直顯示在當前狀態 (以下同)
}
keyflag=1 ; //按鍵標志位
}
}
/***********************/
if(key_out==0) //按鍵退出
{
delays(10);
if(key_out==0)
{
while(key_out==0)
{
Show_key();
}
keyflag=0 ;
set_temper=set ;
}
}
/*************************/
if(key_up==0) //設定溫度的加
{
delays(10);
if(key_up==0)
{
while(key_up==0)
{
Show_key();
}
if(keyflag==1)
{
set++;
if(set>90) //如果大於90°就不在加
set=90 ;
}
}
}
/*************************/
if(key_down==0) //溫度設定的減
{
delays(10);
if(key_down==0)
{
while(key_down==0)
{
Show_key();
}
if(keyflag==1)
{
set--;
if(set<30) //溫度減到30°時不在往下減
set=30 ;
}
}
}
}
/*********************************************************************
按鍵按下時的顯示函數
***********************************************************************/
void Show_key()
{
output=1 ;
LCD_disp_char(3,0,10); //顯示 -
delays(3);
LCD_disp_char(2,0,10); //顯示- (表示溫度設定 )
delays(3);
LCD_disp_char(1,0,set/10); //顯示溫度十位
delays(3);
LCD_disp_char(0,0,set%10); //顯示溫度個位
delays(3);
}
/*****************************************************************/
void main()
{
unsigned int tmp ;//聲明溫度中間變數
unsigned char counter=0 ;
PIDBEGIN(); //PID參數的初始化
output=1 ; //關閉繼電器輸出
while(1)
{
keyscan();
if(keyflag)
{
Show_key(); //顯示溫度設定
}
else
{
if(counter--==0)
{
tmp=ReadTemperature();//每隔一段時間讀取溫度值
counter=20 ;
}
LCD_disp_char(3,0,tmp/1000); //顯示溫度十位
delays(3);
LCD_disp_char(2,1,tmp/100%10); //顯示溫度個位
//顯示小數點
delays(3);
LCD_disp_char(1,0,tmp/10%10); //顯示溫度小數後一位
delays(3);
LCD_disp_char(0,0,tmp%10);//顯示溫度小數後二位
delays(3);
P2=0XFF ;
P0=0xff ;
compare_temper(); //比較溫度

}
}
}
/**********************************************************************************************************************************************/
//PID演算法溫控C語言2008-08-17 18:58
#ifndef _PID_H__
#define _PID_H__
#include<intrins.h>
#include<math.h>
#include<string.h>
struct PID
{
unsigned int SetPoint ;
// 設定目標 Desired Value
unsigned int Proportion ;
// 比例常數 Proportional Const
unsigned int Integral ;
// 積分常數 Integral Const
unsigned int Derivative ;
// 微分常數 Derivative Const
unsigned int LastError ;
// Error[-1]
unsigned int PrevError ;
// Error[-2]
unsigned int SumError ;
// Sums of Errors
}
;
struct PID spid ;
// PID Control Structure
unsigned int rout ;
// PID Response (Output)
unsigned int rin ;
// PID Feedback (Input)

sbit output=P1^4;
unsigned char high_time,low_time,count=0 ;
//占空比調節參數
unsigned char set_temper ;
void PIDInit(struct PID*pp)
{
memset(pp,0,sizeof(struct PID)); //PID參數初始化全部設置為0
}
unsigned int PIDCalc(struct PID*pp,unsigned int NextPoint)
{
unsigned int dError,Error ;
Error=pp->SetPoint-NextPoint ;
// 偏差
pp->SumError+=Error ;
// 積分
dError=pp->LastError-pp->PrevError ;
// 當前微分
pp->PrevError=pp->LastError ;
pp->LastError=Error ;
//比例
//積分項
return(pp->Proportion*Error+pp->Integral*pp->SumError+pp->Derivative*dError);
// 微分項
}
/***********************************************************
溫度比較處理子程序
***********************************************************/
void compare_temper()
{
unsigned char i ;
//EA=0;
if(set_temper>temper)
{
if(set_temper-temper>1)
{
high_time=100 ; //大於1°不進行PID運算
low_time=0 ;
}
else
{ //在1°范圍內進行PID運算
for(i=0;i<10;i++)
{
//get_temper();
rin=s;
// Read Input
rout=PIDCalc(&spid,rin); //執行PID運算
// Perform PID Interation
}
if(high_time<=100) //限制最大值
high_time=(unsigned char)(rout/800);
else
high_time=100;
low_time=(100-high_time);
}
}
/****************************************/
else if(set_temper<=temper) //當實際溫度大於設置溫度時
{
if(temper-set_temper>0)//如果實際溫度大於設定溫度
{
high_time=0 ;
low_time=100 ;
}
else
{
for(i=0;i<10;i++)
{
//get_temper();
rin=s ;
// Read Input
rout=PIDCalc(&spid,rin);
// Perform PID Interation
}
if(high_time<100) //此變數是無符號字元型
high_time=(unsigned char)(rout/10000);
else
high_time=0 ;//限制不輸出負值
low_time=(100-high_time);
//EA=1;
}
}
}

/*****************************************************
T0中斷服務子程序,用於控制電平的翻轉 ,40us*100=4ms周期
******************************************************/
void serve_T0()interrupt 1 using 1
{
if(++count<=(high_time))
output=0 ;
else if(count<=100)
{
output=1 ;
}
else
count=0 ;
TH0=0x2f ;
TL0=0xe0 ;
}
void PIDBEGIN()
{

TMOD=0x01 ;
TH0=0x2f ;
TL0=0x40 ;

EA=1 ;
ET0=1 ;
TR0=1 ;

high_time=50 ;
low_time=50 ;
PIDInit(&spid);
// Initialize Structure
spid.Proportion=10 ;
// Set PID Coefficients
spid.Integral=8 ;
spid.Derivative=6 ;
spid.SetPoint=100 ;
// Set PID Setpoint

}
#endif

轉自他人程序。

『貳』 請教溫控PID增量型演算法公式

南京星德機械提供:增量式PID控制演算法

當執行機構需要的不是控制量的絕對值,而是控制量的增量(例如去驅

動步進電動機)時,需要用PID的「增量演算法」。

『叄』 關於Android系統實現PID演算法控制溫度

第一步:把器件等各種實物連上
第二步:開環,對PWM的控溫信號加階躍(改變PWM的占空比),由輸入輸出的結果大致得出加熱器的數學模型
第三部:由理論公式整定出PID參數
第四部:根據實際結果調節PID以達到你想要的指標

『肆』 PID演算法控制溫度加熱系統,室溫(為防止分數流失,做成追加100分以上)

剛好前不久搞過PID,部分程序如下,僅供參考

/*==============================================================================
在使用單片機作為控制cpu時,請稍作簡化,具體的PID參數必須由具體對象通過實驗確定。
由於單片機的處理速度和ram資源的限制,一般不採用浮點數運算,而將所有參數全部用整數,
運算到最後再除以一個2的N次方數據(相當於移位),作類似定點數運算,可大大提高運算速度,
根據控制精度的不同要求,當精度要求很高時,注意保留移位引起的「余數」,做好余數補償。
這個程序只是一般常用pid演算法的基本架構,沒有包含輸入輸出處理部分。
==============================================================================*/

#include <string.h>
#include <stdio.h>

/*===============================================================================
PID Function
The PID function is used in mainly
control applications. PID Calc performs one iteration of the PID
algorithm.
While the PID function works, main is just a mmy program showing
a typical usage.

PID功能
在PID功能主要用於控制應用。 PID 計算器執行一個PID的迭代演算法。雖然PID功能的工程,
主要只是一個虛擬程序顯示一個典型的使用。
================================================================================*/
typedef struct PID {
double SetPoint; // 設定目標 Desired Value
double Proportion; // 比例常數 Proportional Const
double Integral; // 積分常數 Integral Const
double Derivative; // 微分常數 Derivative Const
double LastError; // Error[-1]
double PrevError; // Error[-2]
double SumError; // Sums of Errors
} PID;

/*================================ PID計算部分===============================*/
double PIDCalc( PID *pp, double NextPoint )
{
double dError, Error;

Error = pp->SetPoint - NextPoint; // 偏差
pp->SumError += Error; // 積分
dError = pp->LastError - pp->PrevError; // 當前微分
pp->PrevError = pp->LastError;
pp->LastError = Error;
return (pp->Proportion * Error // 比例項
+ pp->Integral * pp->SumError // 積分項
+ pp->Derivative * dError // 微分項
);
}

/*======================= 初始化的PID結構 Initialize PID Structure===========================*/
void PIDInit (PID *pp)
{
memset ( pp,0,sizeof(PID));
}

/*======================= 主程序 Main Program=======================================*/
double sensor (void) // 虛擬感測器功能 Dummy Sensor Function{ return 100.0;}
void actuator(double rDelta) // 虛擬驅動器功能 Dummy Actuator Function{}
void main(void)
{
PID sPID; // PID控制結構 PID Control Structure
double rOut; // PID響應(輸出) PID Response (Output)
double rIn; // PID反饋(輸入) PID Feedback (Input)
PIDInit ( &sPID ); // 初始化結構 Initialize Structure
sPID.Proportion = 0.5; // 設置PID系數 Set PID Coefficients
sPID.Integral = 0.5;
sPID.Derivative = 0.0;
sPID.SetPoint = 100.0; // 設置PID設定 Set PID Setpoint
for (;;)
{ // 模擬最多的PID處理 Mock Up of PID Processing
rIn = sensor (); // 讀取輸入 Read Input
rOut = PIDCalc ( &sPID,rIn ); // 執行的PID迭代 Perform PID Interation
actuator ( rOut ); // 所需的更改的影響 Effect Needed Changes
}

『伍』 怎樣用PID演算法對恆溫箱的溫度進行控制,求相關的51單片機匯編程序

本設計要求:本溫度控制系統為以單片機為核心,實現了對溫度實時監測和控制,實現了控制的智能化。設計恆溫箱溫度控制系統,配有溫度感測器,採用DS18B20數字溫度感測器,無需數模擬∕數字轉換,可直接與單片機進行數字傳輸,採用了PID控制技術,可以使溫度保持在要求的一個恆定范圍內,配有鍵盤,用於輸入設定溫度;配有數碼管LED用來顯示溫度。
技術參數和設計任務:
1、利用單片機AT89C2051實現對溫度的控制,實現保持恆溫箱在最高溫度為110℃。
2、可預置恆溫箱溫度,烘乾過程恆溫控制,溫度控制誤差小於±2℃。
3、預置時顯示設定溫度,恆溫時顯示實時溫度,採用PID控制演算法顯示精確到0.1℃。
4、溫度超出預置溫度±5℃時發出聲音報警。
5、對升、降溫過程沒有線性要求。
6、溫度檢測部分採用DS18B20數字溫度感測器,無需數模擬∕數字轉換,可直接與單片機進行數字傳輸
7、人機對話部分由鍵盤、顯示和報警三部分組成,實現對溫度的顯示、報警。
需要的話聯系用戶名扣扣

閱讀全文

與pid演算法控溫相關的資料

熱點內容
飛盧破解版網址 瀏覽:632
怎麼在米家app裡面找到小愛同學 瀏覽:208
網盤的小說在哪個文件夾 瀏覽:59
阿里程序員約炮 瀏覽:939
java語言程序設計題 瀏覽:464
法和經濟學pdf 瀏覽:703
statafgls命令 瀏覽:737
汽車壓縮機電磁閥檢測 瀏覽:543
c編譯器如何打開 瀏覽:760
小受重生回80年代 瀏覽:797
夢幻無敵伺服器什麼時候開的 瀏覽:903
cnc編程程序走不了g代碼不正確 瀏覽:772
轉轉app賣家怎麼收錢 瀏覽:883
app綁定微信支付讓拒是怎麼回事 瀏覽:568
radan編程 瀏覽:52
高清pdf電子書 瀏覽:339
騰訊的伺服器用什麼處理器 瀏覽:405
安卓開發如何配置 瀏覽:848
門戶網站整站源碼 瀏覽:613
如何使用伺服器gpu 瀏覽:874