闭环步进电机

Dependencies:   mbed

Revision:
1:cbd6a3232d5b
Parent:
0:5b4f19f8cd85
--- a/motor.cpp	Fri Mar 30 12:03:12 2018 +0000
+++ b/motor.cpp	Thu Apr 05 01:37:17 2018 +0000
@@ -8,7 +8,7 @@
 //////////////////////////////////////////////////////////////////////////////////
 void StepMotorInit(MotorType *Motor, DigitalOut *MotorDirPin, DigitalOut *EncoderCsPin, SPI *SPI_EncoderObj)
 {
-    RCC->APB1ENR|=1<<0;     //TIM2时钟使能   
+    /*RCC->APB1ENR|=1<<0;     //TIM2时钟使能   
     RCC->APB2ENR|=1<<2;     //GPIOA时钟使能 
     GPIOA->CRL&=0XFFFFFF0F; //PA1清除之前的设置
     GPIOA->CRL|=0X000000B0; //复用功能输出 
@@ -21,7 +21,24 @@
     TIM2->CCER|=1<<4;       //OC2 输出使能  
 
     TIM2->CR1=0x0080;       //ARPE使能
-    TIM2->CR1|=0x01;        //使能定时器2
+    TIM2->CR1|=0x01;        //使能定时器2*/
+    
+    RCC->APB2ENR|=1<<11;    //TIM1时钟使能    
+    RCC->APB2ENR|=1<<2;     //GPIOA时钟使能 
+    GPIOA->CRH&=0XFFFFFFF0; //PA8清除之前的设置
+    GPIOA->CRH|=0X0000000B; //复用功能输出 
+    
+    TIM1->ARR=899;          //设定计数器自动重装值 
+    TIM1->PSC=999;          //预分频器设置
+  
+    TIM1->CCMR1|=7<<4;      //CH1 PWM2模式         
+    TIM1->CCMR1|=1<<3;      //CH1预装载使能   
+    TIM1->CCER|=1<<0;       //OC1 输出使能    
+
+    TIM1->BDTR|=1<<15;      //MOE 主输出使能    
+
+    TIM1->CR1=0x0080;       //ARPE使能 
+    TIM1->CR1|=0x01;        //使能定时器1                                       
     
     //初始胡电机控制结构体
     Motor->Encoder = 0;
@@ -89,7 +106,7 @@
     Motor->ChangeRateDifference = Motor->Difference - Motor->Difference_pre;
     
     //切换参数
-    if(Math_abs(Motor->Difference) <= ApproachDistance)  //很接近目标距离了
+    /*if(Math_abs(Motor->Difference) <= ApproachDistance)  //很接近目标距离了
     {
         Motor->Ki = MotorKi_End;
         Motor->Kp = MotorKp_End;
@@ -109,7 +126,7 @@
         Motor->Kp = MotorKp;
         Motor->V_Ki = MotorVKi;
         Motor->V_Kp = MotorVKp;
-    }
+    }*/
         
         Motor->Integral += Motor->Difference;  //计算积分量
         if(Motor->Integral>15000)   Motor->Integral=15000; //积分限幅
@@ -220,7 +237,7 @@
     //500-100 720k
     //100-1 72k
     //Motor->PIDControl = 5000;
-    if(Motor->PIDControl > PulseFreqLimit)
+    /*if(Motor->PIDControl > PulseFreqLimit)
         Motor->PIDControl = PulseFreqLimit;
     else if((Motor->PIDControl <= PulseFreqLimit) && (Motor->PIDControl > 5000))
         TIM2->PSC=0;            //预分频器设置
@@ -239,9 +256,30 @@
     }
     
     Motor_ARR = APB2Freq / (TIM2->PSC+1) / Motor->PIDControl;
+    TIM2->ARR = Motor_ARR;
+    TIM2->CCR2 = Motor_ARR/2;*/
+    if(Motor->PIDControl > PulseFreqLimit)
+        Motor->PIDControl = PulseFreqLimit;
+    else if((Motor->PIDControl <= PulseFreqLimit) && (Motor->PIDControl > 5000))
+        TIM1->PSC=0;            //预分频器设置
+    else if((Motor->PIDControl <= 5000) && (Motor->PIDControl > 1000))
+        TIM1->PSC=1;            //预分频器设置
+    else if((Motor->PIDControl <= 1000) && (Motor->PIDControl > 500))
+        TIM1->PSC=35;           //预分频器设置
+    else if((Motor->PIDControl <= 5000) && (Motor->PIDControl > 100))
+        TIM1->PSC=99;           //预分频器设置
+    else if((Motor->PIDControl <= 100) && (Motor->PIDControl > 0))
+        TIM1->PSC=999;          //预分频器设置
+    else
+    {
+        Motor->PIDControl = 1;
+        TIM1->PSC=999;          //预分频器设置
+    }
+    
+    Motor_ARR = APB2Freq / (TIM1->PSC+1) / Motor->PIDControl;
     //printf("ll%dll \r\n", Motor_ARR);
-    TIM2->ARR = Motor_ARR;
-    TIM2->CCR2 = Motor_ARR/2;
+    TIM1->ARR = Motor_ARR;
+    TIM1->CCR1 = Motor_ARR/2;
 }
 
 //控制接口