C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 1   


C51 COMPILER V9.52.0.0, COMPILATION OF MODULE ADDFUNCTION
OBJECT MODULE PLACED IN .\Output\AddFunction.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ..\User\Source\Application\AddFunction.c LARGE OMF2 WARNINGLEVEL(0) BROWSE 
                    -INCDIR(..\Library\FU68xx_Hardware_Driver\Include;..\User\Include) DEBUG PRINT(.\Listing\AddFunction.lst) TABS(2) OBJECT(
                    -.\Output\AddFunction.obj)

line level    source

   1          /**************************** (C) COPYRIGHT 2017 Fortiortech shenzhen *****************************
   2          * File Name          : AddFunction.c
   3          * Author             : Vina Peng,Fortiortech  Appliction Team
   4          * Version            : V1.0
   5          * Date               : 2017-12-26
   6          * Description        : This file contains all the add function used for Motor Control.
   7          ***************************************************************************************************
   8          * All Rights Reserved
   9          **************************************************************************************************/
  10          
  11          
  12          /* Includes -------------------------------------------------------------------------------------*/
  13          // #include <AddFunction.h>
  14          #include <FU68xx_2.h>
  15          #include <Myproject.h>
  16          
  17          /* Private variables ---------------------------------------------------------*/
  18          FaultStateType             mcFaultSource;
  19          PWMINPUTCAL        idata   mcPwmInput;
  20          FaultVarible       idata   mcFaultDect;
  21          MotorRSDTypeDef    idata   RSDDetect;
  22          CurrentVarible     idata   mcCurVarible;
  23          ProtectVarible     idata   mcProtectTime;
  24          
  25          FOCCTRL            xdata   mcFocCtrl;
  26          ADCSample          xdata   AdcSampleValue;
  27          ONVarible          xdata   ONOFFTest;
  28          MCLedDisplay       xdata   mcLedDisplay;
  29          MCRAMP             xdata   mcSpeedRamp;
  30          SLEEPMODE          xdata   SleepSet;
  31          MotorFRTypeDef     xdata   mcFRState;
  32          int16              xdata   VSP;
  33          
  34          const int16  PowerGiven[10] ={0,500,1000,2200,2900,4300,5900,7300,7900,8550};
  35          /*---------------------------------------------------------------------------*/
  36          /* Name   : int16 KLPF_VALUE(int16 INVlaue, int16 OutLastValue)
  37          /* Input  : INVlaue，OutLastValue
  38          /* Output : int16的变量
  39          /* Description: 滤波函数,用乘法器做的
  40          /*---------------------------------------------------------------------------*/
  41          int16 KLPF_VALUE(int16 INVlaue, int16 OutLastValue)
  42          {
  43   1        int16 Result = 0;
  44   1        MDU_MA = (INVlaue-OutLastValue);
  45   1        MDU_MB = (int16)480;                    /*写被乘数和乘数*/
  46   1      
  47   1        Result = MDU_MB;
  48   1        Result += OutLastValue;
  49   1        return(Result);
  50   1      }
  51          
  52          /*---------------------------------------------------------------------------*/
  53          /* Name   : void FaultProcess(void)
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 2   

  54          /* Input  : NO
  55          /* Output : NO
  56          /* Description: 保护处理函数，关闭FOC输出，同时将状态变为mcFault
  57          /*---------------------------------------------------------------------------*/
  58          void FaultProcess(void)
  59          {
  60   1        MOE     = 0;
  61   1        ClrBit(DRV_CR, FOCEN);  //关闭FOC
  62   1        mcState = mcFault;
  63   1      
  64   1      }
  65          
  66          /*---------------------------------------------------------------------------*/
  67          /* Name   : int16 Abs_F16(int16 value)
  68          /* Input  : value
  69          /* Output : int16
  70          /* Description: 对变量取16位的绝对值
  71          /*---------------------------------------------------------------------------*/
  72          uint16 Abs_F16(int16 value)
  73          {
  74   1        if(value < 0)
  75   1        {
  76   2          return (- value);
  77   2        }
  78   1        else
  79   1        {
  80   2          return (value);
  81   2        }
  82   1      }
  83          /*---------------------------------------------------------------------------*/
  84          /* Name   : int32 Abs_F32(int32 value)
  85          /* Input  : value
  86          /* Output : int16
  87          /* Description: 对变量取16位的绝对值
  88          /*---------------------------------------------------------------------------*/
  89          uint32 Abs_F32(int32 value)
  90          {
  91   1        if(value < 0)
  92   1        {
  93   2          return (- value);
  94   2        }
  95   1        else
  96   1        {
  97   2          return (value);
  98   2        }
  99   1      }
 100          /*---------------------------------------------------------------------------*/
 101          /* Name   : void APP_DIV(void)
 102          /* Input  : void
 103          /* Output : void
 104          /* Description: 将所有用到除法的地方，放在同一个中断，以避免中断串扰
 105          /*---------------------------------------------------------------------------*/
 106          void APP_DIV(void)
 107          {
 108   1          if( mcPwmInput.PWMDivFlag==1)  //启动除法器，避免与过调值中的除法冲突
 109   1          {
 110   2             mcPwmInput.PWMDuty = MDU_DIV_IDATA_U32(&mcPwmInput.pwm.PWMCompareAMP, &mcPwmInput.PWMARRUpdate);
 111   2             mcPwmInput.PWMDivFlag=0;
 112   2          }
 113   1          if( mcFocCtrl.ESDIVFlag==1)  //启动除法器，避免与过调值中的除法冲突
 114   1          {
 115   2             mcFocCtrl.SQUSpeedDIVEs = MDU_DIV_XDATA_U32(&mcFocCtrl.SQUSysSpeed,&mcFocCtrl.EsValue);
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 3   

 116   2             mcFocCtrl.ESDIVFlag=0;
 117   2          }
 118   1      }
 119          /*---------------------------------------------------------------------------*/
 120          /* Name   : void PWMInputCapture(void)
 121          /* Input  : NO
 122          /* Output : NO
 123          /* Description: 输入PWM处理
 124          /*---------------------------------------------------------------------------*/
 125          void PWMInputCapture(void)
 126          {
 127   1        uint16 MotorControlVSP;
 128   1      
 129   1         if(mcPwmInput.PWMUpdateFlag==1)  // 有新的duty更新
 130   1         {
 131   2            if((Abs_F32(mcPwmInput.PWMCompare-mcPwmInput.PWMCompareOld)<50)// 两次比较值相近，减少读�
             -��率
 132   2              &&(Abs_F32(mcPwmInput.PWMARROld-mcPwmInput.PWMARR)<50)// 两次周期值相近，减少读错率
 133   2      //        &&((100<mcPwmInput.PWMARR)&&(mcPwmInput.PWMARR<6000))// 周期值在一定范围内才认为有�
             -��，即一定频率范围
 134   2              &&(mcPwmInput.PWMDivFlag==0))
 135   2              {
 136   3                mcPwmInput.PWMFlag=1;                               // PWMFlag置1期间，不读取TIM3__DR和T
             -IM3__ARR;，防止出错
 137   3                mcPwmInput.pwm.PWMCompareUpdate[0]=(mcPwmInput.PWMCompare>>1);// 对其乘以32768
 138   3                mcPwmInput.PWMARRUpdate=mcPwmInput.PWMARR;
 139   3                mcPwmInput.PWMDivFlag=1;                            // 启动除法
 140   3                mcPwmInput.PWMFlag=0;
 141   3              }
 142   2             if(mcPwmInput.PWMDivFlag==0)
 143   2              {
 144   3                if(mcPwmInput.PWMcnt<3)//2次求平均值
 145   3                {
 146   4                  mcPwmInput.PWMcnt++;
 147   4                  mcPwmInput.PWMVSum +=mcPwmInput.PWMDuty;
 148   4                }
 149   3                else
 150   3                {
 151   4                  MotorControlVSP= (mcPwmInput.PWMVSum >>1);//注意其有一个右移与前面的比较值标�
             -�化有关
 152   4                  mcPwmInput.PWMVSum=0;
 153   4                  mcPwmInput.PWMcnt =0;
 154   4                }
 155   3                 MotorControlVSP=mcPwmInput.PWMDuty;
 156   3               if((MotorControlVSP > ONPWMDuty)&&(MotorControlVSP <=(OFFPWMDutyHigh+1)))
 157   3                {
 158   4                  mcSpeedRamp.FlagONOFF = 1;
 159   4                }
 160   3               else if(MotorControlVSP < OFFPWMDuty)//||(MotorControlVSP >= OFFPWMDutyHigh))
 161   3                {
 162   4                  mcSpeedRamp.FlagONOFF = 0;
 163   4                }
 164   3      
 165   3                //转速曲线计算
 166   3                if(mcSpeedRamp.FlagONOFF==1)
 167   3                {
 168   4                  if(MotorControlVSP <= MINPWMDuty)
 169   4                  {
 170   5                    mcSpeedRamp.TargetValue = Motor_Min_Speed;
 171   5                  }
 172   4                  else if(MotorControlVSP < MAXPWMDuty)
 173   4                  {
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 4   

 174   5                    mcSpeedRamp.TargetValue = Motor_Min_Speed + SPEED_K*(MotorControlVSP-MINPWMDuty);
 175   5                  }
 176   4                  else
 177   4                  {
 178   5                    mcSpeedRamp.TargetValue = Motor_Max_Speed;
 179   5                  }
 180   4                }
 181   3                else
 182   3                {
 183   4                  mcSpeedRamp.TargetValue =0;
 184   4                }
 185   3              }
 186   2      
 187   2            mcPwmInput.PWMUpdateFlag =0;
 188   2            mcPwmInput.PWMCompareOld=mcPwmInput.PWMCompare;//将此次比较值赋值给上次比较值
 189   2            mcPwmInput.PWMARROld=mcPwmInput.PWMARR;//将此次周期值赋值给上次周期值
 190   2          }
 191   1      }
 192          /*****************************************************************************
 193           * Function:     void Fault_OverVoltage(mcFaultVarible *h_Fault)
 194           * Description:  过压欠压保护函数：程序每5ms判断一次，母线电压大于过压保护值�
             -�，计数器加一，计数器值超过20次，判断为过压保护，关闭输出;反之，计数器慢慢减
 195                           同理，欠压保护。
 196                           电机过欠压保护状态下，母线电压恢复到欠压恢复值以上，过压恢复
             -值以下时，计数器加一，超过200次后，恢复。根据档位信息来决定恢复到哪个状态。
 197           * Parameter:    mcFaultVarible *h_Fault
 198           * Return:       no
 199           *****************************************************************************/
 200          void Fault_OverUnderVoltage(FaultVarible *h_Fault)
 201          {
 202   1        //过压保护
 203   1          if(mcFaultSource == FaultNoSource)//程序无其他保护下
 204   1          {
 205   2              if(mcFocCtrl.mcDcbusFlt > OVER_PROTECT_VALUE)  //母线电压大于过压保护值时，计数，超�
             -�20次，判断为过压保护，关闭输出;反之，计数器慢慢减
 206   2              {
 207   3                h_Fault->OverVoltDetecCnt++;
 208   3                if(h_Fault->OverVoltDetecCnt > 20)//检测100ms
 209   3                {
 210   4                  h_Fault->OverVoltDetecCnt = 0;
 211   4                  mcFaultSource=FaultOverVoltage;
 212   4                  FaultProcess();
 213   4                }
 214   3              }
 215   2              else
 216   2              {
 217   3                if(h_Fault->OverVoltDetecCnt>0)
 218   3                {
 219   4                  h_Fault->OverVoltDetecCnt--;
 220   4                }
 221   3              }
 222   2      
 223   2            //欠压保护
 224   2      
 225   2              if(mcFocCtrl.mcDcbusFlt< UNDER_PROTECT_VALUE)
 226   2              {
 227   3                h_Fault->UnderVoltDetecCnt++;
 228   3      
 229   3                if(h_Fault->UnderVoltDetecCnt > 20)//检测100ms
 230   3                {
 231   4                  h_Fault->UnderVoltDetecCnt = 0;
 232   4                  mcFaultSource=FaultUnderVoltage;
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 5   

 233   4                  FaultProcess();
 234   4                }
 235   3              }
 236   2              else
 237   2              {
 238   3                if(h_Fault->UnderVoltDetecCnt>0)
 239   3                {
 240   4                  h_Fault->UnderVoltDetecCnt--;
 241   4                }
 242   3              }
 243   2          }
 244   1      
 245   1          /*******过压欠压保护恢复*********/
 246   1          if((mcState == mcFault) &&((mcFaultSource==FaultUnderVoltage)||(mcFaultSource==FaultOverVoltage)))
 247   1          {
 248   2            if((mcFocCtrl.mcDcbusFlt< OVER_RECOVER_VALUE)&&(mcFocCtrl.mcDcbusFlt> UNDER_RECOVER_VALUE))
 249   2            {
 250   3              h_Fault->VoltRecoverCnt++;
 251   3              if(h_Fault->VoltRecoverCnt>200)//连续检测1s，若正常则恢复
 252   3              {
 253   4                mcState = mcReady;
 254   4                mcFaultSource=FaultNoSource;
 255   4                h_Fault->VoltRecoverCnt = 0;
 256   4              }
 257   3            }
 258   2            else
 259   2            {
 260   3              h_Fault->VoltRecoverCnt = 0;
 261   3            }
 262   2         }
 263   1      }
 264          
 265          /*****************************************************************************
 266           * Function:     void Fault_Overcurrent(CurrentVarible *h_Cur)
 267           * Description:  电机运行或者启动时，当三相中某一相最大值大于OverCurrentValue，则O
             -verCurCnt加1。
 268                           连续累加3次，判断为软件过流保护。执行时间约30.4us。
 269           * Parameter:    mcFaultVarible *h_Fault
 270           * Return:       no
 271           *****************************************************************************/
 272          void Fault_Overcurrent(CurrentVarible *h_Cur)
 273          {
 274   1        if((mcState == mcRun)||(mcState == mcStart))            // check over current in rum and open mode
 275   1        {
 276   2      
 277   2          h_Cur->Abs_ia = Abs_F16(FOC__IA);
 278   2          h_Cur->Abs_ib = Abs_F16(FOC__IB);
 279   2          h_Cur->Abs_ic = Abs_F16(FOC__IC);
 280   2          if(h_Cur->Abs_ia> h_Cur->Max_ia)                      // 此部分既用于软件过流保护，又用�
             -�缺相保护
 281   2          {
 282   3             h_Cur->Max_ia = h_Cur->Abs_ia;
 283   3          }
 284   2          if(h_Cur->Abs_ib > h_Cur->Max_ib)
 285   2          {
 286   3             h_Cur->Max_ib = h_Cur->Abs_ib;
 287   3          }
 288   2          if(h_Cur->Abs_ic > h_Cur->Max_ic)
 289   2          {
 290   3             h_Cur->Max_ic = h_Cur->Abs_ic;
 291   3          }
 292   2      
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 6   

 293   2          if((h_Cur->Max_ia>=OverSoftCurrentValue)||(h_Cur->Max_ib>=OverSoftCurrentValue)||(h_Cur->Max_ic>=OverSof
             -tCurrentValue))
 294   2          {
 295   3            h_Cur->OverCurCnt++;
 296   3            if(h_Cur->OverCurCnt>=3)
 297   3            {
 298   4              GP36=~GP36;
 299   4              h_Cur->Max_ia=0;
 300   4              h_Cur->Max_ib=0;
 301   4              h_Cur->Max_ic=0;
 302   4              h_Cur->OverCurCnt=0;
 303   4              mcFaultSource=FaultSoftOVCurrent;
 304   4              FaultProcess();
 305   4            }
 306   3          }
 307   2          else
 308   2          {
 309   3            if(h_Cur->OverCurCnt>0)
 310   3            {
 311   4              h_Cur->OverCurCnt--;
 312   4            }
 313   3          }
 314   2        }
 315   1      }
 316          
 317          /*****************************************************************************
 318           * Function:     void Fault_OverCurrentRecover(mcFaultVarible *h_Fault)
 319           * Description:  软硬件过流保护恢复
 320           * Parameter:    mcFaultVarible *h_Fault
 321           * Return:       no
 322           *****************************************************************************/
 323          void Fault_OverCurrentRecover(FaultVarible *h_Fault)
 324          {
 325   1        if((mcState == mcFault)&&((mcFaultSource==FaultSoftOVCurrent)||(mcFaultSource==FaultHardOVCurrent))&&(mcP
             -rotectTime.CurrentPretectTimes<5))
 326   1        {
 327   2          h_Fault->CurrentRecoverCnt++;
 328   2          if(h_Fault->CurrentRecoverCnt>=OverCurrentRecoverTime)//200*5=1s
 329   2          {
 330   3            h_Fault->CurrentRecoverCnt=0;
 331   3            mcProtectTime.CurrentPretectTimes++;
 332   3            mcState = mcReady;
 333   3            mcFaultSource=FaultNoSource;
 334   3          }
 335   2        }
 336   1      }
 337          
 338          /*****************************************************************************
 339           * Function:     void Fault_Start(mcFaultVarible *h_Fault)
 340           * Description:  启动保护函数，电机运行状态下，电机在前5s估算转速达到堵转保护
             -值或者5s后反电动势值太低(此方法未验证)
 341                            或4s内还在CtrlMode状态，即速度低于MOTOR_LOOP_RPM，程序判断为启动失�
             -��，电机停机。
 342                            当程序判断为启动失败后，若重启次数少于或等于5次，程序立即进
             -入校准状态，等待重启。
 343           * Parameter:    mcFaultVarible *h_Fault
 344           * Return:       no
 345           *****************************************************************************/
 346            void Fault_Start(FaultVarible *h_Fault)
 347            {
 348   1          /*******启动保护恢复*********/
 349   1      //    h_Fault->mcEsValue = FOC__ESQU;
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 7   

 350   1      
 351   1          if(mcState == mcRun)
 352   1          {
 353   2            //方法一，5s内速度大于最大速度，同时反电动势值低于一定值
 354   2            if(h_Fault->StartSpeedCnt<=1000)
 355   2            {
 356   3              h_Fault->StartSpeedCnt++;
 357   3              if((mcFocCtrl.SpeedFlt > Motor_Max_Speed)&&(mcFocCtrl.EsValue<50))
 358   3              {
 359   4                h_Fault->StartSpeedCnt = 0;
 360   4                mcFaultSource=FaultStart;
 361   4                FaultProcess();
 362   4                mcProtectTime.SecondStartTimes++;
 363   4                mcProtectTime.StartFlag  =  1;
 364   4              }
 365   3            }
 366   2           //方法二
 367   2            if(h_Fault->StartEsCnt<=1200)//前6s，等待1.5s后，开始判断ES，如果超过一定次数，则�
             -��败
 368   2            {
 369   3              h_Fault->StartEsCnt++;
 370   3              h_Fault->StartDelay++;
 371   3              if(h_Fault->StartDelay>=300)        // 1.5s
 372   3              {
 373   4                 h_Fault->StartDelay=300;
 374   4                 if((mcFocCtrl.EsValue <5))//&&(mcFocCtrl.CtrlMode==0))
 375   4                  {
 376   5                    h_Fault->StartESCount++;
 377   5                    if(h_Fault->StartESCount>=20)
 378   5                    {
 379   6                      mcFaultSource=FaultStart;
 380   6                      FaultProcess();
 381   6                      mcProtectTime.SecondStartTimes++;
 382   6                      h_Fault->StartDelay=0;
 383   6                      h_Fault->StartESCount=0;
 384   6                      mcProtectTime.StartFlag  =  2;
 385   6                    }
 386   5                  }
 387   4                  else
 388   4                  {
 389   5                    if(h_Fault->StartESCount>0)
 390   5                      h_Fault->StartESCount--;
 391   5                  }
 392   4             }
 393   3           }
 394   2           else
 395   2           {
 396   3             h_Fault->StartESCount=0;
 397   3           }
 398   2            //方法三，长时间在CtrlMode=0状态
 399   2            if(mcFocCtrl.CtrlMode==0)         //
 400   2            {
 401   3              h_Fault->StartFocmode++;
 402   3              if(h_Fault->StartFocmode>=400)
 403   3              {
 404   4                h_Fault->StartFocmode=0;
 405   4                mcFaultSource=FaultStart;
 406   4                FaultProcess();
 407   4                mcProtectTime.SecondStartTimes++;
 408   4                mcProtectTime.StartFlag  =  3;
 409   4              }
 410   3            }
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 8   

 411   2          }
 412   1          #if (!StartONOFF_Enable)
 413   1          {
 414   2           if((mcFaultSource==FaultStart)&&(mcState == mcFault)&&(mcProtectTime.SecondStartTimes<=StartProtectRest
             -artTimes))
 415   2           {
 416   3             mcFaultSource=FaultNoSource;
 417   3             mcState = mcInit;
 418   3           }
 419   2          }
 420   1         #endif
 421   1      
 422   1        }
 423           /*****************************************************************************
 424           * Function:     void Fault_Stall(mcFaultVarible *h_Fault)
 425           * Description:  堵转保护函数，有三种保护方式，
 426                           第一种，
 427                           第二种，电机运行状态下，延迟4s判断，估算速度绝对值超过堵转速�
             -��连续5次；
 428                           第三种，电机运行状态下，当U,V两相电流绝对值大于堵转电流保护值
             -连续6次；
 429                           当以上三种的任何一种保护触发时，电机停机，程序判断为堵转保护
             -；
 430                           当堵转保护状态下，U相采集值低于堵转恢复值时，若堵转次数小于�
             -�等于堵转重启次数8次，
 431                           程序延迟mcStallRecover重新启动，进行校准状态。
 432           * Parameter:    mcFaultVarible *h_Fault
 433           * Return:       no
 434           *****************************************************************************/
 435          //堵转保护
 436          void Fault_Stall(FaultVarible *h_Fault)
 437          {
 438   1      //  h_Fault->mcEsValue = FOC__ESQU;
 439   1        if(mcState == mcRun)
 440   1        {
 441   2          if(h_Fault->StallDelayCnt <=1000)//5s
 442   2          {
 443   3            h_Fault->StallDelayCnt ++;
 444   3          }
 445   2          else
 446   2          {
 447   3            //method 1，判断反电动势太小或当反电动势太小，转速太大
 448   3            if((&mcFocCtrl.EsValue< 10)||((FOC__EOME > _Q15(1600.0/MOTOR_SPEED_BASE))&&(mcFocCtrl.EsValue< 80)))
 449   3            {
 450   4              h_Fault->StallDectEs++;
 451   4              if(h_Fault->StallDectEs >= 10)
 452   4              {
 453   5                h_Fault->StallDectEs=0;
 454   5                mcFaultSource=FaultStall;
 455   5                mcProtectTime.StallTimes++;
 456   5                FaultProcess();
 457   5                mcProtectTime.StallFlag  =  1;
 458   5              }
 459   4            }
 460   3            else
 461   3            {
 462   4              if( h_Fault->StallDectEs>0)
 463   4                h_Fault->StallDectEs--;
 464   4            }
 465   3            //method 2，判断速度低于堵转最小值或者超过堵转最大值
 466   3            if((mcFocCtrl.SpeedFlt<Motor_Stall_Min_Speed)||(mcFocCtrl.SpeedFlt > Motor_Stall_Max_Speed))
 467   3            {
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 9   

 468   4              h_Fault->StallDectSpeed++;
 469   4              if(h_Fault->StallDectSpeed>=8)
 470   4              {
 471   5                h_Fault->StallDectSpeed=0;
 472   5                mcFaultSource=FaultStall;
 473   5                mcProtectTime.StallTimes++;
 474   5                FaultProcess();
 475   5                mcProtectTime.StallFlag =2;
 476   5              }
 477   4            }
 478   3            else
 479   3            {
 480   4              if(h_Fault->StallDectSpeed>0)
 481   4              h_Fault->StallDectSpeed--;
 482   4            }
 483   3          }
 484   2        }
 485   1          #if (!StartONOFF_Enable)
 486   1          {
 487   2           /*******堵转保护恢复*********/
 488   2              if((mcFaultSource==FaultStall)&&(mcState == mcFault)&&(mcProtectTime.StallTimes<=4))
 489   2              {
 490   3                h_Fault->StallReCount++;
 491   3                if(h_Fault->StallReCount>=StallRecoverTime)
 492   3                {
 493   4                  h_Fault->StallReCount=16000;
 494   4                  mcFaultSource=FaultNoSource;
 495   4                  mcState = mcInit;
 496   4                }
 497   3              }
 498   2              else
 499   2              {
 500   3                h_Fault->StallReCount=0;
 501   3              }
 502   2          }
 503   1         #endif
 504   1      
 505   1      }
 506           /*****************************************************************************
 507           * Function:     void Fault_phaseloss(mcFaultVarible *h_Fault)
 508           * Description:  缺相保护函数，当电机运行状态下，10ms取三相电流的最大值，
 509                           1.5s判断各相电流最大值，若存在两相电流值大于一定值，而第三相�
             -�流值却非常小，则判断为缺相保护，电机停机；
 510           * Parameter:    mcFaultVarible *h_Fault
 511           * Return:       no
 512           *****************************************************************************/
 513            void Fault_phaseloss(FaultVarible *h_Fault)
 514            {
 515   1            if(mcState == mcRun)
 516   1           {
 517   2              h_Fault->Lphasecnt++;
 518   2              if(h_Fault->Lphasecnt>100)//100*5=500ms
 519   2              {
 520   3                 h_Fault->Lphasecnt=0;
 521   3                 if(((mcCurVarible.Max_ia>(mcCurVarible.Max_ib*3))||(mcCurVarible.Max_ia>(mcCurVarible.Max_ic*3)))&&(
             -mcCurVarible.Max_ia>PhaseLossCurrentValue))
 522   3                 {
 523   4                    h_Fault->AOpencnt++;
 524   4                 }
 525   3                 else
 526   3                 {
 527   4                  if(h_Fault->AOpencnt>0)
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 10  

 528   4                    h_Fault->AOpencnt --;
 529   4                 }
 530   3                 if(((mcCurVarible.Max_ib >(mcCurVarible.Max_ia*3))||(mcCurVarible.Max_ib >(mcCurVarible.Max_ic*3)))&
             -&(mcCurVarible.Max_ib >PhaseLossCurrentValue))
 531   3                 {
 532   4                   h_Fault->BOpencnt++;
 533   4                 }
 534   3                else
 535   3                 {
 536   4                   if(h_Fault->BOpencnt>0)
 537   4                    h_Fault->BOpencnt --;
 538   4                 }
 539   3                 if(((mcCurVarible.Max_ic >(mcCurVarible.Max_ia*3))||(mcCurVarible.Max_ic >(mcCurVarible.Max_ib*3)))&
             -&(mcCurVarible.Max_ic >PhaseLossCurrentValue))
 540   3                 {
 541   4                   h_Fault->COpencnt++;
 542   4                 }
 543   3                else
 544   3                 {
 545   4                   if(h_Fault->COpencnt>0)
 546   4                    h_Fault->COpencnt --;
 547   4                 }
 548   3                  mcCurVarible.Max_ia = 0;
 549   3                  mcCurVarible.Max_ib = 0;
 550   3                  mcCurVarible.Max_ic = 0;
 551   3                if(h_Fault->AOpencnt > 1|| h_Fault->BOpencnt > 1 || h_Fault->COpencnt > 1)
 552   3                 {
 553   4                    mcProtectTime.LossPHTimes++;
 554   4                    mcFaultSource=FaultLossPhase;
 555   4                    FaultProcess();
 556   4                 }
 557   3              }
 558   2            }
 559   1      
 560   1          #if (!StartONOFF_Enable)
 561   1          {
 562   2           /*******缺相保护恢复*********/
 563   2            if((mcFaultSource==FaultLossPhase)&&(mcState == mcFault)&&
 564   2            (mcProtectTime.LossPHTimes<5))//可重启5次
 565   2             {
 566   3                 h_Fault->mcLossPHRecCount++;
 567   3                 if(h_Fault->mcLossPHRecCount>=PhaseLossRecoverTime)
 568   3                 {
 569   4                   h_Fault->AOpencnt=0;
 570   4                   h_Fault->BOpencnt=0;
 571   4                   h_Fault->COpencnt=0;
 572   4                   mcState = mcReady;
 573   4                   mcFaultSource=FaultNoSource;
 574   4                 }
 575   3             }
 576   2          else
 577   2             {
 578   3                 h_Fault->mcLossPHRecCount=0;
 579   3             }
 580   2      
 581   2          }
 582   1         #endif
 583   1      
 584   1        }
 585          
 586          /*---------------------------------------------------------------------------*/
 587          /* Name   : void Fault_Detection(void)
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 11  

 588          /* Input  : NO
 589          /* Output : NO
 590          /* Description: 保护函数，因保护的时间响应不会很高，采用分段处理，每5个定时�
             -�中断执行一次对应的保护
 591                          常见保护有过欠压、过温、堵转、启动、缺相等保护，调试时，可根�
             -��需求，一个个的调试加入。
 592          /*---------------------------------------------------------------------------*/
 593          void Fault_Detection(void)
 594          {
 595   1          mcFaultDect.segment++;
 596   1          if(mcFaultDect.segment>=5)
 597   1          {
 598   2            mcFaultDect.segment=0;
 599   2          }
 600   1          if(mcFaultDect.segment==0)
 601   1            {
 602   2              if(CurrentRecoverEnable)//过流保护恢复使能
 603   2              {
 604   3                Fault_OverCurrentRecover(&mcFaultDect);
 605   3              }
 606   2            }
 607   1          else if(mcFaultDect.segment==1)
 608   1            {
 609   2              if(VoltageProtectEnable==1)//过压保护使能
 610   2              {
 611   3                Fault_OverUnderVoltage(&mcFaultDect);
 612   3              }
 613   2            }
 614   1          else if(mcFaultDect.segment==2)
 615   1            {
 616   2              if(StartProtectEnable==1)//启动保护使能
 617   2              {
 618   3                Fault_Start(&mcFaultDect);
 619   3              }
 620   2            }
 621   1          else if(mcFaultDect.segment==3)
 622   1            {
 623   2              if(StallProtectEnable==1)//堵转保护使能
 624   2              {
 625   3                Fault_Stall(&mcFaultDect);
 626   3              }
 627   2            }
 628   1          else if(mcFaultDect.segment==4)
 629   1            {
 630   2              if(PhaseLossProtectEnable==1)//缺相保护使能
 631   2              {
 632   3                Fault_phaseloss(&mcFaultDect);
 633   3              }
 634   2            }
 635   1          else
 636   1            {
 637   2      
 638   2            }
 639   1      }
 640          
 641          /*---------------------------------------------------------------------------*/
 642          /* Name   : void ONOFF_Starttest(void)
 643          /* Input  : NO
 644          /* Output : NO
 645          /* Description: 启动测试
 646          /*---------------------------------------------------------------------------*/
 647          void ONOFF_Starttest(ONVarible  *h_test)
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 12  

 648          {
 649   1        if(h_test->ONOFF_Flag==1)
 650   1        {
 651   2           h_test->ON_Count++;
 652   2          if(h_test->ON_Count>StartON_Time)
 653   2          {
 654   3            h_test->ON_Count=0;
 655   3            h_test->ONOFF_Times++;
 656   3            h_test->ONOFF_Flag=0;
 657   3            mcSpeedRamp.FlagONOFF = 0;
 658   3            mcSpeedRamp.TargetValue = 0;
 659   3      
 660   3          }
 661   2        }
 662   1        else
 663   1        {
 664   2          if(mcState!=mcFault)
 665   2          {
 666   3            h_test->OFF_Count++;
 667   3            if(h_test->OFF_Count>StartOFF_Time)
 668   3            {
 669   4              h_test->OFF_Count=0;
 670   4              h_test->ONOFF_Flag=1;
 671   4               mcSpeedRamp.FlagONOFF = 1;
 672   4               mcSpeedRamp.TargetValue = Motor_Min_Speed;
 673   4      
 674   4            }
 675   3          }
 676   2      
 677   2        }
 678   1      }
 679          
 680          /*---------------------------------------------------------------------------*/
 681          /* Name   : void Speed_response(void)
 682          /* Input  : NO
 683          /* Output : NO
 684          /* Description: 速度响应函数，可根据需求加入控制环，如恒转矩控制、恒转速控制
             -、恒功率控制
 685          /*---------------------------------------------------------------------------*/
 686          void Speed_response(void)
 687          {
 688   1      
 689   1        if((mcState ==mcRun)||(mcState ==mcStop))
 690   1        {
 691   2          switch(mcFocCtrl.CtrlMode)
 692   2          {
 693   3            case 0:
 694   3            {
 695   4              if(mcFocCtrl.SpeedFlt >Motor_Loop_Speed)//>=MOTOR_LOOP_RPM
 696   4              {
 697   5                mcFocCtrl.CtrlMode= 1;
 698   5                FOC_DQKP = DQKP;
 699   5                FOC_DQKI = DQKI;
 700   5                #if (Motor_Speed_Control_Mode == SPEED_LOOP_CONTROL)
 701   5                {
 702   6                  mcSpeedRamp.ActualValue = FOC__EOME;
 703   6                }
 704   5                #elif (Motor_Speed_Control_Mode == POWER_LOOP_CONTROL)
                        {
                          mcSpeedRamp.ActualValue = mcFocCtrl.Powerlpf;
                        }
                        #endif
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 13  

 709   5      
 710   5                mcFocCtrl.TorqueLoopTime= SPEED_LOOP_TIME;
 711   5                PI_UK = mcFocCtrl.mcIqref;
 712   5              }
 713   4            }
 714   3            break;
 715   3            case 1:
 716   3            {
 717   4              mcFocCtrl.TorqueLoopTime++;
 718   4              if(mcFocCtrl.TorqueLoopTime>SPEED_LOOP_TIME)
 719   4              {
 720   5      
 721   5                mcFocCtrl.TorqueLoopTime=0;
 722   5                #if (OUTLoop_Mode== OUTLoop_Disable)
 723   5                {
 724   6                  mcFocCtrl.mcIqref = FOC_IQREF;
 725   6                  if (FOC_IQREF < QOUTVALUE)
 726   6                  {
 727   7                    mcFocCtrl.mcIqref += QOUTINC;
 728   7                    if (mcFocCtrl.mcIqref > QOUTVALUE) mcFocCtrl.mcIqref = QOUTVALUE;
 729   7                    FOC_IQREF = mcFocCtrl.mcIqref;
 730   7                  }
 731   6                  else if (FOC_IQREF > QOUTVALUE)
 732   6                  {
 733   7                    mcFocCtrl.mcIqref -= QOUTINC;
 734   7                    if (mcFocCtrl.mcIqref < QOUTVALUE) mcFocCtrl.mcIqref = QOUTVALUE;
 735   7                    FOC_IQREF = mcFocCtrl.mcIqref;
 736   7                  }
 737   6                }
 738   5                #elif (OUTLoop_Mode== OUTLoop_Enable)
                        {
                          mc_ramp(&mcSpeedRamp);
              
                          #if (Motor_Speed_Control_Mode == SPEED_LOOP_CONTROL)
                          {
                            FOC_IQREF= HW_One_PI(mcSpeedRamp.ActualValue- mcFocCtrl.SpeedFlt);//COMPESATE是作为补偿
                          }
                          #elif (Motor_Speed_Control_Mode == POWER_LOOP_CONTROL)
                          {
                            FOC_IQREF= HW_One_PI(mcSpeedRamp.ActualValue- mcFocCtrl.Powerlpf);
                          }
                          #endif
                        }
                        #endif //END OUTLoop_Mode
 753   5                if(FOC_EK2 < OBS_K2T_Actual-10)
 754   5                {
 755   6                  mcFocCtrl.Smo_EK2=FOC_EK2;
 756   6                  mcFocCtrl.Smo_EK2+=10;
 757   6                  FOC_EK2=mcFocCtrl.Smo_EK2;
 758   6                }
 759   5                else if(FOC_EK2 > OBS_K2T_Actual+10)
 760   5                {
 761   6                  mcFocCtrl.Smo_EK2=FOC_EK2;
 762   6                  mcFocCtrl.Smo_EK2-=10;
 763   6                  FOC_EK2=mcFocCtrl.Smo_EK2;
 764   6                }
 765   5                else
 766   5                {
 767   6                  FOC_EK2=OBS_K2T_Actual;
 768   6                }
 769   5              }
 770   4            }
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 14  

 771   3            break;
 772   3          }
 773   2        }
 774   1        #if (FRDetectMethod!=FOCMethod)
                {
                  if(mcState==mcRun)
                  {
                    if(mcFocCtrl.RunStateCnt<15000)//15s的延迟，用于清RSDCCWFlag,BEMFCCWFlag
                    {
                      mcFocCtrl.RunStateCnt++;
                    }
                    else
                    {
                      BEMFDetect.BEMFCCWFlag= 0;
                      RSDDetect.RSDCCWFlag  = 0;
                    }
                  }
                }
                #endif
 790   1      
 791   1      
 792   1      }
 793          
 794          /*---------------------------------------------------------------------------*/
 795          /* Name   : void FGOutput(void)
 796          /* Input  : NO
 797          /* Output : NO
 798          /* Description: FG信号输出
 799          /*---------------------------------------------------------------------------*/
 800          void FGOutput(void)
 801          {//FG可用中断FGIF进行判断
 802   1        if(mcState==mcRun)
 803   1        {
 804   2          if((FOC__THETA>=0)&&(FOC__THETA<32768))//0-180
 805   2          {
 806   3            ResetFGPin;
 807   3          }
 808   2          else if((FOC__THETA>=32768)&&(FOC__THETA<65536))//180-360
 809   2          {
 810   3            SetFGPin;
 811   3          }
 812   2        }
 813   1        else if(mcState == mcFault)
 814   1        {
 815   2            SetFGPin;
 816   2        }
 817   1        else
 818   1        {
 819   2            ResetFGPin;
 820   2        }
 821   1      }
 822          /*---------------------------------------------------------------------------*/
 823          /* Name   :  uint16 SoftLPF(uint16 Xn1, uint16 Xn0, uint16 K)
 824          /* Input  : uint16 Xn1, uint16 Xn0, uint16 K
 825          /* Output : uint16
 826          /* Description: 软件低通滤波
 827          /*---------------------------------------------------------------------------*/
 828           int16 SoftLPF(int16 Xn1, int16 Xn0, int16 K)
 829           {
 830   1        int16 Temp16 = 0;
 831   1        int32 Temp32 = 0;
 832   1      
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 15  

 833   1        Temp32 = (((int32)Xn1 - (int32)Xn0) * (int32)K) >> 15;
 834   1        Temp16 = Xn0 + (int16)Temp32;
 835   1        return Temp16;
 836   1       }
 837          
 838          
 839          /*---------------------------------------------------------------------------*/
 840          /* Name   : void LED_Display(void)
 841          /* Input  : NO
 842          /* Output : NO
 843          /* Description: LED灯显示
 844          /*---------------------------------------------------------------------------*/
 845          void LED_Display(void)
 846          {
 847   1        switch(mcFaultSource)
 848   1        {
 849   2              case FaultNoSource:
 850   2                  ResetLEDPin;        //低电平点亮，高电平灭
 851   2                break;
 852   2              case FaultHardOVCurrent:
 853   2                  Led_OnOff(&mcLedDisplay,1);
 854   2                break;
 855   2              case FaultSoftOVCurrent:
 856   2                  Led_OnOff(&mcLedDisplay,2);
 857   2                break;
 858   2              case FaultUnderVoltage:
 859   2                  Led_OnOff(&mcLedDisplay,3);
 860   2                break;
 861   2              case FaultOverVoltage:
 862   2                  Led_OnOff(&mcLedDisplay,4);
 863   2                break;
 864   2              case FaultLossPhase:
 865   2                  Led_OnOff(&mcLedDisplay,5);
 866   2                break;
 867   2              case FaultStall:
 868   2                  Led_OnOff(&mcLedDisplay,6);
 869   2                break;
 870   2              case  FaultStart:
 871   2                  Led_OnOff(&mcLedDisplay,7);
 872   2                break;
 873   2              case  FaultOverwind:
 874   2                  Led_OnOff(&mcLedDisplay,8);
 875   2                break;
 876   2              default:
 877   2                break;
 878   2        }
 879   1      
 880   1      }
 881          
 882            //LED灯的闪烁
 883            void Led_OnOff(MCLedDisplay *hLedDisplay,uint8 htime)
 884            {
 885   1          hLedDisplay->LedCount++;
 886   1          if(hLedDisplay->LedCount<hLedDisplay->Counttime)
 887   1           {
 888   2              if(hLedDisplay->Count<200)
 889   2              {
 890   3               hLedDisplay->Count++;
 891   3      
 892   3              }
 893   2              else if((hLedDisplay->Count>=200)&&(hLedDisplay->Count<201))
 894   2              {
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 16  

 895   3                hLedDisplay->Count=0;
 896   3                LEDPinONOFF;
 897   3                hLedDisplay->LedTimCot++;
 898   3              }
 899   2              else
 900   2                ;
 901   2              if(hLedDisplay->LedTimCot>=2*htime)
 902   2              {
 903   3                hLedDisplay->Count=202;
 904   3                SetLEDPin;
 905   3              }
 906   2            }
 907   1            else if(hLedDisplay->LedCount>=hLedDisplay->Counttime)
 908   1            {
 909   2             hLedDisplay->LedCount=0;
 910   2             hLedDisplay->LedTimCot=0;
 911   2             hLedDisplay->Count=0;
 912   2            }
 913   1       }
 914          /*---------------------------------------------------------------------------*/
 915          /* Name   : void mc_ramp(void)
 916          /* Input  : hTarget,MC_RAMP *hSpeedramp
 917          /* Output : NO
 918          /* Description:
 919          /*---------------------------------------------------------------------------*/
 920          
 921          void mc_ramp(MCRAMP *hSpeedramp)
 922          {
 923   1          if( --hSpeedramp->DelayCount < 0)
 924   1          {
 925   2              hSpeedramp->DelayCount = hSpeedramp->DelayPeriod;
 926   2      
 927   2              if (hSpeedramp->ActualValue < hSpeedramp->TargetValue)
 928   2              {
 929   3                  if(hSpeedramp->ActualValue + Motor_Speed_Inc < hSpeedramp->TargetValue)
 930   3                  {
 931   4                      hSpeedramp->ActualValue += Motor_Speed_Inc;
 932   4                  }
 933   3                  else
 934   3                  {
 935   4                      hSpeedramp->ActualValue = hSpeedramp->TargetValue;
 936   4                  }
 937   3              }
 938   2              else
 939   2              {
 940   3                  if(hSpeedramp->ActualValue - Motor_Speed_Dec > hSpeedramp->TargetValue)
 941   3                  {
 942   4      
 943   4                      hSpeedramp->ActualValue -= Motor_Speed_Dec;
 944   4                  }
 945   3                  else
 946   3                  {
 947   4                      hSpeedramp->ActualValue = hSpeedramp->TargetValue;
 948   4                  }
 949   3              }
 950   2          }
 951   1      }
 952          void HW_Speed_PI(void)
 953          {
 954   1      //    PI_EK =  mcSpeedRamp.ActualValue- mcFocCtrl.Powerlpf; //给定转速与实际转速之差，可区分�
             -��功率或恒转速，或恒电压
 955   1          PI_EK =  mcSpeedRamp.ActualValue- FOC__EOME;  //给定转速与实际转速之差，直接减FOC__EOME有
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 17  

             -风险
 956   1          PI_LPF_CR |= 0x02;                  // Start PI
 957   1          _nop_();  _nop_();  _nop_();  _nop_();  _nop_();
 958   1      //    mcFocCtrl.mcIqref= PI_UK;//可区分限功率与不限功率
 959   1      //    FOC_IQREF =mcFocCtrl.mcIqref;
 960   1          PI_UK+=(SKP/4096 +1);
 961   1          FOC_IQREF= PI_UK;
 962   1      
 963   1      }
 964          /*-------------------------------------------------------------------------------------------------
 965            Function Name : int16 HW_Genal_PI(int16 Xn1, int16 Yn0, int16 Xn2)
 966            U(K) = U(k-1) + Kp*(E(k)-E(k-1)) + Ki*E(k) ----- (Uk_min < U(k) < Uk_max)b
 967            Description   : PI控制
 968            Input         : Xn1--E(K-1)
 969                            Yn0--U(K-1)
 970                    Xn2--E(K)
 971            Output      : PI_UK--当前PI输出值,执行时间为3.8us
 972          -------------------------------------------------------------------------------------------------*/
 973          int16 HW_Genal_PI(int16 Xn1, int16 Yn0, int16 Xn2)
 974          {
 975   1          PI_KP = SKP;
 976   1          PI_KI = SKI;
 977   1          PI_UKMAX = SOUTMAX;
 978   1          PI_UKMIN = SOUTMIN;
 979   1          PI_EK =  Xn1;         //初始化E(K-1)
 980   1          PI_LPF_CR |= 0x02;    // Start PI
 981   1          _nop_();  _nop_();  _nop_();  _nop_();  _nop_();
 982   1          PI_UK =  Yn0;           //初始化U(K-1)
 983   1          PI_EK =  Xn2;       //填入EK
 984   1          PI_LPF_CR |= 0x02;    // Start PI
 985   1          _nop_();  _nop_();  _nop_();  _nop_();  _nop_();
 986   1          PI_UK+=(SKP/4096 +1);
 987   1          return PI_UK;
 988   1      }
 989          /*-------------------------------------------------------------------------------------------------
 990            Function Name : int16 HW_One_PI(int16 Xn1, int16 Yn0, int16 Xn2)
 991            Description   : PI控制
 992            Input         : Xn1--E(K)
 993            Output      : PI_UK--当前PI输出值,执行时间us
 994          -------------------------------------------------------------------------------------------------*/
 995          int16 HW_One_PI(int16 Xn1)
 996          {
 997   1          PI_EK =  Xn1;       //填入EK
 998   1          PI_LPF_CR |= 0x02;    // Start PI
 999   1          _nop_();  _nop_();  _nop_();  _nop_();  _nop_();
1000   1          PI_UK+=(SKP/4096 +1);
1001   1          return PI_UK;
1002   1      }
1003          /*-------------------------------------------------------------------------------------------------
1004            Function Name : int16 LPF(int16 Xn1, int16 Xn0, int8 K)
1005            Description   : LFP控制
1006            Input         : Xn1--当前输入值
1007                            Xn0--上一次滤波输出值
1008                            K--LPF滤波系数
1009            Output        : LPF_Y--当前滤波输出值，执行时间为4us。
1010          -------------------------------------------------------------------------------------------------*/
1011          int16 LPFFunction(int16 Xn1, int16 Xn0, int8 K)
1012          {
1013   1        LPF_K = K;
1014   1        LPF_X = Xn1;
1015   1        LPF_Y = Xn0;
1016   1        SetBit(PI_LPF_CR, LPFSTA);
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 18  

1017   1        _nop_();_nop_();_nop_();_nop_();_nop_();
1018   1        return LPF_Y;
1019   1      }
1020          
1021          /*---------------------------------------------------------------------------*/
1022          /* Name   : void VSPSample(void)
1023          /* Input  : NO
1024          /* Output : NO
1025          /* Description: VSP采样
1026          /*---------------------------------------------------------------------------*/
1027          void VSPSample(void)
1028          {
1029   1        /*****VREF的采样获取值并滤波******/
1030   1        AdcSampleValue.ADCVref = (ADC6_DR<<3);//采集对应端口
1031   1        VSP=LPFFunction(AdcSampleValue.ADCVref,VSP,30);//低通滤波
1032   1      
1033   1         if((VSP > ONPWMDuty)&&(VSP <= OFFPWMDutyHigh))//在ONPWMDuty-OFFPWMDutyHigh之间，电机有转速运�
             -�
1034   1          {
1035   2            mcSpeedRamp.FlagONOFF = 1;
1036   2          }
1037   1         else if((VSP < OFFPWMDuty)||(VSP > OFFPWMDutyHigh))//电机停机
1038   1          {
1039   2            mcSpeedRamp.FlagONOFF = 0;
1040   2          }
1041   1          //转速曲线计算
1042   1          if(mcSpeedRamp.FlagONOFF==1)//
1043   1          {
1044   2            #if (Motor_Speed_Control_Mode == SPEED_LOOP_CONTROL)
1045   2            {
1046   3              if(VSP <= MINPWMDuty)    //最小转速运行
1047   3              {
1048   4                mcSpeedRamp.TargetValue = Motor_Min_Speed;
1049   4              }
1050   3              else if(VSP < MAXPWMDuty)//调速
1051   3              {
1052   4                mcSpeedRamp.TargetValue = Motor_Min_Speed + SPEED_K*(VSP-MINPWMDuty);
1053   4              }
1054   3              else         //最大转速运行
1055   3              {
1056   4                mcSpeedRamp.TargetValue = Motor_Max_Speed;
1057   4              }
1058   3            }
1059   2            #elif (Motor_Speed_Control_Mode == POWER_LOOP_CONTROL)
                    {
                      if(VSP <= MINPWMDuty)    //最小转速运行
                      {
                        mcSpeedRamp.TargetValue = Motor_Min_Power;
                      }
                      else if(VSP < MAXPWMDuty)//调速
                      {
                        mcSpeedRamp.TargetValue = Motor_Min_Power + POWER_K*(VSP-MINPWMDuty);
                      }
                      else         //最大转速运行
                      {
                        mcSpeedRamp.TargetValue = Motor_Max_Power;
                      }
                    }
                    #endif
1075   2      
1076   2          }
1077   1          else
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 19  

1078   1          {
1079   2            mcSpeedRamp.TargetValue =0;
1080   2          }
1081   1      }
1082          /*---------------------------------------------------------------------------*/
1083          /* Name   : void Sleepmode(void)
1084          /* Input  : NO
1085          /* Output : NO
1086          /* Description: 睡眠模式测试
1087          /*---------------------------------------------------------------------------*/
1088           void Sleepmode(void)
1089           {
1090   1            SleepSet.SleepDelayCout++;
1091   1          if(SleepSet.SleepDelayCout>=20000)//最大65530，若要再大，需改数据类型
1092   1          {
1093   2      //        FOC_EFREQMIN  = -Motor_Omega_Ramp_Min;
1094   2      //        FOC_EFREQHOLD = -Motor_Omega_Ramp_End;
1095   2            mcSpeedRamp.TargetValue=0;
1096   2            MOE     = 0;
1097   2            ClrBit(DRV_CR, FOCEN);  //关闭FOC
1098   2            SleepSet.SleepDelayCout=0;
1099   2            SleepSet.SleepFlag=1;
1100   2            SetBit(P1_IE, P11);   // config P11 as the source of EXTI1
1101   2            SetBit(PCON, STOP);
1102   2          }
1103   1      
1104   1       }
1105          /*---------------------------------------------------------------------------*/
1106          /* Name   : void StarRampDealwith(void)
1107          /* Input  : NO
1108          /* Output : NO
1109          /* Description:
1110          /*---------------------------------------------------------------------------*/
1111          void StarRampDealwith(void)
1112          {
1113   1      //    int temp=0;
1114   1          if((mcState == mcRun))
1115   1          {
1116   2      //            if(FOC_IQREF<mcFocCtrl.mcIqref)
1117   2      //            {
1118   2      //                temp=FOC_IQREF;
1119   2      //                temp+=20;
1120   2      //                FOC_IQREF = temp;
1121   2      //            }
1122   2            if(mcFocCtrl.State_Count == 1200)//2300
1123   2            {
1124   3              FOC_EKP = OBSW_KP_GAIN_RUN;                           // 估算器里的PI的KP
1125   3              FOC_EKI = OBSW_KI_GAIN_RUN;                           // 估算器里的PI的KI
1126   3              GP07 = 1;
1127   3            }
1128   2            else if(mcFocCtrl.State_Count == 800)//2000
1129   2            {
1130   3              FOC_EKP = OBSW_KP_GAIN_RUN1;                          // 估算器里的PI的KP
1131   3              FOC_EKI = OBSW_KI_GAIN_RUN1;                          // 估算器里的PI的KI
1132   3              GP07 = 0;
1133   3            }
1134   2            else if(mcFocCtrl.State_Count == 500)//1600
1135   2            {
1136   3              FOC_EKP = OBSW_KP_GAIN_RUN2;                          // 估算器里的PI的KP
1137   3              FOC_EKI = OBSW_KI_GAIN_RUN2;                          // 估算器里的PI的KI
1138   3              GP07 = 1;
1139   3            }
C51 COMPILER V9.52.0.0   ADDFUNCTION                                                       09/12/2018 13:51:59 PAGE 20  

1140   2            else if(mcFocCtrl.State_Count ==200)//1200
1141   2            {
1142   3              FOC_EKP = OBSW_KP_GAIN_RUN3;                          // 估算器里的PI的KP
1143   3              FOC_EKI = OBSW_KI_GAIN_RUN3;                          // 估算器里的PI的KI
1144   3              GP07 = 0;
1145   3            }
1146   2            else if(mcFocCtrl.State_Count == 50)
1147   2            {
1148   3              FOC_EKP = OBSW_KP_GAIN_RUN4;                          // 估算器里的PI的KP
1149   3              FOC_EKI = OBSW_KI_GAIN_RUN4;                          // 估算器里的PI的KI
1150   3              GP07 = 1;
1151   3            }
1152   2            else ;
1153   2          }
1154   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   4516    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =    113      16
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =    120    ----
   BIT SIZE         =   ----    ----
   EDATA SIZE       =   ----    ----
   HDATA SIZE       =   ----    ----
   XDATA CONST SIZE =   ----    ----
   FAR CONST SIZE   =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
