代码设计
#include <REGX51.H>
#define Kp 5 //⽐例系数
#define Ki 2 //积分系数
#define Kd 1 //微分系数
unsigned char m,n,p; //温度的⼗位个位⼩数unsigned char test_temp; //温度检定标志unsigned char key_set_flag; //按键设定进⼊标志unsigned char flag=1; //按键保持标志
unsigned char Change_step=1,e; //温度设置步进unsigned char T0_H = 0,T0_L = 0,T1_H=0,T1_L=0; int Real_temp; //实际温度值
int Set_temp; //设置温度
int Disp_temp; //显⽰温度
sbit DQ=P2^4; //定义DS18b20的管脚
sbit RS = P2^0; //定义液晶屏定义端⼝
sbit RW = P2^1; //定义液晶屏定义端⼝
sbit EN = P2^2; //定义液晶屏定义端⼝
sbit k1=P1^0;
sbit k2=P1^1;
sbit k3=P1^2;
sbit PWM=P2^7; //PWM控制脚
sbit led1=P3^5;
sbit led2=P3^6;
sbit led3=P3^7;
sbit beep=P1^3;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
unsigned char aa[]={' ',' ','S','e','t','u','p',':',' ',' ','.',' ',' ',' ',' ',' '}; //Distance unsigned char bb[]={' ',' ',' ',' ',' ',' ',' ','.',' ',' ',' ',' ',' ',' ',' ',' '};
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s); void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data); void init(); //初始化
/*------------------------------------------------
uS延时函数,含有输⼊参数 unsigned char t,⽆返回值
unsigned char 是定义⽆符号字符变量,其值的范围是
0~255 这⾥使⽤晶振12M,精确延时请使⽤汇编,⼤致延时
长度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延时函数,含有输⼊参数 unsigned char t,⽆返回值
unsigned char 是定义⽆符号字符变量,其值的范围是
0~255 这⾥使⽤晶振12M,精确延时请使⽤汇编
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--) //⼤致延时1mS
{
DelayUs2x(245);
DelayUs2x(245);
}
}
/*****延时⼦程序*****/
void delay(unsigned int t)
{
for(;t>0;t--);
}
void delay_50us(unsigned int t)
{
unsigned char j;
for(;t>0;t--)
for(j=19;j>0;j--);
for(j=19;j>0;j--);
}
/
*****初始化DS18B20*****/
unsigned char Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay(8); //稍做延时
DQ = 0; //单⽚机将DQ拉低
delay(80); //精确延时,⼤于480us
DQ = 1; //拉⾼总线
delay(8);
x = DQ; //稍做延时后,如果x=0则初始化成功,x=1则初始化失败 delay(4);
return x;
}
/*****读⼀个字节*****/
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(4);
DQ = 1;
dat>>=1;
}
delay(4);
}
/*****读取温度*****/
int ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
t=Init_DS18B20();
if(t) return Real_temp;
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0x44); //启动温度转换
t=Init_DS18B20();
if(t) return Real_temp;
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器
a=ReadOneChar(); //读低8位
b=ReadOneChar(); //读⾼8位
b=ReadOneChar(); //读⾼8位
t=b;
t<<=8;
t=t|a;
if(t<=0||t>0x900)
return Real_temp;
t=t*0.625+0.5;
return(t);
}
void key_set(void)
{
if(k1==0)
{
delay(10);
while(!k1);
key_hold=~key_hold;
}
if(key_hold==0)
{
if(k2==0)
{
delay(10);
while(!k2);
Set_temp=Set_temp+1;
if(Set_temp>99)
Set_temp=99;
}
write的返回值if(k3==0)
{
delay(10);
while(!k3);
Set_temp=Set_temp-1;
if(Set_temp<1)
Set_temp=1;
}
aa[8]=10*Set_temp/100+'0';
aa[9]=10*Set_temp/10%10+'0';
aa[11]=10*Set_temp%10+'0';
LCD_Write_String(0,0,aa);
}
}
int PID(int Set_value,int Real_value) //标准PID温度控制算法{
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论