C51 implements PID algorithm code

Publisher:SerendipityDawnLatest update time:2012-07-03 Source: 21ic Keywords:C51 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; //The current EK is a negative 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
}
}[page]

/*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 amount 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, it will output 0
{
PID.Uk_Uint16 = 0;
}

Keywords:C51 Reference address:C51 implements PID algorithm code

Previous article:Design of a handheld electronic nose based on STC89C516RD+ single chip microcomputer
Next article:16X2 character LCD display module driver

Recommended ReadingLatest update time:2024-11-16 16:46

How to read and write CAT24C32 using C51 microcontroller
#include #include //My custom LCD1602 header file /*--------------------------------------------------------------- 24C32 can store 4K (1024*4) bytes (8 bits), so the maximum addressing address is 0x0FFF, and 24C32 is a slave. ----------------------------------------------------------------*/ #defineWriteDev
[Microcontroller]
How to read and write CAT24C32 using C51 microcontroller
Use of C51 MCU P4 port
P4 of C51 MCU (STC11L32/48XE) can be used as normal IO like other IO.    As shown in the figure:
[Microcontroller]
Use of C51 MCU P4 port
Use of Keil C51 software
The board is equipped with an EEPROM memory, model AT24C02. The hardware connection of the board is: SCL--- P2.4, SDA--- P2.5. Straightforward, paste the program! ========================24cxx.h文件=========================#ifndef _24CXX_H_#define _24CXX_H_#include  msp430x14x.h #define WRITE_ADDR  0xA0  //A0 A1 A2均接
[Microcontroller]
Use of Keil C51 software
C51 Compiler - Language Extension (3) - Pointers
Pointers Cx51 supports the use of the character * to declare a pointer type variable. Cx51 pointers can perform all the functions of standard C. However, due to the special architecture of 8051 and its variants, Cx51 uses two types of pointers: memory-specific pointers and generic pointers. Generic Pointers A gene
[Microcontroller]
c51: 16-bit keyboard program 1
1. Program Function 1. Realize the 16-bit keyboard input function of 0~f, and output the numbers represented by the keys through port P1. 2. Code #include"reg52.h" #define uchar unsigned char   //16-bit keyboard row flip method corresponding key value. //The corresponding key value is 0~f flying code kco
[Microcontroller]
In the Keil C51 environment, some Chinese characters cannot be displayed on the LCD
     A small project, writing a program in keilC51, displaying a menu on a 12864 LCD with a font library. When writing the Chinese character "数" into the LCD, the LCD cannot display normally, but other Chinese characters can be replaced. After many unsuccessful debugging, I suspected that there was a problem with the c
[Microcontroller]
Keil C51 Tutorial --- C51 vs Standard C (Part 3)
One of the keys to learning C51 is to deeply understand and apply the extensions of C51 to standard ANSI C. This is because most of the extensions are directly targeted at the 8051 series CPU hardware. There are roughly 8 categories:         ●8051 storage type and storage area         ●Storage mode         ●Memory typ
[Microcontroller]
240x128 LCD T6963 controller driver (C51)
Introduction: This article mainly introduces the 240x128 LCD T6963 controller driver (C51). #include "absacc.h" #include "math.h" #include "ASCII816.h" //Standard ASCII library #include "HZTable.h" //Chinese character dot matrix library (self-made) #include "menu.h" //Menu library (self-made) #define ComPort 0x
[Microcontroller]
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号