⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 BLDC_SENSORLESS controller using freeescale mcu aw60
💻 C
📖 第 1 页 / 共 5 页
字号:
    TPM1C5V = temp;    
  }      
}

/*****************************************************************************
*
* Function: PWMTransitionVectorCCD(void)
*
* Description: Generates the 6-channel complementary bipolar PWM output 
*              for 3-phase power stages. This vector is used for transition
*              from alignment to first vector after alignment
*              rotation direction: counter clockwise
*     
* Returns: None
*
* Global Data: 
*     unsigned int    duty_cycle - actual duty cycle
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: 
*    PWM_MODULO     - defines PWM output frequency
*    PWM_DEAD_TIME  - define PWM dead time
*
*    IMPORTANT: To ensure correct PWM output the function must be called
*               within each PWM cycle. The function must end before new
*               PWM cycle starts!!!
*****************************************************************************/
void PWMTransitionVectorCCD(void)
{
  signed int temp;
  TPM1C0SC =  0;                   // PWM A, TOP
  TPM1C1SC =  0;                   // PWM A, BOT
  TPM1C2SC =  0;                   // PWM B, TOP
  TPM1C3SC =  0;                   // PWM B, BOT
  TPM1C4SC =  0;                   // PWM C, TOP
  TPM1C5SC =  0;                   // PWM C, TOP
  temp = PWM_MODULO/2 - PWM_DEAD_TIME - duty_cycle;
  TPM1C1V = temp;
  TPM1C2V = temp;
  TPM1C5V = temp;  
  if (temp <= 0)
  {
     TPM1C0V = 0;
     TPM1C3V = 0;
     TPM1C4V = 0;
  }
  else
  {
    temp = PWM_MODULO/2 + PWM_DEAD_TIME - duty_cycle;
    TPM1C0V = temp;
    TPM1C3V = temp;
    TPM1C4V = temp;
  }
}


/*****************************************************************************
*
* Function: signed int controllerPI(..)
*
* Description:
* PI Controller 16 bit inputs, 16 bit outputs without limitation
*                           ---          -----------
*             w(k)  -------| + |  e(k)  |           |  u(k)
*                          |   |--------|    PI     |---------
*             y(k)  -------| - |        |           |
*                           ---          -----------
*
*      w(t) - desiredValue in continuous time domain
*      y(t) - MeasuredValue (feedback) in continuous time domain
*      e(t) - input error in continuous time domain
*      u(t) - controller output in continuous time domain
*
*      w(k) - desiredValue in step k - discrete time domain
*      y(k) - MeasuredValue (feedback) in step k - discrete time domain
*      e(k) - input error in step k - discrete time domain
*      u(k) - controller output in step k - discrete time domain
*
*
*      The PI controller algorithm in continuous time domain:
*                             /t
*      u(t) = K[e(t) + 1/Ti * | e(t)*dt]                               (1)
*                             /0
*
*      K  - controller gain
*      Ti - integral time constant
*
*      e(t) = w(t) - y(t)                                              (2)
*
*      PI controller expressed in signed integer:
*
*      u_si(k) = KP * e_si(k) + ui_si(k - 1) + KI * e_si(k)            (3)
*
*      e_si(k) = w_si(k) - y_si(k)                                     (4)
*
*      ui_si = ui_si(k - 1) +  KI * e_si(k)                            (5)
*
*      T - sampling time
*     
* Returns: signed int = ui_si
*
* Global Data:None
*
* Arguments:
*   desiredValue   signed int      - Desired value (input)
*   measuredValue  signed int      - Measured value (input)
*   sPIparams      sPIparams    - controller parameters (input/output)
*
* Range Issues: None
*
* Special Issues:
*   Output, Integral Portion and Control Difference are saturated
*   Set proper value to IntegralPortionK_1 before first calling is recommended
*
*****************************************************************************/
signed int controllerPI(signed int desiredValue, signed int measuredValue, sPIparams *pParams, signed int NegativePILimit, signed int PositivePILimit)
{
    signed int proportionalPortion, integralPortion, integralPortionK_1, PIoutput;
    signed int controlDifference;
    unsigned char proportionalGain, integralGain;

    controlDifference = sub(desiredValue, measuredValue);
    proportionalGain = pParams -> ProportionalGain;
    integralGain = pParams -> IntegralGain;
    integralPortionK_1 = pParams -> IntegralPortionK_1;
  
    if (controlDifference >= 0)
    {
        /* Positive difference */
        proportionalPortion = (signed int) umul_16x8((unsigned int)controlDifference, proportionalGain);
        integralPortion     = (signed int) umul_16x8((unsigned int)controlDifference, integralGain);
    }
    else  
    {
        /* Negative difference */
        controlDifference   = neg(controlDifference);
        proportionalPortion = -(signed int) umul_16x8((unsigned int)controlDifference, proportionalGain);
        integralPortion     = -(signed int) umul_16x8((unsigned int)controlDifference, integralGain);
    }
    
    integralPortion = add(integralPortionK_1, integralPortion);
    
    /* integral portion limitation */
    if(integralPortion >= PositivePILimit)
        pParams -> IntegralPortionK_1 = PositivePILimit;
    else if(integralPortion <= NegativePILimit)
        pParams -> IntegralPortionK_1 = NegativePILimit;
    else
        pParams -> IntegralPortionK_1 = integralPortion;

    PIoutput = add(integralPortion, proportionalPortion); /* controller output */
   
    /* controller output limitation */
    if(PIoutput >= PositivePILimit)
        PIoutput = PositivePILimit;
    else if(PIoutput <= NegativePILimit)
        PIoutput = NegativePILimit;

    return PIoutput;
}


/*****************************************************************************
*
* Function: rampGetValue16
*
* Description:
*   The function increases or decrements the Output value by step defined
*   by Increment to bring the Actual value closer to the Required Value.
*     
* Returns:
*   signed int = ActValue+(ReqValue-ActValue)*Increment/abs(ReqValue-ActValue)
*
* Global Data:None
*
* Arguments:
*   Increment signed int    - Increment
*   ActValue  signed int    - Actual value
*   ReqValue  signed int    - Required value
*
* Range Issues:
*
* Special Issues:
*
*****************************************************************************/
signed int rampGetValue16(signed int Increment, signed int ActValue, signed int ReqValue)
{
signed int Temp;  
  
  Temp = add(ActValue,Increment);
  
  /* Is there space to increment ?*/
  if (Temp <= ReqValue) return (Temp);
  else
  {
      Temp = sub(ActValue,Increment); 
      /* Is there space to decrement ? */
      if (Temp >= ReqValue) return (Temp);
      else return (ReqValue);
  }
}


/*****************************************************************************
*
* Function: void CheckManualInterface (tInterfaceStatus *Status)
*
* Description:
*   The function checks status of user interface Up/Down Buttons and
*   and START/STOP switch. Any event on user interface is stored in
*   *Status variable. The function considers following I/O ports for
*   user interface:
*   Down Button:        port G - bit 0
*   Up button:          port G - bit 1
*   START/STOP switch:  port C - bit 4
*     
* Returns: None
*   
* Global Data:None
*
* Arguments:
*   tInterfaceStatus *Status - Status of user interface
*
* Range Issues:
*   DEBOUNCE_VALUE - define debounce period
*                    debounce period = DEBOUNCE_VALUE * execution time of
*                    this function
* Special Issues:
*   The function should be regularly called to solidly detect even on user
*   interface
*****************************************************************************/
void CheckManualInterface (tInterfaceStatus *Status)
{
  static unsigned char upCounter, downCounter, switchCounter;
  
  
  if ((PTGD_PTGD0 == 0) && (Status->bit.Down == 0))
  {
    if (downCounter < DEBOUNCE_VALUE)
    {
      downCounter++;
    }
  }
  else
  {
     if (downCounter < DEBOUNCE_VALUE)
    {
      downCounter = 0;
    }
  }
  if ((downCounter == DEBOUNCE_VALUE) && (PTGD_PTGD0 == 1))
  {
    Status->bit.Down = 1;
    downCounter = 0;
  }
  
  if ((PTGD_PTGD1 == 0) && (Status->bit.Up == 0))
  {
    if (upCounter < DEBOUNCE_VALUE)
    {
      upCounter++;
    }
  }
  else
  {
    if (upCounter < DEBOUNCE_VALUE)
    {
      upCounter = 0;
    }
  }
  if ((upCounter == DEBOUNCE_VALUE) && (PTGD_PTGD1 == 1))
  {
    Status->bit.Up = 1;
    upCounter = 0;
  }
  if (PTCD_PTCD4 == 1)
  {
    if (switchCounter < DEBOUNCE_VALUE)
    {
      switchCounter++;
    }
  }
  else
  {
    if (switchCounter > 0)
    {
      switchCounter--;
    }
  }
  if (switchCounter == DEBOUNCE_VALUE)
  {
    if (Status->bit.Switch == 0)
    {
      Status->bit.Switch = 1;
      Status->bit.SwitchChange = 1;
    }
  }
  if (switchCounter == 0)
  {
    if (Status->bit.Switch == 1)
    {      
      Status->bit.Switch = 0;
      Status->bit.SwitchChange = 1;
    }
  }
}


/*****************************************************************************
*
* Function: static void SaveDCBvoltage(void)
*
* Description: Saves ADC result to the respective variable              
*     
* Returns: None
*
* Global Data: 
*    unsigned int dcb_voltage - DC bus voltage 
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void SaveDCBvoltage(void)
{
  dcb_voltage = AD1R;
}


/*****************************************************************************
*
* Function: static void SaveDCBcurrent(void)
*
* Description: Saves ADC result to the respective variable              
*     
* Returns: None
*
* Global Data: 
*    signed int dcb_current - DC bus current 
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void SaveDCBcurrent(void)
{
  dcb_current = AD1R;
}


/*****************************************************************************
*
* Function: static void SaveDCBtemperature(void)
*
* Description: Saves ADC result to the respective variable              
*     
* Returns: None
*
* Global Data: 
*    unsigned int temperature - heatsing temperature 
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void SaveDCBtemperature(void)
{
  temperature = AD1R;
}


/*****************************************************************************
*
* Function: static void SaveBEMFA(void)
*
* Description: Saves ADC result to the respective variable              
*     
* Returns: None
*
* Global Data: 
*    unsigned int phA_voltage - Back-EMF voltage of Phase A 
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void SaveBEMFA(void)
{
  phA_voltage = AD1R;
}


/*****************************************************************************
*
* Function: static void SaveBEMFB(void)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -