版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、PID温度控制程序 /PID 算法温控 C语言 2008-08-17 18:58 #include #include #include #include struct PID unsigned int SetPoint; / unsigned int Proportion; / unsigned int Integral; / unsigned int Derivative; / unsigned int LastError; / Error-1 unsigned int PrevError; / Error-2 unsigned int SumError; / Sums of Errors
2、; struct PID spid; / PID Control Structure unsigned int rout; / PID Response (Output) unsigned int rin; / PID Feedback (Input) sbit data1=P1A0; sbit clk=P1A1; sbit plus=P2A0; sbit subs=P2A1; sbit stop=P2A2; sbit output=P3A4; sbit DQ=P3A3; unsigned char flag,flag_1=0; unsigned char high_time,low_time
3、,count=0;/ unsigned char set_temper=35; unsigned char temper; unsigned char i; unsigned char j=0; unsigned int s; /* 延时子程序,延时时间以12M晶振为准,延时时间为30usxtime * 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) ( 设定目标 De
4、sired Value Proportional Const 积分常数 Integral Const 微分常数 Derivative Const 占空比调节参数 EA=0; DQ=0; /*拉低DQ以开始一个写时序*/ if(bitval=1) ( _nop_(); DQ=1; /*如要写1,则将总线置高*/ delay(5); /* 延时 90us 供 DA18B2探样*/ DQ=1; /*释放DQ总线*/ _nop_(); _nop_(); EA=1; /* 写一字节数据子程序 */ void write_byte(unsigned char val) ( unsigned char i;
5、 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,value_bit; EA=0; DQ=0; /*拉低DQ开始读时序*/ _nop_(); _nop_(); DQ=1; /*释放总线*/
6、 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|=0 x01i; delay(4); /* 延时80us以完成此次都时序,之后再读下一数据 */EA=1; return(value); /* 复位子程序 */ unsigned char reset() ( uns
7、igned 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
8、为无反馈信号*/ i=0 xcc; /* 发送设备定位命令*/ write_byte(i); i=0 x44; /*发送开始转换命令*/ write_byte(i); delay(180); /* 延时 */ do ( i=reset(); /* 复位 */ while(i!=0); i=0 xcc; /* 设备定位*/ write_byte(i); i=0 xbe; /* 读出缓冲区内容*/ write_byte(i); j=read_byte(); i=read_byte(); i=(i4; temper=i|j; /* 获取的温度放在temper中*/ /*= Initialize PI
9、D 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-P
10、revError = 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; else ( for(i=0;i1
11、0;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;
13、 else if(count=100) ( output=0; else count=0; TH0=0 x2f; TL0=0 xe0; * 申行口中断服务程序,用于上位机通讯 */ 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=0 x02; while(TI=0)( TI=0; else if(i=3) ( TI=0; SBUF=temper; while(TI=0)( TI=0; EA=1; */ void disp
14、_1(unsigned char disp_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=0 xfc,0 x60,0 xda,0 xf2,0 x66,0 xb6,0 xbe,0 xe0,0 xfe,0 xf6; uns
15、igned char disp_num6; unsigned int k,k1; k=high_time; k=k%1000; k1=k/100; if(k1=0) disp_num0=0; else disp_num0=0 x60; 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() ( unsig
16、ned char z; unsigned char a,b,flag_2=1,count1=0; unsigned char phil=2,0 xce,0 x6e,0 x60,0 x1c,2; TMOD=0 x21; TH0=0 x2f; TL0=0 x40; SCON=0 x50; PCON=0 x00; TH1=0 xfd; TL1=0 xfd; 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;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-;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 煤粉工持续改进强化考核试卷含答案
- 6.5频数直方图 教案(浙教版七年级数学下册)
- 《流淌的情感》教案-2025-2026学年苏少版(新教材)小学美术三年级下册
- 2025年竞争情报分析报告
- 2025年内蒙古自治区公需课学习-绿色债券发行指引详解1311
- 2026年潜水摄影大赛征稿
- 2026年寒潮低温防护试题及答案
- 2026年大学大四(汽车检测与维修技术)汽车底盘异响排查综合测试题及答案
- 2026年法院聘用制书记员招聘笔试试题(含答案)
- 内镜清洗消毒环境要求
- 建筑材料美学:塑造建筑美感的关键因素
- 【千禾味业公司内部控制现状问题及优化的案例分析7100字(论文)】
- 烟草购销员(五级)技能理论考试题库(浓缩500题)
- 大学生台球俱乐部计划书
- GB/T 13750-2023振动沉拔桩机安全操作规程
- 建设工程前期工作咨询费收费计算表
- 土地开发整理项目预算编制课件
- 兰亭集序 公开课比赛一等奖
- 中国政治思想史考试重点
- 初中生物-绿色植物的呼吸作用教学设计学情分析教材分析课后反思
- 收入专项审计报告收入专项审计报告八篇
评论
0/150
提交评论