Implementing PID Algorithm in 51 Single Chip Microcomputer with C Language

Publisher:甜美瞬间Latest update time:2016-05-17 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//pid.h
#ifndef __PID__
#define __PID__
/*PID = Uk + KP*[E(k)-E(k-1)]+KI*E(k)+KD*[E(k)-2E(k-1)+E(k-2)]; (Incremental PID formula)
Function entry: RK (set value), CK (actual value), KP, KI, KD
Function export: U(K)*/
typedef struct PIDValue
{
	int8 KP;
	int8 KI;
	int8 KD;
	int8 F;
	int8 BITMOV;
	int EK[3];
	
	int UK;
	int RK;
	int CK;
	int UK_REAL;

}pid_str;
//PIDValueStr PID;
void pid_exe(pid_str *PID);
#endif

//pid.c
/*PID = PID->UK_REAL + PID->KP*[E(k)-E(k-1)]+PID->KI*E(k)+PID->KD*[E(k)-2E(k-1)+E(k-2)]; (Incremental PID formula)
Function entry: PID->RK (set value), PID->CK (actual value), PID->KP, PID->KI, PID->KD
Function export: U(K)*/
#include "defines.h"
#include "pid.h"
#define MAXOUT 0xff
//#define MAXGAP 100

void pid_exe(pid_str*PID)
{
	PID->EK[2]=PID->EK[1];
	PID->EK[1]=PID->EK[0];
	PID->EK[0]=PID->RK-PID->CK;
	PID->UK_REAL=PID->UK_REAL
		+PID->KP*(PID->EK[0]-PID->EK[1])//differentiate once and integrate to get the original number
		+(float)PID->KI*PID->EK[0]/PID->F//direct integration
		+(float)PID->KD*(PID->EK[0]-2*PID->EK[1]+PID->EK[2])*PID->F; //Integration after second-order differential is first-order differential
	if((PID->UK_REAL>>PID->BITMOV)>=MAXOUT)
	{
		PID->UK=MAXOUT;
	}else if(PID->UK_REAL>>PID->BITMOV<=-MAXOUT)
	{
		PID->UK=-MAXOUT;
	}else
	{
		PID->UK=PID->UK_REAL>>PID->BITMOV;
	}
		
}

The code I wrote here uses an incremental PID (i.e. UK_REAL + PID->KP*[E(k)-E(k-1)]+PID->KI*E(k)+PID->KD*[E(k)-2E(k-1)+E(k-2)]; this sentence corresponds to the value of the PID control amount added to the previous PID control amount, which is equivalent to taking a derivative). The final output result is the cumulative output of each operation.

Attached is an excerpt of the difference between positional pid and incremental pid.

(1) The output of the position PID control is related to the entire past state, and the accumulated value of the error is used; while the output of the incremental PID is only related to the error between the current beat and the previous two beats, so the accumulated error of the position PID control is relatively larger;

(2) The incremental PID control output is the control quantity increment and has no integral effect. Therefore, this method is suitable for objects whose actuators have integral components, such as stepper motors, while the position PID is suitable for objects whose actuators do not have integral components, such as electro-hydraulic servo valves.

(3) Since the incremental PID output is the increment of the control quantity, if the computer fails, the impact of the false operation is small, and the actuator itself has a memory function and can remain in place, which will not seriously affect the operation of the system. The output of the position type directly corresponds to the output of the object, so it has a greater impact on the system.

Reference address:Implementing PID Algorithm in 51 Single Chip Microcomputer with C Language

Previous article:Serial port interrupt mode in 51 single chip microcomputer
Next article:51 MCU UART serial communication mode 1 implementation

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号