当前位置:首页 > 单片机 > 单片机
[导读]此系统是基于PID的单片机温度控制系统,我在网上搜到一段完整的程序,并按他的程序做出了相应的proteus硬件仿真,但是并没有达到预期的效果.待提高。程序如下:#include<reg51.h>#include<intrins.h>#include<mat

此系统是基于PID的单片机温度控制系统,我在网上搜到一段完整的程序,并按他的程序做出了相应的proteus硬件仿真,但是并没有达到预期的效果.待提高。程序如下:#include<reg51.h>#include<intrins.h>#include<math.h>#include<string.h>structPID{unsignedintSetPoint;//设定目标DesiredValueunsignedintProportion;//比例常数ProportionalConstunsignedintIntegral;//积分常数IntegralConstunsignedintDerivative;//微分常数DerivativeConstunsignedintLastError;//Error[-1]unsignedintPrevError;//Error[-2]unsignedintSumError;//SumsofErrors};structPIDspid;//PIDControlStructureunsignedintrout;//PIDResponse(Output)unsignedintrin;//PIDFeedback(Input)sbitdata1=P1^0;sbitclk=P1^1;sbitplus=P2^0;sbitsubs=P2^1;sbitstop=P2^2;sbitoutput=P3^4;sbitDQ=P3^3;unsignedcharflag,flag_1=0;unsignedcharhigh_time,low_time,count=0;//占空比调节参数unsignedcharset_temper=35;unsignedchartemper;unsignedchari;unsignedcharj=0;unsignedints;/***********************************************************延时子程序,延时时间以12M晶振为准,延时时间为30us×time***********************************************************/voiddelay(unsignedchartime){unsignedcharm,n;for(n=0;n<time;n++)for(m=0;m<2;m++){}}/***********************************************************写一位数据子程序***********************************************************/voidwrite_bit(unsignedcharbitval){EA=0;DQ=0;/*拉低DQ以开始一个写时序*/if(bitval==1){_nop_();DQ=1;/*如要写1,则将总线置高*/}delay(5);/*延时90us供DA18B20采样*/DQ=1;/*释放DQ总线*/_nop_();_nop_();EA=1;}/***********************************************************写一字节数据子程序***********************************************************/voidwrite_byte(unsignedcharval){unsignedchari;unsignedchartemp;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;}/***********************************************************读一位数据子程序***********************************************************/unsignedcharread_bit(){unsignedchari,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);}/***********************************************************读一字节数据子程序***********************************************************/unsignedcharread_byte(){unsignedchari,value=0;EA=0;for(i=0;i<8;i++){if(read_bit())/*读一字节数据,一个时序中读一次,并作移位处理*/value|=0x01<<i;delay(4);/*延时80us以完成此次都时序,之后再读下一数据*/}EA=1;return(value);}/***********************************************************复位子程序***********************************************************/unsignedcharreset(){unsignedcharpresence;EA=0;DQ=0;/*拉低DQ总线开始复位*/delay(30);/*保持低电平480us*/DQ=1;/*释放总线*/delay(3);presence=DQ;/*获取应答信号*/delay(28);/*延时以完成整个时序*/EA=1;return(presence);/*返回应答信号,有芯片应答返回0,无芯片则返回1*/}/***********************************************************获取温度子程序***********************************************************/voidget_temper(){unsignedchari,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=(unsignedint)(j&0x0f);s=(s*100)/16;j=j>>4;temper=i|j;/*获取的温度放在temper中*/}/*====================================================================================================InitializePIDStructure=====================================================================================================*/voidPIDInit(structPID*pp){memset(pp,0,sizeof(structPID));}/*====================================================================================================PID计算部分=====================================================================================================*/unsignedintPIDCalc(structPID*pp,unsignedintNextPoint){unsignedintdError,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);//微分项}/***********************************************************温度比较处理子程序***********************************************************/compare_temper(){unsignedchari;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;//ReadInputrout=PIDCalc(&spid,rin);//PerformPIDInteration}if(high_time<=100){high_time=(unsignedchar)(rout/800);}else{high_time=100;}low_time=(100-high_time);}}elseif(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;//ReadInputrout=PIDCalc(&spid,rin);//PerformPIDInteration}if(high_time<100){high_time=(unsignedchar)(rout/10000);}else{high_time=0;}low_time=(100-high_time);}}//else//{}}/*****************************************************T0中断服务子程序,用于控制电平的翻转,40us*100=4ms周期******************************************************/voidserve_T0()interrupt1using1{if(++count<=(high_time))output=1;elseif(count<=100){output=0;}elsecount=0;TH0=0x2f;TL0=0xe0;}/*****************************************************串行口中断服务程序,用于上位机通讯******************************************************/voidserve_sio()interrupt4using2{/*EA=0;RI=0;i=SBUF;if(i==2){while(RI==0){}RI=0;set_temper=SBUF;SBUF=0x02;while(TI==0){}TI=0;}elseif(i==3){TI=0;SBUF=temper;while(TI==0){}TI=0;}EA=1;*/}voiddisp_1(unsignedchardisp_num1[6]){unsignedcharn,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;elsedata1=0;_nop_();clk=1;_nop_();}}}/*****************************************************显示子程序功能:将占空比温度转化为单个字符,显示占空比和测得到的温度******************************************************/voiddisplay(){unsignedcharcodenumber[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};unsignedchardisp_num[6];unsignedintk,k1;k=high_time;k=k%1000;k1=k/100;if(k1==0)disp_num[0]=0;elsedisp_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(){unsignedcharz;unsignedchara,b,flag_2=1,count1=0;unsignedcharphil[]={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);//InitializeStructurespid.Proportion=10;//SetPIDCoefficientsspid.Integral=8;spid.Derivative=6;spid.SetPoint=100;//SetPIDSetpointwhile(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;}}elseif(subs==0){for(a=0;a<5;a++)for(b=0;a<102;b++){}if(subs==0){set_temper--;flag=0;}}elseif(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;}}


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

单片机内部有很多的特殊功能寄存器,每个寄存器在单片机内部都分配有唯一的地址,一般我们会根据寄存器功能的不同给寄存器赋予各自的名称,当我们需要在程序中操作这些特殊功能寄存器时,必须要在程序的最前面将这些名称加以声明,声明的...

关键字: C51 数据类型 扩充定义

数据元(Data Element),也称为数据元素,是用一组属性描述其定义、标识、表示和允许值的数据单元,在一定语境下,通常用于构建一个语义正确、独立且无歧义的特定概念语义的信息单元。数据元可以理解为数据的基本单元,将若...

关键字: C51 数据类型

▼点击下方名片,关注公众号▼欢迎关注【玩转单片机与嵌入式】公众号,回复关键字获取更多免费资料。回复【加群】,限时免费进入知识共享群;回复【3D封装库】,常用元器件的3D封装库;回复【电容】,获取电容、元器件选型相关的内容...

关键字: C51 MDK RealView

在Keil C51软件中51单片机的中断服务和外设驱动程序的开发

关键字: keil5 编译 C51

Intel公司1980年推出了MCS-51系列单片机:集成 8位CPU、4K字节ROM、128字节RAM、4个8位并口、1个全双工串行口、2个16位定时/计数器。寻址范围64K,并有控制功能较强的布尔处理器。 80C5...

关键字: C51 KEIL 编程

c上标3下标5怎么算用计算机,c上标3下标5怎么算

关键字: C51 KEIL

▼点击下方名片,关注公众号▼大家好,很高兴和各位一起分享我的第16篇原创文章,喜欢和支持我的工程师,一定记得给我点赞、收藏、分享。加微信[xyzn3333]与作者沟通交流,免费获取更多单片机与嵌入式的海量电子资料。很多初...

关键字: 51单片机 C51

常看见初学者要求使用_at_,这是一种谬误,把C当作ASM看待了。在C中变量的定位是编译器的事情,初学者只要定义变量和变量的作 用域,编译器就把一个固定地址给这个变量。

关键字: C51 单片机 误区 注意事项

简介:编程首要是要考虑程序的可行性,然后是可读性、可移植性、健壮性以及可测试性。这是总则。但是很多人忽略了可读性、可移植性和健壮性(可调试的方法可能歌不相同),这是不对的。

关键字: C51 编程规范 文件配置

如果你用 Keil C51 进行编译,记住一点:它不区分大小写!!!卧槽,今天编程序那个调错啊,就因为一个数组名和一个变量名完全一样,只是大小写不一样罢了,标准 C 我怎么记得这样可以啊……上网一查,卧槽,Keil C5...

关键字: C51 单片机 编程要点
关闭
关闭