C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 1   


C51 COMPILER V9.52.0.0, COMPILATION OF MODULE MC_MOTORCOTROL_LAYER
OBJECT MODULE PLACED IN .\obj\MC_MotorCotrol_layer.obj
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE User\Source\MC_MotorCotrol_layer.c LARGE OMF2 WARNINGLEVEL(0) OPTIMIZE(SIZE
                    -) BROWSE NOAREGS INCDIR(.\Library\Include;.\User\Include) DEBUG PRINT(.\obj\MC_MotorCotrol_layer.lst) TABS(2) OBJECT(.\o
                    -bj\MC_MotorCotrol_layer.obj)

line level    source

   1          
   2          /************************ (C) COPYRIGHT 2015 FT *******************************
   3          * File Name          : 
   4          * Author             : Application Team  Tom.wang 
   5          * Version            : V2.0.0
   6          * Date               : 06/15/2015
   7          * Description        : 电机控制层
   8          ********************************************************************************
   9          /*******************************************************************************
  10          * All Rights Reserved
  11          *******************************************************************************/
  12          
  13          /* Includes ------------------------------------------------------------------*/
  14          #include <FU6812_MCU.h>
  15          #include <sys_conf.h>
  16          #include <fu681x_Device.h>
  17          #include <interrupt.h>
  18          #include <Ftech_BLDC.h>
  19          #include <ESC_PPM.h>
  20          #include <MC_Led.h>
  21          #include <MC_init.h>
  22          #include <MC_UserInterface_layer.h>
  23          #include <MC_MotorCotrol_layer.h>  
  24          #include <MC_MotorFunction_layer.h> 
  25          #include <MC_MotorDriver_layer.h>   
  26          
  27          
  28          /** @addtogroup FT68xx
  29            * @{
  30            */
  31          
  32          /* Private typedef -----------------------------------------------------------*/
  33          /* Private define ------------------------------------------------------------*/
  34          /* Private macro -------------------------------------------------------------*/
  35          /* Private variables ---------------------------------------------------------*/
  36          MCL_TypeDef xdata Ctl _at_ 0x00;
  37          
  38          extern u16 idata giEventCounter;
  39          
  40          /* Private function prototypes -----------------------------------------------*/
  41          
  42          void MCL_Function(void);
  43          void MCL_Bkin_Isr(void);
  44          void MainISR(void);
  45          
  46          static void MCL_Init(void); 
  47          static void MCL_Ready(void);
  48          static void MCL_Precharge(void);
  49          static void MCL_Alignment(void);
  50          static void MCL_OpenLoop(void);
  51          static void MCL_Normal(void);
  52          static void MCL_Stop(void);
  53          static void MCL_Failure(void);
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 2   

  54          
  55          static void U_Task_Ptr(void);
  56          static void A_Task_Ptr(void);  
  57          static void B_Task_Ptr(void);
  58          static void C_Task_Ptr(void);
  59          static void D_Task_Ptr(void);
  60          
  61          /* Private functions ---------------------------------------------------------*/
  62          /*******************************************************************************
  63          * Function Name  : MCL_Function
  64          * Description    : 
  65          * Input          : None
  66          * Output         : None
  67          * Return         : 
  68          *******************************************************************************/
  69          void MCL_Function(void)
  70          {
  71   1        switch(Ctl.State)
  72   1        {
  73   2          case MOTOR_INIT:
  74   2            MCL_Init();
  75   2            break;
  76   2          case MOTOR_STOP:
  77   2            MCL_Stop();
  78   2            break;
  79   2          case MOTOR_READY:
  80   2            MCL_Ready();
  81   2            break; 
  82   2          case MOTOR_PRECHARGE:
  83   2            MCL_Precharge();
  84   2            break;
  85   2          case MOTOR_ALIGNMENGT:
  86   2            MCL_Alignment();
  87   2            break;
  88   2          case MOTOR_OPENLOOP:     
  89   2            MCL_OpenLoop();
  90   2            break;
  91   2          case MOTOR_NORMAL:
  92   2            MCL_Normal();
  93   2            break;
  94   2          case MOTOR_FAILURE:
  95   2            MCL_Failure();
  96   2            break;
  97   2          default:
  98   2            break;
  99   2        }
 100   1      }
 101          
 102          /*******************************************************************************
 103          * Function Name  : MCL_Init
 104          * Description    : 
 105          * Input          : None
 106          * Output         : None
 107          * Return         : 
 108          *******************************************************************************/
 109          void MCL_Init(void)
 110          {
 111   1        MCL_ModuleInit();
 112   1      
 113   1        #if(FD6536PIN_EN) 
 114   1      //  FD6536_EN;
 115   1        #endif
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 3   

 116   1      
 117   1        #if (EFAL == FO_CMP)
                SetBit(CMP_CR1, CMP3EN);  //下桥打开，自举电容充电导致硬件过流
                #elif (EFAL == FO_INT)
                EX0 = 1;
                #elif (EFAL == FO_CMPINT)
 122   1        SetBit(CMP_CR1, CMP3EN);
 123   1        EX0 = 1;
 124   1        #endif
 125   1      
 126   1        Ctl.State = MOTOR_STOP;
 127   1      
 128   1      }
 129          
 130          /*******************************************************************************
 131          * Function Name  : MCL_Stop
 132          * Description    : 
 133          无HALL停机模式，打开6个边沿检测，SAMR=0
 134          * Input          : None
 135          * Output         : None
 136          * Return         : 
 137          *******************************************************************************/
 138          void MCL_Stop(void)
 139          {
 140   1        u16 tValue;                         
 141   1        #if (POS_FB_MODE == SensorLess) 
 142   1        if((CMP_SAMR != 0)||(TIM1_CR4 != 7))
 143   1        {
 144   2          CMP_SAMR = 0;
 145   2          ANGLE_MASK(0);
 146   2          SetBit(TIM1_CR2, T1BRS);
 147   2          ClrBit(TIM1_CR3, T1TIS1);
 148   2          SetBit(TIM1_CR3, T1TIS0);       //CMP
 149   2          TIM1_CR4 = 0x07;                //选择TIM1_DBRx 1 2 3 4 5 6 7
 150   2        }
 151   1        #endif
 152   1      
 153   1        if((Ctl.gStopmodeC == FREE_DOWN)||(Ctl.gStopmodeC == SLOWING_DOWN))
 154   1        {
 155   2          DRV_CMR = PWMOUT_OFF;      //写 DRV_CMP
 156   2      
 157   2          ClrBit(TIM1_CR0, T1OPS0);
 158   2          ClrBit(TIM1_CR0, T1OPS1);
 159   2        }
 160   1        else if(Ctl.gStopmodeC == BREAK_DOWN) 
 161   1        {
 162   2      //    tValue = (BREAK_VALUE)? BREAK_VALUE:Ctl.gBreakValueC;
 163   2      //    Drv.PWM.DutyTar = tValue;                                
 164   2      //    Drv.PWM.DutyCur = Drv.PWM.DutyTar;
 165   2      //    Drv_PWM_Update(Drv.PWM.DutyCur); 
 166   2      
 167   2          DRV_CMR = PWM_ULVLWL_ON;//PWM_ULVLWL_PWM;
 168   2      
 169   2        }
 170   1        if(Ctl.gStartC == FALSE) 
 171   1        {
 172   2          Ctl.State = MOTOR_STOP;
 173   2        }
 174   1        else
 175   1        {
 176   2          Ctl.State =  MOTOR_READY;
 177   2          DRV_OE_ON;
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 4   

 178   2        }
 179   1      }
 180          
 181          /*******************************************************************************
 182          * Function Name  : MCL_Ready
 183          * Description    : 
 184          * Input          : None
 185          * Output         : None
 186          * Return         : 
 187          *******************************************************************************/
 188          void MCL_Ready(void)
 189          {
 190   1        #if (POS_FB_MODE == HallSensor)
                MCL_ModuleDefault();
                giEventCounter = 0;
                Ctl.State =  MOTOR_PRECHARGE ;
              
                #elif (POS_FB_MODE == SensorLess)
 196   1        if(Drv.Stk.Calcnms == 0x7F)
 197   1        { 
 198   2          MCL_ModuleDefault();
 199   2          
 200   2          Drv.Stk.Calcnms = 0;
 201   2          
 202   2          if((CMP_SAMR != 0)||(TIM1_CR4 != 7))
 203   2          {
 204   3            CMP_SAMR = 0;
 205   3            ANGLE_MASK(0);
 206   3            ClrBit(TIM1_CR3, T1TIS1);       //TIM1_DBR7
 207   3            SetBit(TIM1_CR3, T1TIS0);       //CMP
 208   3            TIM1_CR4 = 0x07;                //选择 TIM1_DBRx 1 2 3 4 5 6 7
 209   3          }    
 210   2          
 211   2          giEventCounter = 0;
 212   2          Drv.Stk.Calcnms = 0;
 213   2          SetBit(TIM1_CR2, T1BRS);         
 214   2        }
 215   1        else
 216   1        {
 217   2          Drv_PosTrack();
 218   2        }
 219   1      
 220   1        #endif
 221   1        if(Ctl.gStartC == FALSE) 
 222   1        {
 223   2          Ctl.State = MOTOR_STOP;
 224   2        }
 225   1           
 226   1      }
 227          
 228          /*******************************************************************************
 229          * Function Name  : MCL_Precharge
 230          * Description    : 20170825验证
 231          * Input          : None
 232          * Output         : None
 233          * Return         : 
 234          *******************************************************************************/
 235          void MCL_Precharge(void)
 236          {
 237   1        #if (CHARGENMS != 0)
                u8 temp = CHARGENMS;
                if(temp >= 10)
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 5   

                  temp = 10;
                Drv.PWM.DutyCur = CHARGEDUTY;      //充电占空比
                Drv_PWM_Update(Drv.PWM.DutyCur);   //占空比给定
                EA = 0;
                DRV_DR = MDU_MULA_U16(MDUControl.DutyCur,Drv.PWM.DutyArr+2,15);
                DRV_CMR = PWM_UL_PWM;    //PWM_UL_ON;
                gDelayms(temp);
                DRV_CMR = PWM_ULVL_PWM;  //PWM_VL_ON; //TPWM_UVL_PWM
                gDelayms(temp);
                DRV_CMR = PWM_ULVLWL_PWM;//PWM_WL_ON; //PWM_ULVLWL_ON
                gDelayms(temp);
                DRV_CMR = PWMOUT_OFF;
                gDelayms(1);
                EA = 1;
                #endif
 255   1      
 256   1        #if (POS_FB_MODE == HallSensor)
                  #if(SPEED_CLOSE_EN)
                  pid_spd.Out = 0;
                  #endif
                  Ctl.Tim.STAnms = 0;
                  Ctl.Spd.refTar = SPEED_REF_TAR;
                  Ctl.Spd.refCur = SPEED_REF_INIT; 
                  Drv.PWM.DutyTar = MOTOR_INIT_DUTY;          //充电占空比
                  Drv.PWM.DutyCur = Drv.PWM.DutyTar;
                  Drv_PWM_Update(Drv.PWM.DutyCur);           //占空比给定
              
                  Hall_IRQHandler();                             //hall读取  
                  TIM1_CR4 = Ctl.gStepCur;
                  Ctl.State = MOTOR_NORMAL;                          
              
                #elif (POS_FB_MODE == SensorLess)
 272   1        Ctl.Alig.NmsCount = 0;
 273   1        Drv.PWM.DutyTar = Ctl.Alig.duty;
 274   1        Drv.PWM.DutyCur = Drv.PWM.DutyTar; 
 275   1        Drv_PWM_Update(Drv.PWM.DutyCur);  //占空比给定
 276   1        Ctl.State = MOTOR_ALIGNMENGT; 
 277   1      
 278   1        #endif
 279   1      
 280   1        if(Ctl.gStartC == FALSE)
 281   1        {
 282   2          Ctl.State = MOTOR_STOP;
 283   2        }    
 284   1      }
 285          
 286          /*******************************************************************************
 287          * Function Name  : MCL_Alignment
 288          * Description    : 
 289          * Input          : None
 290          * Output         : None
 291          * Return         : 
 292          *******************************************************************************/
 293          void MCL_Alignment(void)
 294          {
 295   1      #if (POS_FB_MODE == SensorLess )
 296   1        if(Ctl.Alig.NmsCount < Ctl.Alig.timNms)     // 1000ms   2*Ctl.Alig.timNms
 297   1        {
 298   2          //AH->BL,CL    U->V/W         
 299   2          DRV_CMR = PWM_UHVL_PWM;//PWM_WHVL_PWM;
 300   2          Drv.PWM.DutyCur = Ctl.Alig.duty; 
 301   2          Drv_PWM_Update(Ctl.Alig.duty);     //占空比给定
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 6   

 302   2        }
 303   1        else
 304   1        {
 305   2          DRV_CMR = PWMOUT_OFF;
 306   2      
 307   2          Drv.PWM.DutyTar = Ctl.Ramp.DutySta;
 308   2          Drv.PWM.DutyCur = Ctl.Ramp.DutySta; 
 309   2      
 310   2          #if (MOTORROTORCALC ==2)        
                  Ctl.gStepCur = DRV_IRPD_F(5,20,2);    //12 24 10
                  #else
 313   2          Ctl.gStepPre = 0;
 314   2      
 315   2          if (Ctl.gDirectionC == CW) //U->W
 316   2          {
 317   3            Ctl.gStepCur = 1;          //1
 318   3          }
 319   2          else if (Ctl.gDirectionC == CCW)
 320   2          {                        //W->V
 321   3            Ctl.gStepCur = 1;        //2
 322   3          }    
 323   2          #endif
 324   2          
 325   2          CMP_SAMR = (PWM_MASK+PWM_DELAY)<<4;
 326   2          CMP_SAMR += PWM_MASK;
 327   2          //ClrBit(TIM1_IER, T1PDIE);     //位置检测中断
 328   2          SetBit(CMP_CR3, SAMSEL1);     // 使能ON 采样
 329   2          ClrBit(CMP_CR3, SAMSEL0);  
 330   2          
 331   2          TIM1_CR4 = 1;                   //选择TIM1_DBRx  
 332   2          ANGLE_MASK(60);                   //BCCR7 -> 60 度
 333   2      
 334   2          Drv.PWM.DutyArr = RAMP_PWMARR;
 335   2          DRV_ARR = Drv.PWM.DutyArr;
 336   2          DRV_COMR = PWM_FLOW;
 337   2          //Ctl.Ramp.cpNmsCount = Ctl.Ramp.cpNms; 
 338   2          Drv.PWM.DutyIncValue = Ctl.Ramp.Dutystep;   //loop INC DEC
 339   2          Drv.PWM.DutyDecValue = Ctl.Ramp.Dutystep;
 340   2          Drv.PWM.DutyTar = Ctl.Ramp.DutyEnd;
 341   2          Drv.PWM.DutyCur = Ctl.Ramp.DutySta;
 342   2          MDUControl.DutyCur = Drv.PWM.DutyCur;
 343   2          DRV_DR = MDU_MULA_U16(MDUControl.DutyCur,Drv.PWM.DutyArr+2,15);
 344   2          //NSS_ONOFF;
 345   2          Ctl.State = MOTOR_OPENLOOP;
 346   2        }
 347   1        if(Ctl.gStartC == FALSE)
 348   1        {
 349   2          Ctl.State = MOTOR_STOP;
 350   2        }    
 351   1      #endif  
 352   1      }
 353          
 354          /*******************************************************************************
 355          * Function Name  : MCL_OpenLoop  RampUp
 356          * Description    : 
 357          * Input          : None
 358          * Output         : None
 359          * Return         : 
 360          *******************************************************************************/
 361          void MCL_OpenLoop(void)
 362          {  
 363   1        Drv_SmartStartCalc();                 
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 7   

 364   1      #if (POS_FB_MODE == SensorLess)
 365   1        #if (WAIT_STEP == WAIT_RAMPUP)
                Ctl.Ramp.cpNumB = 0;
                #endif
 368   1      
 369   1        if(Ctl.Ramp.cpNmsCount >= Ctl.Ramp.cpNms) 
 370   1        { 
 371   2          //启动力矩给定
 372   2          //NSS_ONOFF;
 373   2          #if (RAMP_MODE != 3)         
 374   2          if(Drv.PWM.DutyCur == Ctl.Ramp.DutyEnd)
 375   2          {
 376   3            Drv.PWM.DutyTar = Ctl.Ramp.DutySta;
 377   3            Drv.PWM.DutyCur = Ctl.Ramp.DutySta; 
 378   3          }
 379   2          #else
                  Drv.PWM.DutyCur = Ctl.Ramp.DutySta;
                  Drv.PWM.DutyTar = Ctl.Ramp.DutyEnd;
                  #endif
 383   2          
 384   2          //Drv_DutyRampCale();
 385   2          Drv_PWM_Update(Drv.PWM.DutyCur);
 386   2         // DRV_DR = MDU_MULA_U16(MDUControl.DutyCur,Drv.PWM.DutyArr+2,15); 
 387   2          //换相时间给定
 388   2          Ctl.Ramp.cpNmsCount = 0;
 389   2          
 390   2          if((Ctl.Ramp.cpNms - Ctl.Ramp.TimEnd) > Ctl.Ramp.TimStep)
 391   2          {
 392   3            Ctl.Ramp.cpNms = Ctl.Ramp.cpNms - Ctl.Ramp.TimStep;
 393   3          }
 394   2          else if((Ctl.Ramp.TimEnd - Ctl.Ramp.cpNms) > Ctl.Ramp.TimStep)
 395   2          {
 396   3            Ctl.Ramp.cpNms = Ctl.Ramp.cpNms + Ctl.Ramp.TimStep;
 397   3          }
 398   2            #if (RAMP_MODE != 3)
 399   2          else if(Ctl.Ramp.cpNms == Ctl.Ramp.TimEnd)
 400   2          {
 401   3            Ctl.Ramp.cpNms = Ctl.Ramp.TimSta;
 402   3          }
 403   2            #endif
 404   2          else 
 405   2          {
 406   3            Ctl.Ramp.cpNms = Ctl.Ramp.TimEnd;
 407   3            if(Drv.PWM.DutyTar ==Ctl.Ramp.DutyEnd)
 408   3            {
 409   4              Ctl.Ramp.ComCnt++;                    //换相次数
 410   4            }
 411   3          }
 412   2      
 413   2          //检测反电势
 414   2          #if ((RAMP_MODE == 1)||(RAMP_MODE == 2))
 415   2            SetBit(TIM1_IER, T1PDIE);             //位置检测中断
 416   2            SetBit(TIM1_IER, T1UPD); 
 417   2            TIM1__RARR = Ctl.Ramp.MaskTime ;      //0.050ms   
 418   2      
 419   2          
 420   2          #elif (RAMP_MODE == 3)
                    Ctl.gStepCur = TIM1_CR4;
                    SetBit(TIM1_IER, T1UPD);
                    Drv.speed.EventPeriod = TIM1__BCCR;
                  #endif
 425   2        }
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 8   

 426   1        
 427   1        
 428   1        #if (RAMP_MODE == 3)
                if(((Ctl.Ramp.ComCnt >= Ctl.Ramp.ComNum)&&(Ctl.gStepCur == 1)))  
                { 
                  DRV_CMR = PWMOUT_OFF;
                  Ctl.State = MOTOR_READY;
                }
                #endif
 435   1                
 436   1        if(Ctl.gStartC == FALSE)
 437   1        {
 438   2          Ctl.State = MOTOR_STOP;
 439   2        }  
 440   1      #endif  
 441   1      
 442   1      }
 443          
 444          /*******************************************************************************
 445          * Function Name  : MCL_Normal
 446          * Description    :
 447          * Input          : None
 448          * Output         : None
 449          * Return         : 
 450          *******************************************************************************/
 451          void MCL_Normal(void)
 452          { 
 453   1        if(Ctl.gStartC == FALSE)
 454   1        {
 455   2          DRV_CMR = PWMOUT_OFF;
 456   2          Ctl.State = MOTOR_STOP;
 457   2        }  
 458   1        #if(OCRESTART_EN) 
                if(Ctl.OC.u16Runms >= 5000)         //运行5s
                {
                  Ctl.OC.u16Runms = 5000;
                  Ctl.OC.u8ReNum = 0;               
                  Ctl.OC.u16nmsCount =0;
                }
                #endif  
 466   1        #if(FO_EN==2)
                if(Ctl.FO.ReCounter >= 1000)     //1S后清除FO故障
                {
                  Ctl.FO.FoCounter = 0;           //清FO故障计数器
                  Ctl.FO.ReCounter = 1000;
                }
                #endif
 473   1        #if(STALLRESTARTNUM) 
 474   1        if((Ctl.Stall.u16NormalRunms >= STANMS+500)) //运行5s         
 475   1          {
 476   2            Ctl.Bemf.RstartNmsConuter = 0;           //连续重启时间计时器
 477   2            Ctl.Stall.WaitReatartNms = 0;            //堵转重启等待时间
 478   2            Ctl.Stall.u16NormalRunms = STANMS+500;   //
 479   2            Ctl.Stall.u8Num = 0;                     //清除堵转故障 次数标记     
 480   2          }
 481   1        #endif
 482   1      
 483   1      }
 484          
 485          /*******************************************************************************
 486          * Function Name  : MCL_Failure
 487          * Description    : 
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 9   

 488          * Input          : None
 489          * Output         : None
 490          * Return         : 
 491          *******************************************************************************/
 492          void MCL_Failure(void)
 493          {
 494   1        DRV_CMR = PWMOUT_OFF;
 495   1        DRV_OE_OFF;             //
 496   1        Ctl.gStartC = FALSE;
 497   1        
 498   1      //-------------------------------------------------------  
 499   1      //故障记录    
 500   1        #if(FAILLOGEN)
                if(Ctl.E_message.ErrorF==0)
                {
                  //故障信息记录
                  Ctl.E_message.ErrorF   = 1 ;
                  Ctl.E_message.SysError = Ctl.SysError;
                  //Ctl.E_message.State  = Ctl.State ;
                  Ctl.E_message.FR       = Ui.flg.FR;
                  //Ctl.E_message.SpeedRef = pid_spd.Ref;
                  Ctl.E_message.SpeedFdb = Drv.speed.SpeedRpm;
                  Ctl.E_message.Duty     = Drv.PWM.DutyCurrent;
                  Ctl.E_message.ImeasBus = Drv.AdcMeas.ImeasBus;
                  Ctl.E_message.VdcMeas = Drv.AdcMeas.VdcAvgMeas;
                  Ctl.E_message.Ibus     = Drv.AdcMeas.Ibus;
                  Ctl.E_message.Vdc      = Drv.AdcMeas.Vdc;
              //-------------------------------------------------------  
              //故障信息flash 存储。
                  #if(FAILSTOREEN)
                  if((Ctl.SysError != E_OV)&&(Ctl.SysError != E_UV))       
                      ;//Flash_Save_Data();
                  #endif     
                }
                #endif
 523   1      //-------------------------------------------------------
 524   1      //故障重启
 525   1        #if(VBUSRECOVER_EN)
                if((Ctl.SysError == E_OV)||(Ctl.SysError == E_UV))
                {
                  if(Ctl.Tim.Nonms>=NONMS)
                  {
                    Mcl_MotorRestart();
                  }
                }
                #endif
 534   1      //-------------------------------------------------------
 535   1      //软件过流重启  
 536   1        #if (OCRESTARTNUM == 1)        
                if(Ctl.SysError == E_OC) 
                //if((Ctl.SysError == E_OC)||(Ctl.SysError == E_FAIL))  
                {
                  if(Ctl.OC.ReFlag == 0)
                  {
                    if(Ctl.OC.u8ReNum < OCRESTARTNUM)
                    {
                      Ctl.OC.u8ReNum ++;
                      Ctl.OC.u16nmsCount = 0;  
                      Ctl.OC.ReFlag = 1;
                    }
                  }
                  if(Ctl.OC.ReFlag == 1)
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 10  

                  {
                    if(Ctl.OC.u16nmsCount >= OCRESTARTTIM)
                    {
                      Ctl.OC.ReFlag = 0;
                      Mcl_MotorRestart();
                    }
                  }
                }
                #endif
 559   1      //-------------------------------------------------------
 560   1      //堵转重启
 561   1        #if(STALLRESTARTNUM)    
 562   1        if((Ctl.SysError == E_STA)||(Ctl.SysError == E_STB3)||(Ctl.SysError == E_STB2)||((Ctl.SysError == E_STB1
             -)))
 563   1        {
 564   2          if(Ctl.Stall.u8FL == 0)
 565   2          {
 566   3            if(Ctl.Stall.WaitReatartNms >= STALLRESTARTTIM)
 567   3            {
 568   4              Ctl.Stall.WaitReatartNms = 0;
 569   4              Drv_StallRestart(); 
 570   4            }
 571   3          }
 572   2        }
 573   1        #endif
 574   1          
 575   1      //-------------------------------------------------------
 576   1      //过温重启  
 577   1        #if(OHRE_EN)           
                if((Ctl.SysError == E_OH1)||(Ctl.SysError == E_OH2)||(Ctl.SysError == E_OH3))
                {
                  if((Ctl.Tim.OH1REnms>=OH1NMS)&&((Ctl.Tim.OH2REnms>=OH2NMS)))
                  {
                    Mcl_MotorRestart();
                  }
                }
                #endif
 586   1      
 587   1      }
 588          
 589          /*******************************************************************************
 590          * Function Name  : MCL_Bkin_Isr
 591          * Description    : 
 592          * Input          : break input
 593          * Output         : None
 594          * Return         : 
 595          *******************************************************************************/
 596          void MCL_Bkin_Isr(void)
 597          {
 598   1        #if (FO_EN == 1)
                  DRV_OE_OFF;
                  Ctl.SysError = E_FAIL;
                  Ctl.State = MOTOR_FAILURE;    
                #elif (FO_EN == 2)
                  Ctl.FO.FoCounter++;
                  Ctl.FO.ReCounter = 0;  //FO信号后重新计数
                 if(FO_NUM == 10000)
                 {
                   ;
                 }
                 else if(Ctl.FO.FoCounter >= FO_NUM)
                  {
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 11  

                    DRV_OE_OFF;
                    Ctl.SysError = E_FAIL;
                    Ctl.State = MOTOR_FAILURE;  
                  }
                #elif (FO_EN == 3)
                  ;
                #endif
 618   1            
 619   1        #if(FAILLOGEN)
                if(Ctl.E_message.State == 0)
                {
                  Ctl.E_message.State = Ctl.State; 
                }
                #endif  
 625   1      }
 626          /*******************************************************************************
 627          * Function Name  : MainISR
 628          * Description    : 100us中断执行，用于同步PWM计算 速度闭环，电流闭环
 629          * Input          : 
 630          * Output         : 
 631          * Return         : 1
 632          *******************************************************************************/
 633          void MainISR(void)
 634          {
 635   1        s16 xdata tDutyCur;
 636   1        
 637   1        Ctl.Tim.gIsrTicker++;
 638   1      
 639   1        if(Ctl.Tim.gIsrTicker >= 10)
 640   1        {
 641   2          Ctl.Tim.gIsrTicker = 0;
 642   2          A_Task_Ptr();                  
 643   2        }
 644   1        else if(Ctl.Tim.gIsrTicker == 1)
 645   1        {
 646   2          if(Ctl.gPowerOnF == 0X7f)
 647   2          {
 648   3            B_Task_Ptr();     
 649   3          }
 650   2        }
 651   1        else if(Ctl.Tim.gIsrTicker == 2) 
 652   1        {
 653   2          if(Ctl.gPowerOnF == 0X7f)
 654   2          {
 655   3            C_Task_Ptr();       
 656   3          }
 657   2        }
 658   1        else if(Ctl.Tim.gIsrTicker == 2) 
 659   1        {
 660   2          if(Ctl.gPowerOnF == 0X7f)
 661   2          {
 662   3            D_Task_Ptr();       
 663   3          }
 664   2        }
 665   1        else if(Ctl.Tim.gIsrTicker == 4) 
 666   1        {
 667   2          U_Task_Ptr();   //用户使用1ms计数器
 668   2        }
 669   1        #if(SPEED_TICKPEREN)
 670   1        else if(Ctl.Tim.gIsrTicker == 9)
 671   1        #endif
 672   1        {
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 12  

 673   2          if(Ctl.State != MOTOR_NORMAL)
 674   2          {
 675   3            //输入： Ctl.Spd.refTar     输出： Ctl.Spd.refCur
 676   3            if(Ctl.Spd.refTar >= _IQ(0.2))
 677   3            Ctl.Spd.refTar = _IQ(0.2);
 678   3          }
 679   2          else
 680   2          {
 681   3          
 682   3          }
 683   2          Drv_SpeedRampCale();
 684   2        }
 685   1        
 686   1        //if(Ctl.State == MOTOR_NORMAL)
 687   1        {
 688   2          #if(SPEED_CLOSE_EN)                        //速度环
                  Ctl.Tim.counterSpeed++;
                  if(Ctl.Tim.counterSpeed > Ctl.Tim.numTicksPerSpeed)
                  {
                    Ctl.Tim.counterSpeed = 0;
              
                      pid_spd.Ref = Ctl.Spd.refCur;          //REF
                      pid_spd.Fdb = Drv.speed.Speed;         //FDB
                      #if (CURRENT_CLOSE_EN>1)
                      Ctl.Is.refTar = Pid_calc(&pid_spd);    //速度环输出
                      pid_is.Ref = Ctl.Is.refTar;            //电流环输入
                      #else
                      tDutyCur = Pid_calc(&pid_spd);
                      #endif
                  }
                  #elif (CURRENT_CLOSE_EN>1)                 //电流环
                  pid_is.Ref = Ctl.Is.refTar;                //电流环输入
                  #else
 706   2          tDutyCur = Ctl.Spd.refCur;
 707   2          #endif
 708   2      
 709   2          #if (CURRENT_CLOSE_EN>1)
                  Ctl.Tim.counterCurrent++;
                  if(Ctl.Tim.counterCurrent > Ctl.Tim.numTicksPerCurrent)
                  {
                    Ctl.Tim.counterCurrent = 0;
              
                      #if (CURRENT_CLOSE_EN == 2)
                      if(pid_is.Ref > CURRENT_INMAX)
                      {
                        pid_is.Ref = CURRENT_INMAX;
                      }
                      pid_is.Fdb = Drv.AdcMeas.ImeasBus;
                      #elif (CURRENT_CLOSE_EN == 3) 
                      Power.fdb = ((u32)Drv.AdcMeas.IBusAvgMeas*(u32)Drv.AdcMeas.VdcMeas)>>10;
                      pid_is.Fdb = Power.fdb;
                      pid_is.Ref = Power.ref;
                      #endif
                      Drv.PWM.DutyCur = Pid_calc(&pid_is); //电流环输出
                    }
                  }
                  #endif
 730   2          
 731   2          Drv_DutyLimit(); //输出电压限制
 732   2      
 733   2          #if (POS_FB_MODE == HallSensor)  //有HALL换相，根据 DutyCur 
                    if(tDutyCur < 0)
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 13  

                    {
                      Drv.PWM.DutyCur = -tDutyCur; 
                      if(Ctl.gDirectionC != CW)
                      {
                        ClrBit(TIM1_CR0, T1OPS1);        
                        ClrBit(TIM1_CR0, T1OPS0);           
                        giEventCounter = 0;
                        Ctl.gDirectionC = CW;   
                        ClrBit(DRV_CR, DDIR);
                      }
                    }
                    else if(tDutyCur > 0)
                    {
                      Drv.PWM.DutyCur = tDutyCur;
                      if(Ctl.gDirectionC != CCW)
                      {
                        ClrBit(TIM1_CR0, T1OPS1);       
                        ClrBit(TIM1_CR0, T1OPS0);                
                        giEventCounter = 0;
                        Ctl.gDirectionC = CCW;   
                        SetBit(DRV_CR, DDIR);
                      }      
                    }
                  #else
 759   2            Drv.PWM.DutyCur = (tDutyCur<0)? -tDutyCur:tDutyCur;
 760   2          #endif
 761   2          Drv_PWM_Update(Drv.PWM.DutyCur);  
 762   2        }
 763   1      }
 764          
 765          /*******************************************************************************
 766          * Function Name  : A_Task_Ptr
 767          * Description    : 系统运行计时：上电、定位、拖动、转速跟踪、刹车、
 768          * Input          : 
 769          * Output         : 
 770          * Return         : 1
 771          *******************************************************************************/
 772          void A_Task_Ptr(void)
 773          {  
 774   1        if(Ctl.gPowerOnF == 0X7f)
 775   1        {
 776   2          #if(FAILLAMPEN)
 777   2          gUserLEDmsCunter++;    //故障指示led闪烁
 778   2          #endif
 779   2          
 780   2          #if (POS_FB_MODE == SensorLess)
 781   2          Ctl.Alig.NmsCount++;      //定位定时器
 782   2          Ctl.Ramp.cpNmsCount++;    //换相定时器
 783   2          if(Drv.Stk.Calcnms != 0x7F)
 784   2          {  
 785   3            Drv.Stk.Calcnms++;
 786   3          }
 787   2          else
 788   2          {
 789   3            Drv.Stk.Calcnms = 0x7F;
 790   3          }
 791   2          #endif
 792   2          
 793   2          
 794   2          #if(STARSTARTNMS)
                  if((Ctl.gStartC == TRUE)&&(Ctl.State != MOTOR_NORMAL))
                  {
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 14  

                    Ctl.Bemf.RstartNmsConuter++;
                  }
                  else if(Ui.flg.START == FALSE)
                  {
                    Ctl.Bemf.RstartNmsConuter = 0;
                  }
                  #endif
 804   2      
 805   2        
 806   2          #if(STALLRESTARTNUM)               //堵转重启
 807   2          if((Ctl.SysError == E_STA)||(Ctl.SysError == E_STB3)||(Ctl.SysError == E_STB2)||((Ctl.SysError == E_ST
             -B1)))
 808   2          {
 809   3            Ctl.Stall.WaitReatartNms++;
 810   3          }
 811   2          #endif
 812   2      
 813   2          #if(OCRESTART_EN) 
                  Ctl.OC.u16nmsCount++;              //过载保护重启等待时间
                  #endif
 816   2        }
 817   1        else
 818   1        {
 819   2          Ctl.Tim.PowerOnNms++;            //上电定时器
 820   2        }
 821   1      }
 822          /*******************************************************************************
 823          * Function Name  : B_Task_Ptr
 824          * Description    : 保护计时：过流计时器、过欠压计时器、堵转计时器、过温计时器
             -、过载
 825          * Input          : 
 826          * Output         : 
 827          * Return         : 1
 828          *******************************************************************************/
 829          void B_Task_Ptr(void)  
 830          {
 831   1        //_nop_(); 
 832   1      //------------------------------------------
 833   1        if(Ctl.State == MOTOR_NORMAL)
 834   1        {
 835   2          Ctl.Stall.u16NormalRunms++;
 836   2          #if(OCRESTART_EN)
                  Ctl.OC.u16Runms++;
                  #endif
 839   2      
 840   2          #if(STARSTARTNMS) 
                  Ctl.Tim.STAnms++;               //堵转计时器    
                  if(Ctl.Tim.STAnms >= STANMS)
                  {
                    Ctl.SysError = E_STA; 
                  }   
                  #endif
 847   2        }
 848   1        else
 849   1        {
 850   2          giEventCounter = 0;
 851   2          Ctl.Stall.u16NormalRunms = 0;
 852   2        }
 853   1        
 854   1        #if(OVERLOAD_EN)
                Ctl.OL.msFlag = 0x7F;              //过载保护
                #endif
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 15  

 857   1      //------------------------------------------
 858   1      //过温检测
 859   1        #if(OH1_EN)
                if(Drv.AdcMeas.Therm1 >= 1000)       // (100℃)
                {
                  Ctl.Tim.OH1nms++;  //
                  Ctl.Tim.OH1REnms = 0;
                  if(Ctl.Tim.OH1nms >= OH1NMS)
                  {
                    Ctl.SysError = E_OH1;
                    Ctl.Tim.OH1nms = OH1NMS;
                  }    
                }
                else if(Drv.AdcMeas.Therm1 <= 900)   //  (80~120℃)
                {
                  Ctl.Tim.OH1REnms++;
                  
                  Ctl.Tim.OH1nms--;
                  if(Ctl.Tim.OH1nms < 0)
                  {
                    Ctl.Tim.OH1nms = 0;
                  }
                } 
                else                                  //  (~80)
                {
                  Ctl.Tim.OH1REnms++;
                 
                  Ctl.Tim.OH1nms--;
                  if(Ctl.Tim.OH1nms<0)
                  {
                    Ctl.Tim.OH1nms = 0;
                  }
                }     
                #endif
 891   1      //------------------------------------------
 892   1      //过流检测
 893   1        #if (CBCCP_EN != 0)
                Ctl.FO.ReCounter++; 
                #endif   
 896   1        #if(OVERCURRENT_EN)
                if(Drv.AdcMeas.ImeasBus > MECASEOVERCURRENT)
                  {
                    Ctl.Tim.OCnms++;
                    if(Ctl.Tim.OCnms >= OCNMS)      //过流
                    {
                      Ctl.SysError = E_OC;
                    }      
                  }
                else
                  {
                    Ctl.Tim.OCnms--;
                    if(Ctl.Tim.OCnms<0)
                    {
                      Ctl.Tim.OCnms = 0; 
                    }
                  }    
                #endif  
 914   1      
 915   1      //------------------------------------------
 916   1      //过压欠压检测
 917   1        #if((OVERVOLTAGE_EN)||(UNDERVOLTAGE_EN)||(VBUSRECOVER_EN))
                if(Drv.AdcMeas.VdcAvgMeas != 0)             
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 16  

                {
                  if(Drv.AdcMeas.VdcAvgMeas > MECASEOVERVOLTAGE)
                  {
                    Ctl.Tim.OVnms++;
                    Ctl.Tim.UVnms = 0;
                    Ctl.Tim.Nonms = 0;
                    #if(OVERVOLTAGE_EN)
                    if(Ctl.Tim.OVnms >= OVNMS)      //过压
                    {
                      Ctl.SysError = E_OV;
                    }
                    #endif      
                  }
                  else if (Drv.AdcMeas.VdcAvgMeas < MECASEUNDERVOLTAGE)
                  {
                    Ctl.Tim.OVnms = 0;
                    Ctl.Tim.UVnms++;
                    Ctl.Tim.Nonms = 0;
                    #if(UNDERVOLTAGE_EN)
                    if(Ctl.Tim.UVnms >= UVNMS)
                    {
                      Ctl.SysError = E_UV;
                    }
                    #endif      
                  }
                  else if ((Drv.AdcMeas.VdcAvgMeas < MECASEOVREERVOLTAGE)&&(Drv.AdcMeas.VdcAvgMeas > MECASEUNREDERVOLTAG
             -E))
                  {
                    Ctl.Tim.OVnms = 0;
                    Ctl.Tim.UVnms = 0;
                    Ctl.Tim.Nonms++;
                  }      
                }
                #endif
 952   1      //------------------------------------------
 953   1      //无HALL 重启时间到 故障
 954   1        #if(STARSTARTNMS)               
                if(Ctl.Bemf.RstartNmsConuter > STANMS)   
                {
                  Ctl.SysError = E_ERR1;
                }
                #endif
 960   1      //------------------------------------------  
 961   1      //过载  
 962   1        #if(OVERLOAD_EN)
                if(Ctl.OL.Value != 0x7F)      
                {
                  Ctl.SysError = E_OL;
                }
                #endif 
 968   1      //------------------------------------------  
 969   1        if (Ctl.SysError!=NONE)
 970   1        {
 971   2          DRV_CMR = PWMOUT_OFF;
 972   2          DRV_OE_OFF;
 973   2      
 974   2          #if(FAILSTOREEN)
                  if(Ctl.E_message.State == 0)
                  {
                   Ctl.E_message.State = Ctl.State;
                  }
                  #endif
C51 COMPILER V9.52.0.0   MC_MOTORCOTROL_LAYER                                              08/21/2018 12:02:27 PAGE 17  

 980   2          Ctl.State = MOTOR_FAILURE;
 981   2        }
 982   1      }
 983          /*******************************************************************************
 984          * Function Name  : C_Task_Ptr
 985          * Description    : 
 986          * Input          : 
 987          * Output         : 
 988          * Return         : 1
 989          *******************************************************************************/
 990          void C_Task_Ptr(void)  
 991          {
 992   1        Ctl.Tim.OnOffnms++;        //ONOFF定时器
 993   1      
 994   1        #if(LAMP_EN)               //无操作延时定时器
                Ctl.Tim.NoOperationDelaynms++;   
                #endif 
 997   1        
 998   1        #if(ESC_PPMSREFEN) 
                ESC_PPMCheckErr();
                #endif
1001   1      }
1002          /*******************************************************************************
1003          * Function Name  :  D_Task_Ptr
1004          * Description    : 
1005          * Input          : 
1006          * Output         : 
1007          * Return         : 
1008          *******************************************************************************/
1009          void D_Task_Ptr(void)
1010          {
1011   1      
1012   1      }
1013          /*******************************************************************************
1014          * Function Name  : U_Task_Ptr
1015          * Description    : 留给用户的1ms计时器
1016          * Input          : 
1017          * Output         : 
1018          * Return         : 
1019          *******************************************************************************/
1020          void U_Task_Ptr(void)
1021          {   
1022   1        //……………………
1023   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   1394    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----       4
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   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)
