⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 text2.c

📁 温度测控在日常生活、工业生产工程各领域均具有广阔的应用前景。目前我国各类实际温度控制系统 中主要以传统控制方式为主
💻 C
字号:

#include<REG52.H>
//#include<STDIO.H>
//#include<ABSACC.H>
#include <string.h>
//#include<intrins.h>
#define    OSC    20000000 
#define BAUDRATE    9600
//#define BUFSIZE    32
#define KP 3 //比例系数 
#define KIP 3 //积分系数
#define KD 30 //微分系数
//#define KCP 10 //功率校正系数
//#define T_c 16 //采样周期(单位:秒)
unsigned char tem[9];
char tem1,tem2,temz,e,settem;
unsigned char coms[32];//串口接受缓冲
//unsigned char xdata comf[32];//串口发送缓冲
unsigned  char murt,pidtime=0,ledy=0,ledw=0,q,b,time1,ok=0,pidyn=1;
char  xdata diff[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int sum_diff=0;
char curr_=0,pwmj=0;
char alarm,fenshang	 ;
sbit pid_port=P2^0; //控制输出端口
//float settem=0; //目标温度
//float temz=0; //当前温度
int PWM=0; //输出控制量

//unsigned char *strin=coms; //串口指针
unsigned char xdata LED7Code[]={
	0xEB,		// 0
	0x28,		// 1
	0xB3,		// 2
	0xBA,		// 3
	0x78,		// 4
	0xDA,		// 5
	0xDB,		// 6
	0xA8,		// 7
	0xFB,		// 8
	0xFA,		// 9
	0xF9,		// A
	0x5B,		// B
	0xC3,		// C
	0x3B,		// D
	0xD3,		// E
	0xD1,		// F
	0x10 , //-  16
	0x80,//~	 17
	0x02,//_	   18
	0x12, //	= 19
	0x82//
};
sbit DQ =P1^2; 
unsigned char led4[4]={0,0,0,0};//数码管内容
unsigned char stri=0,wstr=0; //串口数据位 ,记录标志
sbit P20=P2^0; //控制输出端口
sbit P21=P2^1;//降温控制信号
sbit P22=P2^2;//报警信号
sbit P23=P2^3;
sbit P24=P2^4;
sbit P25=P2^5;
sbit P26=P2^6;
//数码管位定义
sbit P15=P1^5;
sbit P16=P1^6;
sbit P17=P1^7;
void twoto10 (char x);

void pid(void)
{

int p_out,d_out,temp;
int i_out;
int pwm_0;


temp=diff[curr_];
if(curr_>=19)curr_=0;
else curr_++;

sum_diff-=diff[curr_];
diff[curr_]=settem-temz;
sum_diff+=diff[curr_];
p_out=3*diff[curr_]; //比例项输出

i_out=sum_diff/3; //积分项输出
d_out=20*(diff[curr_]-temp); //微分项输出
pwm_0=pwmj; //维持功率项
if(i_out>100) i_out=100; //积分分离
if(i_out<-100) i_out=-100;
PWM=p_out+i_out+d_out+pwm_0; //总输出量
if(PWM<0) PWM=0;
else if(PWM>100) PWM=100;
}
// 输出函数
void PWM_OUT(int PWMw)
{
static unsigned char t=1; //t=(1--100)周期为4秒
unsigned char limit; //pid_value输出百分比
limit=(unsigned char)PWMw;
if(t<=limit)pid_port=1; //加热
else pid_port=0; //停止加热
t++;

if(t>100)t=1;

}



void send_char_com(unsigned char ch)
{
    SBUF = ch;
    while(TI==0);
    TI = 0;
}


void send_string_com(unsigned char *string)
{
    int i;
    for(i=0; i<strlen(string); i++)
    {
    send_char_com(string[i]);
//	comf[i]=string[i];
    }
//	comf[i]='\0';
}
void ledoutput()
{//ledy++;
//if(ledy==1)
//{
P2=P2&0x07;
P0=LED7Code[led4[ledw]];
	switch(ledw)
	{
		case 0: P24=1;break;
		case 1: P25=1;break;
		case 2: P26=1;break;
		case 3: P23=1;break;
	}
if(ledw>3)
ledw=0;
else
ledw++;
//ledy=0;
//}

}
/*void delay() 
{
unsigned char m,n,s; 
for(m=20;m>0;m--) 
	for(n=20;n>0;n--) 
 		for(s=20;s>0;s--); 
} 
*/


void int1() interrupt 2 using 1
{ 	b=1;
	time1=0;
	led4[0]=14;
while(P15==1)
	
	{

	if(b==1)
	{
		if(P16==0) 
		{	if(settem<125&&settem>=-55)
			settem++;
			b=0;
			time1=0;
		}
		  if(P17==0) 
		{  if(settem<=125&&settem>-55)
			settem--;
			b=0;
			time1=0;
	 	}
	
	}
	
	if(time1>=7)
	{ 
		b=1;
		time1=0;
	}

twoto10(settem);
		ledy++;
		if (ledy>=80)
		{ledoutput()	;
		 ledy=0;
		}
	}
 led4[0]=0;
}
void ComISR(void) interrupt 4 using 2
{
	if(RI)
	{
	RI=0;
		if(SBUF =='$'&&wstr==0)
		{
		stri=0;
		wstr=1;
		}
		if(wstr==1)
		{
		coms[stri++]=SBUF;
			if(SBUF =='~')
			{wstr=0;
			coms[stri]='\0';
			if((!memcmp(coms, "$S+", 3))&& coms[4]=='~') 
				{settem=coms[3];
				ok=1;
				memset(coms,0,32);	
				}
			if(!memcmp(coms, "$HERE", 5)&&coms[7]=='~') 
				{murt=1;
				pidyn=coms[6];
				if (pidyn==0)
				PWM=coms[5];
				memset(coms,0,32);
				}
		/*	if(!memcmp(coms, "$PID", 4)&&coms[5]=="~") 
			{pidyn=coms[4];
			memset(coms,0,32);
			}		
		   */
			}
		}
	
	}
  
}


//初始化函数
Init_DS18B20(void)
{
 unsigned char x=0;
 DQ = 1;    //DQ复位
 for(b=3;b>0;b--);
 DQ = 0;    //单片机将DQ拉低
 for(b=208;b>0;b--);
 for(b=208;b>0;b--);
 DQ = 1;    //拉高总线
 for(b=83;b>0;b--);

 x=DQ;      //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
 for(b=120;b>0;b--);
}

//读一个字节
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
 {
  DQ = 0; // 给脉冲信号
  dat>>=1;
  DQ = 1; 
  for(b=3;b>0;b--);
  if(DQ==1)
   dat|=0x80;
  for(b=30;b>0;b--);
 }
 return(dat);
}

//写一个字节
WriteOneChar(unsigned char dat)
{
 unsigned char i=0;
 for (i=8; i>0; i--)
 {
  DQ = 0;
 for(b=2;b>0;b--);
  DQ = dat&0x01;
for(b=38;b>0;b--);
  DQ = 1;
  dat>>=1;
 }
}

//读取温度
ReadTemperature(void)
{

//unsigned char i;
//float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
//for(i=8;i>=0;i++)
tem[8]=ReadOneChar();
tem[7]=ReadOneChar();




//t=b;
//t<<=8;
//t=t|a;
//tt=t*0.0625;        //将温度的高位与低位合并
//t= tt*10+0.5;       //对结果进行4舍5入
}

void twoto10 (unsigned char x)
{  
if (x>127)
{led4[1]=16;
x= ~(x-1);
}
else
led4[1]=x/100;
led4[2]=(x/10)%10;
led4[3]= x%10;
}

void t0() interrupt 1 using 0
{ 

ReadTemperature();
tem1=tem[8]>>4;
tem2=tem[7]<<4;
temz=tem1|tem2;
if (pidyn==1)
{
pid();
if(pidtime>=100)
{ 

if(tem[6]>=temz)
pwmj++;
tem[6]=temz;
pidtime=0;
 
}

}

if (temz>settem)
{pwmj=0;
}


PWM_OUT(PWM) ;
q++;
time1++;  
pidtime++;
}
void main()
{
//unsigned char ii; 
PT0=1;
TMOD=0x01;
TH0=0x00;
TL0=0x00;

EA=1;
ET0=1;
EX1=1;
ES=1;

TR0=1;
IT1=1;

SCON    = 0x50;                    // 串口模式1,允许接收
PCON    = 0x80;
T2CON    = 0x30;                    // RCLK=TCLK=1
RCAP2H    = (65536 - OSC/32/BAUDRATE)/256;
RCAP2L    = (65536 - OSC/32/BAUDRATE)%256;
TH2    = (65536 - OSC/32/BAUDRATE)/256;
TL2    = (65536 - OSC/32/BAUDRATE)%256;

TR2    = 1;    
TI = 0;
RI = 0;


while(1)
{
ledy++;
if (ledy>=70)
{ledoutput();
 ledy=0;
 }
 if (ok==1)
 { 
 send_char_com(0x24);
 send_string_com("OK~")	;
 ok=0;
 }
if(murt==1)
{
send_char_com(0x54);
send_char_com(0x01);
send_char_com(tem[7]);
send_char_com(tem[8]);

send_char_com(0x7c);
send_char_com(settem);
send_char_com(0x7e);
murt=0;

}

/*if(temz<=128)		 //e(t)=设置温度-检测温度
tem2=settem+(~temz)+1	;	//tem>=0 [-temz]补=~temz+1
else
tem2=settem+tem1;	//tem<0	   [-temz]补=~(tem1-1)
*/
//e=settem-temz ;

if(q<60)
{
twoto10(temz);
//if (e==0)
led4[0]=0;
//else
//	if (e>0)
	//led4[0]=18;
//	else
//	led4[0]=17;
}
else
{
led4[0]=12;
twoto10(PWM)	;
}
if(q>120)
q=0;
if (temz>settem+10)
{P22=1;	}
else
{
P22=0;
}
if (temz>settem+5)
{
P21=1;
}
else
{
P21=0; 
}

}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -