#include "reg51.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 data1=P1^0;

sbit clk=P1^1;

sbit plus=P2^0;

sbit subs=P2^1;

sbit stop=P2^2;

sbit output=P3^4;

sbit DQ=P3^3;

unsigned char flag,flag_1=0;

unsigned char high_time,low_time,count=0;//占空比调节参数

unsigned char set_temper=25;

unsigned char temper;

unsigned char i;

unsigned char j=0;

unsigned int s;

void delay(unsigned char time)

{

unsigned char m,n;

for(n=0;n

for(m=0;m<2;m++){}

}

void write_bit(unsigned char bitval)

{

EA=0;

DQ=0;

if(bitval==1)

{

_nop_();

DQ=1;

}

delay(5);

DQ=1;

_nop_();

_nop_();

EA=1;

}

void write_byte(unsigned char val)

{

unsigned char i;

unsigned char temp;

EA=0;

TR0=0;

for(i=0;i<8;i++)

{

temp=val>>i;

temp=temp&1;

write_bit(temp);

}

delay(7);

// TR0=1;

EA=1;

}

unsigned char read_bit()

{

unsigned char i,value_bit;

EA=0;

DQ=0;

_nop_();

_nop_();

DQ=1;

for(i=0;i<2;i++){}

value_bit=DQ;

EA=1;

return(value_bit);

}

unsigned char read_byte()

{

unsigned char i,value=0;

EA=0;

for(i=0;i<8;i++)

{

if(read_bit())

value|=0x01<

delay(4);

}

EA=1;

return(value);

}

unsigned char reset()

{

unsigned char presence;

EA=0;

DQ=0;

delay(30);

DQ=1;

delay(3);

presence=DQ;

delay(28);

EA=1;

return(presence);

}

void get_temper()

{

unsigned char i,j;

do

{

i=reset();

} while(i!=0);

i=0xcc;

write_byte(i);

i=0x44;

write_byte(i);

delay(180);

do

{

i=reset();

} while(i!=0);

i=0xcc;

write_byte(i);

i=0xbe;

write_byte(i);

j=read_byte();

i=read_byte();

i=(i<<4)&0x7f;

s=(unsigned int)(j&0x0f); //得到小数部分

s=(s*100)/16;

j=j>>4;

temper=i|j;

}

void PIDInit (struct PID *pp)

{

memset ( pp,0,sizeof(struct 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;

if(set_temper>temper) //是否设置的温度大于实际温度

{

if(set_temper-temper>1) //设置的温度比实际的温度是否是大于1度

{

high_time=100; //如果是,则全速加热

low_time=0;

}

else //如果是在1度范围内,则运行PID计算

{

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/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);

}

}

// else

// {}

}

void serve_T0() interrupt 1 using 1

{

if(++count<=(high_time))

output=1;

else if(count<=100)

{

output=0;

}

else

count=0;

TH0=0x2f;

TL0=0xe0;

}

void serve_sio() interrupt 4 using 2

{

}

void disp_1(unsigned char disp_num1[6])

{

unsigned char n,a,m;

for(n=0;n<6;n++)

{

// k=disp_num1[n];

for(a=0;a<8;a++)

{

clk=0;

m=(disp_num1[n]&1);

disp_num1[n]=disp_num1[n]>>1;

if(m==1)

data1=1;

else

data1=0;

_nop_();

clk=1;

_nop_();

}

}

}

void display()

{

unsigned char code number[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};

unsigned char disp_num[6];

unsigned int k,k1;

k=high_time;

k=k00;

k1=k/100;

if(k1==0)

disp_num[0]=0;

else

disp_num[0]=0x60;

k=k0;

disp_num[1]=number[k/10];

disp_num[2]=number[k];

k=temper;

k=k0;

disp_num[3]=number[k/10];

disp_num[4]=number[k]+1;

disp_num[5]=number[s/10];

disp_1(disp_num);

}

void main()

{

unsigned char z;

unsigned char a,b,flag_2=1,count1=0;

unsigned char phil[]={2,0xce,0x6e,0x60,0x1c,2};

TMOD=0x21;

TH0=0x2f;

TL0=0x40;

SCON=0x50;

PCON=0x00;

TH1=0xfd;

TL1=0xfd;

PS=1;

EA=1;

EX1=0;

ET0=1;

ES=1;

TR0=1;

TR1=1;

high_time=50;

low_time=50;

PIDInit ( &spid ); // Initialize Structure

spid.Proportion = 10; // Set PID Coefficients 比例常数 Proportional Const

spid.Integral = 8; //积分常数 Integral Const

spid.Derivative =6; //微分常数 Derivative Const

spid.SetPoint = 100; // Set PID Setpoint 设定目标 Desired Value

while(1)

{

if(plus==0)

{

EA=0;

for(a=0;a<5;a++)

for(b=0;b<102;b++){}

if(plus==0)

{

set_temper++;

flag=0;

}

}

else if(subs==0)

{

for(a=0;a<5;a++)

for(b=0;a<102;b++){}

if(subs==0)

{

set_temper--;

flag=0;

}

}

else if(stop==0)

{

for(a=0;a<5;a++)

for(b=0;b<102;b++){}

if(stop==0)

{

flag=0;

break;

}

EA=1;

}

get_temper();

b=temper;

if(flag_2==1)

a=b;

if((abs(a-b))>5)

temper=a;

else

temper=b;

a=temper;

flag_2=0;

if(++count1>30)

{

display();

count1=0;

}

compare_temper();

}

TR0=0;

z=1;

while(1)

{

EA=0;

if(stop==0)

{

for(a=0;a<5;a++)

for(b=0;b<102;b++){}

if(stop==0)

disp_1(phil);

// break;

}

EA=1;

}

}

温度控制pid c语言程序,51单片机温度PID算法(C程序)相关推荐

  1. pid控制器c51语言编程,51单片机电机pid控制系统程序

    51单片机电机pid控制系统程序 本程序来源网上,小编进行了简单修改,还未验证. /************************* 应用背景:直流电机的额定功率12V,额定转速3000rpm,光码 ...

  2. 110报警声+单片机c语言,用51单片机做发声小玩意及程序

    用51单片机做发声小玩意及程序 "嘀嘀"警报 生活中我们常常到各种各样的报警声,例如"嘀.嘀-"就是常见的一种声音报警声,但对于这种报警声,嘀0.2秒钟,然后断 ...

  3. 单片机显示屏c语言程序,51单片机驱动1602液晶显示器c程序

    51单片机驱动1602液晶显示器c程序 /* 程序效果:单片机控制液晶显示器1602显示字母数字,用户自行更改io 程序版权:http://www.51hei.com */ #include //头文 ...

  4. 51单片机c语言定时器程序,51单片机定时器设置入门与程序解析

    89C51单片机有两个计数器T0和T1,每个计数器都是由两个8位的RAM 单元组成的,即每个计数器都是16 位的计数器,最大的计数容量是216=65536,记住是从0-65535. 其次.提供给定时器 ...

  5. STC12C5A60S2最小系统板/51单片机温度显示和温度控制风扇

    STC12C5A60S2最小系统板/51单片机温度显示和温度控制风扇,板载有DS18B20和TM1650+4位数码管. 项目简介 单片机课课程设计,要做一个智能温控风扇,达到设定的下限温度值就开启风扇 ...

  6. c语言编写51单片机中断程序,执行过程是怎样的?

    Q:c语言编写51单片机中断程序,执行过程是怎样的? 例如程序: #include<reg52.h>   void main(void)   {    EA=1;      //开放总中断 ...

  7. c语言单片机定时器的应用程序,51单片机定时器中断程序(C语言)

    51c程序 51单片机定时器中断程序(C语言) 用C语言实现的,先要定义好定时器的初值 不管你使用多大的晶振,使用51单片机,一般都是12分频出来,也就可以得出一个机器周期 机器周期=12/n(n指晶 ...

  8. 单片机红外线c语言,自己写的51单片机的红外线遥控接收程序(C语言)

    自己写的51单片机的红外线遥控接收程序(C语言) (6页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 11.90 积分 //51单片机做的红外遥控实验(C ...

  9. 8255A红绿灯c语言程序,51单片机外接8255A做成的交通灯程序及PROTEUS仿真结果(附对应C语言程序).doc...

    51单片机外接8255A做成的交通灯程序及PROTEUS仿真结果(附对应C语言程序) 51单片机外接8255A做成的交通灯程序及PROTEUS仿真结果(附对应C语言程序)2010-04-21 22:0 ...

  10. 51单片机74ls273并行输出地址c语言程序,51单片机I/O口的扩展问题:使用 锁存器74LS273连接后还是只有8个端口,为什么说是扩展呢?...

    满意答案 a26264679 2014.04.21 采纳率:44%    等级:8 已帮助:313人 用8255扩展之后就是PA,PB,PC了,怎么还会只有8个端口呢? 用8255扩展51单片机 I/ ...

最新文章

  1. web项目404错误统一跳转
  2. 【转】log4net使用详解
  3. azkaban mysql参数_学习azkaban的笔记以及心得
  4. SQL疑难杂症【4 】大量数据查询的时候避免子查询
  5. linux天气软件,类似智能手机!Linux中安装Conky天气插件
  6. 3 上传分段_32式太极拳教材分段教学:【3】32式太极拳背向演示 .3/25.
  7. SSIS变量属性中EvaluateAsExpression设置的作用
  8. spark中dataframe解析_Spark 结构流处理介绍和入门教程
  9. 台湾大学生来厦门参访交流
  10. poj 2594 Treasure Exploration 匈牙利二分匹配+floyd
  11. window docker 查看镜像仓库_10.Docker容器镜像Registry仓库安全构建与GC回收
  12. 怎么把mp3格式的音频文件转为文字?
  13. Java后台获取Cookie
  14. dubbo源码解析之dubbo配置解析
  15. pr2020lut导入_PS PR AE怎么批量导入LUTS调色预设
  16. 实现局部滚动的两种方法:1.三行css代码2.使用BScroll框架
  17. 5G关键技术简介带你揭开5G神秘面纱
  18. 《手机游戏引擎架构》信息总汇
  19. 2022年最新河南建筑安全员模拟题库及答案
  20. 高级路由交换题库 答案与解释

热门文章

  1. 学习双拼必看(最全教程):双拼输入法的心得以及快速入门办法
  2. [技术杂谈][转载]cuda下载官方通道
  3. 阿里icon图标库使用说明
  4. 您应该知道的代码审查工具
  5. CAD贱人工具箱6.0免注册
  6. 5分钟学会制作自动化脚本——自动化脚本辅助开发IDE——Selenium IDE介绍(测试工程师必备)
  7. Sentaurus TCAD 2013 在RedHat7.0 Linux系统的安装教程
  8. 【电脑配置】开发人员必备,黑苹果双系统安装教程
  9. 查T结果与Z结果的P值[转载]
  10. 文字降重-临逸版-论文降重