導航:首頁 > 操作系統 > 單片機採集不到負值

單片機採集不到負值

發布時間:2024-09-08 20:13:56

Ⅰ 如何用PID演算法編程,使單片機通過控制繼電器來實現恆溫功能。

/***********************************************************************
PID溫度控製程序
程序說明:
系統上電後顯示 「--溫度」
表示需要先設定溫度才開始進行溫度檢測
溫度設定完畢後程序才開始進行PID溫控
***********************************************************************/
#include <reg52.h>
#include <absacc.h>
#include"DS18B20.H"
#include"PID.H"
#define uchar unsigned char
#define uint unsigned int
unsigned char code tab[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF
}
;
/*個位0~9的數碼管段碼*/
unsigned char code sao[]=
{
0x7f,0xbf,0xdf,0xef
}
;
//掃描碼
uchar set=30,keyflag=1 ; //set初始化為30° keyflag為進入溫度設定的標志位
//4個按鍵使用說明
sbit key_out=P1^0 ; //用於溫度設定後的退出
sbit key_up=P1^1 ; //設定溫度加
sbit key_down=P1^2 ; //設定溫度減
sbit key_in=P1^3 ; //在程序的運行中如需要重新設定溫度 按下此鍵才能進入設置模式並且此時是停在溫度控制的,按下key_out鍵後才表示設定完畢
void Show_key();
/***********************************************************/
void delays(unsigned char k)
{
unsigned char i,j ;
for(i=0;i<k;i++)
for(j=0;j<50;j++);
}
/*********************************************************
//數碼管顯示函數
P0口 作為數據口
P2口的低四位作為掃描口
變數 x表示掃描
d表示是否要加小數點 為1是 為0不加
y表示傳遞的數值
*********************************************************/
LCD_disp_char(uchar x,bit d,uchar y)
{
P2=0XFF ;
P0=0xFF ;
if(d==0)
P0=tab[y];
else
P0=tab[y]&0x7f ; //與上0x7f表示是否要加小數點
P2=sao[x]; //打開掃描端號

}
/*********************************************************
按鍵掃描
*********************************************************/
void keyscan(void)
{
if(key_in==0) //按鍵進入函數
{
delays(10); //延時消抖 (以下同)
if(key_in==0)
{
while(key_in==0)
{
Show_key(); //如果一直按著鍵不放 就一直顯示在當前狀態 (以下同)
}
keyflag=1 ; //按鍵標志位
}
}
/***********************/
if(key_out==0) //按鍵退出
{
delays(10);
if(key_out==0)
{
while(key_out==0)
{
Show_key();
}
keyflag=0 ;
set_temper=set ;
}
}
/*************************/
if(key_up==0) //設定溫度的加
{
delays(10);
if(key_up==0)
{
while(key_up==0)
{
Show_key();
}
if(keyflag==1)
{
set++;
if(set>90) //如果大於90°就不在加
set=90 ;
}
}
}
/*************************/
if(key_down==0) //溫度設定的減
{
delays(10);
if(key_down==0)
{
while(key_down==0)
{
Show_key();
}
if(keyflag==1)
{
set--;
if(set<30) //溫度減到30°時不在往下減
set=30 ;
}
}
}
}
/*********************************************************************
按鍵按下時的顯示函數
***********************************************************************/
void Show_key()
{
output=1 ;
LCD_disp_char(3,0,10); //顯示 -
delays(3);
LCD_disp_char(2,0,10); //顯示- (表示溫度設定 )
delays(3);
LCD_disp_char(1,0,set/10); //顯示溫度十位
delays(3);
LCD_disp_char(0,0,set%10); //顯示溫度個位
delays(3);
}
/*****************************************************************/
void main()
{
unsigned int tmp ;//聲明溫度中間變數
unsigned char counter=0 ;
PIDBEGIN(); //PID參數的初始化
output=1 ; //關閉繼電器輸出
while(1)
{
keyscan();
if(keyflag)
{
Show_key(); //顯示溫度設定
}
else
{
if(counter--==0)
{
tmp=ReadTemperature();//每隔一段時間讀取溫度值
counter=20 ;
}
LCD_disp_char(3,0,tmp/1000); //顯示溫度十位
delays(3);
LCD_disp_char(2,1,tmp/100%10); //顯示溫度個位
//顯示小數點
delays(3);
LCD_disp_char(1,0,tmp/10%10); //顯示溫度小數後一位
delays(3);
LCD_disp_char(0,0,tmp%10);//顯示溫度小數後二位
delays(3);
P2=0XFF ;
P0=0xff ;
compare_temper(); //比較溫度

}
}
}
/**********************************************************************************************************************************************/
//PID演算法溫控C語言2008-08-17 18:58
#ifndef _PID_H__
#define _PID_H__
#include<intrins.h>
#include<math.h>
#include<string.h>
struct PID
{
unsigned int SetPoint ;
// 設定目標 Desired Value
unsigned int Proportion ;
// 比例常數 Proportional Const
unsigned int Integral ;
// 積分常數 Integral Const
unsigned int Derivative ;
// 微分常數 Derivative Const
unsigned int LastError ;
// Error[-1]
unsigned int PrevError ;
// Error[-2]
unsigned int SumError ;
// Sums of Errors
}
;
struct PID spid ;
// PID Control Structure
unsigned int rout ;
// PID Response (Output)
unsigned int rin ;
// PID Feedback (Input)

sbit output=P1^4;
unsigned char high_time,low_time,count=0 ;
//占空比調節參數
unsigned char set_temper ;
void PIDInit(struct PID*pp)
{
memset(pp,0,sizeof(struct PID)); //PID參數初始化全部設置為0
}
unsigned int PIDCalc(struct PID*pp,unsigned int NextPoint)
{
unsigned 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);
// 微分項
}
/***********************************************************
溫度比較處理子程序
***********************************************************/
void compare_temper()
{
unsigned char i ;
//EA=0;
if(set_temper>temper)
{
if(set_temper-temper>1)
{
high_time=100 ; //大於1°不進行PID運算
low_time=0 ;
}
else
{ //在1°范圍內進行PID運算
for(i=0;i<10;i++)
{
//get_temper();
rin=s;
// Read Input
rout=PIDCalc(&spid,rin); //執行PID運算
// Perform PID Interation
}
if(high_time<=100) //限制最大值
high_time=(unsigned char)(rout/800);
else
high_time=100;
low_time=(100-high_time);
}
}
/****************************************/
else if(set_temper<=temper) //當實際溫度大於設置溫度時
{
if(temper-set_temper>0)//如果實際溫度大於設定溫度
{
high_time=0 ;
low_time=100 ;
}
else
{
for(i=0;i<10;i++)
{
//get_temper();
rin=s ;
// Read Input
rout=PIDCalc(&spid,rin);
// Perform PID Interation
}
if(high_time<100) //此變數是無符號字元型
high_time=(unsigned char)(rout/10000);
else
high_time=0 ;//限制不輸出負值
low_time=(100-high_time);
//EA=1;
}
}
}

/*****************************************************
T0中斷服務子程序,用於控制電平的翻轉 ,40us*100=4ms周期
******************************************************/
void serve_T0()interrupt 1 using 1
{
if(++count<=(high_time))
output=0 ;
else if(count<=100)
{
output=1 ;
}
else
count=0 ;
TH0=0x2f ;
TL0=0xe0 ;
}
void PIDBEGIN()
{

TMOD=0x01 ;
TH0=0x2f ;
TL0=0x40 ;

EA=1 ;
ET0=1 ;
TR0=1 ;

high_time=50 ;
low_time=50 ;
PIDInit(&spid);
// Initialize Structure
spid.Proportion=10 ;
// Set PID Coefficients
spid.Integral=8 ;
spid.Derivative=6 ;
spid.SetPoint=100 ;
// Set PID Setpoint

}
#endif

轉自他人程序。

閱讀全文

與單片機採集不到負值相關的資料

熱點內容
androidapk安裝流程 瀏覽:422
郵箱顯示伺服器加密 瀏覽:133
linux最低配置 瀏覽:509
java生成pdf並列印 瀏覽:357
pagerhelper源碼 瀏覽:124
收款app軟體哪個好用 瀏覽:233
哈佛管理pdf 瀏覽:742
韓國語pdf 瀏覽:129
溯源碼為啥有的有有的沒 瀏覽:155
語音組合播報演算法 瀏覽:355
華為p6手機怎麼加密圖片 瀏覽:82
豆豆引流哪個app好 瀏覽:577
php頁面框架 瀏覽:24
安卓手機如何打開pdt文件 瀏覽:140
抽獎翻牌規則演算法 瀏覽:138
如何打開電腦自主編程器 瀏覽:563
加密貨幣刑事犯罪 瀏覽:692
哪裡可以買單片機 瀏覽:444
學習緩解壓力的原因 瀏覽:514
天津ug編程教育機構排名 瀏覽:295