소스 검색

更改了周期和保护

danny wang 1 년 전
부모
커밋
273ad32ad5

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+/F2803x_FLASH/

BIN
F2803x_FLASH/DLOG4CHC.obj


BIN
F2803x_FLASH/DSP2803x_CpuTimers.obj


BIN
F2803x_FLASH/DSP2803x_GlobalVariableDefs.obj


BIN
F2803x_FLASH/DSP2803x_usDelay.obj


BIN
F2803x_FLASH/HVBLDC_Sensored-DevInit_F2803x.obj


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 371 - 361
F2803x_FLASH/HVBLDC_Sensored.map


BIN
F2803x_FLASH/HVBLDC_Sensored.obj


BIN
F2803x_FLASH/HVBLDC_Sensored.out


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 262 - 262
F2803x_FLASH/HVBLDC_Sensored_linkInfo.xml


BIN
F2803x_FLASH/UserSource/UserCan.obj


BIN
F2803x_FLASH/UserSource/lowpass.obj


BIN
F2803x_FLASH/UserSource/var.obj


BIN
F2803x_FLASH/include/device_support/f2803x/v121/DSP2803x_common/source/DSP2803x_CodeStartBranch.obj


BIN
F2803x_FLASH/include/device_support/f2803x/v125/DSP2803x_common/source/DSP2803x_ECan.obj


BIN
F2803x_FLASH/include/libs/app_libs/motor_control/math_blocks/v4.0/pi.obj


BIN
F2803x_FLASH/include/libs/app_libs/motor_control/math_blocks/v4.0/rmp_cntl.obj


BIN
F2803x_FLASH/include/libs/app_libs/motor_control/math_blocks/v4.0/speed_pr.obj


+ 491 - 503
HVBLDC_Sensored.c

@@ -23,8 +23,8 @@ Description:	Primary system file for the (Trapezoidal) Sensored BLDC Control
 void MemCopy();
 void InitFlash();
 #endif
-#define TIMER1_PER1 60
-#define TIMER1_PER2 500000
+#define TIMER1_S1 60
+#define TIMER1_S2 500000
 #define TIMER1_PER 30000000
 // Prototype statements for functions found within this file.
 interrupt void MainISR(void);//主中断
@@ -37,8 +37,9 @@ void speed_direction(Uint16 hall,Uint16 hall_last);//使
 void speed_cal(void);//使用中断计算转速
 void pwmlimit_speed(void);//
 void ClosePwm(void);
+void bldcpwm_close(void);
 void OpenPwm(void);
-
+void mainLoop(void);
 // State Machine function prototypes
 //------------------------------------
 int16 MastIsrTimePercent(void);
@@ -144,6 +145,8 @@ Uint16 pwm_limit_table2[]= {950,1650,2320,3050,3750,4460,5200,5929,6623,7358,805
                            22602,23232,23935,24609,25240};
 
 int32 pwmlimitrate = 32768;
+
+Uint32 IsrTicker=0;
 void main(void)
 {
 	
@@ -177,10 +180,10 @@ void main(void)
    StartCpuTimer0();
 
 // Configure CPU-Timer 1,2 for background loops 
-   ConfigCpuTimer(&CpuTimer1, TIMER1_PER1, TIMER1_PER2);//2hz
-  // ConfigCpuTimer(&CpuTimer2, 60, 50000);//20HZ
+   ConfigCpuTimer(&CpuTimer1, TIMER1_S1, TIMER1_S2);//2hz
+   ConfigCpuTimer(&CpuTimer2, 60, 1000000);//20HZ
    StartCpuTimer1();
-  // StartCpuTimer2();
+   StartCpuTimer2();
 
 	
 // Reassign ISRs. 
@@ -211,7 +214,7 @@ void main(void)
     GpioIntRegs.GPIOXINT1SEL.bit.GPIOSEL = 16;  //定位到哪个引脚中断
     GpioIntRegs.GPIOXINT2SEL.bit.GPIOSEL = 24;   // XINT2 is GPIO24
     EDIS;
-    XIntruptRegs.XINT1CR.bit.POLARITY = 1;                 // 1为上升沿,0和2为下降沿,3为双边沿
+    XIntruptRegs.XINT1CR.bit.POLARITY = 0;                 // 1为上升沿,0和2为下降沿,3为双边沿
     XIntruptRegs.XINT2CR.bit.POLARITY = 3;                 // 1为上升沿,0和2为下降沿,3为双边沿
     //中断配置步骤-----1,开启模块中断使能,
     XIntruptRegs.XINT1CR.bit.ENABLE = 1;
@@ -280,13 +283,10 @@ void main(void)
 // IDLE loop. Just sit and loop forever:	
 	for(;;)  //infinite loop
 	{
-		//BackTicker++;
-		taskfree();
-		// State machine entry & exit point
-		//===========================================================
-		//(*Alpha_State_Ptr)();	// jump to an Alpha state (A0,B0,...)
-		//===========================================================
-
+	    if(IsrTicker%2 ==0)
+	    {
+	        mainLoop();
+	    }
 	}
 } //END MAIN CODE
 
@@ -301,468 +301,17 @@ void main(void)
 // =============================== MAIN ISR =====================================
 // ==============================================================================
 
-Uint32 IsrTicker=0;
-interrupt void MainISR(void)
 
+interrupt void MainISR(void)
 {
 
 //--------------------------------------------------------------------------------------
 
 // Verifying the ISR
     IsrTicker++;
-    CanMaster();
-    SerialCommsTimer++;
-    if(SerialCommsTimer>0x3FFF){
-        SerialCommsTimer = 0x3FFF;
-    }
-
-    ServiceDog();
-    speed_cal();
-    pwmlimit_speed();
-    taskfree();
-    FaultTreat();
-#if (BUILDLEVEL==LEVEL1)
-
-// ------------------------------------------------------------------------------
-//    Connect inputs of the RMP3 module and call the RAMP Control 3 macro.
-// ------------------------------------------------------------------------------
-      rmp3.DesiredInput = CmtnPeriodTarget;
-      rmp3.Ramp3Delay = RampDelay;
-      RC3_MACRO(&rmp3);
-
-// ------------------------------------------------------------------------------
-//    Connect inputs of the IMPULSE module and call the Impulse macro.
-// ------------------------------------------------------------------------------
-      impl1.Period = rmp3.Out;
-      IMPULSE_MACRO(&impl1);
-      HALL3_READ_MACRO(&hall1);
-// ------------------------------------------------------------------------------
-//    Connect inputs of the MOD6 module and call the Mod 6 counter macro.
-// ------------------------------------------------------------------------------
-      mod1.TrigInput = impl1.Out;
-      MOD6CNT_MACRO(&mod1);
-
-// ------------------------------------------------------------------------------
-//    Connect inputs of the PWM_DRV module and call the PWM signal generation
-//    update macro.
-// ------------------------------------------------------------------------------
-      pwm1.CmtnPointerIn = test2;//(int16)mod1.Counter;
-      if(EnableFlag == 0){
-          pwm1.DutyFuncIn = 0;
-      }else{
-          pwm1.DutyFuncIn = test;
-      }
-
-      BLDCPWM_MACRO(1,2,3,&pwm1);
-
-
-#endif // (BUILDLEVEL==LEVEL1)
-// =============================== LEVEL 3 ======================================
-//	  Level 3 describes the closed-loop operation of sensored trapezoidal 
-//	  drive of BLDC motor using Hall sensor.
-// ==============================================================================
-
-#if (BUILDLEVEL==LEVEL3)
-
-// ------------------------------------------------------------------------------
-//    ADC conversion and offset adjustment (observing back-emfs is optinal for this prj.) 
-// ------------------------------------------------------------------------------
-	  BemfA =  _IQ12toIQ(AdcResult.ADCRESULT1);
-	  BemfB =  _IQ12toIQ(AdcResult.ADCRESULT2); 
-	  BemfC =  _IQ12toIQ(AdcResult.ADCRESULT3);
-	  DCbus_current = _IQ12toIQ(AdcResult.ADCRESULT4);//-_IQ(0.5); //1.65V offset added on HVDMC board.
-
-// ------------------------------------------------------------------------------
-//    Connect inputs of the RMP3 module and call the Ramp control 3 macro.
-// ------------------------------------------------------------------------------
-
-
-	 HALL3_READ_MACRO(&hall1);
-
-	 ClosedFlag=TRUE;
-
-// ------------------------------------------------------------------------------
-//    Connect inputs of the RMP2 module and call the Ramp control 2 macro.
-// ------------------------------------------------------------------------------
-      rmp2.DesiredInput = (int32)DFuncDesired;
-	  RC2_MACRO(&rmp2);
-
-// ------------------------------------------------------------------------------
-//    Connect inputs of the PWM_DRV module and call the PWM signal generation
-//    update macro.
-// ------------------------------------------------------------------------------
-   if (ClosedFlag==TRUE)  {
-     if (hall1.CmtnTrigHall==0x7FFF) {
-
-      PreviousState = pwm1.CmtnPointer;
-
-// Comment the following if-else-if statements in case of 
-// inverted Hall logics for commutation states. 
-    /*  if (hall1.HallGpioAccepted==5)
-      { pwm1.CmtnPointer = 0;
-
-      }
-      else if (hall1.HallGpioAccepted==1) 
-      { pwm1.CmtnPointer = 1;
-
-      }
-      else if (hall1.HallGpioAccepted==3) 
-      { pwm1.CmtnPointer = 2;
-
-      }
-      else if (hall1.HallGpioAccepted==2)
-      { pwm1.CmtnPointer = 3;
-
-      }
-      else if (hall1.HallGpioAccepted==6)
-      {  pwm1.CmtnPointer = 4;
-
-      }
-      else if (hall1.HallGpioAccepted==4) 
-      {
-          pwm1.CmtnPointer = 5;
-      }*/
-
-
-
-
-// Comment the following if-else-if statements in case of 
-// non-inverted Hall logics for commutation states. 
-      if (hall1.HallGpioAccepted==2) 
-        pwm1.CmtnPointer = 0;
-
-      else if (hall1.HallGpioAccepted==6) 
-        pwm1.CmtnPointer = 1;
-
-      else if (hall1.HallGpioAccepted==4) 
-        pwm1.CmtnPointer = 2;
-
-      else if (hall1.HallGpioAccepted==5)
-        pwm1.CmtnPointer = 3;
-
-      else if (hall1.HallGpioAccepted==1)
-        pwm1.CmtnPointer = 4; 
-
-      else if (hall1.HallGpioAccepted==3) 
-        pwm1.CmtnPointer = 5;
-
-
-    }    //hall1.CmtnTrigHall == 0x7FFF   
-  } // ClosedFlag==TRUE
-  else{
-      pwm1.CmtnPointer = (int16)mod1.Counter;
-  }
-	  pwm1.DutyFunc = DfuncTesting;
-	  BLDCPWM_MACRO(1,2,3,&pwm1);
-
-// ------------------------------------------------------------------------------
-//    Connect inputs of the SPEED_PR module and call the speed calculation macro.
-// ------------------------------------------------------------------------------  
-  if ((pwm1.CmtnPointer==5)&&(PreviousState==4)&&(hall1.CmtnTrigHall==0x7FFF))
-  {
-       speed1.TimeStamp = VirtualTimer;
-	   SPEED_PR_MACRO(&speed1);
-	   test  = (test ==0 )? 1 : 0;
-  }
-
-// ------------------------------------------------------------------------------
-//    Connect inputs of the PWMDAC module 
-// ------------------------------------------------------------------------------	   
-      pwmdac1.MfuncC1 = (int32)hall1.HallGpioAccepted<<20; 
-      pwmdac1.MfuncC2 = BemfA; 
-      PWMDAC_MACRO(6,pwmdac1)	  						// PWMDAC 6A, 6B
-    
-      pwmdac1.MfuncC1 = DCbus_current; 
-      pwmdac1.MfuncC2 = (int32)mod1.Counter<<20; 				
-	  PWMDAC_MACRO(7,pwmdac1)	 						// PWMDAC 7A, 7B
-
-// ------------------------------------------------------------------------------
-//    Connect inputs of the DATALOG module 
-// ------------------------------------------------------------------------------
-      DlogCh2 = (int16)mod1.Counter; 
-	  DlogCh1 = (int16)hall1.HallGpioAccepted;
-      DlogCh3 = (int16)_IQtoIQ15(BemfA);
-      DlogCh4 = (int16)_IQtoIQ15(BemfB);
-      
-
-#endif // (BUILDLEVEL==LEVEL3)
-
-// =============================== LEVEL 4 ======================================
-//	  Level 4 verifies the closed current loop and current PI controller.
-// ==============================================================================
-      //-------------平均电流和can-------------------------------
-      static _iq last_DC_current=0;
-       static Uint16 DC_Current_cnt = 0;
-       //static int32 DC_Current_sum = 0;
-       static int32 DC_Current_sum_filter = 0;
-
-       DC_current_real = (Uint32)AdcResult.ADCRESULT5*33;//Q12  I真实 = adc * 3.3 / 4096 /0.1
-       DC_current_filer = lowpassfilter(last_DC_current,DC_current_real,_IQ12(1),_IQ12(0.05));
-       last_DC_current = DC_current_filer;
-       carveData1();
-       CanCurve();
-
-       UserCANprocess();
-       if(Fault_clear == 1) {
-           static int16 fault_cnt = 0;
-           GpioDataRegs.GPBCLEAR.bit.GPIO32 = 1;
-           FaultFlag.bit.OverCurFlag = 0;
-           fault_cnt++;
-           if(fault_cnt>20000){
-           Fault_clear = 0;
-           fault_cnt = 0;
-
-           }
-       }else{
-           GpioDataRegs.GPBSET.bit.GPIO32 = 1;
-       }
-       DC_Current_sum_filter += DC_current_filer*pwm1.DutyFunc>>15;
-     //  DC_Current_sum += DC_current_real*pwm1.DutyFunc>>15;
-       DC_Current_cnt++;//由于stall最多16384/20K秒 所以最多0.819715秒计算一次平均值
-
-
-
-#if (BUILDLEVEL==LEVEL4) 
-       BemfA =  AdcResult.ADCRESULT1;
-       BemfB =  AdcResult.ADCRESULT2;
-       BemfC =  AdcResult.ADCRESULT3;
-
-	  HALL3_READ_MACRO(&hall1);
-
-      if (hall1.Revolutions>=0)
-         ClosedFlag=TRUE;
-// ------------------------------------------------------------------------------
-//    电流闭环
-// ------------------------------------------------------------------------------  
-      pid1_spd.Ref = _IQ(0.3);
-      pid1_spd.Fbk = _IQabs(speed1.Speed);
-     // tempIdc=pid1_idc.Fbk;
-      pid1_idc.Umax = pwmlimit<<9;
-      pid1_idc.Umin = (-pwmlimit)<<9;
-      pid1_idc.Ref = CurrentSet;//给定电流 反转给-
-      if(speed1.SpeedRpm<0){
-          pid1_idc.Fbk = -_IQ12toIQ(DC_current_filter_avr);// 电流改为Q15格式
-       }else{
-           pid1_idc.Fbk = _IQ12toIQ(DC_current_filter_avr);// 电流改为Q15格式
-      }
-//	  if(pid1_idc.Fbk<0) pid1_idc.Fbk=tempIdc; // Eliminate negative values
-
-
-
-      step++;
-      if(step == 1){
-            PI_MACRO(&pid1_idc);
-       }else if(step == 2){
-           PI_MACRO(&pid1_spd);
-           step = 0;
-       }
-
-	  if(EnableFlag == 0){
-	       SetPI(&pid1_spd);
-           resetPI(&pid1_idc);
-           rmp2.DesiredInput = 0;
-           RC2_MACRO(&rmp2);
-           pwm1.DutyFuncIn = (int16)_IQtoIQ15(rmp2.Out);   // controlled speed duty-cycle
-
-	   }else{
-	         pwm1.DutyFuncIn = (int16)_IQtoIQ15(_IQmpy(pid1_idc.Out,pid1_spd.Out));   // controlled speed duty-cycle
-	         rmp2.Out = pwm1.DutyFuncIn;
-	        // pwm1.DutyFuncIn = (int16)(_IQtoIQ15(pid1_idc.Out));
-	   }
-
-
-   	if (ClosedFlag==TRUE)  
-   	{
-     if (hall1.CmtnTrigHall==0x7FFF) 
-     {
-         DC_current_avr = DC_Current_sum/DC_Current_cnt;
-         DC_current_filter_avr = DC_Current_sum_filter/DC_Current_cnt;
-         DC_Current_sum = 0;
-         DC_Current_sum_filter =0;
-         DC_Current_cnt = 0;
-         //计算速度
-         speed1.TimeStamp = VirtualTimer;
-         SPEED_PR_MACRO(&speed1,hall1.HallGpioAccepted,PreviousState,hall1.Stall_status);
-         PreviousState = hall1.HallGpioAccepted;
-         if (hall1.HallGpioAccepted==2)
-           pwm1.CmtnPointerIn = 0;
-
-         else if (hall1.HallGpioAccepted==6)
-           pwm1.CmtnPointerIn = 1;
-
-         else if (hall1.HallGpioAccepted==4)
-           pwm1.CmtnPointerIn = 2;
-
-         else if (hall1.HallGpioAccepted==5)
-           pwm1.CmtnPointerIn = 3;
-
-         else if (hall1.HallGpioAccepted==1)
-           pwm1.CmtnPointerIn = 4;
-
-         else if (hall1.HallGpioAccepted==3)
-         {     pwm1.CmtnPointerIn = 5;
-
-         }//hall1.HallGpioAccepted==3
-     }
-  }
-
-	 BLDCPWM_MACRO(1,2,3,&pwm1);
-	  
-#endif // (BUILDLEVEL==LEVEL4)
-
-
-// =============================== LEVEL 5 ======================================
-//	  Level 5 verifies the closed speed loop and speed PI controller.
-// ============================================================================== 
-
-#if (BUILDLEVEL==LEVEL5)
-
-// ------------------------------------------------------------------------------
-//    ADC conversion and offset adjustment (observing back-emfs is optinal for this prj.) 
-// ------------------------------------------------------------------------------
-	  BemfA =  AdcResult.ADCRESULT1;
-	  BemfB =  AdcResult.ADCRESULT2;
-	  BemfC =  AdcResult.ADCRESULT3;
-	  //DCbus_current = _IQ12toIQ(AdcResult.ADCRESULT5); //采样值<<12
-
-	  rc1.TargetValue = SpeedRef;
-   	  RC_MACRO(&rc1);
-
-      HALL3_READ_MACRO(&hall1);
-      pid1_spd.Ref = rc1.SetpointValue;
-      pid1_spd.Fbk = speed1.Speed;
-
-
-	  if(EnableFlag == 0){
-	      resetPI(&pid1_spd);
-	      rmp2.DesiredInput = 0;
-	      RC2_MACRO(&rmp2);
-	      pwm1.DutyFuncIn = (int16)_IQtoIQ15(rmp2.Out);   // controlled speed duty-cycle
-
-	  }else{
-	      pid1_spd.Umax = _IQ((float)pwmlimit/32767);
-	      pid1_spd.Umin = _IQ((float)(-pwmlimit)/32767);
-	      PI_MACRO(&pid1_spd);
-	      if((int16)_IQtoIQ15(pid1_spd.Out)>pwmlimit){
-	          pwm1.DutyFuncIn = pwmlimit;   // controlled speed duty-cycle
-	      }else if((int16)_IQtoIQ15(pid1_spd.Out)<-pwmlimit){
-	          pwm1.DutyFuncIn = -pwmlimit;   // controlled speed duty-cycle
-	      }else{
-	          pwm1.DutyFuncIn = (int16)_IQtoIQ15(pid1_spd.Out);   // controlled speed duty-cycle
-	      }
-	      //pwm1.DutyFuncIn = test;
-	      rmp2.Out = pwm1.DutyFuncIn;
-	  }
-	  if(pwm1.DutyFunc>0){
-	      OpenPwm();
-	  }else{
-	       ClosePwm();
-	  }
-
-      ClosedFlag = TRUE;
-   if (ClosedFlag==TRUE)  
-   {
-
-     if (hall1.CmtnTrigHall==0x7FFF) 
-     {
-
-    //  DC_current_avr = DC_Current_sum/DC_Current_cnt;
-      DC_current_filter_avr = DC_Current_sum_filter/DC_Current_cnt;
-     // DC_Current_sum = 0;
-      DC_Current_sum_filter =0;
-      DC_Current_cnt = 0;
-      speed_direction(hall1.HallGpioAccepted,PreviousState);
-
-      if (hall1.HallGpioAccepted==5)
-        { pwm1.CmtnPointerIn = 5;
-         speed1.TimeStamp = VirtualTimer;
-
-     //    SPEED_PR_MACRO(&speed1,hall1.HallGpioAccepted,PreviousState,hall1.Stall_status);
-
-        }
-        else if (hall1.HallGpioAccepted==1)
-        { pwm1.CmtnPointerIn = 0;
-
-        }
-        else if (hall1.HallGpioAccepted==3)
-        { pwm1.CmtnPointerIn = 1;
-
-        }
-        else if (hall1.HallGpioAccepted==2)
-        { pwm1.CmtnPointerIn = 2;
-
-        }
-        else if (hall1.HallGpioAccepted==6)
-        {  pwm1.CmtnPointerIn = 3;
-
-        }
-        else if (hall1.HallGpioAccepted==4)
-        {
-            pwm1.CmtnPointerIn = 4;
-        }
-      PreviousState = hall1.HallGpioAccepted;
-      /*
-          if (hall1.HallGpioAccepted==2)
-            pwm1.CmtnPointerIn = 0;
-
-          else if (hall1.HallGpioAccepted==6)
-            pwm1.CmtnPointerIn = 1;
-
-          else if (hall1.HallGpioAccepted==4)
-            pwm1.CmtnPointerIn = 2;
-
-          else if (hall1.HallGpioAccepted==5)
-            pwm1.CmtnPointerIn = 3;
-
-          else if (hall1.HallGpioAccepted==1)
-            pwm1.CmtnPointerIn = 4;
-
-          else if (hall1.HallGpioAccepted==3)
-          {     pwm1.CmtnPointerIn = 5;
-
-          }//hall1.HallGpioAccepted==3
-*/
-        //}//direction == Normal
-
-    }    // delay
-  }      // ClosedFlag==TRUE
-
-
-	  BLDCPWM_MACRO(1,2,3,&pwm1);
-
-
-// ------------------------------------------------------------------------------
-//    Connect inputs of the PWMDAC module 
-// ------------------------------------------------------------------------------	
-      pwmdac1.MfuncC1 = BemfA; 
-      pwmdac1.MfuncC2 = BemfB; 
-      PWMDAC_MACRO(6,pwmdac1)	  						// PWMDAC 6A, 6B
-    
-      pwmdac1.MfuncC1 = DCbus_current; 
-      pwmdac1.MfuncC2 = mod1.Counter<<20; 				
-	  PWMDAC_MACRO(7,pwmdac1)	 						// PWMDAC 7A, 7B
-
-// ------------------------------------------------------------------------------
-//    Connect inputs of the DATALOG module 
-// ------------------------------------------------------------------------------
-
-
-
-#endif // (BUILDLEVEL==LEVEL5)
-
-
-// ------------------------------------------------------------------------------
-//    Call the DATALOG update function.
-// ------------------------------------------------------------------------------
-
-
-// ------------------------------------------------------------------------------
-//    Increase virtual timer and force 15 bit wrap around
-// ------------------------------------------------------------------------------
-	VirtualTimer++;
-	VirtualTimer &= 0x00007FFF;
+    IsrTicker = (IsrTicker>20000)? 0 : IsrTicker;
    
-	IsrTime = MastIsrTimePercent();
+	//IsrTime = MastIsrTimePercent();
 // Acknowledge interrupt to recieve more interrupts from PIE group 1
 	CpuTimer0.RegsAddr->TCR.bit.TIF=1;
 	PieCtrlRegs.PIEACK.bit.ACK1 = 1;
@@ -826,13 +375,14 @@ interrupt void xint2_isr(void)
 {
     static int32 last_XintTime = TIMER1_PER;
 
-   if(CpuTimer1.RegsAddr->TCR.bit.TIF == 1){
+/*   if(CpuTimer1.RegsAddr->TCR.bit.TIF == 1){
         // 速度为0
         XintTime = (int32)TIMER1_PER;
         CpuTimer1.RegsAddr->TCR.bit.TIF = 1;//写1清中断标志位
     }else{
         XintTime = (int32)TIMER1_PER - CpuTimer1.RegsAddr->TIM.all;
-    }
+    }*/
+    XintTime = (int32)TIMER1_PER - CpuTimer1.RegsAddr->TIM.all;
 
    if(XintTime<30000){
        //filter 防抖
@@ -855,7 +405,7 @@ interrupt void xint2_isr(void)
 interrupt void xint1_isr(void)
 {
 
-    FaultFlag.bit.IgbtTempFaultFlag = 1;
+    FaultFlag.bit.Ipmfault = 1;
     PieCtrlRegs.PIEACK.bit.ACK1 =1;
 
    // PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
@@ -865,8 +415,11 @@ interrupt void EPWM1TZint_isr(void)
 {
 //過流
     FaultFlag.bit.OverCurFlag = 1;
-    EPwm1Regs.TZCLR.bit.OST = 1;
-    EPwm1Regs.TZCLR.bit.INT = 1;
+    EALLOW;
+
+   // EPwm1Regs.TZCLR.bit.OST = 1;
+   // EPwm1Regs.TZCLR.bit.INT = 1;
+    EDIS;
     PieCtrlRegs.PIEACK.bit.ACK2 = 1;//PIEACK_GROUP2;
 }
 void ServiceDog(void)
@@ -932,17 +485,11 @@ void speed_direction(Uint16 hall,Uint16 hall_last){
 
 }
 void speed_cal(void){
-  /*  int32 RotarfreOut = 0;
-     int32 RotarfreqNew = 0;//电周期频率
 
-     static int32 RotarfreSum = 0;
-     static int32 RotarfreqArr[8] = {0,0,0,0,0,0,0,0};
-     static int16 j = 0;*/
     int32 RotarRPMNew = 0;
      int32 RotarrpmOut = 0;
     static int32 RotarrpmSum = 0;
-    static int32 RotarrpmArr[16] = {0,0,0,0,0,0,0,0,0,0,
-                              0,0,0,0,0,0};
+    static int32 RotarrpmArr[8] = {0,0,0,0,0,0,0,0};
 
     static int16 k = 0;
     if(CpuTimer1.RegsAddr->TCR.bit.TIF == 1){
@@ -950,35 +497,18 @@ void speed_cal(void){
          XintTime = TIMER1_PER;
          Xint2Cnt = 1;
          CpuTimer1.RegsAddr->TCR.bit.TIF = 1;//写1清零
-        // RotarfreqNew =0;
+
          RotarRPMNew =0;
      }else{
-         //RotarfreqNew = (int32)(30000000)/XintTime;//60M clock /N
          RotarRPMNew =(int32)360000000/XintTime;
      }
      if(Xint2Cnt == 1){
          Xint2Cnt = 0;
-         /* RotarfreqArr[j] = RotarfreqNew;
-         RotarfreSum += RotarfreqArr[j];
-         RotarfreOut = RotarfreSum >> 3;
-         j++;
-         if (j >= 8)
-         {
-             j = 0;
-         }
-         RotarfreSum -= RotarfreqArr[j];
-
-
-         speed1.Speed = _IQdiv(RotarfreOut,BASE_FREQ);
-         speed1.SpeedRpm = _IQmpy(speed1.BaseRpm,speed1.Speed);
-          */
-
-
          RotarrpmArr[k] = RotarRPMNew;
          RotarrpmSum += RotarrpmArr[k];
-         RotarrpmOut = RotarrpmSum>>4;
+         RotarrpmOut = RotarrpmSum>>3;
          k++;
-         if (k >= 16)
+         if (k >= 8)
          {
              k = 0;
          }
@@ -1047,14 +577,472 @@ void taskfree(void){
 }
 int16 MastIsrTimePercent(void)
 {
-    int16 MasterIsrT = 0;
+    Uint32 T1 = 0;
 
+    int16 MasterIsrT =0;
+    static int32 T1_L = 60000000;
 
-    MasterIsrT = 3000 - CpuTimer0.RegsAddr->TIM.all;
+    T1 = CpuTimer2.RegsAddr->TIM.all;
+    if(T1_L < T1){
+        MasterIsrT = (Uint32)(T1_L +CpuTimer2.RegsAddr->PRD.all -T1)* 100 /9000;
+    }else{
+        MasterIsrT = (Uint32)(T1_L-T1)* 100 /9000;
+    }
 
-    MasterIsrT = (Uint32)MasterIsrT* 100 /3000;
+    T1_L = T1;
+   // MasterIsrT = (Uint32)MasterIsrT* 100 /3000;
 
 
     return MasterIsrT;
 }
 
+void mainLoop(void){
+    Uint32 T1 = 0;
+    Uint32 T2 = 0;
+    T1 = CpuTimer2.RegsAddr->TIM.all;
+    CanMaster();
+     SerialCommsTimer++;
+     if(SerialCommsTimer>0x3FFF){
+         SerialCommsTimer = 0x3FFF;
+     }
+
+     ServiceDog();
+     speed_cal();
+     pwmlimit_speed();
+     taskfree();
+     FaultTreat();
+
+ #if (BUILDLEVEL==LEVEL1)
+
+ // ------------------------------------------------------------------------------
+ //    Connect inputs of the RMP3 module and call the RAMP Control 3 macro.
+ // ------------------------------------------------------------------------------
+       rmp3.DesiredInput = CmtnPeriodTarget;
+       rmp3.Ramp3Delay = RampDelay;
+       RC3_MACRO(&rmp3);
+
+ // ------------------------------------------------------------------------------
+ //    Connect inputs of the IMPULSE module and call the Impulse macro.
+ // ------------------------------------------------------------------------------
+       impl1.Period = rmp3.Out;
+       IMPULSE_MACRO(&impl1);
+       HALL3_READ_MACRO(&hall1);
+ // ------------------------------------------------------------------------------
+ //    Connect inputs of the MOD6 module and call the Mod 6 counter macro.
+ // ------------------------------------------------------------------------------
+       mod1.TrigInput = impl1.Out;
+       MOD6CNT_MACRO(&mod1);
+
+ // ------------------------------------------------------------------------------
+ //    Connect inputs of the PWM_DRV module and call the PWM signal generation
+ //    update macro.
+ // ------------------------------------------------------------------------------
+       pwm1.CmtnPointerIn = test2;//(int16)mod1.Counter;
+       if(EnableFlag == 0){
+           pwm1.DutyFuncIn = 0;
+       }else{
+           pwm1.DutyFuncIn = test;
+       }
+
+       BLDCPWM_MACRO(1,2,3,&pwm1);
+
+
+ #endif // (BUILDLEVEL==LEVEL1)
+ // =============================== LEVEL 3 ======================================
+ //    Level 3 describes the closed-loop operation of sensored trapezoidal
+ //    drive of BLDC motor using Hall sensor.
+ // ==============================================================================
+
+ #if (BUILDLEVEL==LEVEL3)
+
+ // ------------------------------------------------------------------------------
+ //    ADC conversion and offset adjustment (observing back-emfs is optinal for this prj.)
+ // ------------------------------------------------------------------------------
+       BemfA =  _IQ12toIQ(AdcResult.ADCRESULT1);
+       BemfB =  _IQ12toIQ(AdcResult.ADCRESULT2);
+       BemfC =  _IQ12toIQ(AdcResult.ADCRESULT3);
+       DCbus_current = _IQ12toIQ(AdcResult.ADCRESULT4);//-_IQ(0.5); //1.65V offset added on HVDMC board.
+
+ // ------------------------------------------------------------------------------
+ //    Connect inputs of the RMP3 module and call the Ramp control 3 macro.
+ // ------------------------------------------------------------------------------
+
+
+      HALL3_READ_MACRO(&hall1);
+
+      ClosedFlag=TRUE;
+
+ // ------------------------------------------------------------------------------
+ //    Connect inputs of the RMP2 module and call the Ramp control 2 macro.
+ // ------------------------------------------------------------------------------
+       rmp2.DesiredInput = (int32)DFuncDesired;
+       RC2_MACRO(&rmp2);
+
+ // ------------------------------------------------------------------------------
+ //    Connect inputs of the PWM_DRV module and call the PWM signal generation
+ //    update macro.
+ // ------------------------------------------------------------------------------
+    if (ClosedFlag==TRUE)  {
+      if (hall1.CmtnTrigHall==0x7FFF) {
+
+       PreviousState = pwm1.CmtnPointer;
+
+ // Comment the following if-else-if statements in case of
+ // inverted Hall logics for commutation states.
+     /*  if (hall1.HallGpioAccepted==5)
+       { pwm1.CmtnPointer = 0;
+
+       }
+       else if (hall1.HallGpioAccepted==1)
+       { pwm1.CmtnPointer = 1;
+
+       }
+       else if (hall1.HallGpioAccepted==3)
+       { pwm1.CmtnPointer = 2;
+
+       }
+       else if (hall1.HallGpioAccepted==2)
+       { pwm1.CmtnPointer = 3;
+
+       }
+       else if (hall1.HallGpioAccepted==6)
+       {  pwm1.CmtnPointer = 4;
+
+       }
+       else if (hall1.HallGpioAccepted==4)
+       {
+           pwm1.CmtnPointer = 5;
+       }*/
+
+
+
+
+ // Comment the following if-else-if statements in case of
+ // non-inverted Hall logics for commutation states.
+       if (hall1.HallGpioAccepted==2)
+         pwm1.CmtnPointer = 0;
+
+       else if (hall1.HallGpioAccepted==6)
+         pwm1.CmtnPointer = 1;
+
+       else if (hall1.HallGpioAccepted==4)
+         pwm1.CmtnPointer = 2;
+
+       else if (hall1.HallGpioAccepted==5)
+         pwm1.CmtnPointer = 3;
+
+       else if (hall1.HallGpioAccepted==1)
+         pwm1.CmtnPointer = 4;
+
+       else if (hall1.HallGpioAccepted==3)
+         pwm1.CmtnPointer = 5;
+
+
+     }    //hall1.CmtnTrigHall == 0x7FFF
+   } // ClosedFlag==TRUE
+   else{
+       pwm1.CmtnPointer = (int16)mod1.Counter;
+   }
+       pwm1.DutyFunc = DfuncTesting;
+       BLDCPWM_MACRO(1,2,3,&pwm1);
+
+ // ------------------------------------------------------------------------------
+ //    Connect inputs of the SPEED_PR module and call the speed calculation macro.
+ // ------------------------------------------------------------------------------
+   if ((pwm1.CmtnPointer==5)&&(PreviousState==4)&&(hall1.CmtnTrigHall==0x7FFF))
+   {
+        speed1.TimeStamp = VirtualTimer;
+        SPEED_PR_MACRO(&speed1);
+        test  = (test ==0 )? 1 : 0;
+   }
+
+ // ------------------------------------------------------------------------------
+ //    Connect inputs of the PWMDAC module
+ // ------------------------------------------------------------------------------
+       pwmdac1.MfuncC1 = (int32)hall1.HallGpioAccepted<<20;
+       pwmdac1.MfuncC2 = BemfA;
+       PWMDAC_MACRO(6,pwmdac1)                           // PWMDAC 6A, 6B
+
+       pwmdac1.MfuncC1 = DCbus_current;
+       pwmdac1.MfuncC2 = (int32)mod1.Counter<<20;
+       PWMDAC_MACRO(7,pwmdac1)                           // PWMDAC 7A, 7B
+
+ // ------------------------------------------------------------------------------
+ //    Connect inputs of the DATALOG module
+ // ------------------------------------------------------------------------------
+       DlogCh2 = (int16)mod1.Counter;
+       DlogCh1 = (int16)hall1.HallGpioAccepted;
+       DlogCh3 = (int16)_IQtoIQ15(BemfA);
+       DlogCh4 = (int16)_IQtoIQ15(BemfB);
+
+
+ #endif // (BUILDLEVEL==LEVEL3)
+
+ // =============================== LEVEL 4 ======================================
+ //    Level 4 verifies the closed current loop and current PI controller.
+ // ==============================================================================
+       //-------------平均电流和can-------------------------------
+       static _iq last_DC_current=0;
+        static Uint16 DC_Current_cnt = 0;
+        //static int32 DC_Current_sum = 0;
+        static int32 DC_Current_sum_filter = 0;
+
+        DC_current_real = (Uint32)AdcResult.ADCRESULT5*33;//Q12  I真实 = adc * 3.3 / 4096 /0.1
+        DC_current_filer = lowpassfilter(last_DC_current,DC_current_real,_IQ12(1),_IQ12(0.05));
+        last_DC_current = DC_current_filer;
+        carveData1();
+        CanCurve();
+
+        UserCANprocess();
+
+        DC_Current_sum_filter += DC_current_filer*pwm1.DutyFunc>>15;
+      //  DC_Current_sum += DC_current_real*pwm1.DutyFunc>>15;
+        DC_Current_cnt++;//由于stall最多16384/20K秒 所以最多0.819715秒计算一次平均值
+
+
+
+ #if (BUILDLEVEL==LEVEL4)
+        BemfA =  AdcResult.ADCRESULT1;
+        BemfB =  AdcResult.ADCRESULT2;
+        BemfC =  AdcResult.ADCRESULT3;
+
+       HALL3_READ_MACRO(&hall1);
+
+       if (hall1.Revolutions>=0)
+          ClosedFlag=TRUE;
+ // ------------------------------------------------------------------------------
+ //    电流闭环
+ // ------------------------------------------------------------------------------
+       pid1_spd.Ref = _IQ(0.3);
+       pid1_spd.Fbk = _IQabs(speed1.Speed);
+      // tempIdc=pid1_idc.Fbk;
+       pid1_idc.Umax = pwmlimit<<9;
+       pid1_idc.Umin = (-pwmlimit)<<9;
+       pid1_idc.Ref = CurrentSet;//给定电流 反转给-
+       if(speed1.SpeedRpm<0){
+           pid1_idc.Fbk = -_IQ12toIQ(DC_current_filter_avr);// 电流改为Q15格式
+        }else{
+            pid1_idc.Fbk = _IQ12toIQ(DC_current_filter_avr);// 电流改为Q15格式
+       }
+ //    if(pid1_idc.Fbk<0) pid1_idc.Fbk=tempIdc; // Eliminate negative values
+
+
+
+       step++;
+       if(step == 1){
+             PI_MACRO(&pid1_idc);
+        }else if(step == 2){
+            PI_MACRO(&pid1_spd);
+            step = 0;
+        }
+
+       if(EnableFlag == 0){
+            SetPI(&pid1_spd);
+            resetPI(&pid1_idc);
+            rmp2.DesiredInput = 0;
+            RC2_MACRO(&rmp2);
+            pwm1.DutyFuncIn = (int16)_IQtoIQ15(rmp2.Out);   // controlled speed duty-cycle
+
+        }else{
+              pwm1.DutyFuncIn = (int16)_IQtoIQ15(_IQmpy(pid1_idc.Out,pid1_spd.Out));   // controlled speed duty-cycle
+              rmp2.Out = pwm1.DutyFuncIn;
+             // pwm1.DutyFuncIn = (int16)(_IQtoIQ15(pid1_idc.Out));
+        }
+
+
+     if (ClosedFlag==TRUE)
+     {
+      if (hall1.CmtnTrigHall==0x7FFF)
+      {
+          DC_current_avr = DC_Current_sum/DC_Current_cnt;
+          DC_current_filter_avr = DC_Current_sum_filter/DC_Current_cnt;
+          DC_Current_sum = 0;
+          DC_Current_sum_filter =0;
+          DC_Current_cnt = 0;
+          //计算速度
+          speed1.TimeStamp = VirtualTimer;
+          SPEED_PR_MACRO(&speed1,hall1.HallGpioAccepted,PreviousState,hall1.Stall_status);
+          PreviousState = hall1.HallGpioAccepted;
+          if (hall1.HallGpioAccepted==2)
+            pwm1.CmtnPointerIn = 0;
+
+          else if (hall1.HallGpioAccepted==6)
+            pwm1.CmtnPointerIn = 1;
+
+          else if (hall1.HallGpioAccepted==4)
+            pwm1.CmtnPointerIn = 2;
+
+          else if (hall1.HallGpioAccepted==5)
+            pwm1.CmtnPointerIn = 3;
+
+          else if (hall1.HallGpioAccepted==1)
+            pwm1.CmtnPointerIn = 4;
+
+          else if (hall1.HallGpioAccepted==3)
+          {     pwm1.CmtnPointerIn = 5;
+
+          }//hall1.HallGpioAccepted==3
+      }
+   }
+
+      BLDCPWM_MACRO(1,2,3,&pwm1);
+
+ #endif // (BUILDLEVEL==LEVEL4)
+
+
+ // =============================== LEVEL 5 ======================================
+ //    Level 5 verifies the closed speed loop and speed PI controller.
+ // ==============================================================================
+
+ #if (BUILDLEVEL==LEVEL5)
+
+ // ------------------------------------------------------------------------------
+ //    ADC conversion and offset adjustment (observing back-emfs is optinal for this prj.)
+ // ------------------------------------------------------------------------------
+       BemfA =  AdcResult.ADCRESULT1;
+       BemfB =  AdcResult.ADCRESULT2;
+       BemfC =  AdcResult.ADCRESULT3;
+       //DCbus_current = _IQ12toIQ(AdcResult.ADCRESULT5); //采样值<<12
+
+       rc1.TargetValue = SpeedRef;
+       RC_MACRO(&rc1);
+
+       HALL3_READ_MACRO(&hall1);
+       pid1_spd.Ref = rc1.SetpointValue;
+       pid1_spd.Fbk = speed1.Speed;
+       if(EnableFlag == 0 || FaultFlag.all !=0){
+           Enable_ALLOW = FALSE;
+       }else{
+           Enable_ALLOW = TRUE;
+       }
+
+       if(Enable_ALLOW == FALSE){
+           resetPI(&pid1_spd);
+           rmp2.DesiredInput = 0;
+           RC2_MACRO(&rmp2);
+           pwm1.DutyFuncIn = (int16)_IQtoIQ15(rmp2.Out);   // controlled speed duty-cycle
+
+       }else{
+           pid1_spd.Umax = _IQ((float)pwmlimit/32767);
+           pid1_spd.Umin = _IQ((float)(-pwmlimit)/32767);
+
+           PI_MACRO(&pid1_spd);
+           int16 pwmtmp =(int16)_IQtoIQ15(pid1_spd.Out);
+           if((int16)_IQtoIQ15(pid1_spd.Out)>pwmlimit){
+               pwmtmp = pwmlimit;   // controlled speed duty-cycle
+           }else if((int16)_IQtoIQ15(pid1_spd.Out)<-pwmlimit){
+               pwmtmp = -pwmlimit;   // controlled speed duty-cycle
+           }else{
+               pwmtmp = (int16)_IQtoIQ15(pid1_spd.Out);   // controlled speed duty-cycle
+           }
+
+           pwm1.DutyFuncIn = ((int32)pwmtmp*LimitMotCtrTemp(600000))>>15;//1分钟降一次
+           //pwm1.DutyFuncIn = test;
+           rmp2.Out = pwm1.DutyFuncIn;
+       }
+
+
+       ClosedFlag = TRUE;
+    if (ClosedFlag==TRUE)
+    {
+
+      if (hall1.CmtnTrigHall==0x7FFF)
+      {
+
+     //  DC_current_avr = DC_Current_sum/DC_Current_cnt;
+       DC_current_filter_avr = DC_Current_sum_filter/DC_Current_cnt;
+      // DC_Current_sum = 0;
+       DC_Current_sum_filter =0;
+       DC_Current_cnt = 0;
+       speed_direction(hall1.HallGpioAccepted,PreviousState);
+
+       if (hall1.HallGpioAccepted==5)
+         { pwm1.CmtnPointerIn = 5;
+
+        }
+         else if (hall1.HallGpioAccepted==1)
+         { pwm1.CmtnPointerIn = 0;
+
+         }
+         else if (hall1.HallGpioAccepted==3)
+         { pwm1.CmtnPointerIn = 1;
+
+         }
+         else if (hall1.HallGpioAccepted==2)
+         { pwm1.CmtnPointerIn = 2;
+
+         }
+         else if (hall1.HallGpioAccepted==6)
+         {  pwm1.CmtnPointerIn = 3;
+
+         }
+         else if (hall1.HallGpioAccepted==4)
+         {
+             pwm1.CmtnPointerIn = 4;
+         }
+       PreviousState = hall1.HallGpioAccepted;
+       /*
+           if (hall1.HallGpioAccepted==2)
+             pwm1.CmtnPointerIn = 0;
+
+           else if (hall1.HallGpioAccepted==6)
+             pwm1.CmtnPointerIn = 1;
+
+           else if (hall1.HallGpioAccepted==4)
+             pwm1.CmtnPointerIn = 2;
+
+           else if (hall1.HallGpioAccepted==5)
+             pwm1.CmtnPointerIn = 3;
+
+           else if (hall1.HallGpioAccepted==1)
+             pwm1.CmtnPointerIn = 4;
+
+           else if (hall1.HallGpioAccepted==3)
+           {     pwm1.CmtnPointerIn = 5;
+
+           }//hall1.HallGpioAccepted==3
+ */
+         //}//direction == Normal
+
+     }    // delay
+   }      // ClosedFlag==TRUE
+
+
+
+       if(Enable_ALLOW == 0 && pwm1.DutyFunc==0){
+           bldcpwm_close();
+
+        }else{
+            BLDCPWM_MACRO(1,2,3,&pwm1);
+
+        }
+
+      // BLDCPWM_MACRO(1,2,3,&pwm1);
+ // ------------------------------------------------------------------------------
+ //    Connect inputs of the DATALOG module
+ // ------------------------------------------------------------------------------
+
+
+
+ #endif // (BUILDLEVEL==LEVEL5)
+       //程序占用率
+       //IsrTime = MastIsrTimePercent();
+       T2 = CpuTimer2.RegsAddr->TIM.all;
+       if(T1 < T2){
+           IsrTime = (Uint32)(T1 +CpuTimer2.RegsAddr->PRD.all -T2)* 100 /6000;
+       }else{
+          IsrTime = (Uint32)(T1-T2)* 100 /6000;
+       }
+
+}
+void bldcpwm_close(void){
+    EPwm1Regs.AQCSFRC.bit.CSFA = 1;            /* Forcing a continuous Low on output A of EPWM3    */
+    EPwm1Regs.AQCSFRC.bit.CSFB = 1;            /* Forcing a continuous Low on output B of EPWM3    */
+
+    EPwm2Regs.AQCSFRC.bit.CSFA = 1;            /* Forcing a continuous Low on output A of EPWM3    */
+    EPwm2Regs.AQCSFRC.bit.CSFB = 1;            /* Forcing a continuous Low on output B of EPWM3    */
+
+    EPwm3Regs.AQCSFRC.bit.CSFA = 1;            /* Forcing a continuous Low on output A of EPWM3    */
+    EPwm3Regs.AQCSFRC.bit.CSFB = 1;            /* Forcing a continuous Low on output B of EPWM3    */
+
+}

+ 1 - 1
UserSource/UserCan.c

@@ -285,7 +285,7 @@ void CanDebugTx(void)
         CanMboxUserRegsT.MboxDLC = 8;
         CanMboxUserRegsT.MboxWord1 = pwmlimit;//¹ýÁãµã
         CanMboxUserRegsT.MboxWord2 = IsrTime;
-        CanMboxUserRegsT.MboxWord3 = 0;
+        CanMboxUserRegsT.MboxWord3 = test;
         CanMboxUserRegsT.MboxWord4 =0;
 
         ECanMboxSend(&CanMboxUserRegsT);

+ 72 - 25
UserSource/var.c

@@ -37,6 +37,7 @@ _iq CurrentSet = _IQ(0.0);
 Uint16 Fault_clear = 0;
 int32 XintTime = 60000000;
 volatile Uint16 EnableFlag = FALSE;
+volatile Uint16 Enable_ALLOW = FALSE;
 void InitVar(void){
     FaultFlag.all = 0;
     // Initialize the PI module for dc-bus current
@@ -62,17 +63,17 @@ void FaultTreat(void){
     OverVoltage();
     OverMotorRpm();
     OverMotCtrTemp();
+    Fault_clear_fuc();
 }
 
 void OverVoltage(void)
 {
+//过压不可恢复  欠压可恢复
 
-    static int16 i = 0;
     static int16 j = 0;
 
     if (DCbus_voltage >= 360)
     {
-        i = 20000;
         FaultFlag.bit.OverVolFlag = 1;
     }
     else if (DCbus_voltage <= 200)  //80
@@ -82,21 +83,7 @@ void OverVoltage(void)
     }
     else
     {
-        if (FaultFlag.bit.OverVolFlag == 1)
-        {
-            i--;
-            if (i <= 0)
-            {
-                i = 0;
-                FaultFlag.bit.OverVolFlag = 0;
-            }
-        }
-        else
-        {
-            i = 0;
-        }
-
-        if (FaultFlag.bit.LowVolFlag == 1)
+            if (FaultFlag.bit.LowVolFlag == 1)
         {
             j--;
             if (j <= 0)
@@ -112,20 +99,17 @@ void OverVoltage(void)
     }
 }
 void OverMotorRpm(void)
-{
+{//飞车不可恢复
     if (speed1.BaseRpm >= 3800)
     {
         FaultFlag.bit.OverRpmFlag = 1;
     }
-    else if (speed1.BaseRpm < 100)
-    {
-        FaultFlag.bit.OverRpmFlag = 0;
-    }
+
 }
 void OverMotCtrTemp(void)
-{
+{//过温可恢复
 
-    if (Tmotor >= 85)
+    if (Tmotor >= 100)
     {
         FaultFlag.bit.OverMotTempFlag = 1;
     }
@@ -133,7 +117,7 @@ void OverMotCtrTemp(void)
     {
         FaultFlag.bit.OverMotTempFlag = 0;
     }
-    if (Tvot >= 85)
+    if (Tvot >= 70)
     {
         FaultFlag.bit.IgbtTempFaultFlag = 1;
     }
@@ -143,3 +127,66 @@ void OverMotCtrTemp(void)
     }
 
 }
+void Fault_clear_fuc(void){
+    if(Fault_clear == 1) {
+         static int16 fault_cnt = 0;
+         GpioDataRegs.GPBCLEAR.bit.GPIO32 = 1;
+         FaultFlag.bit.OverCurFlag = 0;
+         fault_cnt++;
+         if(fault_cnt>20000){
+         Fault_clear = 0;
+         fault_cnt = 0;
+         OpenPwm();
+         if (speed1.BaseRpm < 700)
+         {
+            FaultFlag.bit.OverRpmFlag = 0;
+         }
+         if(GpioDataRegs.GPADAT.bit.GPIO16==1){
+               FaultFlag.bit.Ipmfault = 0;
+          }
+
+        }
+         if(FaultFlag.bit.OverVolFlag == 1 && DCbus_voltage<320){
+             FaultFlag.bit.OverVolFlag = 0;
+         }
+
+     }else{
+         GpioDataRegs.GPBSET.bit.GPIO32 = 1;
+     }
+}
+int16 LimitMotCtrTemp(int32 period){
+    static int16 result = 32767;
+    static int32 cnt1=0;
+    static int32 cnt2=0;
+
+    if(Tmotor>30){
+        cnt1++;
+        cnt2 = 0;
+        if(cnt1>period){
+            result = (int32)result*31128>>15;
+            cnt1 =0;
+        }
+
+    }else{
+        cnt1 =0;
+        if(result<31128){
+            cnt2++;
+            if(cnt2>period){
+                cnt2 = 0;
+                int32 tmp = (int32)result*34491>>15;
+                if(tmp>32767){
+                    result = 32767;
+                }else{
+                    result = tmp;
+                }
+            }
+
+        }else{
+            result = 32767;
+            cnt2 = 0;
+        }
+
+    }
+    test = result;
+    return result;
+}

+ 7 - 3
UserSource/var.h

@@ -30,15 +30,15 @@ struct FAULTFLAG_STR
 {
     Uint16 OverMotTempFlag:1;
     Uint16 IgbtTempFaultFlag:1;
-    Uint16 OverVolFlag:1;
     Uint16 LowVolFlag:1;
-    Uint16 rsv1:4;
+    Uint16 rsv1:5;
 
+    Uint16 OverVolFlag:1;
     Uint16 CanErrFlag:1;
     Uint16 OverCurFlag:1;
     Uint16 Ipmfault:1;
     Uint16 OverRpmFlag:1;
-    Uint16 rsv2:4;
+    Uint16 rsv2:3;
 
 
 };
@@ -79,10 +79,14 @@ extern int32 DC_current_filter_avr;
 extern Uint16 Fault_clear;
 extern int32 XintTime;
 extern volatile Uint16 EnableFlag;
+extern volatile Uint16 Enable_ALLOW;
 extern void InitVar(void);
 extern void OverVoltage(void);
 extern void FaultTreat(void);
 extern void OverMotCtrTemp(void);
 extern void OverMotorRpm(void);
+extern void OpenPwm(void);
+extern void Fault_clear_fuc(void);
+extern int16 LimitMotCtrTemp(int32 period);
 
 #endif /* USERSOURCE_VAR_H_ */

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.