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;
}
}
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- BLDC driver based on GD32
- The development board mounts the NFS server shared directory
- Want to learn about Microchip PIC32MZ DA Disc Graphics Development Board? Welcome to sign up for the online seminar
- ADXL345 sensor acceleration detection in vibration environment
- Contactless facial recognition access control system
- Look! Moderator benefits have increased again! New rules for moderators announced
- Basic knowledge of NFC architecture and standards involved
- [Perf-V Evaluation] Exception (interrupt) handling of Hummingbird E203
- Calculation and setting of baud rate of msp430 microcontroller
- What challenges do you face in powering handling and harvesting robots? Let’s talk with Vicor engineers!