
//51 温度PID经典算法
#include<reg51.h>
#include<intrins.h>
#include<math.h>
#include<string.h>
struct PID {
unsigned int SetPoint// 设定目标 Desired Value
unsigned int Proportion// 比例常数 Proportional Const
unsigned int Integral// 积分常数 Integral Const
unsigned int Derivative// 微分常数 Derivative Const
unsigned int LastError// Error[-1]
unsigned int PrevError// Error[-2]
unsigned int SumError// Sums of Errors
}
struct PID spid// PID Control Structure
unsigned int rout// PID Response (Output)
unsigned int rin// PID Feedback (Input)
sbit data1=P1^0
sbit clk=P1^1
sbit plus=P2^0
sbit subs=P2^1
sbit stop=P2^2
sbit output=P3^4
sbit DQ=P3^3
unsigned char flag,flag_1=0
unsigned char high_time,low_time,count=0//占空比调节参数
unsigned char set_temper=25
unsigned char temper
unsigned char i
unsigned char j=0
unsigned int s
/***********************************************************
延时子程序,延时时间以12M晶振为准,延时时间为30us×time
***********************************************************/
void delay(unsigned char time)
{
unsigned char m,n
for(n=0n<timen++)
for(m=0m<2m++){}
}
/***********************************************************
写一位数据子程序
***********************************************************/
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 char val)
{
unsigned char i
unsigned char temp
EA=0
TR0=0
for(i=0i<8i++) /*写一字节数据,一次写一位*/
{
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,开始读时序*/
_nop_()
_nop_()
DQ=1/*释放总线*/
for(i=0i<2i++){}
value_bit=DQ
EA=1
return(value_bit)
}
/***********************************************************
读一字节数据子程序
***********************************************************/
unsigned char read_byte()
{
unsigned char i,value=0
EA=0
for(i=0i<8i++)
{
if(read_bit()) /*读一字节数据,一个时序中读一次,并作移位处理*/
value|=0x01<<i
delay(4)/*延时80us以完成此次都时序,之后再读下一数据*/
}
EA=1
return(value)
}
/***********************************************************
复位子程序
***********************************************************/
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)/*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=(unsigned int)(j&0x0f)//得到小数部分
s=(s*100)/16
j=j>>4
temper=i|j/*获取的温度放在temper中*/
}
/*====================================================================================================
Initialize PID Structure
=====================================================================================================*/
void PIDInit (struct PID *pp)
{
memset ( pp,0,sizeof(struct PID)) //全部初始化为0
}
/*====================================================================================================
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->PrevError = pp->LastError
pp->LastError = Error
return (pp->Proportion * Error // 比例项
+ pp->Integral * pp->SumError // 积分项
+ pp->Derivative * dError) // 微分项
}
/***********************************************************
温度比较处理子程序
***********************************************************/
void compare_temper()
{
unsigned char i
if(set_temper>temper) //是否设置的温度大于实际温度
{
if(set_temper-temper>1) //设置的温度比实际的温度是否是大于1度
{
high_time=100 //如果是,则全速加热
low_time=0
}
else //如果是在1度范围内,则运行PID计算
{
for(i=0i<10i++)
{
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<=temper)
{
if(temper-set_temper>0)
{
high_time=0
low_time=100
}
else
{
for(i=0i<10i++)
{
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
// {}
}
/*****************************************************
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 disp_num1[6])
{
unsigned char n,a,m
for(n=0n<6n++)
{
// k=disp_num1[n]
for(a=0a<8a++)
{
clk=0
m=(disp_num1[n]&1)
disp_num1[n]=disp_num1[n]>>1
if(m==1)
data1=1
else
data1=0
_nop_()
clk=1
_nop_()
}
}
}
/*****************************************************
显示子程序
功能:将占空比温度转化为单个字符,显示占空比和测得到的温度
******************************************************/
void display()
{
unsigned char code number[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6}
unsigned char disp_num[6]
unsigned int k,k1
k=high_time
k=k%1000
k1=k/100
if(k1==0)
disp_num[0]=0
else
disp_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)
}
/***********************************************************
主程序
***********************************************************/
void main()
{
unsigned char z
unsigned char a,b,flag_2=1,count1=0
unsigned char phil[]={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 ) // Initialize Structure
spid.Proportion = 10// Set PID Coefficients 比例常数 Proportional Const
spid.Integral = 8 //积分常数 Integral Const
spid.Derivative =6 //微分常数 Derivative Const
spid.SetPoint = 100// Set PID Setpoint 设定目标 Desired Value
while(1)
{
if(plus==0)
{
EA=0
for(a=0a<5a++)
for(b=0b<102b++){}
if(plus==0)
{
set_temper++
flag=0
}
}
else if(subs==0)
{
for(a=0a<5a++)
for(b=0a<102b++){}
if(subs==0)
{
set_temper--
flag=0
}
}
else if(stop==0)
{
for(a=0a<5a++)
for(b=0b<102b++){}
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=0a<5a++)
for(b=0b<102b++){}
if(stop==0)
disp_1(phil)
// break
}
EA=1
}
}
/***********************************************************************PID温度控制程序
程序说明:
系统上电后显示 “--温度”
表示需要先设定温度才开始进行温度检测
温度设定完毕后程序才开始进行PID温控
***********************************************************************/
#include <reg52.h>
#include <absacc.h>
#include"DS18B20.H"
#include"PID.H"
#define uchar unsigned char
#define uint unsigned int
unsigned char code tab[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF
}
/*个位0~9的数码管段码*/
unsigned char code sao[]=
{
0x7f,0xbf,0xdf,0xef
}
//扫描码
uchar set=30,keyflag=1 //set初始化为30° keyflag为进入温度设定的标志位
//4个按键使用说明
sbit key_out=P1^0 //用于温度设定后的退出
sbit key_up=P1^1 //设定温度加
sbit key_down=P1^2 //设定温度减
sbit key_in=P1^3 //在程序的运行中如需要重新设定温度 按下此键才能进入设置模式并且此时是停在温度控制的,按下key_out键后才表示设定完毕
void Show_key()
/***********************************************************/
void delays(unsigned char k)
{
unsigned char i,j
for(i=0i<ki++)
for(j=0j<50j++)
}
/*********************************************************
//数码管显示函数
P0口 作为数据口
P2口的低四位作为扫描口
变量 x表示扫描
d表示是否要加小数点 为1是 为0不加
y表示传递的数值
*********************************************************/
LCD_disp_char(uchar x,bit d,uchar y)
{
P2=0XFF
P0=0xFF
if(d==0)
P0=tab[y]
else
P0=tab[y]&0x7f //与上0x7f表示是否要加小数点
P2=sao[x]//打开扫描端号
}
/*********************************************************
按键扫描
*********************************************************/
void keyscan(void)
{
if(key_in==0) //按键进入函数
{
delays(10) //延时消抖 (以下同)
if(key_in==0)
{
while(key_in==0)
{
Show_key()//如果一直按着键不放 就一直显示在当前状态 (以下同)
}
keyflag=1 //按键标志位
}
}
/***********************/
if(key_out==0) //按键退出
{
delays(10)
if(key_out==0)
{
while(key_out==0)
{
Show_key()
}
keyflag=0
set_temper=set
}
}
/*************************/
if(key_up==0) //设定温度的加
{
delays(10)
if(key_up==0)
{
while(key_up==0)
{
Show_key()
}
if(keyflag==1)
{
set++
if(set>90) //如果大于90°就不在加
set=90
}
}
}
/*************************/
if(key_down==0) //温度设定的减
{
delays(10)
if(key_down==0)
{
while(key_down==0)
{
Show_key()
}
if(keyflag==1)
{
set--
if(set<30) //温度减到30°时不在往下减
set=30
}
}
}
}
/*********************************************************************
按键按下时的显示函数
***********************************************************************/
void Show_key()
{
output=1
LCD_disp_char(3,0,10)//显示 -
delays(3)
LCD_disp_char(2,0,10)//显示- (表示温度设定 )
delays(3)
LCD_disp_char(1,0,set/10)//显示温度十位
delays(3)
LCD_disp_char(0,0,set%10)//显示温度个位
delays(3)
}
/*****************************************************************/
void main()
{
unsigned int tmp //声明温度中间变量
unsigned char counter=0
PIDBEGIN()//PID参数的初始化
output=1 //关闭继电器输出
while(1)
{
keyscan()
if(keyflag)
{
Show_key()//显示温度设定
}
else
{
if(counter--==0)
{
tmp=ReadTemperature()//每隔一段时间读取温度值
counter=20
}
LCD_disp_char(3,0,tmp/1000) //显示温度十位
delays(3)
LCD_disp_char(2,1,tmp/100%10)//显示温度个位
//显示小数点
delays(3)
LCD_disp_char(1,0,tmp/10%10)//显示温度小数后一位
delays(3)
LCD_disp_char(0,0,tmp%10)//显示温度小数后二位
delays(3)
P2=0XFF
P0=0xff
compare_temper()//比较温度
}
}
}
/**********************************************************************************************************************************************/
//PID算法温控C语言2008-08-17 18:58
#ifndef _PID_H__
#define _PID_H__
#include<intrins.h>
#include<math.h>
#include<string.h>
struct PID
{
unsigned int SetPoint
// 设定目标 Desired Value
unsigned int Proportion
// 比例常数 Proportional Const
unsigned int Integral
// 积分常数 Integral Const
unsigned int Derivative
// 微分常数 Derivative Const
unsigned int LastError
// Error[-1]
unsigned int PrevError
// Error[-2]
unsigned int SumError
// Sums of Errors
}
struct PID spid
// PID Control Structure
unsigned int rout
// PID Response (Output)
unsigned int rin
// PID Feedback (Input)
sbit output=P1^4
unsigned char high_time,low_time,count=0
//占空比调节参数
unsigned char set_temper
void PIDInit(struct PID*pp)
{
memset(pp,0,sizeof(struct PID))//PID参数初始化全部设置为0
}
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->PrevError=pp->LastError
pp->LastError=Error
//比例
//积分项
return(pp->Proportion*Error+pp->Integral*pp->SumError+pp->Derivative*dError)
// 微分项
}
/***********************************************************
温度比较处理子程序
***********************************************************/
void compare_temper()
{
unsigned char i
//EA=0
if(set_temper>temper)
{
if(set_temper-temper>1)
{
high_time=100 //大于1°不进行PID运算
low_time=0
}
else
{ //在1°范围内进行PID运算
for(i=0i<10i++)
{
//get_temper()
rin=s
// Read Input
rout=PIDCalc(&spid,rin)//执行PID运算
// 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<=temper) //当实际温度大于设置温度时
{
if(temper-set_temper>0)//如果实际温度大于设定温度
{
high_time=0
low_time=100
}
else
{
for(i=0i<10i++)
{
//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)
//EA=1
}
}
}
/*****************************************************
T0中断服务子程序,用于控制电平的翻转 ,40us*100=4ms周期
******************************************************/
void serve_T0()interrupt 1 using 1
{
if(++count<=(high_time))
output=0
else if(count<=100)
{
output=1
}
else
count=0
TH0=0x2f
TL0=0xe0
}
void PIDBEGIN()
{
TMOD=0x01
TH0=0x2f
TL0=0x40
EA=1
ET0=1
TR0=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
}
#endif
转自他人程序。
本设计要求:本温度控制系统为以单片机为核心,实现了对温度实时监测和控制,实现了控制的智能化。设计恒温箱温度控制系统,配有温度传感器,采用DS18B20数字温度传感器,无需数模拟∕数字转换,可直接与单片机进行数字传输,采用了PID控制技术,可以使温度保持在要求的一个恒定范围内,配有键盘,用于输入设定温度配有数码管LED用来显示温度。技术参数和设计任务:
1、利用单片机AT89C2051实现对温度的控制,实现保持恒温箱在最高温度为110℃。
2、可预置恒温箱温度,烘干过程恒温控制,温度控制误差小于±2℃。
3、预置时显示设定温度,恒温时显示实时温度,采用PID控制算法显示精确到0.1℃。
4、温度超出预置温度±5℃时发出声音报警。
5、对升、降温过程没有线性要求。
6、温度检测部分采用DS18B20数字温度传感器,无需数模拟∕数字转换,可直接与单片机进行数字传输
7、人机对话部分由键盘、显示和报警三部分组成,实现对温度的显示、报警。
需要的话联系用户名扣扣
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)