温度控制的PID算法的C语言程序(共38页).doc
《温度控制的PID算法的C语言程序(共38页).doc》由会员分享,可在线阅读,更多相关《温度控制的PID算法的C语言程序(共38页).doc(38页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上我的题目是:基于PID算法的温度控制系统 89C51单片机,通过键盘输入预设值,与DS18B20测得的实际值做比较,然后驱动制冷或加热电路。用keil C语言来实现PID的控制。 最佳答案/PID算法温控C语言2008-08-17 18:58#include #include #include #include struct PID unsigned int SetPoint; / 设定目标 Desired Value unsigned int Proportion; / 比例常数 Proportional Const unsigned int Integral; /
2、 积分常数 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 (In
3、put) sbit data1=P10; sbit clk=P11; sbit plus=P20; sbit subs=P21; sbit stop=P22; sbit output=P34; sbit DQ=P33; 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; /* 延时子程序
4、,延时时间以12M晶振为准,延时时间为30ustime */ void delay(unsigned char time) unsigned char m,n; for(n=0;ntime;n+) for(m=0;m2;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
5、_(); EA=1; /* 写一字节数据子程序 */ void write_byte(unsigned char val) unsigned char i; unsigned char temp; EA=0; /*关中断*/TR0=0; for(i=0;ii; /*移位操作,将本次要写的位移到最低位*/ temp=temp&1; write_bit(temp); /*向总线写该位*/ delay(7); /*延时120us后*/ / TR0=1; EA=1; /*开中断*/ /* 读一位数据子程序 */ unsigned char read_bit() unsigned char i,valu
6、e_bit; EA=0; DQ=0; /*拉低DQ,开始读时序*/ _nop_(); _nop_(); DQ=1; /*释放总线*/ for(i=0;i2;i+) value_bit=DQ; EA=1; return(value_bit); /* 读一字节数据子程序 */ unsigned char read_byte() unsigned char i,value=0; EA=0; for(i=0;i8;i+) if(read_bit() /*读一字节数据,一个时序中读一次,并作移位处理*/ value|=0x01i; delay(4); /*延时80us以完成此次都时序,之后再读下一数据*
7、/ 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
8、 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=(i4; temper=i|j; /*
9、获取的温度放在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-Las
10、tError - pp-PrevError; / 当前微分 pp-PrevError = pp-LastError; pp-LastError = Error; return (pp-Proportion * Error/比例+ pp-Integral * pp-SumError /积分项+ pp-Derivative * dError); / 微分项 /* 温度比较处理子程序 */ compare_temper() unsigned char i; if(set_tempertemper) if(set_temper-temper1) high_time=100; low_time=0; e
11、lse for(i=0;i10;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_temper0) high_time=0; low_time=100; else for(i=0;i10;i+) get_temper(); rin
12、 = s; / Read Input rout = PIDCalc ( &spid,rin ); / Perform PID Interation if (high_time100) 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
13、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 d
14、isp_num16) unsigned char n,a,m; for(n=0;n6;n+) / k=disp_num1n; for(a=0;a1; 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_num6; unsigned i
15、nt k,k1; k=high_time; k=k%1000; k1=k/100; if(k1=0) disp_num0=0; else disp_num0=0x60; k=k%100; disp_num1=numberk/10; disp_num2=numberk%10; k=temper; k=k%100; disp_num3=numberk/10; disp_num4=numberk%10+1; disp_num5=numbers/10; disp_1(disp_num); /* 主程序 */ main() unsigned char z; unsigned char a,b,flag_
16、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.Inte
17、gral = 8; spid.Derivative =6; spid.SetPoint = 100; / Set PID Setpoint while(1) if(plus=0) EA=0; for(a=0;a5;a+) for(b=0;b102;b+) if(plus=0) set_temper+; flag=0; else if(subs=0) for(a=0;a5;a+) for(b=0;a102;b+) if(subs=0) set_temper-; flag=0; else if(stop=0) for(a=0;a5;a+) for(b=0;b5) temper=a; else te
18、mper=b; a=temper; flag_2=0; if(+count130) display(); count1=0; compare_temper(); TR0=0; z=1; while(1) EA=0; if(stop=0) for(a=0;a5;a+) for(b=0;b102;b+) if(stop=0) disp_1(phil); / break; EA=1; /DS18b20 子程序#include sbit DQ=P21; /定义端口typedef unsigned char byte;typedef unsigned int word; /延时void delay(wo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 温度 控制 PID 算法 语言 程序 38
限制150内