亚洲春色中文字幕久久久-三上亚,一吻二脱三床四吻胸,国产真实伦对白视频全集,在线毛片观看,精品成品入口黄网,国产毛aⅴ片久久久,亚洲AV色香蕉一区二区三区老师,萧皇后A级艳片,色情日本视频更新,99久久亚洲精品日本无码
標(biāo)題:
單片機(jī)PID溫控源碼,變量值為什么總是不對(duì)?
[打印本頁]
作者:
saintsus
時(shí)間:
2019-3-27 16:35
標(biāo)題:
單片機(jī)PID溫控源碼,變量值為什么總是不對(duì)?
求助論壇大神,為什么我學(xué)習(xí)改寫別人的PID溫控源碼代碼,其中PWM 和temp_PID.SetPoint 總是不對(duì),用串口調(diào)試的結(jié)果總是不對(duì)。
輸出結(jié)果: PWM:100 PWM:12288 PWM_P: 設(shè)置溫度:30 實(shí)?飾露?2619 temp_s:3000 temp_PID.SetPoint:
按理說我用了這樣的 PWM應(yīng)該在0到100之間temp_PID.SetPoint:應(yīng)該是3000才對(duì)可是輸出為什么不是,實(shí)在查不出原因,請(qǐng)大神指正。
引腳分配 lcd lcddata: P0
lcd_e: P2^7
lcd_rs: P2^6
lcd_rw: P2^5
設(shè)置按鍵 limit_choise: P //溫度上下限選擇按鍵
increase_temperature P //增加溫度限值按鍵
reduce_temperature P //減少溫度限值按鍵
蜂鳴器報(bào)警 warning P
溫度傳感器 temperature_sensor P
制熱 heatting P
制冷 refrigerating P
LED顯示 normal P //正常溫度指示燈
high_temperature P //高溫指示燈
low_temperature P //低溫指示燈
單片機(jī)源程序如下:
/*******************************************************************************
* 包含頭文件
*******************************************************************************/
#include <main.h>
/*******************************************************************************
* 變量聲明
*******************************************************************************/
uint PWM=0;
uint PWM_P;//PWM_P 功率占比顯示
uchar time_value=0;
uchar time_heatting=0;
uint temp_m=0; //實(shí)際溫度
uint up_limit_temp=30;
uint temp_s; //設(shè)置溫度*100
PID temp_PID;
uchar massage[]=0;
/*******************************************************************************
* 函數(shù)名 : void main()
* 函數(shù)功能 : 主函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void main()//主函數(shù)
{
init();//初始化函數(shù)
temp_PID.Proportion =150; // 設(shè)置 PID 系數(shù)
temp_PID.Integral =5;
temp_PID.Derivative =2;
while(1)
{
temp_s = up_limit_temp*100;
temp_PID.SetPoint = temp_s;
if(time_value==10) //讀取溫度
{
temp_m=get_temp(Ds18b20ReadTemp());
}
if(time_value==50)
{
if(temp_s-temp_m>=100)
{
PWM=100;
}
else
{
PWM = pid_calc(&temp_PID,temp_m);
if( PWM>=100 ) PWM=100;
else if(PWM<=1) PWM=1;
}
}
if(time_value==60) //顯示溫度
{
display_real_temp(temp_m);
}
if(time_value==70)
{
send_string(" PWM:");
DtoA(PWM,massage);
send_string(massage);
/*
PWM_P = PWM;
LcdWriteCom(0x80+0X40+0x0C);
LcdWriteData('0'+PWM_P/100);
LcdWriteCom(0x80+0X40+0x0D);
LcdWriteData('0'+PWM_P%100/10);
LcdWriteCom(0x80+0X40+0x0E);
LcdWriteData('0'+PWM_P%10);
*/
send_string(" PWM:");
DtoA(PWM,massage);
send_string(massage);
send_string(" PWM_P:");
DtoA(PWM_P,massage);
send_string(massage);
send_string(" 設(shè)置溫度:");
DtoA(up_limit_temp,massage);
send_string(massage);
send_string(" 實(shí)際溫度:");
DtoA(temp_m,massage);
send_string(massage);
send_string(" temp_s:");
DtoA(temp_s,massage);
send_string(massage);
send_string(" temp_PID.SetPoint:");
DtoA(temp_PID.SetPoint,massage);
send_string(massage);
send_string("\n");
}
if(time_value>=100) //指令執(zhí)行時(shí)間,一個(gè)單位時(shí)間是10ms
{
time_value=0;
}
if(time_heatting >= 500) time_heatting = 0;
if(time_heatting <= PWM*5)
{
heatting=1;
}
else
{
heatting=0;
}
}
}
/*******************************************************************************
* 函數(shù)名 : void init()
* 函數(shù)功能 : 初始化函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void init()//初始化函數(shù)
{
uint i,j;
Ds18b20Init();//Ds18b20初始化
//heatting=0;//不制熱
IT0Init();
IT1Init();
InitUart0();
InitTimer0();//定時(shí)器初始化
pid_init (&temp_PID);//PID初始化
LcdInit();//LCD初始化函數(shù)
LcdWriteCom(0x80);//第一行顯示
j=strlen(num1);
for(i=0; i<j; i++)
{
LcdWriteData(num1[i]);
delay_ms(1);
}
LcdWriteCom(0x80+0x40);//第二行顯示
j=strlen(num2);
for(i=0; i<j; i++)
{
LcdWriteData(num2[i]);
delay_ms(1);
}
LcdWriteCom(0x04); //關(guān)閉寫一個(gè)指針加1
}
/*******************************************************************************
* 函數(shù)名 : uint get_temp(uint temp)
* 函數(shù)功能 : 計(jì)算溫度函數(shù)
* 輸入 : 溫度
* 輸出 : 溫度
*******************************************************************************/
uint get_temp(uint temp)//計(jì)算溫度函數(shù)
{
float tp;
tp=temp;//因?yàn)閿?shù)據(jù)處理有小數(shù)點(diǎn)所以將溫度賦給一個(gè)浮點(diǎn)型變量
//如果溫度是正的那么,那么正數(shù)的原碼就是補(bǔ)碼它本身
temp=tp*0.0625*100+0.5;
//留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
//后面的數(shù)自動(dòng)去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)1了,小于0.5的就
//算加上0.5,還是在小數(shù)點(diǎn)后面。
return temp;
}
/*******************************************************************************
* 函數(shù)名 : void display_real_temp(uint temp)
* 函數(shù)功能 : 實(shí)時(shí)溫度顯示函數(shù)
* 輸入 : 溫度
* 輸出 : 無
*******************************************************************************/
void display_real_temp(uint temp)//實(shí)時(shí)溫度顯示函數(shù)
{
uchar datas[] = {0, 0, 0, 0}; //定義數(shù)組
datas[0] = temp % 10000 / 1000;
datas[1] = temp % 1000 / 100;
datas[2] = temp % 100 / 10;
datas[3] = temp % 10;
LcdWriteCom(0x80+0x0a); //寫地址 80表示初始地址
LcdWriteData('0'+datas[0]); //十位
LcdWriteCom(0x80+0x0b); //寫地址 80表示初始地址
LcdWriteData('0'+datas[1]); //個(gè)位
LcdWriteCom(0x80+0x0d); //寫地址 80表示初始地址
LcdWriteData('0'+datas[2]); //顯示小數(shù)點(diǎn)
LcdWriteCom(0x80+0x0e); //寫地址 80表示初始地址
LcdWriteData('0'+datas[3]); //顯示小數(shù)點(diǎn)
}
/*******************************************************************************
* 函數(shù)名 : void InitTimer0(void)
* 函數(shù)功能 : 定時(shí)器初始化函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void InitTimer0(void)
{
TMOD &= 0xF0;
TMOD |= 0x01;//設(shè)置定時(shí)模式1
TH0 = 0xD8;
TL0 = 0xF0;
EA = 1;
ET0 = 1;
TR0 = 1;
}
/*******************************************************************************
* 函數(shù)名 : void Timer0Interrupt(void) interrupt 1
* 函數(shù)功能 : 定時(shí)器中斷函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0xD8;
TL0 = 0xF0;
time_value++; //功能計(jì)數(shù)值+1
time_heatting++; //加熱計(jì)數(shù)值+1
}
/*******************************************************************************
* 函數(shù)名 : void INIT0(void) interrupt 0
* 函數(shù)功能 : 外部中斷0執(zhí)行函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void INIT0(void) interrupt 0
{
delay_ms(5);
if (increase_temperature == 0)
{
while(increase_temperature == 0);
if(up_limit_temp<=110)
{
up_limit_temp=up_limit_temp+5;
LcdWriteCom(0x80+0X40+0x03);
LcdWriteData('0'+up_limit_temp%1000/100);
LcdWriteCom(0x80+0X40+0x04);
LcdWriteData('0'+up_limit_temp%100/10);
LcdWriteCom(0x80+0X40+0x05);
LcdWriteData('0'+up_limit_temp%10);
}
}
}
/*******************************************************************************
* 函數(shù)名 : void INIT1(void) interrupt 2
* 函數(shù)功能 : 外部中斷1執(zhí)行函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void INIT1(void) interrupt 2
{
delay_ms(5);
if (reduce_temperature == 0)
{
while(reduce_temperature == 0);
if(up_limit_temp>=20)
{
up_limit_temp=up_limit_temp-5;
LcdWriteCom(0x80+0X40+0x03);
LcdWriteData('0'+up_limit_temp%1000/100);
LcdWriteCom(0x80+0X40+0x04);
LcdWriteData('0'+up_limit_temp%100/10);
LcdWriteCom(0x80+0X40+0x05);
LcdWriteData('0'+up_limit_temp%10);
}
}
}
/*******************************************************************************
* 函數(shù)名 : void IT0Init(void)
* 函數(shù)功能 : 外部中斷0初始化
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void IT0Init(void)
{
IT0 = 0;//0為電平,1為下降沿
EX0 = 1;//外部中斷1
}
/*******************************************************************************
* 函數(shù)名 : void IT1Init(void)
* 函數(shù)功能 : 外部中斷1初始化
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void IT1Init(void)
{
IT1 = 0;//0為電平,1為下降沿
EX1 = 1;//外部中斷0
}
/*******************************************************************************
* 函數(shù)名 : void InitUart0(void)
* 函數(shù)功能 : 串口中斷初始化
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void InitUart0(void)
{//設(shè)計(jì)波特率:4800bps;實(shí)際波特率:4464bps
TMOD&=0x0F;
TMOD|=0x20;
SCON=0x50;
PCON|=0x80;
TH1 = 0xF3;
TL1 = 0xF3;
ES = 1; //使能串口中斷 ,無論是TI/RI出現(xiàn),只要中斷打開,單片機(jī)就進(jìn)入中斷函數(shù)。
TR1 = 1;
//ET1 = 0;//禁止T1中斷
}
/*******************************************************************************
* 函數(shù)名 : void IT1Init(void)
* 函數(shù)功能 : 串口中斷函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void Usart() interrupt 4
{
//uchar receiveData;
//receiveData=SBUF;//出去接收到的數(shù)據(jù)
RI = 0;//清除接收中斷標(biāo)志位
//SBUF=receiveData;//將接收到的數(shù)據(jù)放入到發(fā)送寄存器
//while(!TI); //等待發(fā)送數(shù)據(jù)完成
//TI=0; //清除發(fā)送完成標(biāo)志
//massage[0]=0;
}
/*******************************************************************************
* 函數(shù)名 : void send_byte(uchar by)
* 函數(shù)功能 : 串口發(fā)送字節(jié)函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void send_byte(uchar by)
{
SBUF = by;
while(!TI);//當(dāng)寫下這句的時(shí)候,就不要在中斷函數(shù)里面在寫TI = 0;這句了,不然進(jìn)入中斷函數(shù)將TI清零之后,程序就會(huì)一直卡在這里
TI = 0; //在這里將TI清零
}
/*發(fā)送一個(gè)字符串*/
/*******************************************************************************
* 函數(shù)名 : void send_string(uchar *p)
* 函數(shù)功能 : 串口發(fā)送字符串函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void send_string(uchar *p)
{
while(*p!= '\0')
{
send_byte(*p);
p++;
}
}
/*******************************************************************************
* 函數(shù)名 : void DtoA(unsigned long dat, unsigned char* buffer)
* 函數(shù)功能 : 串口發(fā)送字符串函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void DtoA(uint dat, unsigned char* buffer)
{
uint tmp = 0;
char length = 0;
tmp = dat;
while(tmp != 0)//求出數(shù)字的實(shí)際長度
{
tmp = tmp/10;
length++;
}
buffer[length] = '\0';//長度數(shù)為字符串截止位
length--;
while(length >= 0)//數(shù)字的低位放入數(shù)組的高位
{
tmp = dat%10;
buffer[length--] = 0x30|tmp;
dat = dat/10;
}
}
復(fù)制代碼
全部資料51hei下載地址:
PID恒溫控制器.zip
(113.68 KB, 下載次數(shù): 20)
2019-3-27 16:34 上傳
點(diǎn)擊文件名下載附件
作者:
ld345649543
時(shí)間:
2019-4-25 12:58
沒有看到PID實(shí)現(xiàn)的函數(shù)呀!
歡迎光臨 (http://www.denmoz.com/bbs/)
Powered by Discuz! X3.1