A. 單片機如何寫PID程序
PID演算法有公式,直接套用就可以了。
公式在這個鏈接說的很明白了。
http://ke..com/view/1759810.htm
1、如果加入D抖動的特別厲害,試試只用PI控制。
2、還有PID參數都是一步一步調出來的,我建議你做個上位機,就是個簡單的VB串口程序,用來設置PID參數
3、然後在單片機這邊弄個串口接收程序,這里就是個簡單的串口程序,人人都會,把接收到的PID存儲在緩沖區里。
4、然後單片機程序直接調用。單片機帶EEPROM的話,當接收到改變的PID參數時,存儲這些參數。去STC官網下你的單片機資料,上面有EEPROM測試程序,直接套用。
上面純屬個人建議,都容易實現,不過有點費時。
B. 51單片機實現電動機的PID恆速控制。
這是倒立擺系統的PID控制函數的一部分,你看看有沒有思路
/****************************************************************/
//定義結構體
/****************************************************************/
struct may_PID{
signed long Proportion; //比例 ;調節系數
signed long Integral; //積分 ;調節系數
signed long Derivative; //微分 ;調節系數
signed long SetPoint; //設定值 ;定值
signed long SumError; //偏差積分
signed long PrevError; //之前偏差值
}PID/*此處可放結構體變數名*/;
struct may_PID *pp; //定義結構體類型指針
//pp=malloc(sizeof(struct may_PID)); //為指針變數分配安全的地址空間;sizeof:其為計算位元組長度函數
/*****************************************************************/
/**************************PTD函數**************************************/
signed long PIDCalc(signed long NextPoint/*當前值*/ )
{
signed long dError;
Error = pp->SetPoint - NextPoint;//當前偏差
pp->SumError+=Error; //積分
dError=Error-pp->PrevError;//當前微分=當前偏差-之前偏差
pp->PrevError=Error; //把當前偏差賦予之前偏差變數,使其充當下次取樣的之前偏差
return (long)(pp->Proportion *Error //比例項
+pp->Integral*pp->SumError //積分項
+pp->Derivative*dError); //微分項
}
/*****************************************************************************/
C. 求助51單片機自適應PID液位控制
這個網上有專門的程序模板的你下載一個改一下就行
D. 求基於單片機溫控的PID演算法程序,謝謝啊!!!!
//溫控PID程序
#include<reg51.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 data1=P1^0;
sbit clk=P1^1;
sbit plus=P2^0;
sbit subs=P2^1;
sbit stop=P2^2;
sbit output=P3^4;
sbit DQ=P3^3;
unsigned char flag,flag_1=0;
unsigned char high_time,low_time,count=0;//占空比調節參數
unsigned char set_temper=35;
unsigned char temper;
unsigned char i;
unsigned char j=0;
unsigned int s;
/***********************************************************
延時子程序,延時時間以12M晶振為准,延時時間為30us×time
***********************************************************/
void delay(unsigned char time)
{
unsigned char m,n;
for(n=0;n<time;n++)
for(m=0;m<2;m++){}
}
/***********************************************************
寫一位數據子程序
***********************************************************/
void write_bit(unsigned char bitval)
{
EA=0;
DQ=0; /*拉低DQ以開始一個寫時序*/
if(bitval==1)
{
_nop_();
DQ=1; /*如要寫1,則將匯流排置高*/
}
delay(5); /*延時90us供DA18B20采樣*/
DQ=1; /*釋放DQ匯流排*/
_nop_();
_nop_();
EA=1;
}
/***********************************************************
寫一位元組數據子程序
***********************************************************/
void write_byte(unsigned char val)
{
unsigned char i;
unsigned char temp;
EA=0;
TR0=0;
for(i=0;i<8;i++) /*寫一位元組數據,一次寫一位*/
{
temp=val>>i; /*移位操作,將本次要寫的位移到最低位*/
temp=temp&1;
write_bit(temp); /*向匯流排寫該位*/
}
delay(7); /*延時120us後*/
// TR0=1;
EA=1;
}
/***********************************************************
讀一位數據子程序
***********************************************************/
unsigned char read_bit()
{
unsigned char i,value_bit;
EA=0;
DQ=0; /*拉低DQ,開始讀時序*/
_nop_();
_nop_();
DQ=1; /*釋放匯流排*/
for(i=0;i<2;i++){}
value_bit=DQ;
EA=1;
return(value_bit);
}
/***********************************************************
讀一位元組數據子程序
***********************************************************/
unsigned char read_byte()
{
unsigned char i,value=0;
EA=0;
for(i=0;i<8;i++)
{
if(read_bit()) /*讀一位元組數據,一個時序中讀一次,並作移位處理*/
value|=0x01<<i;
delay(4); /*延時80us以完成此次都時序,之後再讀下一數據*/
}
EA=1;
return(value);
}
/***********************************************************
復位子程序
***********************************************************/
unsigned char reset()
{
unsigned char presence;
EA=0;
DQ=0; /*拉低DQ匯流排開始復位*/
delay(30); /*保持低電平480us*/
DQ=1; /*釋放匯流排*/
delay(3);
presence=DQ; /*獲取應答信號*/
delay(28); /*延時以完成整個時序*/
EA=1;
return(presence); /*返回應答信號,有晶元應答返回0,無晶元則返回1*/
}
/***********************************************************
獲取溫度子程序
***********************************************************/
void get_temper()
{
unsigned char i,j;
do
{
i=reset(); /*復位*/
}while(i!=0); /*1為無反饋信號*/
i=0xcc; /*發送設備定位命令*/
write_byte(i);
i=0x44; /*發送開始轉換命令*/
write_byte(i);
delay(180); /*延時*/
do
{
i=reset(); /*復位*/
}while(i!=0);
i=0xcc; /*設備定位*/
write_byte(i);
i=0xbe; /*讀出緩沖區內容*/
write_byte(i);
j=read_byte();
i=read_byte();
i=(i<<4)&0x7f;
s=(unsigned int)(j&0x0f);
s=(s*100)/16;
j=j>>4;
temper=i|j; /*獲取的溫度放在temper中*/
}
/*====================================================================================================
Initialize PID Structure
=====================================================================================================*/
void PIDInit (struct PID *pp)
{
memset ( pp,0,sizeof(struct PID));
}
/*====================================================================================================
PID計算部分
=====================================================================================================*/
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->SumEror // 積分項
+ pp->Derivative * dError); // 微分項
}
/***********************************************************
溫度比較處理子程序
***********************************************************/
compare_temper()
{
unsigned char i;
if(set_temper>temper)
{
if(set_temper-temper>1)
{
high_time=100;
low_time=0;
}
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/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);
}
}
// else
// {}
}
/*****************************************************
T0中斷服務子程序,用於控制電平的翻轉 ,40us*100=4ms周期
******************************************************/
void serve_T0() interrupt 1 using 1
{
if(++count<=(high_time))
output=1;
else if(count<=100)
{
output=0;
}
else
count=0;
TH0=0x2f;
TL0=0xe0;
}
/*****************************************************
串列口中斷服務程序,用於上位機通訊
******************************************************/
void serve_sio() interrupt 4 using 2
{
/* EA=0;
RI=0;
i=SBUF;
if(i==2)
{
while(RI==0){}
RI=0;
set_temper=SBUF;
SBUF=0x02;
while(TI==0){}
TI=0;
}
else if(i==3)
{
TI=0;
SBUF=temper;
while(TI==0){}
TI=0;
}
EA=1; */
}
void disp_1(unsigned char disp_num1[6])
{
unsigned char n,a,m;
for(n=0;n<6;n++)
{
// k=disp_num1[n];
for(a=0;a<8;a++)
{
clk=0;
m=(disp_num1[n]&1);
disp_num1[n]=disp_num1[n]>>1;
if(m==1)
data1=1;
else
data1=0;
_nop_();
clk=1;
_nop_();
}
}
以前收藏的一個程序,與你分享一下,希望對你有用、
E. 誰有51單片機PID程序,通過光電碼盤控制電機轉速
基本的PID演算法是很簡單的,只要設置好合適的參數,其所能達到的控制效果也還是可以接受的。
不過,在實際應用中,往往是需要在PID演算法的基礎上做一些擴充的,一般不會直接用最基礎的PID演算法。
在很多時候,也可能只用到PI,而沒有D。
F. 求一個51/52單片機pid溫控電機代碼,主要就是利用獲取的溫度和預設溫度來控制電機轉速(pwm)
沒錯啊,pid的核心就算參數,做試驗來定.
G. 完整可用的51單片機PID運算公式
沒意義。PID是有很強的針對性的,我以前看到的幾個網上的PID程序都還是可以用的。但是需要你自己定的,可不只是PID三個數。采樣周期是多少?PID計算後打算用多少位的數據,都是要自己定的,等等。
而所謂溫度控制的程序,直接套用的(參數不套用,自己調),沒有幾個能好用的,要麼計算量太大,要麼調節不夠好。
還是看看公式自己寫的好。就算我寫了一個在我手上好用的,給你,你那也未必好用,因為還要根據實際去修改一些輸出、輸入量,或加以限制,不同的系統,這些都是不同的。就像是汽車底盤都是4個輪子,你要的是越野車,我給你個轎車的底盤,上面再怎麼改也不好用阿!
H. 如何編寫89SDc51單片機控制電阻絲加熱和製冷片製冷的程序最好是PIDg演算法或者是C語言編寫的,謝謝
這個正適合你用,pid溫度控製程序
PID溫度控製程序
//PID演算法溫控C語言
#include<reg51.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 data1=P1^0;
sbit clk=P1^1;
sbit plus=P2^0;
sbit subs=P2^1;
sbit stop=P2^2;
sbit output=P3^4;
sbit DQ=P3^3;
unsigned char flag,flag_1=0;
unsigned char high_time,low_time,count=0;//占空比調節參數
unsigned char set_temper=35;
unsigned char temper;
unsigned char i;
unsigned char j=0;
unsigned int s;
/***********************************************************
延時子程序,延時時間以12M晶振為准,延時時間為30us×time
***********************************************************/
void delay(unsigned char time)
{
unsigned char m,n;
for(n=0;n<time;n++)
for(m=0;m<2;m++){}
}
/***********************************************************
寫一位數據子程序
***********************************************************/
void write_bit(unsigned char bitval)
{
EA=0;
DQ=0; /*拉低DQ以開始一個寫時序*/
if(bitval==1)
{
_nop_();
DQ=1; /*如要寫1,則將匯流排置高*/
}
delay(5); /*延時90us供DA18B20采樣*/
DQ=1; /*釋放DQ匯流排*/
_nop_();
_nop_();
EA=1;
}
/***********************************************************
寫一位元組數據子程序
***********************************************************/
void write_byte(unsigned char val)
{
unsigned char i;
unsigned char temp;
EA=0; /*關中斷*/
TR0=0;
for(i=0;i<8;i++) /*寫一位元組數據,一次寫一位*/
{
temp=val>>i; /*移位操作,將本次要寫的位移到最低位*/
temp=temp&1;
write_bit(temp); /*向匯流排寫該位*/
}
delay(7); /*延時120us後*/
// TR0=1;
EA=1; /*開中斷*/
}
/***********************************************************
讀一位數據子程序
***********************************************************/
unsigned char read_bit()
{
unsigned char i,value_bit;
EA=0;
DQ=0; /*拉低DQ,開始讀時序*/
_nop_();
_nop_();
DQ=1; /*釋放匯流排*/
for(i=0;i<2;i++){}
value_bit=DQ;
EA=1;
return(value_bit);
}
/***********************************************************
讀一位元組數據子程序
***********************************************************/
unsigned char read_byte()
{
unsigned char i,value=0;
EA=0;
for(i=0;i<8;i++)
{
if(read_bit()) /*讀一位元組數據,一個時序中讀一次,並作移位處理*/
value|=0x01<<i;
delay(4); /*延時80us以完成此次都時序,之後再讀下一數據*/
}
EA=1;
return(value);
}
/***********************************************************
復位子程序
***********************************************************/
unsigned char reset()
{
unsigned char presence;
EA=0;
DQ=0; /*拉低DQ匯流排開始復位*/
delay(30); /*保持低電平480us*/
DQ=1; /*釋放匯流排*/
delay(3);
presence=DQ; /*獲取應答信號*/
delay(28); /*延時以完成整個時序*/
EA=1;
return(presence); /*返回應答信號,有晶元應答返回0,無晶元則返回1*/
}
/***********************************************************
獲取溫度子程序
***********************************************************/
void get_temper()
{
unsigned char i,j;
do
{
i=reset(); /*復位*/
}while(i!=0); /*1為無反饋信號*/
i=0xcc; /*發送設備定位命令*/
write_byte(i);
i=0x44; /*發送開始轉換命令*/
write_byte(i);
delay(180); /*延時*/
do
{
i=reset(); /*復位*/
}while(i!=0);
i=0xcc; /*設備定位*/
write_byte(i);
i=0xbe; /*讀出緩沖區內容*/
write_byte(i);
j=read_byte();
i=read_byte();
i=(i<<4)&0x7f;
s=(unsigned int)(j&0x0f);
s=(s*100)/16;
j=j>>4;
temper=i|j; /*獲取的溫度放在temper中*/
}
/*====================================================================================================
Initialize PID Structure
=====================================================================================================*/
void PIDInit (struct PID *pp)
{
memset ( pp,0,sizeof(struct PID));
}
/*====================================================================================================
PID計算部分
=====================================================================================================*/
unsigned int PIDCalc( struct PID *pp, unsigned int NextPoint )
{
unsigned int dError,Error;
Error = pp->SetPoint - NextPoint; // 偏差
pp->SumError += Error; // 積分
dError = pp->LastError - pp->revError; // 當前微分
pp->revError = pp->LastError;
pp->LastError = Error;
return (pp->roportion * Error//比例
+ pp->Integral * pp->SumError //積分項
+ pp->Derivative * dError); // 微分項
}
/***********************************************************
溫度比較處理子程序
***********************************************************/
compare_temper()
{
unsigned char i;
if(set_temper>temper)
{
if(set_temper-temper>1)
{
high_time=100;
low_time=0;
}
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/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);
}
}
// else
// {}
}
/*****************************************************
T0中斷服務子程序,用於控制電平的翻轉 ,40us*100=4ms周期
******************************************************/
void serve_T0() interrupt 1 using 1
{
if(++count<=(high_time))
output=1;
else if(count<=100)
{
output=0;
}
else
count=0;
TH0=0x2f;
TL0=0xe0;
}
/*****************************************************
串列口中斷服務程序,用於上位機通訊
******************************************************/
void serve_sio() interrupt 4 using 2
{
/* EA=0;
RI=0;
i=SBUF;
if(i==2)
{
while(RI==0){}
RI=0;
set_temper=SBUF;
SBUF=0x02;
while(TI==0){}
TI=0;
}
else if(i==3)
{
TI=0;
SBUF=temper;
while(TI==0){}
TI=0;
}
EA=1; */
}
void disp_1(unsigned char disp_num1[6])
{
unsigned char n,a,m;
for(n=0;n<6;n++)
{
// k=disp_num1[n];
for(a=0;a<8;a++)
{
clk=0;
m=(disp_num1[n]&1);
disp_num1[n]=disp_num1[n]>>1;
if(m==1)
data1=1;
else
data1=0;
_nop_();
clk=1;
_nop_();
}
}
}
/*****************************************************
顯示子程序
功能:將占空比溫度轉化為單個字元,顯示占空比和測得到的溫度
******************************************************/
void display()
{
unsigned char code number[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};
unsigned char disp_num[6];
unsigned int k,k1;
k=high_time;
k=k%1000;
k1=k/100;
if(k1==0)
disp_num[0]=0;
else
disp_num[0]=0x60;
k=k%100;
disp_num[1]=number[k/10];
disp_num[2]=number[k%10];
k=temper;
k=k%100;
disp_num[3]=number[k/10];
disp_num[4]=number[k%10]+1;
disp_num[5]=number[s/10];
disp_1(disp_num);
}
/***********************************************************
主程序
***********************************************************/
main()
{
unsigned char z;
unsigned char a,b,flag_2=1,count1=0;
unsigned char phil[]={2,0xce,0x6e,0x60,0x1c,2};
TMOD=0x21;
TH0=0x2f;
TL0=0x40;
SCON=0x50;
PCON=0x00;
TH1=0xfd;
TL1=0xfd;
PS=1;
EA=1;
EX1=0;
ET0=1;
ES=1;
TR0=1;
TR1=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
while(1)
{
if(plus==0)
{
EA=0;
for(a=0;a<5;a++)
for(b=0;b<102;b++){}
if(plus==0)
{
set_temper++;
flag=0;
}
}
else if(subs==0)
{
for(a=0;a<5;a++)
for(b=0;a<102;b++){}
if(subs==0)
{
set_temper--;
flag=0;
}
}
else if(stop==0)
{
for(a=0;a<5;a++)
for(b=0;b<102;b++){}
if(stop==0)
{
flag=0;
break;
}
EA=1;
}
get_temper();
b=temper;
if(flag_2==1)
a=b;
if((abs(a-b))>5)
temper=a;
else
temper=b;
a=temper;
flag_2=0;
if(++count1>30)
{
display();
count1=0;
}
compare_temper();
}
TR0=0;
z=1;
while(1)
{
EA=0;
if(stop==0)
{
for(a=0;a<5;a++)
for(b=0;b<102;b++){}
if(stop==0)
disp_1(phil);
// break;
}
EA=1;
}
}
//DS18b20 子程序
#include <REG52.H>
sbit DQ=P2^1; //定義埠
typedef unsigned char byte;
typedef unsigned int word;
//延時
void delay(word useconds)
{
for(;useconds>0;useconds--);
}
//復位
byte ow_reset(void)
{
byte presence;
DQ=0; //DQ低電平
delay(29); //480us
DQ=1; //DQ高電平
delay(3); //等待
presence=DQ; //presence信號
delay(25);
return(presence);
} //0允許,1禁止
//從1-wire 匯流排上讀取一個位元組
byte read_byte(viod)
{
byte i;
byte value=0;
for (i=8;i>0;i--)
{
value>>=1;
DQ=0;
DQ=1;
delay(1);
if(DQ)value|=0x80;
delay(6);
}
return(value);
}
//向1-wire匯流排上寫一個位元組
void write_byte(char val)
{
byte i;
for (i=8;i>0;i--) //一次寫一個位元組
{
DQ=0;
DQ=val&0x01;
delay(5);
DQ=1;
val=val/2;
}
delay(5);
}
//讀取溫度
char Read_Temperature(void)
{
union{
byte c[2];
int x;
}temp;
ow_reset();
write_byte(0xcc);
write_byte(0xBE);
temp.c[1]=read_byte();
temp.c[0]=read_byte();
ow_reset();
write_byte(0xCC);
write_byte(0x44);
return temp.x/2;
}
I. 51單片機控製程序
pid的原理(你要用單片機那就要用這個原理最好):歸一參數整定法
你自己到網上搜一下歸一參數整定法
J. 51單片機c語言PID運算公式
格式可能不對