温度控制PID系统中,继电器不工作,电路图和程序如下,谢谢啦~
2019-07-14 18:44发布
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
#include#include #include #include #define uchar unsigned char #define uint unsigned intsbit DATA = P1^1; //DS18B20接入口uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//共阴极字型码int temp; //温度值int ss; //中间的一个变量int dd;int j;uchar data b;//定时器中断次数uchar data buf[4];//字型显示中间变量int alARMH=400; //默认报警值int alarmL=-500;//定义开关的接入口sbit k1=P2^5;//+sbit k2=P2^6;//-sbit k3=P2^7;//确认sbit bell=P1^0;
sbit Red=P1^6; //nousesbit output=P3^5;unsigned int s;unsigned char high_time,low_time,count=0;bit set=0;bit Flag=0;int n;unsigned char set_temp;struct PID {unsigned int SetPoint; // 设定目标 Desired Valueunsigned int Proportion; // 比例常数 Proportional Constunsigned int Integral; // 积分常数 Integral Constunsigned int Derivative; // 微分常数 Derivative Constunsigned int LastError; // Error[-1]unsigned int PrevError; // Error[-2]unsigned int SumError; // Sums of Errors};struct PID spid; // the control PIDunsigned int rout;//the reaction of PIDunsigned int rin;//the feedback of PID//函数的声明区void key_to1(); //键盘++void key_to2(); //键盘--void delay(uint); //延时void key(); //按键扫描子程序void Show(); //显示//函数的定义区/*延时子函数*/void delay(uint num){ while(num--) ;
void Show(){ if(temp>=0) display();//显示函数 if(temp<0) display00();}void PIDInit(struct PID *pp) //jie gou bian liang chu shi hua { memset(pp,0,sizeof(struct PID));}
unsigned int PIDCalc( struct PID *pp, unsigned int NextPoint ){ unsigned int dError,Error; Error=pp->SetPoint-NextPoint; // 偏差EK,设定值减去当前采样值 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(){ unsigned char i; if(set_temp>temp) { if(set_temp-temp>1) { high_time=100; low_time=0; } else { for(i=0;i<10;i++) { int ReadTemperature(); rin=s; rout=PIDCalc(&spid,rin); } if( high_time<=100) high_time=(unsigned char)(rout/800); else high_time=100; low_time=(100-high_time); } } else if(set_temp<=temp) { if(temp-set_temp>1) { high_time=0; low_time=100; } else { for(i=0;i<10;i++) { int ReadTemperature(); rin=s; rout=PIDCalc( &spid, rin ); } if(high_time<100) high_time=(unsigned char) (rout/10000); else high_time=0; low_time=(100-high_time); } } }
void main(){ TCON=0x01; TMOD=0X01; TH0=(65536-5000)/256; TL0=(65536-5000)%256; EA=1; ET0=1; TR0=1; EX0=1; for(n=0;n<500;n++)//显示启动LOGo"- - - -" {bell=0;logo();} Red=0; while(1) { key(); ss=ReadTemperature(); Show(); alarm(); if(Flag==1) {bell=!bell;} //蜂鸣器滴滴响 else {bell=0;} } }void sever_T1() interrupt 1 using 1 //control the turn of electict{ if(++count<=(high_time)) output=1; else if(count<=100) { output=0; } else count=0; TH0=0X2f; TL0=0Xe0;}
void time0(void) interrupt 1 using 1 //每隔10ms执行一次此子程序 { TH0=(65536-5000)/256; TL0=(65536-5000)%256; temp=ss; }
-
-
恒温燃气热水器硬件设计.PDF
下载积分: 积分 -1 分
143.3 KB, 下载次数: 14, 下载积分: 积分 -1 分
一周热门 更多>