找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 4149|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

一個(gè)溫控程序 測(cè)量溫度和按鍵設(shè)定溫度值都沒(méi)問(wèn)題,加了個(gè)PID控制就出問(wèn)題了,求指點(diǎn)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:187588 發(fā)表于 2017-4-10 12:36 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

/***主函數(shù)****/#include <reg52.h>
#include<stdlib.h>
#include<intrins.h>

sbit RELAY = P2^4;                    //定義繼電器對(duì)應(yīng)單片機(jī)管腳

bit flag1s = 0;          //1s定時(shí)標(biāo)志
unsigned char T0RH = 0;  //T0重載值的高字節(jié)
unsigned char T0RL = 0;  //T0重載值的低字節(jié)
unsigned char T1RH = 0;  //T0重載值的高字節(jié)
unsigned char T1RL = 0;  //T0重載值的低字節(jié)

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;//用戶設(shè)定溫度
float PV; //測(cè)量溫度
float KP; //比例常數(shù)
float T;  //PID計(jì)算周期--采樣周期
float TI;        //積分常數(shù)
float TD;         //微分常數(shù)               
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計(jì)算
void pid_out();         //PID輸出結(jié)果到負(fù)載
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;        //讀取到的當(dāng)前溫度值
  int intT, decT;  //溫度值的整數(shù)和小數(shù)部分
  unsigned char len;
  unsigned char str[5];
  unsigned char stt[16] = {"T1:"};
  unsigned char sty[16] = {"T2:"};
       
  EA = 1;            //開總中斷
  ConfigTimer0(10);  //T0定時(shí)10ms
  ConfigTimer1(1);  //T1定時(shí)1ms
  Start18B20();      //啟動(dòng)DS18B20
  InitLcd1602();     //初始化液晶
  pid_init() ;
  LcdWriteCmd(0x0C); //關(guān)閉光標(biāo)閃爍

/*實(shí)時(shí)溫度*/
  while(1)
   {
   LcdWriteDat(PID.OUT);            
/*顯示實(shí)時(shí)溫度標(biāo)識(shí)"T1:"*/
    LcdSetCursor(0x00);               
    m = 0;
    while(stt[m] != '\0')
     {                        
       LcdWriteDat(stt[m]);
       m++;
     }
/*顯示設(shè)定溫度標(biāo)識(shí)"T2:"*/
        LcdSetCursor(0x40);               
    m = 0;
    while(sty[m] != '\0')
     {                        
       LcdWriteDat(sty[m]);
       m++;
     }
           if (flag1s)  //每秒更新一次溫度
        {
        flag1s = 0;         
        res = Get18B20Temp(&temp);  //讀取當(dāng)前溫度
        if (res)                    //讀取成功時(shí),刷新當(dāng)前溫度顯示
        {          
        intT = temp >> 4;             //分離出溫度值整數(shù)部分
        decT = temp & 0xF;            //分離出溫度值小數(shù)部分
        len = IntToString(str, intT); //整數(shù)部分轉(zhuǎn)換為字符串
        str[len++] = '.';             //添加小數(shù)點(diǎn)
        decT = (decT*10) / 16;        //二進(jìn)制的小數(shù)部分轉(zhuǎn)換為1位十進(jìn)制位
        str[len++] = decT + '0';      //十進(jìn)制小數(shù)位再轉(zhuǎn)換為ASCII字符
        LcdShowStr(str);        //顯示到液晶屏上
        Start18B20();                       
        PID.PV=atof(str);                                                       
        }       
        }
  /*設(shè)定溫度*/                      
           keyscan();                                  
           if(flag1==1)  
            {
         if(key<11)      //10以下是數(shù)字顯示部分  
          {
               LcdSetCursor(0x43+e++) ;
               LcdWriteDat(table[key]);   
               a[i]=table[key];   
               i++;
               PID.SV=atof(&a);
                  
              }       
              else if(key==11)
              {       
               LcdWriteCmd(0x01) ;         //刪除設(shè)定值
                   e=0;
               for(i=0;i<7;i++)
               {         
                    a[i]='0';
               }
              }
             flag1=0;        //鍵盤檢測(cè)標(biāo)志置0進(jìn)行下一次檢測(cè)        
             }
          pid_calc();
}
}      
/* 整型數(shù)轉(zhuǎn)換為字符串,str-字符串指針,dat-待轉(zhuǎn)換數(shù),返回值-字符串長(zhǎng)度 */
unsigned char IntToString(unsigned char *str, int dat)
{
  signed char i = 0;
  unsigned char len = 0;
  unsigned char buf[6];
  if (dat < 0)  //如果為負(fù)數(shù),首先取絕對(duì)值,并在指針上添加負(fù)號(hào)
   {
    dat = -dat;
    *str++ = '-';
    len++;
   }
    do
        {          //先轉(zhuǎn)換為低位在前的十進(jìn)制數(shù)組
     buf[i++] = dat % 10;
     dat /= 10;
    } while (dat > 0);
    len += i;     //i最后的值就是有效字符的個(gè)數(shù)
    while (i-- > 0)   //將數(shù)組值轉(zhuǎn)換為ASCII碼反向拷貝到接收指針上
    {
     *str++ = buf[i] + '0';
    }
    *str = '\0';  //添加字符串結(jié)束符
    return len;   //返回字符串長(zhǎng)度
}
/* 配置并啟動(dòng)T0,ms-T0定時(shí)時(shí)間 10ms*/
void ConfigTimer0(unsigned int ms)
{
    unsigned long tmp;  //臨時(shí)變量

    tmp = 11059200 / 12;      //定時(shí)器計(jì)數(shù)頻率
    tmp = (tmp * ms) / 1000;  //計(jì)算所需的計(jì)數(shù)值
    tmp = 65536 - tmp;        //計(jì)算定時(shí)器重載值
    tmp = tmp + 12;           //補(bǔ)償中斷響應(yīng)延時(shí)造成的誤差
    T0RH = (unsigned char)(tmp>>8);  //定時(shí)器重載值拆分為高低字節(jié)
    T0RL = (unsigned char)tmp;
    TMOD &= 0xF0;   //清零T0的控制位
    TMOD |= 0x01;   //配置T0為模式1
    TH0 = T0RH;     //加載T0重載值
    TL0 = T0RL;          
    ET0 = 1;        //使能T0中斷
    TR0 = 1;        //啟動(dòng)T0
}

/* T0中斷服務(wù)函數(shù),10ms一次,完成1秒定時(shí) */
void InterruptTimer0() interrupt 1
{
    static unsigned char tmr1s = 0;

    TH0 = T0RH;  //重新加載重載值
    TL0 = T0RL;
    tmr1s++;
        PID.C10ms++;
    if (tmr1s >= 100)  //定時(shí)1s
    {
        tmr1s = 0;
        flag1s = 1;
    }
}
/* 配置并啟動(dòng)T1,ms-T1定時(shí)時(shí)間1ms */
void ConfigTimer1(unsigned int ms1)
{
    unsigned long tmp1;  //臨時(shí)變量

    tmp1 = 11059200 / 12;      //定時(shí)器計(jì)數(shù)頻率
    tmp1 = (tmp1 * ms1) / 1000;  //計(jì)算所需的計(jì)數(shù)值
    tmp1 = 65536 - tmp1;        //計(jì)算定時(shí)器重載值
    tmp1 = tmp1 + 12;           //補(bǔ)償中斷響應(yīng)延時(shí)造成的誤差
    T1RH = (unsigned char)(tmp1>>8);  //定時(shí)器重載值拆分為高低字節(jié)
    T1RL = (unsigned char)tmp1;
    TMOD &= 0xF0;   //清零T1的控制位
    TMOD |= 0x01;   //配置T1為模式1
    TH1 = T1RH;     //加載T1重載值
    TL1 = T1RL;          
    ET1 = 1;        //使能T1中斷
    TR1 = 1;        //啟動(dòng)T1
}
/* T1中        斷服務(wù)函數(shù),1ms一次 */
void InterruptTimer1() interrupt 3
{  
    TH1 = T1RH;  //重新加載重載值
    TL1 = T1RL;
//        PID.C10ms++;
    pid_out(); //輸出PID運(yùn)算結(jié)果到負(fù)載
}
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;        //當(dāng)前偏差
  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;//計(jì)算結(jié)果
  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()         //每一毫秒運(yùn)算一次
{
   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;          
/*延時(shí)程序*/
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)   //用于防抖延時(shí)
{  
  unsigned char x,y;
  for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}
/*測(cè)忙狀態(tài)*/
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;
}
/* 寫數(shù)據(jù) */
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;
}

/*  設(shè)定顯示位置 */
void LcdSetCursor(unsigned char addr)
{                          
  LcdWriteCmd(addr | 0x80);  //數(shù)據(jù)指針=80+地址變量
}         
/*溫度傳送*/
void LcdShowStr( unsigned char *str)          
{
  LcdSetCursor(0x03);                //設(shè)置顯示位置為第一行的第1個(gè)字符
  while(*str != '\0')
  {                         //顯示字符
   LcdWriteDat(*str++);
  }
}

/* 初始化1602液晶 */
void InitLcd1602()
{
  LcdWriteCmd(0x38);
  Delay(5);
  LcdWriteCmd(0x38);
  Delay(5);
  LcdWriteCmd(0x38);
  Delay(5);  
  LcdWriteCmd(0x38);  
  LcdWriteCmd(0x0d);
}
/*鍵盤掃描函數(shù) */
void keyscan()
{
  unsigned char temp,xy=0xf7; //xy變量存儲(chǔ)第幾行  
  unsigned int i;  
  for(i=0;i<4;i++)     //行移動(dòng)  
  {   
   xy=_crol_(xy,1);   //_crol_為左移函數(shù)   
   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標(biāo)志按鍵檢測(cè)完畢  
   }
  }
}
}
/******DS18B20子程序****/


#include <reg52.h>
#include <intrins.h>

sbit IO_18B20 = P3^7;  //DS18B20通信引腳

/* 軟件延時(shí)函數(shù),延時(shí)時(shí)間(t*10)us */
void DelayX10us(unsigned char t)
{
  do
  {
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
  } while (--t);
}
/*復(fù)位總線,獲取存在脈沖,初始化DS18B20*/
bit Get18B20Ack()          //檢測(cè)存在脈沖
{
bit ack;
EA = 0;                         //關(guān)閉中斷
IO_18B20 = 0;         //產(chǎn)生500us復(fù)位脈沖
DelayX10us(50);         
IO_18B20 = 1;         //拉高
DelayX10us(6);         //延時(shí)60微秒
ack = IO_18B20;         //讀取存在脈沖
while(!IO_18B20);//等待存在脈沖結(jié)束
EA = 1;                         //打開中斷
return ack;
}
/*DS18B20位寫入時(shí)序*/
void  Write18B20(unsigned char dat)
{
unsigned char mask;
EA = 0;                                                                    //關(guān)閉中斷
for(mask=0x01; mask!= 0; mask<<=1)          //低位在先,依次移出8個(gè)bit
  {
   IO_18B20 = 0;                                          //產(chǎn)生2us低電平脈沖
   _nop_();
   _nop_();
  if((mask&dat) == 0)                                  //輸出該bit值
  IO_18B20 = 0;
  else
  IO_18B20 = 1;
  DelayX10us(6);                                           //延時(shí)60us
  IO_18B20 = 1;                                           //拉高通信引腳
  }
  EA = 1;                                                               //打開中斷
}
/*DS18B20位讀取時(shí)序*/
unsigned char Read18B20()
{
unsigned char dat;
unsigned char mask;
EA = 0;                                                                        //關(guān)閉中斷
for(mask=0x01; mask!=0; mask<<=1)                //低位在先,依次采集8個(gè)bit
{
  IO_18B20 = 0;                                                 //產(chǎn)生2us低電平脈沖
  _nop_();
  _nop_();
  IO_18B20 = 1;                                                   //結(jié)束低電平脈沖,等待18B20輸出數(shù)據(jù)
  _nop_();                                                           //延時(shí)2us
  _nop_();
  if(!IO_18B20)                                                    //讀取通信引腳上的值
  dat &= ~mask;
  else
  dat |= mask;
  DelayX10us(6);                                                    //再延時(shí)60us
}
  EA = 1;                                                                                //打開中斷
  return dat;
}
/*啟動(dòng)DS18B20*/
bit Start18B20()
{
bit ack;
ack = Get18B20Ack();                 //執(zhí)行總線復(fù)位,并獲取18B20應(yīng)答
if(ack == 0)                                 //如18B20正確應(yīng)答,則啟動(dòng)一次轉(zhuǎn)換
{
  Write18B20(0xCC);           //跳過(guò)ROM,只讀一個(gè)18B20
  Write18B20(0x44);           // 啟動(dòng)溫度轉(zhuǎn)換
}
return ~ack;                           //ack==0表示操作成功,所以返回值對(duì)其取反
}
/*獲取溫度*/
bit Get18B20Temp(int *temp)
{
bit ack;
unsigned char LSB, MSB;                 //16bit溫度值的低字節(jié)和高字節(jié)
ack = Get18B20Ack();                      //執(zhí)行總線復(fù)位,并獲取18B20應(yīng)答
if(ack == 0)                                      //如18B20正確應(yīng)答,則讀取溫度值
  {
        Write18B20(0xCC);                       //跳過(guò)ROM操作
        Write18B20(0xBE);                      //發(fā)送讀命令
        LSB = Read18B20();            //讀溫度值的低字節(jié)
        MSB = Read18B20();            //讀溫度值的高字節(jié)
        *temp = ((int)MSB<<8) + LSB;  //合成為16bit整型數(shù)
  }
return ~ack;                                         //ack==0表示操作應(yīng)答,所以返回值為其取反值
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩1

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:170906 發(fā)表于 2017-4-10 23:05 | 只看該作者
大神能否把你的東西發(fā)給我,我也在做相關(guān)畢設(shè)。非常感謝!
回復(fù)

使用道具 舉報(bào)

板凳
ID:82765 發(fā)表于 2017-4-10 23:37 | 只看該作者
提示: 作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽
回復(fù)

使用道具 舉報(bào)

地板
ID:188317 發(fā)表于 2017-4-10 23:38 | 只看該作者
學(xué)習(xí)學(xué)習(xí)學(xué)習(xí)學(xué)習(xí)學(xué)習(xí)
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表