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

📄 stm32f10x_timebase.c

📁 ARM_CORTEX-M3应用实例开发详解光盘
💻 C
字号:
/******************************************************************************
* 文件名      : stm32f10x_Timebase.c
* 功能描述  : 该程序主要用于处理时基,包括显示和故障管理,速度调整等 
********************************************************************************

/* 头文件 ------------------------------------------------------------------*/
#include "stm32f10x.h"

#include "stm32f10x_MClib.h"
#include "MC_Globals.h"
#include "stm32f10x_it.h"


#define TB_Prescaler_5ms    31    // ((31+1)*(9374+1)/60000000) sec -> 5 ms 
#define TB_AutoReload_5ms    9374

#define TB_Prescaler_500us    29    // ((29+1)*(999+1)/60000000) sec -> 500 us 
#define TB_AutoReload_500us    999

#define SYSTICK_PRE_EMPTION_PRIORITY 3
#define SYSTICK_SUB_PRIORITY 0

#define SPEED_SAMPLING_TIME   PID_SPEED_SAMPLING_TIME


static u16 hStart_Up_TimeLeft_500us =0;
static volatile u16 hTimebase_500us = 0;
static volatile u16 hTimebase_display_500us = 0;
static volatile u16 hKey_debounce_500us = 0;
volatile u8 bPID_Speed_Sampling_Time_500us = PID_SPEED_SAMPLING_TIME;
static u16 hSpeedMeas_Timebase_500us = SPEED_SAMPLING_TIME;

#ifdef FLUX_TORQUE_PIDs_TUNING  
static u16 hTorqueSwapping = SQUARE_WAVE_PERIOD; 
#endif
/*******************************************************************************
* 函数名  : TB_Init
* 功能描述   : 时基外设初始化. 时基->500usec,使能相关中断   
* 输入       : 无
* 输出       : 无
* 返回       : 无
*******************************************************************************/
void TB_Init(void)
{   
   u32 SysTick_Prio;
  /* 选AHB时钟(HCLK)作为SysTick时钟源*/
  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  /* SysTick 第500usec中断一次 */
//  SysTick_SetReload(36000);
  /* Enable SysTick Counter */
//  SysTick_CounterCmd(SysTick_Counter_Enable);
  if (SysTick_Config(36000))
  { 
    /* Capture error */ 
    while (1);
  }

//  NVIC_SystemHandlerPriorityConfig(SysTick_IRQn, 
//                            SYSTICK_PRE_EMPTION_PRIORITY, SYSTICK_SUB_PRIORITY); 


    SysTick_Prio = NVIC_EncodePriority(	NVIC_GetPriorityGrouping(), SYSTICK_PRE_EMPTION_PRIORITY, SYSTICK_SUB_PRIORITY);

    NVIC_SetPriority(SysTick_IRQn, SysTick_Prio);


  /* Enable SysTick interrupt */
//  NVIC_EnableIRQ(SysTick_IRQn);
//  SysTick_ITConfig(ENABLE);


    /* Setup SysTick Timer for 1 msec interrupts  */


}

/*******************************************************************************
* 函数名  : TB_Wait
* 功能描述    : The function wait for a delay to be over.   
* 输入          : 无
* 输出        : 无
* 返回         : 无
*******************************************************************************/
void TB_Wait(u16 time)
{
hTimebase_500us = time;    /*延时*/
while (hTimebase_500us != 0) /*等待*/
{}  

}

/*******************************************************************************
* 函数名  : TB_Set_Delay_500us
* 功能描述    : 设置状态机延时  
* 输入        : 延时值
* 输出        : 无
* 返回        : 无
*******************************************************************************/
void TB_Set_Delay_500us(u16 hDelay)
{
  hTimebase_500us = hDelay;
}  

/*******************************************************************************
* 函数名  : TB_Delay_IsElapsed
* 功能描述    : 检查TB_Set_Delay_500us   
* 输入        : 无
* 输出        : 如果延时返回真,否则为假
* 返回        : 无
*******************************************************************************/
bool TB_Delay_IsElapsed(void)
{
 if (hTimebase_500us == 0)
 {
   return (TRUE);
 }
 else 
 {
   return (FALSE);
 }
}  

/*******************************************************************************
* 函数名  : TB_Set_DisplayDelay_500us
* 功能描述    : Set Delay utilized by MC_Display.c module.   
* 输入        : Time out value
* 输出        : 无
* 返回        : 无
*******************************************************************************/
void TB_Set_DisplayDelay_500us(u16 hDelay)
{
  hTimebase_display_500us = hDelay;
}  

/*******************************************************************************
* 函数名  : TB_DisplayDelay_IsElapsed
* 功能描述    : Check if the delay set by TB_Set_DisplayDelay_500us is elapsed.   
* 输入          : 无
* 输出        : True if delay is elapsed, false otherwise 
* 返回         : 无
*******************************************************************************/
bool TB_DisplayDelay_IsElapsed(void)
{
 if (hTimebase_display_500us == 0)
 {
   return (TRUE);
 }
 else 
 {
   return (FALSE);
 }
} 

/*******************************************************************************
* 函数名  : TB_Set_DebounceDelay_500us
* 功能描述    : Set Delay utilized by MC_Display.c module.   
* 输入          : Time out value
* 输出        : 无
* 返回         : 无
*******************************************************************************/
void TB_Set_DebounceDelay_500us(u8 hDelay)
{
  hKey_debounce_500us = hDelay;
}  

/*******************************************************************************
* 函数名  : TB_DebounceDelay_IsElapsed
* 功能描述    : Check if the delay set by TB_Set_DebounceDelay_500us is elapsed.   
* 输入          : 无
* 输出        : True if delay is elapsed, false otherwise 
* 返回         : 无
*******************************************************************************/
bool TB_DebounceDelay_IsElapsed(void)
{
 if (hKey_debounce_500us == 0)
 {
   return (TRUE);
 }
 else 
 {
   return (FALSE);
 }
} 

/*******************************************************************************
* 函数名  : TB_Set_StartUp_Timeout(STARTUP_TIMEOUT)
* 功能描述    : Set Start up time out and initialize Start_up torque in  
*                  torque control.   
* 输入          : Time out value
* 输出        : 无
* 返回         : 无
*******************************************************************************/
void TB_Set_StartUp_Timeout(u16 hTimeout)
{
  hStart_Up_TimeLeft_500us = 2*hTimeout;  
}  

/*******************************************************************************
* 函数名  : TB_StartUp_Timeout_IsElapsed
* 功能描述    : Set Start up time out.   
* 输入          : 无
* 输出        : True if start up time out is elapsed, false otherwise 
* 返回         : 无
*******************************************************************************/
bool TB_StartUp_Timeout_IsElapsed(void)
{
 if (hStart_Up_TimeLeft_500us == 0)
 {
   return (TRUE);
 }
 else 
 {
   return (FALSE);
 }
} 


/*******************************************************************************
* 函数名  : SysTickHandler
* 功能描述    :  SysTick处理即系统嘀答处理。
* 输入        : 无
* 输出        : 无 
* 返回        : 无
*******************************************************************************/
void SysTick_Handler(void)
{ 
  if (hTimebase_500us != 0)  
  {
    hTimebase_500us --;
  }
  
  if (hTimebase_display_500us != 0)  
  {
    hTimebase_display_500us --;
  }
  
  if (hKey_debounce_500us != 0)  
  {
    hKey_debounce_500us --;
  }

  if (hStart_Up_TimeLeft_500us != 0)
  {
    hStart_Up_TimeLeft_500us--;
  }
 
/*每个FLUX_TORQUE_PIDs_TUNING时间周期转矩参考值正
负反转一次,从而模拟一次阶跃响应过程 */

#ifdef FLUX_TORQUE_PIDs_TUNING  
  if (State == RUN) 
  {
    if (hTorqueSwapping!=0)
    {
      hTorqueSwapping--;
    }
    else
    {
      hTorqueSwapping = SQUARE_WAVE_PERIOD;
      hTorque_Reference = - hTorque_Reference;
    }  
  }
#endif
 
  if (hSpeedMeas_Timebase_500us !=0)  //平均速度检测时间周期到。
  {									  //这个周期只是检测使用ENC或者
    hSpeedMeas_Timebase_500us--;	  //STO时,使用HALL不在这里计算。
  }									  //而是在TIMER2计数器中断里计算SPEED.
  else
  {
    hSpeedMeas_Timebase_500us = SPEED_SAMPLING_TIME;
    
    #ifdef ENCODER
      //ENC_Calc_Average_Speed must be called ONLY every SPEED_MEAS_TIMEBASE ms
      ENC_Calc_Average_Speed();
      #ifdef OBSERVER_GAIN_TUNING
      STO_Calc_Speed();   
      STO_Obs_Gains_Update();
      #endif
    #elif (defined HALL_SENSORS && defined OBSERVER_GAIN_TUNING)
      STO_Calc_Speed();      
      STO_Obs_Gains_Update();
    #elif defined NO_SPEED_SENSORS
      STO_Calc_Speed();
      #ifdef OBSERVER_GAIN_TUNING 
      STO_Obs_Gains_Update();
      #endif
      if (State == RUN)
      {        
        if(STO_Check_Speed_Reliability()==FALSE)
        {
          MCL_SetFault(SPEED_FEEDBACK);	   //速度反馈故障,停机。
        }    
      }
      #ifdef VIEW_ENCODER_FEEDBACK
      //ENC_Calc_Average_Speed must be called ONLY every SPEED_MEAS_TIMEBASE ms
      ENC_Calc_Average_Speed();
      #endif
    #endif
      
    #ifdef DAC_FUNCTIONALITY
      #if (defined ENCODER || defined VIEW_ENCODER_FEEDBACK)
      MCDAC_Update_Value(SENS_SPEED,(s16)(ENC_Get_Mechanical_Speed()*250));
      #elif (defined HALL_SENSORS || defined VIEW_HALL_FEEDBACK)
      MCDAC_Update_Value(SENS_SPEED,(s16)(HALL_GetSpeed()*250));
      #endif
      #if (defined NO_SPEED_SENSORS || defined OBSERVER_GAIN_TUNING)
      MCDAC_Update_Value(LO_SPEED,(s16)(STO_Get_Speed()*250));
      #endif
    #endif      
  }

  
  if (bPID_Speed_Sampling_Time_500us != 0 )   // 对速度或扭距的修正周期到。
  {
    bPID_Speed_Sampling_Time_500us --;
  }
  else
  { 
    bPID_Speed_Sampling_Time_500us = PID_SPEED_SAMPLING_TIME;    // 每2ms一次    
    if ((wGlobal_Flags & SPEED_CONTROL) == SPEED_CONTROL) //速度控制
    {
      if (State == RUN) 
      {
#ifdef HALL_SENSORS
        if (HALL_GetSpeed() == HALL_MAX_SPEED)
        {
          MCL_SetFault(SPEED_FEEDBACK);
        }											  
#endif        
// 不同速度模式下,使用不同的PID参数。
//        PID_Speed_Coefficients_update(GET_SPEED_0_1HZ, &PID_Speed_InitStructure);
        FOC_CalcFluxTorqueRef();   //系统每PID_SPEED_SAMPLING_TIME时间修正一次速度或者扭矩值。     
      }
    }
    else  // 扭矩控制
    {
      if (State == RUN)
      {
        FOC_TorqueCtrl();	 //系统每PID_SPEED_SAMPLING_TIME时间修正一次速度或者扭矩值。
      }
    }
  }
}

/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

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