/************* main.c ******************/
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
#include "18b20.c"
#include<pid.c>
uchar count,high_time;
uchar set;
uchar code dis_7[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
main()
{
uint t;
TMOD=0x01;
TH0 =0x20;
TL0 =0x00;
EA = 1;
ET0 = 1;
TR0 = 1;
set=32;
init_pid();
while(1)
{
t=ReadTemperature();
if(t>999)
{
P0=dis_7[t/1000];
P3=0xfe;
delay(200);
P0=dis_7[(t%1000)/100];
P3=0xfd;
delay(200);
}
else
{
P0=dis_7[t/100];
P3=0xfd;
delay(200);
}
P0=(dis_7[(t%100)/10]&0x7f);
P3=0xfb;
delay(200);
P0=dis_7[t%10];
P3=0xf7;
duty_cycle(t);
}
}
//---------------------------------------------------------------
void t0_int(void) interrupt 1//PWM波输出
{
if(++count<=(high_time))
SWH=0;
else if(count<=100)
SWH=1;
else
count=0;
TH0=0x10; //定时器初始化
TL0=0x10;
}
/************* pid.c ******************/
uchar set; //温度初始值
uint rout; // PID Response (Output)
uchar high_time,count=0; //占空比调节参数
/*************PID**********************************/
struct PID {
uint SetPoint; // 设定目标 Desired Value
uint Proportion; // 比例常数 Proportional Const
uint Integral; // 积分常数 Integral Const
uint Derivative; // 微分常数 Derivative Const
signed int LastError; // Error[-1]
signed int PrevError; // Error[-2]
signed int SumError; // Sums of Errors
};
struct PID spid; // PID Control Structure
/****************pid初始化*********************/
void init_pid()
{
high_time=50;
spid.Proportion = 23; // Set PID Coefficients
spid.Integral = 2;
spid.Derivative =6;
spid.SetPoint = set; // Set PID Setpoint
}
/***************************PID算法**************************/
unsigned int PIDCalc( struct PID *pp, unsigned int NextPoint )
{
signed 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);
}
/********************PID控制占空比*************************/
//high_time表示高电平数
void duty_cycle(uint t) // 占空比
{
uchar s;
t=t/10;
s=set;
if(s>t)
{
if(s-t>2)
high_time=100;
else
{
rout = PIDCalc ( &spid,t ); // Perform PID Interation
if(high_time<=100)
high_time=(uchar)(rout/600);
else
high_time=100;
}
}
else
{
high_time=0;
}
}
/************* 18b20.c ******************/
sbit DQ = P2^7; //定义DS18B20数据线
sbit SWH = P2^4; //PWM开关
sbit BEEP = P2^1; //蜂鸣器
void delay(unsigned int t)
{
while(t--)
;
}
void Init_DS18B20(void)//初始化ds1820
{
unsigned char x=0;
DQ = 1; //DQ复位
delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(20);
}
/******************************************************************************/
unsigned char ReadOneChar(void)//读一个字节
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
/******************************************************************************/
void WriteOneChar(unsigned char dat)//写一个字节
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
}
/******************************************************************************/
uint ReadTemperature(void)//读取温度
{
uchar a=0;
uchar b=0;
uint t=0;
float tt=0;
Init_DS18B20(); //第一次复位,执行温度转换
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20(); // 第二次复位,读数据
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器
a=ReadOneChar(); //读低8位
b=ReadOneChar(); //读高8位
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5;
return(t);
}