DS18B20+51單片機+PID
單片機源程序如下:
- /************* 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)//讀取溫度
- {
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
DS18b20 PID 水溫控制.rar
(1.9 KB, 下載次數: 145)
2017-5-16 18:00 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|