⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 text2.c

📁 PID算法程序, 可以用的 用于调速等方向
💻 C
字号:
#include <stdio.h>
#include<reg51.h>
#include<absacc.h>
#include<intrins.h>
#include<math.h>
struct _pid {
int pv;
int sp;
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;






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; 
} 





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; 
} 





void pid_setinteg(struct _pid *pid,float new_integ)
{
pid->integral = new_integ; 
pid->last_error = 0; 
}





void pid_bumpless(struct _pid *pid) 
{
pid->last_error = (pid->sp)-(pid->pv); 
} 



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





float PID(float process_point)
{
float display_value;
int count=0;
pid = &warm;
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);
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_bumpless(&warm);
display_value = pid_calc(&warm); 
count++; 
}
return display_value;
}




/********常用命令及参数定义********/
#define DISPON     0x3f
#define DISPOFF    0x3e
#define DISPFIRST  0xc0
#define SETX       0x40
#define SETY       0xb8
#define LCDBUZY    0x80
#define L          0x00
#define R          0x40
#define LIMIT      0x80
#define uchar unsigned char
#define uint unsigned int
uint bai,shi,ge;
uchar temp,tempshedin;
int k=0;
uchar dispbuf[8];
float e[50],y[50],u[50];
sbit  DQ=P1^3;
sbit  Qo=P1^7;	  
sbit  key1=P1^0;
sbit  key2=P1^1;
sbit  key3=P1^2;
sbit  lck = P3^5;//锁存信号

uchar temper[2];
uchar cbyte;
uchar data statu;
bit xy;
void WrL(uchar x);
void WrR(uchar x);
void Lcmcls(void);
void delay1s(void);
void Lcminit (void);
void delay(unsigned int time);
void VtoH8x16change(uchar *hzbuf);
void Puthalf(uchar *strch,uchar row,uchar col);
void Wrdata(uchar x,uchar row,uchar col);
void Locatexy(uchar row,uchar col);
void vWrite8x16Character(uchar *ch,uchar row,uchar col,bit flag);
void vWrite8x16String(uchar  *str,uchar col, uchar row, bit flag);

2501


/***********************键盘扫描***********************************/   
void keyscan()
{ 
     if(key1==0)
    	{
         delay(10);
         if(key1==0)
           {
    		ge++; 
    		if(ge==10)
     		  {ge=0;shi++;
					if(shi==10)
						{
							shi=0;bai++;
						}
              }
			 }
             while(!key1);
             delay(10);
             while(!key1);
    	}	
    	if(key2==0)
    		{
              delay(10);
              if(key2==0)
                 {
    			   ge--; 
    		      if(ge==0)
    			     {
					   ge=9;shi--;
					   if(shi==0)
						   {
							shi=9;bai--;
						   }
				     }
     
                 }
                 while(!key2);
                 delay(10);
                 while(!key2);
    		}
      tempshedin=bai*10+shi+ge*0.1;
}


/****************************************/ 
                                                                                                                              
     /*名称

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -