📄 bldc_zc_8013.c
字号:
ioctl(GPIO_B, GPIO_SETAS_GPIO, BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5);
ioctl(GPIO_B, GPIO_SETAS_INPUT, BIT_0 | BIT_1 | BIT_3);
ioctl(GPIO_B, GPIO_SETAS_OUTPUT, BIT_2 | BIT_4 | BIT_5);
/* PWM initialization accorging to values in the appconfig.h file*/
PWM_A_Init();
#ifdef INDEPENDENT_PWM_MODE
ioctl(PWM, PWM_SET_INDEPENDENT_MODE, PWM_CHANNEL_01 | PWM_CHANNEL_23 | PWM_CHANNEL_45);
#endif
#ifdef COMPLEMENTARY_PWM_MODE
ioctl(PWM, PWM_SET_COMPLEMENTARY_MODE, PWM_CHANNEL_01 | PWM_CHANNEL_23 | PWM_CHANNEL_45);
#endif
ioctl(PWM, PWM_CLEAR_FAULT_FLAG, PWM_FAULT_0);
ioctl(PWM, PWM_CLEAR_RELOAD_FLAG, NULL);
/* Initialization of QTimer 0 accordint to the values in the appconfig.h file */
/* This timer is used as commutation timer */
QTIMER_0_Init();
ioctl(QTIMER_0, QT_WRITE_COMPARE_REG1, PER_START_TIMEROC_CMT);
/* Initialization of QTimer 2 accordint to the values in the appconfig.h file */
/* This timer is used as speed and alignment current control timer */
QTIMER_2_Init();
/* Initialization of QTimer 3 accordint to the values in the appconfig.h file */
/* This timer is used for ADC to PWM syncronization */
QTIMER_3_Init();
/* This ensures that the input source of QuadTimer3 in PWM Module SYNC Output */
periphBitSet(SYS_CONTROL_TC3_INP, &SYS.control);
ioctl(ADC, ADC_SET_SCAN_MODE, ADC_SCAN_TRIG_SIMULTANEOUS);
ioctl(ADC, ADC_SYNC, ADC_ON);
/* enable all interrupts */
archEnableInt();
}
/*******************************************************************************
*
* Module : InitializeSetup()
*
* Description : The function loads parameter sets for desired power stage and motor.
*
* Returns : None
*
* Global Data : muwwIsrSpeedCurrentCall - Counter of IstSpeedCurrent call
*
* Arguments : None
*
* Range Issues : None
*
*******************************************************************************/
static void InitializeSetup(void)
{
/* Wait until power supply is stabilized, to avoid DC Undervoltage fault at start-up */
while (mwwIsrSpeedCurrentCall < 50);
/* Sets up parameters for used power stage */
#ifdef EVM33395_BOARD
EVM33395BoardSettings();
#endif
#ifdef MP_BOARD
MPBoardSettings();
#endif
/* Sets up parameters for used motor */
#ifdef MOTORA
MotorASettings();
#endif
#ifdef MOTORB
MotorBSettings();
#endif
}
/*******************************************************************************
*
* Module : ApplicationStateMachine()
*
* Description : The function provides the application state machine.
*
* If there's no fault condition (muwwDriveFaultStatus register),
* application toggles between Run and Stop state according switch
* position.
*
* In Stop State, the motor is stopped. If the Run / Stop switch is in
* RUN position, application switches to Run state, if there is no fault.
* If the Run / Stop switch is in STOP position waits in Stop state. During
* Stop state, Current and Speed PI controllers are stopped and actual and
* required speeds are cleared.
*
* In Run State, the motor is running according to speed command from the push
* buttons or FreeMaster. The ApplicationStateMachine() sets desired motor speed
* (mfwOmegaDesiredMech) according to required motor speed (mfwOmegaRequestedMech).
* It also controls start and stop of Current and Speed controllers according to
* status from BLDC motor commutation controller. The control is provided via
* mudtCmdApplication and mStatusCommutation register
*
* If there's any fault condition, aplication enters into Fault state and stops the
* motor with StopApplication.
*
* Returns : None
*
* Global Data : mudtCmdApplication - Application flags
* mApplicationMode - Application status
Run / Stop / Fault
* mStatusCommutation - Commutation status of drive
Stopped / Alignment / Starting / Running
* muwwDriveFaultStatus - Fault status of drive
* miwUDesired - PI controllers output to be used set PWM duty cycle
* application status
* mfwIDCBusDesiredAlign - Desired DC bus current during alignment
* mudtCurAlignParamsPI - PI parameters structure for current controller
* mudtSpeedParamsPI - PI parameters structure for speed controller
* mfwOmegaRequestedMech - Requested motor speed by the user
* mfwOmegaDesiredMech - Processed, desired motor speed, processed from
* mfwOmegaRequestedMech
* mfwOmegaActualMech - Actual motor speed
* mfwOmegaMinSysu - Minimum allowed motor speed (in fractional)
* mwwSpeedPrescaler - Prescaler for speed controller
* miwCurrentLimitingMarker - Marker to enable / disable current limiting active
* warning
*
* Arguments : None
*
* Range Issues : None
*
*******************************************************************************/
static void ApplicationStateMachine()
{
switch(mApplicationMode)
{
case STOP:
StopApplication();
/* Bldc motor Request = Stop */
mudtCmdApplication.uintBldcRunRqFlag = 0;
break;
case RUN:
if (mfwOmegaRequestedMech > 0) mudtCmdApplication.uintDirACBCmtRqFlag = 0;
else mudtCmdApplication.uintDirACBCmtRqFlag = 1;
if (abs(mfwOmegaRequestedMech) < mfwOmegaMinSysu)
{
StopApplication();
/* Bldc motor Request = Stop */
mudtCmdApplication.uintBldcRunRqFlag = 0;
}
else
{
mfwOmegaDesiredMech = abs(mfwOmegaRequestedMech);
/* if Direction Cmt Required differs from Direction Actual */
if (mudtCmdApplication.uintDirACBCmtRqFlag != mudtCmdApplication.uintDirACBCmtActualFlag)
{
/* Bldc motor Request = Stop */
mudtCmdApplication.uintBldcRunRqFlag = 0;
mfwOmegaRequiredMech = 0;
mudtSpeedParamsPI.IntegralPortionK_1 = 0;
}
else
{
/* Bldc motor Request = Run */
mudtCmdApplication.uintBldcRunRqFlag = 1;
}
if ((mStatusCommutation == RUNNING) && mudtCmdApplication.uintStartRunningCmtCmdFlag)
{
/* StartSpeedController */
mudtSpeedParamsPI.IntegralPortionK_1 = ((Frac32) miwUDesired << 16);
mwwSpeedPrescaler = mwwPerSpeedSys;
mudtCmdApplication.uintSpeedCtrlEnblFlag = 1;
mudtCmdApplication.uintStartRunningCmtCmdFlag = 0;
}
if (mudtCmdApplication.uintStartAlignmentCmtCmdFlag)
{
/* Initialize current PID controller */
miwUDesired = ALIGN_DESVOLTAGE_SYSU;
mfwIDCBusDesiredAlign = mfwCurrAlignDesired;
mudtCurAlignParamsPI.IntegralPortionK_1 = ((Frac32) miwUDesired << 16);
ioctl(PWM, PWM_UPDATE_VALUE_REG_0, (VOLTAGE_OFFSET_SYSU + miwUDesired));
ioctl(PWM, PWM_OUTPUT_PAD, PWM_ENABLE);
mudtCmdApplication.uintStartAlignmentCmtCmdFlag = 0;
}
if (miwCurrentLimitingMarker >= CL_MARKER_HIGH_LIMIT)
{
miwCurrentLimitingMarker = CL_MARKER_HIGH_LIMIT;
mudtCmdApplication.uintCurrentLimitingActiveFlag = 1;
}
if (miwCurrentLimitingMarker <= CL_MARKER_LOW_LIMIT)
{
miwCurrentLimitingMarker = CL_MARKER_LOW_LIMIT;
mudtCmdApplication.uintCurrentLimitingActiveFlag = 0;
};
break;
}
default: break;
case FAULT :
StopApplication();
mudtCmdApplication.uintBldcRunRqFlag = 0;
break;
}
if (mStatusCommutation != RUNNING)
{
/* Stop Speed Controller */
mudtCmdApplication.uintSpeedCtrlEnblFlag = 0;
}
if (mudtCmdApplication.uintEndRunningCmtCmdFlag)
{
mfwOmegaActualMech = 0;
mfwOmegaActualMechSum = 0;
mudtCmdApplication.uintEndRunningCmtCmdFlag = 0;
mudtCmdApplication.uintSpeedCtrlEnblFlag = 0;
}
}
/*******************************************************************************
*
* Module : IsrADCEndOfScan()
*
* Description : Interrupt function for ADC conversion complete callback
*
* The function provides:
* - Reading and filtering of DC-bus Voltage & Current samples
* - Reading of phase voltages for BEMF Zero Crossing
* - Setting of ADC Zero Crossing Offset
* - Processing ButtonPrescaler
*
* Returns : None
*
* Global Data : mfwUDCBusSample - DC Bus Voltage single sample, temporary variable
* mfwIDCBusSample - DC Bus Current single sample, temporary variable
* mfwUDCBusSum - DC Bus Voltage filtering buffer
* mfwUDCBusSum - DC Bus Current filtering buffer
* mfwUDCBus - Filtered DC Bus Voltage
* mfwIDCBus - Filtered DC Bus Current
* mudtUZC3Phase - 3 Phase Voltage samples
* mfwUZCPhaseX - Back EMF Voltage of the non-feeded phase
* mfwUDCBusHalf - Half of DC Bus Voltage for setting Zero Cross Threshold,
* temporary variable
*
* Arguments : None
*
* Range Issues : None
*
*******************************************************************************/
#pragma optimization_level 0
#pragma interrupt saveall
void IsrADCEndOfScan(void)
{
/* Read DC bus voltage and filter */
mfwUDCBusSample = ioctl(ADC, ADC_READ_SAMPLE, 0);
mfwUDCBusSum += mfwUDCBusSample;
mfwUDCBus = mfwUDCBusSum >> UDCBUS_FILT_INDEX;
mfwUDCBusSum -= mfwUDCBus;
/* Read DC bus current and filter */
mfwIDCBusSample = ioctl(ADC, ADC_READ_SAMPLE, 1);
mfwIDCBusSum += mfwIDCBusSample;
mfwIDCBus = mfwIDCBusSum >> IDCBUS_FILT_INDEX;
mfwIDCBusSum -= mfwIDCBus;
/* If ADC is enabled, read all phase voltages and save */
mudtUZC3Phase[0] = ioctl(ADC, ADC_READ_SAMPLE, 4);
mudtUZC3Phase[1] = ioctl(ADC, ADC_READ_SAMPLE, 5);
mudtUZC3Phase[2] = ioctl(ADC, ADC_READ_SAMPLE, 6);
/* Save actual phase for other processes */
mfwUZCPhaseX = mudtUZC3Phase[mudtBldcAlgoStates.State_ZCros.Index_ZC_Phase];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -