PID调节C51程序(3)

发布者:冰山火影1977最新更新时间:2016-11-17 来源: eefocus关键字:PID调节  C51程序 手机看文章 扫描二维码
随时随地手机看文章

#include
  #include
  
  struct _pid {
   int pv; /*integer that contains the process value*/
   int sp; /*integer that contains the set point*/
   float integral;
   float pgain;
   float igain;
   float dgain;
   int deadband;
   int last_error;
  };
  
  struct _pid warm,*pid;
  int process_point, set_point,dead_band; 
  float p_gain, i_gain, d_gain, integral_val,new_integ;; 
  
  
  
  /*------------------------------------------------------------------------ 
  pid_init 
  
  DESCRIPTION This function initializes the pointers in the _pid structure 
  to the process variable and the setpoint. *pv and *sp are 
  integer pointers. 
  ------------------------------------------------------------------------*/ 
  void pid_init(struct _pid *warm, int process_point, int set_point)
  { 
   struct _pid *pid; 
   
   pid = warm; 
   pid->pv = process_point; 
   pid->sp = set_point; 
  } 
  
  
  /*------------------------------------------------------------------------ 
  pid_tune 
  
  DESCRIPTION Sets the proportional gain (p_gain), integral gain (i_gain), 
  derivitive gain (d_gain), and the dead band (dead_band) of 
  a pid control structure _pid. 
  ------------------------------------------------------------------------*/ 
  
  void pid_tune(struct _pid *pid, float p_gain, float i_gain, float d_gain, int dead_band) 
  { 
   pid->pgain = p_gain; 
   pid->igain = i_gain; 
   pid->dgain = d_gain; 
   pid->deadband = dead_band; 
   pid->integral= integral_val; 
   pid->last_error=0; 
  } 
  
  /*------------------------------------------------------------------------ 
  pid_setinteg 
  
  DESCRIPTION Set a new value for the integral term of the pid equation. 
  This is useful for setting the initial output of the 
  pid controller at start up. 
  ------------------------------------------------------------------------*/ 
  void pid_setinteg(struct _pid *pid,float new_integ)
  { 
   pid->integral = new_integ; 
   pid->last_error = 0; 
  } 
  
  /*------------------------------------------------------------------------ 
  pid_bumpless 
  
  DESCRIPTION Bumpless transfer algorithim. When suddenly changing 
  setpoints, or when restarting the PID equation after an 
  extended pause, the derivative of the equation can cause 
  a bump in the controller output. This function will help 
  smooth out that bump. The process value in *pv should 
  be the updated just before this function is used. 
  ------------------------------------------------------------------------*/ 
  void pid_bumpless(struct _pid *pid) 
  { 
  
   pid->last_error = (pid->sp)-(pid->pv); 
   
  } 
  
  /*------------------------------------------------------------------------ 
  pid_calc 
  
  DESCRIPTION Performs PID calculations for the _pid structure *a. This function uses the positional form of the pid equation, and incorporates an integral windup prevention algorithim. Rectangular integration is used, so this function must be repeated on a consistent time basis for accurate control. 
  
  RETURN VALUE The new output value for the pid loop. 
  
  USAGE #include "control.h"*/ 
  
  
  float pid_calc(struct _pid *pid)
  { 
   int err;
   float pterm, dterm, result, ferror; 
   
   err = (pid->sp) - (pid->pv); 
   if (abs(err) > pid->deadband) 
   { 
   ferror = (float) err; /*do integer to float conversion only once*/ 
   pterm = pid->pgain * ferror; 
   if (pterm > 100 || pterm < -100)
   {
   pid->integral = 0.0; 
   }
   else 
   { 
   pid->integral += pid->igain * ferror; 
   if (pid->integral > 100.0) 
   {
   pid->integral = 100.0; 
   }
   else if (pid->integral < 0.0) pid->integral = 0.0; 
   } 
   dterm = ((float)(err - pid->last_error)) * pid->dgain; 
   result = pterm + pid->integral + dterm; 
   } 
   else result = pid->integral; 
   pid->last_error = err; 
   return (result); 
  }
  
  
  void main(void)
  {
   float display_value;
   int count=0;
  
   pid = &warm;
  
  // printf("Enter the values of Process point, Set point, P gain, I gain, D gain \n");
  // scanf("%d%d%f%f%f", &process_point, &set_point, &p_gain, &i_gain, &d_gain);
  
  
  
   process_point = 30;
   set_point = 40;
   p_gain = (float)(5.2);
   i_gain = (float)(0.77);
   d_gain = (float)(0.18);
  
  
  
   dead_band = 2;
   integral_val =(float)(0.01);
  
  
   printf("The values of Process point, Set point, P gain, I gain, D gain \n");
   printf(" %6d %6d %4f %4f %4f\n", process_point, set_point, p_gain, i_gain, d_gain);
  
   printf("Enter the values of Process point\n");
  
   while(count<=20)
   {
  
  
  
   scanf("%d",&process_point);
  
   pid_init(&warm, process_point, set_point);
   pid_tune(&warm, p_gain,i_gain,d_gain,dead_band);
   pid_setinteg(&warm,0.0); //pid_setinteg(&warm,30.0);
  
   //Get input value for process point
   pid_bumpless(&warm);
  
   // how to display output
   display_value = pid_calc(&warm); 
   printf("%f\n", display_value); 
   //printf("\n%f%f%f%f",warm.pv,warm.sp,warm.igain,warm.dgain); 
   count++; 
   
   } 
  
  }

关键字:PID调节  C51程序 引用地址:PID调节C51程序(3)

上一篇:PID调节C51程序(2)
下一篇:PID调节C51程序(4)

推荐阅读最新更新时间:2024-03-16 15:20

4线EEPROM的C51程序
sbit RomCS = 0xA0^3; sbit RomCLK = 0xA0^2; sbit RomDI = 0xA0^1; sbit RomDO = 0xA0^0; #define OPCODE_BIT 3 /*功能码位数*/ #define ADDRESS_BIT 8 /*地址位数*/ #define DATA_BIT 16 /*数据位数*/ #define WAITE_TIME 500 /*设置一个最大延时等待数值.注意在不同的晶振下延时是不同的*/ /***********************************************************************/ /*名称: main()
[单片机]
如何使用AVR单片机进行数字PID调节器的设计
数字PID调节器具有操作简单,控制精度准确,安全可靠性高等优点,广泛应用于工业生产过程中。提出一种以AVR单片机ATmega16为核心的数字PID调节器,该调节器充分利用了高性能AVR单片机的片内资源及外围扩展电路,能够接收多种类型的测量信号,具有较强的在线修改和丰富的控制功能,并且采取硬件和软件双重抗干扰措施提高了调节器的可靠性。 在连续生产过程控制中,按偏差的比例(P)、积分(I)、微分(D)进行控制的PID调节器是应用最为广泛的一种自动控制器。 它具有原理简单、易于实现、适用面广等优点。在计算机用于生产过程控制之前,模拟PID控制器一直占主导地位。单片机的出现,使以单片机为核心的数字PID调节器迅速成为应用最广泛的自
[单片机]
如何使用AVR单片机进行数字<font color='red'>PID</font><font color='red'>调节</font>器的设计
一个简单的流水灯c51程序
//代码 #include reg52.h //头文件 #define uchar unsigned char //宏定义 #define uint unsigned int void delay(uint x) //延时函数(控制灯亮与灭的间隔) {  uint i,j; for(i=500;i 0;i--) for(j=x;j 0;j--); } void main() //主函数 {   uchar i; //定义局部变量   P2=0x00; //关闭所有发光二极管   while(1) //死循环   {    P2=0xfe; //第一个二极管亮    delay(100); //延时     fo
[单片机]
C51学习心得体会,递归调用程序设计举例
迭代(循环)与递归(调用)对比 (1)二者都是建立在控制结构基础上的,迭代使用的是循环结构,递归使用的是选择机构; (2)二者都用到了循环,迭代明确使用循环结构,递归通过反复调用函数实现循环; (3)二者都用到了终止条件测试,迭代在继续循环条件为假时结束,递归在到达基本实例时终止的。 (4)二者都可能是无限的。 递归举例 1、阶乘 N!=N*(N-1)*...*2*1 long factorial(long number) { if(number =1) return 1; else return (number*factorial(number-1)); } 2、求和N+(N-1)+(N-2)+...+2+1 int s
[单片机]
X5045读写一体化C51程序
void X5045SpiOpen(void);//打开X5045片选 void X5045SpiClose(void);//关闭X5045片选 void X5045WriteEnable(void);//软件使能X5045写操作 void X5045WriteDisable(void);//软件禁止X5045写操作 unsigned char X5045SpiSend(unsigned char val);//X5045收发SPI协议 void X5045WriteByte(unsigned int addr, unsigned char val);//写X5045一个字节 void X5045WriteWord(unsigned
[单片机]
C51的串口中断处理子程序
此串口处理程序是基于以下的协议写成的: 帧内容FRAME=帧头FA+帧长度len(不包括帧头帧尾)+数据流data+帧尾FB。 串口中断子程序里对整个数据流进行处理,而不是一个字节一个字节地处理,在中断中等待处理完所有的字节。 void Uart_Int(void) interrupt 4 //串口要加帧头与FA帧尾FB { unsigned char len, i; unsigned int j=0; unsigned char serialStart; if(RI) //收到数据 { serialStart=SBUF; RI=0; if(serialStart==0xFA) { w
[单片机]
C51通用串口收发数据C语言程序
#include reg52.h //C51通用串口收发数据C语言程序模块 #define uchar unsigned char #define uint unsigned int uchar shu; bit i; sbit led1=P0^0; void delayms(uint xms) //1ms; { uint i,j; for(i=xms;i 0;i--) for(j=110;j 0;j--) ; } /************************************************************************/ void UART_ini
[单片机]
24C01的IIC 读写的C51程序
/*------------------------------------------------------------------------------ 为了安全起见,程序中很多NOP是冗余的,希望读者能进一步精简,但必须经过验证。 Atmel 24C01 比较特殊,为简约型. 51晶振为11.0592MHz 〖参考〗 http://www.c51bbs.com 〖版本〗V1.00A Build 0918 -----------------------------------------------------------------------------*/ #include reg51.h #incl
[单片机]
小广播
添点儿料...
无论热点新闻、行业分析、技术干货……
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

换一换 更多 相关热搜器件
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved