📄 main.c
字号:
*
* Description: Saves ADC result to the respective variable
*
* Returns: None
*
* Global Data:
* unsigned int phB_voltage - Back-EMF voltage of Phase B
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void SaveBEMFB(void)
{
phB_voltage = AD1R;
}
/*****************************************************************************
*
* Function: static void SaveBEMFC(void)
*
* Description: Saves ADC result to the respective variable
*
* Returns: None
*
* Global Data:
* unsigned int phC_voltage - Back-EMF voltage of Phase C
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void SaveBEMFC(void)
{
phC_voltage = AD1R;
}
/*****************************************************************************
*
* Function: static void ZCdetectPhAfalling(void)
*
* Description: The function detects the zero crossing of phase A
* falling Back EMF voltage and sets the new commutation event
* using Timer 2
*
* Returns: None
*
* Global Data:
* unsigned int dcb_voltage_ZC - corrected DC bus voltage
* unsigned int dcb_voltage - DC bus voltage
* signed int dcb_offset - offset of DC bus voltage
* tProcessStatus processStatus - process status byte
* unsigned int dcb_voltage_HT - upper treshold for Back-EMF detection
* unsigned int dcb_voltage_LT - lower treshold for Back-EMF detection
* unsigned int phA_voltage - Back-EMF voltage of phase A
* unsigned int timeCommutation - time of last commutation
* unsigned int timeZCstep - time difference from last commutation
* when ZC detection is enabled
* unsigned int timeZC_PhA - time of current commutation
* unsigned int timeZC - time of current commutation
* unsigned int lastTimeZC - time of last commutation
* unsigned int actualPeriodZC - commutation period
* unsigned int periodZC_F_PhA - commutation period of phase A
* unsigned int periodNextCommutation - time difference for new commutation
* signed int torque_F_PhA - actual current/torque at ZC of phase A
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void ZCdetectPhAfalling(void)
{
dcb_voltage_ZC = (unsigned int)((signed int)(dcb_voltage >> 1) + dcb_offset);
if (processStatus.bit.EnableZC == 0)
{
if ((phA_voltage < dcb_voltage_HT) && (phA_voltage > dcb_voltage_LT) && ((TPM2CNT - timeCommutation) > timeZCstep))
{
processStatus.bit.EnableZC = 1;
processStatus.bit.AfterCMT = 0;
}
return;
}
if (phA_voltage < dcb_voltage_ZC)
{
timeZC_PhA = TPM2CNT;
lastTimeZC = timeZC;
timeZC = timeZC_PhA;
actualPeriodZC = periodZC_F_PhA = timeZC - lastTimeZC;
periodNextCommutation = umul_16x8(actualPeriodZC, advanceAngle);
TPM2C0V = timeZC + periodNextCommutation;
torque_F_PhA = dcb_current;
processStatus.bit.NewZC = 1;
}
}
/*****************************************************************************
*
* Function: static void ZCdetectPhAraising(void)
*
* Description: The function detects the zero crossing of phase A
* raising Back EMF voltage and sets the new commutation event
* using Timer 2
*
* Returns: None
*
* Global Data:
* unsigned int dcb_voltage_ZC - corrected DC bus voltage
* unsigned int dcb_voltage - DC bus voltage
* signed int dcb_offset - offset of DC bus voltage
* tProcessStatus processStatus - process status byte
* unsigned int dcb_voltage_HT - upper treshold for Back-EMF detection
* unsigned int dcb_voltage_LT - lower treshold for Back-EMF detection
* unsigned int phA_voltage - Back-EMF voltage of phase A
* unsigned int timeCommutation - time of last commutation
* unsigned int timeZCstep - time difference from last commutation
* when ZC detection is enabled
* unsigned int timeZC_PhA - time of current commutation
* unsigned int timeZC - time of current commutation
* unsigned int lastTimeZC - time of last commutation
* unsigned int actualPeriodZC - commutation period
* unsigned int periodZC_R_PhA - commutation period of phase A
* unsigned int periodNextCommutation - time difference for new commutation
* signed int torque_R_PhA - actual current/torque at ZC of phase A
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void ZCdetectPhAraising(void)
{
dcb_voltage_ZC = (unsigned int)((signed int)(dcb_voltage >> 1) + dcb_offset);
if (processStatus.bit.EnableZC == 0)
{
if ((phA_voltage < dcb_voltage_HT) && (phA_voltage > dcb_voltage_LT) && ((TPM2CNT - timeCommutation) > timeZCstep))
{
processStatus.bit.EnableZC = 1;
processStatus.bit.AfterCMT = 0;
}
return;
}
if (phA_voltage > dcb_voltage_ZC)
{
timeZC_PhA = TPM2CNT;
lastTimeZC = timeZC;
timeZC = timeZC_PhA;
actualPeriodZC = periodZC_R_PhA = timeZC - lastTimeZC;
periodNextCommutation = umul_16x8(actualPeriodZC, advanceAngle);
TPM2C0V = timeZC + periodNextCommutation;
torque_R_PhA = dcb_current;
processStatus.bit.NewZC = 1;
}
}
/*****************************************************************************
*
* Function: static void ZCdetectPhBfalling(void)
*
* Description: The function detects the zero crossing of phase B
* falling Back EMF voltage and sets the new commutation event
* using Timer 2
*
* Returns: None
*
* Global Data:
* unsigned int dcb_voltage_ZC - corrected DC bus voltage
* unsigned int dcb_voltage - DC bus voltage
* signed int dcb_offset - offset of DC bus voltage
* tProcessStatus processStatus - process status byte
* unsigned int dcb_voltage_HT - upper treshold for Back-EMF detection
* unsigned int dcb_voltage_LT - lower treshold for Back-EMF detection
* unsigned int phB_voltage - Back-EMF voltage of phase B
* unsigned int timeCommutation - time of last commutation
* unsigned int timeZCstep - time difference from last commutation
* when ZC detection is enabled
* unsigned int timeZC_PhB - time of current commutation
* unsigned int timeZC - time of current commutation
* unsigned int lastTimeZC - time of last commutation
* unsigned int actualPeriodZC - commutation period
* unsigned int periodZC_F_PhB - commutation period of phase B
* unsigned int periodNextCommutation - time difference for new commutation
* signed int torque_F_PhB - actual current/torque at ZC of phase B
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void ZCdetectPhBfalling(void)
{
dcb_voltage_ZC = (unsigned int)((signed int)(dcb_voltage >> 1) + dcb_offset);
if (processStatus.bit.EnableZC == 0)
{
if ((phB_voltage < dcb_voltage_HT) && (phB_voltage > dcb_voltage_LT) && ((TPM2CNT - timeCommutation) > timeZCstep))
{
processStatus.bit.EnableZC = 1;
processStatus.bit.AfterCMT = 0;
}
return;
}
if (phB_voltage < dcb_voltage_ZC)
{
timeZC_PhB = TPM2CNT;
lastTimeZC = timeZC;
timeZC = timeZC_PhB;
actualPeriodZC = periodZC_F_PhB = timeZC - lastTimeZC;
periodNextCommutation = umul_16x8(actualPeriodZC, advanceAngle);
TPM2C0V = timeZC + periodNextCommutation;
torque_F_PhB = dcb_current;
processStatus.bit.NewZC = 1;
}
}
/*****************************************************************************
*
* Function: static void ZCdetectPhBraising(void)
*
* Description: The function detects the zero crossing of phase B
* raising Back EMF voltage and sets the new commutation event
* using Timer 2
*
* Returns: None
*
* Global Data:
* unsigned int dcb_voltage_ZC - corrected DC bus voltage
* unsigned int dcb_voltage - DC bus voltage
* signed int dcb_offset - offset of DC bus voltage
* tProcessStatus processStatus - process status byte
* unsigned int dcb_voltage_HT - upper treshold for Back-EMF detection
* unsigned int dcb_voltage_LT - lower treshold for Back-EMF detection
* unsigned int phB_voltage - Back-EMF voltage of phase B
* unsigned int timeCommutation - time of last commutation
* unsigned int timeZCstep - time difference from last commutation
* when ZC detection is enabled
* unsigned int timeZC_PhB - time of current commutation
* unsigned int timeZC - time of current commutation
* unsigned int lastTimeZC - time of last commutation
* unsigned int actualPeriodZC - commutation period
* unsigned int periodZC_R_PhB - commutation period of phase B
* unsigned int periodNextCommutation - time difference for new commutation
* signed int torque_R_PhB - actual current/torque at ZC of phase B
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void ZCdetectPhBraising(void)
{
dcb_voltage_ZC = (unsigned int)((signed int)(dcb_voltage >> 1) + dcb_offset);
if (processStatus.bit.EnableZC == 0)
{
if ((phB_voltage < dcb_voltage_HT) && (phB_voltage > dcb_voltage_LT) && ((TPM2CNT - timeCommutation) > timeZCstep))
{
processStatus.bit.EnableZC = 1;
processStatus.bit.AfterCMT = 0;
}
return;
}
if (phB_voltage > dcb_voltage_ZC)
{
timeZC_PhB = TPM2CNT;
lastTimeZC = timeZC;
timeZC = timeZC_PhB;
actualPeriodZC = periodZC_R_PhB = timeZC - lastTimeZC;
periodNextCommutation = umul_16x8(actualPeriodZC, advanceAngle);
TPM2C0V = timeZC + periodNextCommutation;
torque_R_PhB = dcb_current;
processStatus.bit.NewZC = 1;
}
}
/*****************************************************************************
*
* Function: static void ZCdetectPhCfalling(void)
*
* Description: The function detects the zero crossing of phase C
* falling Back EMF voltage and sets the new commutation event
* using Timer 2
*
* Returns: None
*
* Global Data:
* unsigned int dcb_voltage_ZC - corrected DC bus voltage
* unsigned int dcb_voltage - DC bus voltage
* signed int dcb_offset - offset of DC bus voltage
* tProcessStatus processStatus - process status byte
* unsigned int dcb_voltage_HT - upper treshold for Back-EMF detection
* unsigned int dcb_voltage_LT - lower treshold for Back-EMF detection
* unsigned int phC_voltage - Back-EMF voltage of phase C
* unsigned int timeCommutation - time of last commutation
* unsigned int timeZCstep - time difference from last commutation
* when ZC detection is enabled
* unsigned int timeZC_PhC - time of current commutation
* unsigned int timeZC - time of current commutation
* unsigned int lastTimeZC - time of last commutation
* unsigned int actualPeriodZC - commutation period
* unsigned int periodZC_F_PhC - commutation period of phase C
* unsigned int periodNextCommutation - time difference for new commutation
* signed int torque_F_PhC - actual current/torque at ZC of phase C
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void ZCdetectPhCfalling(void)
{
dcb_voltage_ZC = (unsigned int)((signed int)(dcb_voltage >> 1) + dcb_offset);
if (processStatus.bit.EnableZC == 0)
{
if ((phC_voltage < dcb_voltage_HT) && (phC_voltage > dcb_voltage_LT) && ((TPM2CNT - timeCommutation) > timeZCstep))
{
processStatus.bit.EnableZC = 1;
processStatus.bit.AfterCMT = 0;
}
return;
}
if (phC_voltage < dcb_voltage_ZC)
{
timeZC_PhC = TPM2CNT;
lastTimeZC = timeZC;
timeZC = timeZC_PhC;
actualPeriodZC = periodZC_F_PhC = timeZC - lastTimeZC;
periodNextCommutation = umul_16x8(actualPeriodZC, advanceAngle);
TPM2C0V = timeZC + periodNextCommutation;
torque_F_PhC = dcb_current;
processStatus.bit.NewZC = 1;
}
}
/*****************************************************************************
*
* Function: static void ZCdetectPhCfalling(void)
*
* Description: The function detects the zero crossing of phase C
* raising Back EMF voltage and sets the new commutation event
* using Timer 2
*
* Returns: None
*
* Global Data:
* unsigned int dcb_voltage_ZC - corrected DC bus voltage
* unsigned int dcb_voltage - DC bus voltage
* signed int dcb_offset - offset of DC bus voltage
* tProcessStatus processStatus - process status byte
* unsigned int dcb_voltage_HT - upper treshold for Back-EMF detection
* unsigned int dcb_voltage_LT - lower treshold for Back-EMF detection
* unsigned int phC_voltage - Back-EMF voltage of phase C
* unsigned int timeCommutation - time of last commutation
* unsigned int timeZCstep - time difference from last commutation
* when ZC detection is enabled
* unsigned int timeZC_PhC - time of current commutation
* unsigned int timeZC - time of current commutation
* unsigned int lastTimeZC - time of last commutation
* unsigned int actualPeriodZC - commutation period
* unsigned int periodZC_R_PhC - commutation period of phase C
* unsigned int periodNextCommutation - time difference for new commutation
* signed int torque_R_PhC - actual current/torque at ZC of phase C
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
static void ZCdetectPhCraising(void)
{
dcb_voltage_ZC = (unsigned int)((signed int)(dcb_voltage >> 1) + dcb_offset);
if (processStatus.bit.EnableZC == 0)
{
if ((phC_voltage < dcb_voltage_HT) && (phC_voltage > dcb_voltage_LT) && ((TPM2CNT - timeCommutation) > timeZCstep))
{
processStatus.bit.EnableZC = 1;
processStatus.bit.AfterCMT = 0;
}
return;
}
if (phC_voltage > dcb_voltage_ZC)
{
timeZC_PhC = TPM2CNT;
lastTimeZC = timeZC;
timeZC = timeZC_PhC;
actualPeriodZC = periodZC_R_PhC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -