版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上经典PID调节,恒温控制。2009-08-11 20:37#include<reg51.h>#include<intrins.h>#include<math.h>#include<string.h>struct PID unsigned int SetPoint; / 设定目标 Desired Valueunsigned int Proportion; / 比例常数 Proportional Constunsigned int Integral; / 积分常数 Integral Constunsigned int Der
2、ivative; / 微分常数 Derivative Constunsigned int LastError; / Error-1unsigned int PrevError; / Error-2unsigned int SumError; / Sums of Errors;struct PID spid; / PID Control Structureunsigned int rout; / PID Response (Output)unsigned int rin; / PID Feedback (Input)sbit data1=P10;sbit clk=P11;sbit plus=P2
3、0;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;/*延时子程序,延时时间以12M晶振为准,延时时间为30us×time*/void delay(unsigned cha
4、r 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 c
5、har 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,开始读时序*/_no
6、p_();_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);/*复位子程序*
7、/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
8、); /*1为无反馈信号*/i=0xcc; /*发送设备定位命令*/write_byte(i);i=0x44; /*发送开始转换命令*/write_byte(i);delay(180); /*延时*/doi=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>&
9、gt;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; / 积分dE
10、rror = 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(
11、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<
12、=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 / /*
13、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 d
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026全国春季高考英语学业考试总复习:阅读理解(知识梳理+考点讲义)原卷版
- 2026年广东高考数学总复习:统计与概率(知识梳理+考点讲义)原卷版
- 2026年外研版三年级英语下册Unit 5 What time is it?(教学设计)
- 2026江苏春季高考物理考试总复习:匀变速直线运动的规律(知识梳理+考点讲义)原卷版
- 医学流行病学答辩档案管理教学课件
- 医学慢阻肺合并肺栓塞案例教学课件
- 《JBT 6188.5-1992 16mm 槽系组合夹具紧固件 过渡螺栓》(2026年)实施指南
- 《JBT 6037-1992 工程机械 电线和电缆的识别标志通则》(2026年)实施指南
- 门窗五金配件制作工班组管理模拟考核试卷含答案
- 耐蚀塑料工岗前客户关系管理考核试卷含答案
- 幼儿园讲故事小鸭子找朋友
- 肿瘤科专业组药物临床试验管理制度及操作规程GCP
- 眼眶病眼眶肿瘤七制讲课4
- 2023年小升初英数题附答案
- GB/T 34940.2-2017静态切换系统(STS)第2部分:电磁兼容性(EMC)要求
- GB/T 21198.4-2007贵金属合金首饰中贵金属含量的测定ICP光谱法第4部分:999‰贵金属合金首饰贵金属含量的测定差减法
- GB/T 21143-2014金属材料准静态断裂韧度的统一试验方法
- 第六章分子的结构与性质
- 大学英语-My Stroke of Luck优秀课件
- 第14章-裁剪《创新设计-TRIZ系统化创新教程》教学课件
- 高三语文现代文阅读《微纪元》课件29张
评论
0/150
提交评论