?? stm32f10x_timebase.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 + -