How to implement PID algorithm in C51

Publisher:740322lwjLatest update time:2011-01-23 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When we really want to use the PID algorithm, we find that it is not so easy to implement the code in the book on our 51. Simply put, it cannot be called directly. A careful analysis will reveal that the C language code for PID implementation in the textbooks and on the Internet is almost all done with floating-point data. You can imagine how painful it would be if our calculations use floating-point data, and how painful it would be for our 51 single-chip microcomputer to run it. Therefore, I figured out how to use an integer variable to implement the PID algorithm. Since it is done with integers, it is not very accurate, but for many occasions, this accuracy is enough. The coefficients and sampling voltages are all magnified 10 times. So the accuracy is not very high, but it is not so low, and it is enough for most occasions. If you really feel that the accuracy is not enough, you can magnify it 10 times or 100 times, but be careful not to exceed the range of the entire data type. What I did is a PID algorithm with dead zone control. For specific reference code, please see the following: typedef struct PIDValue { uint32 Ek_Uint32[3]; //Difference storage, the difference between given and feedback uint8 EkFlag_Uint8[3]; //Sign, 1 corresponds to Ek[i] is a negative number, 0 corresponds to Ek[i] is a positive number uint8 KP_Uint8; uint8 KI_Uint8; uint8 KD_Uint8; uint8 B_Uint8; //Dead zone voltage uint8 KP; //Use when displaying modification uint8 KI; // uint8 KD; // uint8 B; // uint16 Uk_Uint16; //Control voltage at the last moment }PIDValueStr; PIDValueStr xdata PID; /******************************* **PID = Uk + (KP*E(k) - KI*E(k-1) + KD*E(k-2)); ********************************/ void PIDProcess(void) { uint32 idata Temp[3]; // uint32 idata PostSum; //Positive sum uint32 idata NegSum; //Negative sum Temp[0] = 0; Temp[1] = 0; Temp[2] = 0; PostSum = 0; NegSum = 0; if( ADPool.Value_Uint16[UINADCH] > ADPool.Value_Uint16[UFADCH] ) //Given is greater than feedback, then EK is a positive number { Temp[0] = ADPool.Value_Uint16[UINADCH] - ADPool.Value_Uint16[UFADCH]; //Calculate Ek[0] if( Temp[0] > PID.B_Uint8 ) { //Value shift PID.Ek_Uint32[2] = PID.Ek_Uint32[1]; PID.Ek_Uint32[1] = PID.Ek_Uint32[0]; PID.Ek_Uint32[0] = Temp[0]; //Sign shift PID.EkFlag_Uint8[2] = PID.EkFlag_Uint8[1]; PID.EkFlag_Uint8[1] = PID.EkFlag_Uint8[0]; PID.EkFlag_Uint8[0] = 0; //Current EK is a positive number Temp[0] = (uint32)PID.KP_Uint8 * PID.Ek_Uint32[0]; // KP*EK0 Temp[1] = (uint32)PID.KI_Uint8 * PID.Ek_Uint32[1]; // KI*EK1 Temp[2] = (uint32)PID.KD_Uint8 * PID.Ek_Uint32[2]; // KD*EK2 } } else //Feedback is greater than given { Temp[0] = ADPool.Value_Uint16[UFADCH] - ADPool.Value_Uint16[UINADCH]; //Calculate Ek[0] if( Temp[0] > PID.B_Uint8 ) { //Value shift PID.Ek_Uint32[2] = PID.Ek_Uint32[1]; PID.Ek_Uint32[1] = PID.Ek_Uint32[0]; PID.Ek_Uint32[0] = Temp[0]; //Sign shift PID.EkFlag_Uint8[2] = PID.EkFlag_Uint8[1];

































































PID.EkFlag_Uint8[1] = PID.EkFlag_Uint8[0];
PID.EkFlag_Uint8[0] = 1; //Current EK is negative
Temp[0] = (uint32)PID.KP_Uint8
* PID.Ek_Uint32[0]; // KP*EK0 Temp[1] = (uint32)PID.KI_Uint8 * PID.Ek_Uint32[1]; // KI*EK1
Temp[2] = (uint32)PID.KD_Uint8 * PID.Ek_Uint32[2]; // KD*EK2
}
}/*The following code is about the superposition of all positive and negative items*/
if(PID.EkFlag_Uint8[0]==0)
{
PostSum += Temp[0]; //Positive sum
}
else
{
NegSum += Temp[0]; //negative sum
} // KP*EK0
if(PID.EkFlag_Uint8[1]!=0)
{
PostSum += Temp[1]; //positive sum
}
else
{
NegSum += Temp[1]; //negative sum
} // - kI * EK1
if(PID.EkFlag_Uint8[2]==0)
{
PostSum += Temp[2]; //positive sum
}
else
{
NegSum += Temp[2]; //negative sum
} // KD * EK2
PostSum += (uint32)PID.Uk_Uint16; //
if( PostSum > NegSum ) // Is the control value positive?
{
Temp[0] = PostSum - NegSum;
if( Temp[0] < (uint32)ADPool.Value_Uint16[UMAXADCH] ) //If it is less than the limit value, it is the calculated value output
{
PID.Uk_Uint16 = (uint16)Temp[0];
}
else
{
PID.Uk_Uint16 = ADPool.Value_Uint16[UMAXADCH]; //Otherwise, it is the limit value output
}
}
else //If the control quantity output is a negative number, the output is 0
{
PID.Uk_Uint16 = 0;
}
}

Reference address:How to implement PID algorithm in C51

Previous article:Support for Dual Data Pointers and Code Generation in Keil C51
Next article:Design and implementation of infrared remote control password lock based on single chip microcomputer AT89C51RC

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号