PID application in motor closed-loop speed control system

Publisher:VelvetWhisperLatest update time:2016-10-29 Source: elecfansKeywords:PID Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include   
#include 
#include 
#include "lcdbn.h" 
uchar data1[4]={0,2,5,0}; 
uchar data2[4]= {0,0,0,0}; 
uchar j,key=0; 
uchar chr[]="pre: *2%"; 
uchar *chr1[3]={"openc V: ", 
                "shutc V: ", 
                "acceler: "} ; 
uchar counter1=0x84; 
uchar counter2=0xc8; 
uchar flag; //flag is the switch enable  
int E1r; 
int E2r=0,E3r=0; 
uint speedr=15,c_r=0;// maxc=700; 
void init_intr(void) 

SREG=0x80; 
TIMSK=0x40; 
TCCR2=0x06; 
TCCR1A=0xe3; //A is high level, B is low level 
TCCR1B=0x0a;  
MCUCR=0x0a; ///External INT1/0 interrupt rising valid 
GICR=0xc0;  
}  
/////////// /////////////////////////// 
/////Left and right motor PWM settings 
/////////////// ////////////////////// 
void setpwmr(uchar *p) 
{ uint temp1; 
temp1=(uint)p[1]*10+p[2]; 
if( temp1<25) 

speedr=(uint)28-temp1*28/25; 

else 

speedr=(uint)temp1*28/25-28; 

temp1=1023*temp1/50; 
OCR1A=temp1; 
OCR1B=temp1; 
}  
/////Speed ​​increase/decrease////// 
void add(uchar*p)   

if(p[1]!=5) 

p[2]+ +; 
if(p[2]==10) 

p[2]=0;p[1]++; 



void sub(uchar *p) 

uchar temp; 
temp=p[1]*20+ p[2]*2; 
if(temp!=0) 
temp=temp-2; 
p[1]=temp/20; //Setting value restoration// 
p[2]=(temp-p[1]* 20)/2; 
}  

void display() 
{ write_string(0x80,chr);  
    write_string(0xc0,chr1[key]); 
    write_order(counter1+0);  
    write_Data(data1[0]+0x30); 
   for(j=1 ;j<3;j++) 
     {write_order(counter1+j);  
      write_Data(data1[j]+0x30); 
      } 
      for(j=0;j<4;j++) 
     { 
     write_order(counter2+j);  
      write_Data(data2[j]+0x30); 
      }   

//////Speed ​​adjustment//// 
void speed_change(void) 

if(PINA.1==0) 

delay_ms(100); 
if(PINA.1==0) 
{add(data1); } 

 if(PINA.2==0) 
{delay_ms(100); 
if(PINA.2==0) 
{sub(data1);} 
}  
}  
/////Keyboard reading//// 
void testkey(void ) 
{  
uchar flag2=0; 
if(PINA!=0x0f) 
{ delay_ms(10); 
  if(PINA!=0x0f) 
  {flag2=1;} 
}   

if(flag2) 

  while(PINA.3!=0) 
 {    
  while(PINA .0==0) 
  { delay_ms(100); 
  if(PINA.0==0) 
 { 
 if(key<2) 
  {key++;} 
   
  else  
  key=0;} 
  } 
   
switch (key) 
{case 0: //Mode 1PID adjustment 
  flag=0; 
  speed_change(); 
  setpwmr(data1); 
  display(); 
  break; 
case 1: //Mode 2 open loop 
  flag=1; 
   speed_change(); 
   setpwmr(data1); 
   init_intr();  
   display() ; 
 break; 
case 2:    
display(); 
 break; 
default:break; } 

flag2=0; 

 } 
/* void speed_convert(uint c) //Convert speed measurement into pwm value 
 {  
 }*/ 
 void new_pid(uint cl,uchar k) //Integral separation method 
 { int temp3,z,p; //p is the integral coefficient. The larger the error, the smaller P 
  if(flag) 
  { E1r=speedr-cl; 
  if(speedr>0&abs( E1r)<11) 
  {   
  switch(abs(E1r)/2) 
  { 
  case 5: p=1; 
  break; 
  case 4: p=3; 
  break;  
 case 3: p=5; 
  break; 
 case 2: p=8; 
  break; 
 case 1: p=10; 
  break; 
 default : 
  break; 
  } 
  z=23*(E1r-E2r)+p*(E1r-2*E2r+E3r)-10*E3r; 
  z=(int)z*102 /k; 
  temp3=OCR1A; 
  temp3=temp3+z; 
  OCR1A=temp3; 
  OCR1B=temp3;   
  E3r=E2r; 
  E2r=E1r; 
  }  
  } 
 } 
  
/*void pid_motr(uint cr,uchar k) 

 int temp,u; 
 if(flag) 
 { 
 if(speedr>0) 
 { 
E1r=speedr-cr; 
if(E1r==speedr) //E has a large error When the maximum rated current accelerates 
{ OCR1A=maxc;  
OCR1B=maxc; } 
else if(abs(E1r)>0&&abs(E1r)<7) 

u=(int)(24*E1r-35*E2r+14*E3r)* 102/k;  
temp=OCR1A; 
temp=temp+u; 
OCR1A=temp; 
OCR1B=temp; 
E3r=E2r; 
E2r=E1r; 
}  


} */ 
 ////////Speed ​​data decomposition/// ////  
void speed_count(uint r) 

  data2[3]=r%10; 
  r=r/10; 
  data2[2]=r%10; 
  r=r/10; 
  data2[1]=r%10; 
  r=r/10; 
  data2[0]=r%10; 
}  
 /////Use external interrupt to measure speed////////// 
interrupt[EXT_INT0]void int_0(void) 
{ c_r++; 


/////T2 timing 80ms to sample speed//////// 
interrupt[TIM2_OVF]void time_2(void) 
{  
   MCUCR=0x00; 
   GICR=0x00; 
   new_pid(c_r,500); 
   //pid_motr(c_r,100); 
   speed_count(c_r); 
   MCUCR=0x0f; ///External INT1/0 interrupt rising valid 
   GICR=0xc0;   
   c_r=0; 
  
}  
void initall_IO(void) 
{  
DDRB=0xf0; 
PORTB=0x00; 
DDRA=0xF0; 
PORTA=0x0F; 
DDRC=0XFF; 
PORTC=0X00; 
DDRD=0xF0; 
PORTD=0x0F; 


  

void main(void) 
{  
   initall_IO(); 
   init_lcd(); 
   while(1) 
   { 
   DDRA=0xF0; 
   PORTA=0x0F; 
  testkey(); 
  init_intr();   
  display();  
      } 
      } 

The PID algorithm formula is a bit wrong: it should be changed to 

z=100*(E1r-E2r)+p*E1r+125*(E1r-2*E2r+E3r); 

After the teacher's guidance: the M method for speed sampling is not suitable for low speed, so for the large speed regulation range, the M/T method is used, which is suitable for high and low speed measurement! The speed measurement must also be digitally filtered! The first-order inertia/median method, average method, etc. can be used!
Keywords:PID Reference address:PID application in motor closed-loop speed control system

Previous article:Real-time clock chip DS12887 interface driver
Next article:M16-based TC1 can generate 100HZ-1MHZ frequency square wave generator

Recommended ReadingLatest update time:2024-11-16 12:48

Design of intelligent temperature control for water heater based on single chip microcomputer and fuzzy PID control
Temperature is an important physical quantity in the industrial production process, especially in the metallurgical, mechanical, food, chemical and other industries. The processing temperature of the workpiece must be strictly controlled, and there are high requirements for the accuracy and stability of the temperatur
[Microcontroller]
Design of intelligent temperature control for water heater based on single chip microcomputer and fuzzy PID control
Self-made PID program (C51, incremental PID)
void PID(void) {    //Difference VW88, voltage feedback VW50, pid operation feedback: VW10, pid given: VW700  int VW700, VW50; //0-32768~+32768  int VW500, VW502, VW504, VW506;  long VW530, VW538;  int VW526, VW522, VW524;      uchar Kp=7; //P value 0-20 book: 20 PLC test 3\1\7  uchar Ki=4; //I value 0-20 book: 01  uc
[Microcontroller]
PID formula derivation process and implementation code
1. PID block diagram:  n0(t) is the value to be stabilized n(t) is the current output value e(t) = n0(t - n(t) 1. Analog PID control principle   This formula is easy to find on the Internet:        2. Digital PID control Since it is not easy to write code for analog calculus operat
[Microcontroller]
PID formula derivation process and implementation code
Design of PID Temperature Control System Based on AT89S51 Single Chip Microcomputer
  Temperature control technology not only plays a very important role in industrial production, but also plays a vital role in daily life. This paper designs the hardware and software of the system. On the basis of establishing the mathematical model of the temperature control system, the system controller is designed
[Microcontroller]
Design of PID Temperature Control System Based on AT89S51 Single Chip Microcomputer
Playing control with STM32F407 - automatic tuning of PID parameters using relay feedback method
The principle of automatic PID parameter tuning by relay feedback method is shown in Figure 1 and Figure 2 (see Astrom "Adaptive Control"). There is also a literature called limit cycle method PID parameter automatic tuning. In Figure 1, when the system tuning switch is cut to the T position, due to the relay characte
[Microcontroller]
Playing control with STM32F407 - automatic tuning of PID parameters using relay feedback method
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号