PID control algorithm program based on AVR ATMega16

Publisher:EternalWhisperLatest update time:2016-09-18 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Recently, I have some free time, so I thought of making a PID design. I collected a lot of theories about PID control on the Internet, so I planned to use mega16L to make a PID test program. I found some unexpected errors. I wonder if you have encountered similar phenomena as mine: I defined a PID structure, and I couldn’t set the initialization value of each element to 0 during initialization (see the simulation diagram below). Moreover, some parameters in the PID structure should remain unchanged throughout the PID operation, but the parameters that should not change have changed during the PID operation. I don’t know why, and I still can’t find out the reason. Friends who are interested can join the discussion or friends with experience can give relevant help, thank you!
The detailed code and simulation are as follows: (My hardware system is the min Mega16/32 + JTAG ICE of this site)
  1. #include "config.h"
  2.  
  3. struct _PID
  4. {
  5. float PVn; //feedback signal variable
  6. float SPn; //set value
  7. float Mn; //PID calculation result
  8. float Kc; //proportional coefficient
  9. float Ts; //sampling time (ms)
  10. float Ti; //Integral time (ms)
  11. float Td; //differential time (ms)
  12. float Mx; //Integral item adjustment parameter
  13. float PVn_1; //previous feedback variable
  14. float MPn; //result value of the proportional term
  15. float MIn; //Result value of integral term
  16. float MDn; //Result value of the differential term
  17. };
  18.  
  19. struct _PID *myPID;
  20. void init_myPID(void);
  21. void init_ports(void);
  22. void init_device(void);
  23. float MPn_value(struct _PID *PID);
  24. float MIn_value(struct _PID *PID);
  25. float MDn_value(struct _PID *PID);
  26. float Mx_value(struct _PID *PID);
  27. float Mn_value(struct _PID *PID);
  28.  
  29.  
  30. void main (void)
  31. {
  32. init_device();
  33. init_myPID();
  34.  
  35. myPID->SPn = 155.5;
  36. myPID->Kc = 13.2;
  37. myPID->Ts = 0.2;
  38. myPID->If = 600.0;
  39. myPID->Td = 0.0;
  40. myPID->PVn = 108.2;
  41.  
  42.  
  43. while(1)
  44. {
  45. myPID->MPn = MPn_value(myPID);
  46. myPID->MDn = MDn_value(myPID);
  47. myPID->Mx = Mx_value(myPID);
  48. myPID->MIn = MIn_value(myPID);
  49. myPID->Mn = Mn_value(myPID);
  50. myPID->PVn_1 = myPID->PVn;
  51. }
  52.  
  53. }
  54.  
  55. /******************************************************************************/
  56. void init_myPID(void)
  57. {
  58. myPID->PVn = 0.0;
  59. myPID->SPn = 0.0;
  60. myPID->Mn = 0.0;
  61. myPID->Kc = 0.0;
  62. myPID->Ts = 0.0;
  63. myPID->If = 0.0;
  64. myPID->Td = 0.0;
  65. myPID->Mx = 0.0;
  66. myPID->PVn_1 = 0.0;
  67. myPID->MPn = 0.0;
  68. myPID->MIn = 0.0;
  69. myPID->MDn = 0.0;
  70. }
  71.  
  72. //------------------------------------------------------------------------------
  73. void init_ports(void)
  74. {
  75. PORTA = 0x00; //If ADC Function was be used,the PORTA could`t set bit 1
  76. DDRA = 0x00; //the port set input mode.
  77. PORTB = 0x00;
  78. DDRB = 0x00;
  79. PORTC = 0x00; //m103 output only
  80. DDRC = 0x00;
  81. PORTD = 0x00;
  82. DDRD = 0x00;
  83. }
  84.  
  85. //------------------------------------------------------------------------------
  86. void init_device(void)
  87. {
  88. CLI();
  89. init_ports();
  90. MCUCR = 0x00; //Set Power control(State:Close)
  91. GICR = 0x00; //Set boot guide(State:Close).
  92. SEI(); //re-enable interrupts
  93. //all peripherals are now initialized
  94. }
  95.  
  96.  
  97. // Calculate the value of the proportional term
  98. //------------------------------------------------------------------------------
  99. float MPn_value(struct _PID *PID)
  100. {
  101. float myMPn = 0.0;
  102. myMPn = PID->Kc *( PID->SPn - PID->PVn);
  103. return myMPn;
  104. }
  105.  
  106. // Calculate the value of the integral term
  107. //------------------------------------------------------------------------------
  108. float MIn_value(struct _PID *PID)
  109. {
  110. float myMIn = 0.0;
  111. myMIn = PID->Kc*(PID->Ts/PID->Ti)*(PID->SPn - PID->PVn) + PID->Mx;
  112. return myMIn;
  113. }
  114.  
  115.  
  116. //Calculate the value of the differential term
  117. //------------------------------------------------------------------------------
  118. float MDn_value(struct _PID *PID)
  119. {
  120. float myMDn = 0.0;
  121. myMDn = PID->Kc * (PID->Td/PID->Ts) * (PID->PVn_1 - PID->PVn);
  122. return myMDn;
  123. }
  124.  
  125. //Calculate the result of PID
  126. //------------------------------------------------------------------------------
  127. float Mn_value(struct _PID *PID)
  128. {
  129. float myMn = 0.0;
  130. myMn = PID->MPn + PID->MIn + PID->MDn;
  131. return myMn;
  132. }
  133.  
  134. //Calculate the adjustment value of the integral term
  135. //------------------------------------------------------------------------------
  136. float Mx_value(struct _PID *PID)
  137. {
  138. float myMx = 0.0;
  139. if(PID->Mn > 1.0)
  140.    {
  141.    myMx = 1.0 - (PID->MPn + PID->MDn);
  142.    }
  143. else if(PID->Mn < 0.0)
  144.    {
  145.    myMx = -(PID->MPn + PID->MDn);
  146.    }
  147. return myMx;
  148. }

 

 

PID control algorithm program based on AVR ATMega16 - Fengyuli - Fengyuli's blog

The simulation results when running to the PID initialization function: void init_myPID(void) are as follows: Unable to initialize all to 0

 

 

PID control algorithm program based on AVR ATMega16 - Fengyuli - Fengyuli's blog

Run to:
myPID->SPn = 155.5;
   myPID->Kc = 13.2;
   myPID->Ts = 0.2;
   myPID->Ti = 600.0;
   myPID->Td = 0.0;
   myPID->PVn = 108.2;

After reassignment, some parameters Ts are not 0.2

Keywords:AVR Reference address:PID control algorithm program based on AVR ATMega16

Previous article:How to download AVR ISP download line in AVR Studio environment
Next article:AVR interrupt usage example

Recommended ReadingLatest update time:2024-11-16 19:25

Intelligent air conditioner remote controller based on Atmega16 microcontroller
  1 Introduction   In recent years, computer technology, modern communication technology and automatic control technology have developed rapidly. With the development of new technologies, intelligent home systems have also entered thousands of households. Home systems such as air conditioners, televisions, lighting
[Microcontroller]
Intelligent air conditioner remote controller based on Atmega16 microcontroller
Application of AVR microcontroller in industrial control system
1 Introduction The application of single-chip microcomputer in the field of industrial control is different from that in the civil and commercial fields. The environment of industrial control is relatively harsh, with many interference sources. The common interference sources come from the electrostatic induction, pea
[Microcontroller]
Application of AVR microcontroller in industrial control system
Popular explanation of PID control of single chip microcomputer
Control model: You control a person to walk 110 steps and then stop using PID control. 1. P proportional control means that he is asked to walk 110 steps. He will stop after walking more than 100 steps (such as 108 steps) or more than 100 steps (such as 112 steps) at a certain pace. Description: P proportional contr
[Microcontroller]
AVR microcontroller tutorial - lighting up the first LED
After so much preparation, we can finally start doing something with the development board. There are some differences between microcontroller programming and computer programming. Programs must have zero or more inputs and one or more outputs, which both have, but the input and output of computer programming mainly
[Microcontroller]
AVR microcontroller tutorial - lighting up the first LED
AVR microcontroller serial port receiving and sending program
#include "iom16v.h" //header file, different compilers have different   /*Serial port initialization function*/ //Use the same serial port for sending and receiving, mega162 has dual serial port function void Uart_Init(void)  {  UCSRA = 0x02; /*speed*/  UCSRB = 0x18; /*allow receiving and sending*/  UCSRC = 0x
[Microcontroller]
Playing with STM32F407 - Conventional PID Control
The general PID control or algorithm is as follows: uc=Kc*(e+∫e*dt/Ti+Td*de/dt) Or expressed as a transfer function: Gc(s)=Kc*(1+1/(Ti*s)+Td*s) If the PID algorithm is implemented by computer, numerical integration and numerical differentiation must be used to replace integration and differentiation operations, that i
[Microcontroller]
Playing with STM32F407 - Conventional PID Control
AVR MCU Timer/Counter Study Notes (I)
(For specific register configuration, see the mega16 data sheet)      Timer/Counter 1 (16-bit) has normal mode, CTC mode, fast PWM mode, phase correction PWM mode and other operating modes. 1. Normal mode (set to overflow interrupt working mode, vector number is 9)      Working principle: Under the set clock frequency
[Microcontroller]
Implementation and simulation of fuzzy PID self-tuning control algorithm based on single chip microcomputer
0 Introduction Due to the inherent characteristics of hydraulic servo systems (such as dead zone, leakage, time-varying damping coefficients, and the presence of load disturbances), the system often exhibits typical uncertainty and nonlinear characteristics. It is generally difficult to accurately describe
[Microcontroller]
Implementation and simulation of fuzzy PID self-tuning control algorithm based on single chip microcomputer
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号