|
/***主函数****/#include <reg52.h>
#include<stdlib.h>
#include<intrins.h>
sbit RELAY = P2^4; //定义继电器对应单片机管脚
bit flag1s = 0; //1s定时标志
unsigned char T0RH = 0; //T0重载值的高字节
unsigned char T0RL = 0; //T0重载值的低字节
unsigned char T1RH = 0; //T0重载值的高字节
unsigned char T1RL = 0; //T0重载值的低字节
extern bit Start18B20();
extern bit Get18B20Temp(int *temp);
extern void InitLcd1602();
extern void LcdShowStr(unsigned char *str);
extern void LcdSetCursor(unsigned char addr);
extern void LcdWriteDat(unsigned char dat);
extern void LcdWriteCmd(unsigned char del);
extern void keyscan();
extern unsigned char key,flag1;
unsigned char a[7];
/**********************/
struct _pid{
float SV;//用户设定温度
float PV; //测量温度
float KP; //比例常数
float T; //PID计算周期--采样周期
float TI; //积分常数
float TD; //微分常数
float EK; //本次偏差
float EK_1;//上次偏差
float SEK; //历史偏差之和
float IOUT;//积分输出
float POUT; //比例输出
float DOUT;//微分输出
float OUT0;
float OUT;
unsigned int C10ms;
unsigned char pwmcycle;//pwm周期
}PID;
/********************************************/
void pid_init()
{
PID.KP=20;
PID.T=1000;
PID.TI=5000;
PID.TD=1200;
PID.pwmcycle=1000;//PWM的周期
}
/***********************************************/
void ConfigTimer0(unsigned int ms);
void ConfigTimer1(unsigned int ms1);
void pid_calc(); //PID计算
void pid_out(); //PID输出结果到负载
void main()
{
unsigned char m;
unsigned char i,e=0;
unsigned char IntToString(unsigned char *str, int dat);
unsigned char code table[]={'0','1','2','3','4','5','6','7','8','9', '.','-',':',' ','=','.'};
bit res;
int temp; //读取到的当前温度值
int intT, decT; //温度值的整数和小数部分
unsigned char len;
unsigned char str[5];
unsigned char stt[16] = {"T1:"};
unsigned char sty[16] = {"T2:"};
EA = 1; //开总中断
ConfigTimer0(10); //T0定时10ms
ConfigTimer1(1); //T1定时1ms
Start18B20(); //启动DS18B20
InitLcd1602(); //初始化液晶
pid_init() ;
LcdWriteCmd(0x0C); //关闭光标闪烁
/*实时温度*/
while(1)
{
LcdWriteDat(PID.OUT);
/*显示实时温度标识"T1:"*/
LcdSetCursor(0x00);
m = 0;
while(stt[m] != '\0')
{
LcdWriteDat(stt[m]);
m++;
}
/*显示设定温度标识"T2:"*/
LcdSetCursor(0x40);
m = 0;
while(sty[m] != '\0')
{
LcdWriteDat(sty[m]);
m++;
}
if (flag1s) //每秒更新一次温度
{
flag1s = 0;
res = Get18B20Temp(&temp); //读取当前温度
if (res) //读取成功时,刷新当前温度显示
{
intT = temp >> 4; //分离出温度值整数部分
decT = temp & 0xF; //分离出温度值小数部分
len = IntToString(str, intT); //整数部分转换为字符串
str[len++] = '.'; //添加小数点
decT = (decT*10) / 16; //二进制的小数部分转换为1位十进制位
str[len++] = decT + '0'; //十进制小数位再转换为ASCII字符
LcdShowStr(str); //显示到液晶屏上
Start18B20();
PID.PV=atof(str);
}
}
/*设定温度*/
keyscan();
if(flag1==1)
{
if(key<11) //10以下是数字显示部分
{
LcdSetCursor(0x43+e++) ;
LcdWriteDat(table[key]);
a[i]=table[key];
i++;
PID.SV=atof(&a);
}
else if(key==11)
{
LcdWriteCmd(0x01) ; //删除设定值
e=0;
for(i=0;i<7;i++)
{
a[i]='0';
}
}
flag1=0; //键盘检测标志置0进行下一次检测
}
pid_calc();
}
}
/* 整型数转换为字符串,str-字符串指针,dat-待转换数,返回值-字符串长度 */
unsigned char IntToString(unsigned char *str, int dat)
{
signed char i = 0;
unsigned char len = 0;
unsigned char buf[6];
if (dat < 0) //如果为负数,首先取绝对值,并在指针上添加负号
{
dat = -dat;
*str++ = '-';
len++;
}
do
{ //先转换为低位在前的十进制数组
buf[i++] = dat % 10;
dat /= 10;
} while (dat > 0);
len += i; //i最后的值就是有效字符的个数
while (i-- > 0) //将数组值转换为ASCII码反向拷贝到接收指针上
{
*str++ = buf[i] + '0';
}
*str = '\0'; //添加字符串结束符
return len; //返回字符串长度
}
/* 配置并启动T0,ms-T0定时时间 10ms*/
void ConfigTimer0(unsigned int ms)
{
unsigned long tmp; //临时变量
tmp = 11059200 / 12; //定时器计数频率
tmp = (tmp * ms) / 1000; //计算所需的计数值
tmp = 65536 - tmp; //计算定时器重载值
tmp = tmp + 12; //补偿中断响应延时造成的误差
T0RH = (unsigned char)(tmp>>8); //定时器重载值拆分为高低字节
T0RL = (unsigned char)tmp;
TMOD &= 0xF0; //清零T0的控制位
TMOD |= 0x01; //配置T0为模式1
TH0 = T0RH; //加载T0重载值
TL0 = T0RL;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
}
/* T0中断服务函数,10ms一次,完成1秒定时 */
void InterruptTimer0() interrupt 1
{
static unsigned char tmr1s = 0;
TH0 = T0RH; //重新加载重载值
TL0 = T0RL;
tmr1s++;
PID.C10ms++;
if (tmr1s >= 100) //定时1s
{
tmr1s = 0;
flag1s = 1;
}
}
/* 配置并启动T1,ms-T1定时时间1ms */
void ConfigTimer1(unsigned int ms1)
{
unsigned long tmp1; //临时变量
tmp1 = 11059200 / 12; //定时器计数频率
tmp1 = (tmp1 * ms1) / 1000; //计算所需的计数值
tmp1 = 65536 - tmp1; //计算定时器重载值
tmp1 = tmp1 + 12; //补偿中断响应延时造成的误差
T1RH = (unsigned char)(tmp1>>8); //定时器重载值拆分为高低字节
T1RL = (unsigned char)tmp1;
TMOD &= 0xF0; //清零T1的控制位
TMOD |= 0x01; //配置T1为模式1
TH1 = T1RH; //加载T1重载值
TL1 = T1RL;
ET1 = 1; //使能T1中断
TR1 = 1; //启动T1
}
/* T1中 断服务函数,1ms一次 */
void InterruptTimer1() interrupt 3
{
TH1 = T1RH; //重新加载重载值
TL1 = T1RL;
// PID.C10ms++;
pid_out(); //输出PID运算结果到负载
}
void pid_calc()
{
float DELEK;
float ti;
float ki;
float td;
float kd;
float out;
if(PID.C10ms<(PID.T/10))
{return;}
PID.EK=PID.SV-PID.PV; //当前偏差
PID.POUT=PID.KP*PID.EK;//比例输出
PID.SEK+=PID.EK; //历史偏差
DELEK=PID.EK-PID.EK_1;//最近两次偏差之差
ti=PID.T/PID.TI;
ki=ti*PID.KP;
PID.IOUT=ki*PID.SEK*PID.KP; //积分输出
td=PID.TD/PID.T;
kd=PID.KP*td;
PID.DOUT=kd*DELEK;
PID.OUT=PID.POUT+PID.IOUT+PID.DOUT+PID.OUT0;//计算结果
if(PID.OUT>PID.pwmcycle)
{PID.OUT=PID.pwmcycle;}
if(PID.OUT<0)
{PID.OUT=0;}
PID.OUT=out;
PID.EK_1=PID.EK;//更新偏差
PID.C10ms=0;
}
void pid_out() //每一毫秒运算一次
{
static unsigned char pw;
pw++;
if(pw>PID.pwmcycle) //pw=0-999
{pw=0;}
if(pw<PID.OUT)
{
RELAY=0;//加热
}
else
{
RELAY=1;//停止加热
}
}
/*****LCD1602子程序和按键模块程序****/
#include <reg52.h>
#include<intrins.h>
sbit LCD1602_RS = P2^6;
sbit LCD1602_RW = P2^5;
sbit LCD1602_E = P2^7;
unsigned char key,flag1,cal,judge1,judge2;
/*延时程序*/
void delay()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void Delay(unsigned int i)
{
unsigned int x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
void delay1(unsigned char z) //用于防抖延时
{
unsigned char x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*测忙状态*/
bit Busy(void)
{
bit busy_flag = 0;
LCD1602_RS = 0;
LCD1602_RW = 1;
LCD1602_E = 1;
delay();
busy_flag = (bit)(P0 & 0x80);
LCD1602_E = 0;
return busy_flag;
}
/*写指令 */
void LcdWriteCmd(unsigned char del)
{
while(Busy());
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_E = 0;
delay();
P0 = del;
delay();
LCD1602_E = 1;
delay();
LCD1602_E = 0;
}
/* 写数据 */
void LcdWriteDat(unsigned char del)
{
while(Busy());
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_E = 0;
delay();
P0 = del;
delay();
LCD1602_E = 1;
delay();
LCD1602_E = 0;
}
/* 设定显示位置 */
void LcdSetCursor(unsigned char addr)
{
LcdWriteCmd(addr | 0x80); //数据指针=80+地址变量
}
/*温度传送*/
void LcdShowStr( unsigned char *str)
{
LcdSetCursor(0x03); //设置显示位置为第一行的第1个字符
while(*str != '\0')
{ //显示字符
LcdWriteDat(*str++);
}
}
/* 初始化1602液晶 */
void InitLcd1602()
{
LcdWriteCmd(0x38);
Delay(5);
LcdWriteCmd(0x38);
Delay(5);
LcdWriteCmd(0x38);
Delay(5);
LcdWriteCmd(0x38);
LcdWriteCmd(0x0d);
}
/*键盘扫描函数 */
void keyscan()
{
unsigned char temp,xy=0xf7; //xy变量存储第几行
unsigned int i;
for(i=0;i<4;i++) //行移动
{
xy=_crol_(xy,1); //_crol_为左移函数
P1=xy;
temp=P1;
temp=temp&0x0f;
if(temp!=0x0f)
{
delay1(50); //去抖
temp=P1;
temp=temp&0x0f;
if(temp!=0x0f)
{
delay1(50); //去抖
temp=P1;
switch(temp) //按键定位
{
case 0xee: key=7; break;
case 0xde: key=8; break;
case 0xbe: key=9; break;
case 0x7e: key=10; break;
case 0xed: key=4; break;
case 0xdd: key=5; break;
case 0xbd: key=6; break;
case 0x7d: key=11; break;
case 0xeb: key=1; break;
case 0xdb: key=2; break;
case 0xbb: key=3; break;
case 0x7b: key=12; break;
case 0xe7: key=0; break;
case 0xd7: key=14; break;
case 0xb7: key=15; break;
case 0x77: key=13; break;
}
while(temp!=0x0f)
{
temp=P1;
temp=temp&0x0f;
}
flag1=1; //flag=1标志按键检测完毕
}
}
}
}
/******DS18B20子程序****/
#include <reg52.h>
#include <intrins.h>
sbit IO_18B20 = P3^7; //DS18B20通信引脚
/* 软件延时函数,延时时间(t*10)us */
void DelayX10us(unsigned char t)
{
do
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
} while (--t);
}
/*复位总线,获取存在脉冲,初始化DS18B20*/
bit Get18B20Ack() //检测存在脉冲
{
bit ack;
EA = 0; //关闭中断
IO_18B20 = 0; //产生500us复位脉冲
DelayX10us(50);
IO_18B20 = 1; //拉高
DelayX10us(6); //延时60微秒
ack = IO_18B20; //读取存在脉冲
while(!IO_18B20);//等待存在脉冲结束
EA = 1; //打开中断
return ack;
}
/*DS18B20位写入时序*/
void Write18B20(unsigned char dat)
{
unsigned char mask;
EA = 0; //关闭中断
for(mask=0x01; mask!= 0; mask<<=1) //低位在先,依次移出8个bit
{
IO_18B20 = 0; //产生2us低电平脉冲
_nop_();
_nop_();
if((mask&dat) == 0) //输出该bit值
IO_18B20 = 0;
else
IO_18B20 = 1;
DelayX10us(6); //延时60us
IO_18B20 = 1; //拉高通信引脚
}
EA = 1; //打开中断
}
/*DS18B20位读取时序*/
unsigned char Read18B20()
{
unsigned char dat;
unsigned char mask;
EA = 0; //关闭中断
for(mask=0x01; mask!=0; mask<<=1) //低位在先,依次采集8个bit
{
IO_18B20 = 0; //产生2us低电平脉冲
_nop_();
_nop_();
IO_18B20 = 1; //结束低电平脉冲,等待18B20输出数据
_nop_(); //延时2us
_nop_();
if(!IO_18B20) //读取通信引脚上的值
dat &= ~mask;
else
dat |= mask;
DelayX10us(6); //再延时60us
}
EA = 1; //打开中断
return dat;
}
/*启动DS18B20*/
bit Start18B20()
{
bit ack;
ack = Get18B20Ack(); //执行总线复位,并获取18B20应答
if(ack == 0) //如18B20正确应答,则启动一次转换
{
Write18B20(0xCC); //跳过ROM,只读一个18B20
Write18B20(0x44); // 启动温度转换
}
return ~ack; //ack==0表示操作成功,所以返回值对其取反
}
/*获取温度*/
bit Get18B20Temp(int *temp)
{
bit ack;
unsigned char LSB, MSB; //16bit温度值的低字节和高字节
ack = Get18B20Ack(); //执行总线复位,并获取18B20应答
if(ack == 0) //如18B20正确应答,则读取温度值
{
Write18B20(0xCC); //跳过ROM操作
Write18B20(0xBE); //发送读命令
LSB = Read18B20(); //读温度值的低字节
MSB = Read18B20(); //读温度值的高字节
*temp = ((int)MSB<<8) + LSB; //合成为16bit整型数
}
return ~ack; //ack==0表示操作应答,所以返回值为其取反值
}
|
|