📄 main.c
字号:
/*****************************************************************************
*
* Function: void PeripheralInit(void)
*
* Description: Initialization of all peripherals (calls all initialization
* functions)
*
* Returns: None
*
* Global Data:None
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
void PeripheralInit(void)
{
SysInit();
TPM1Init();
TPM2Init();
IO_PortsInit();
ADC_Init();
/* default pointers set up before interrupts enable */
pAdcSecondSample = (unsigned char *) adcSecondSampleCD;
PWMCommutationFcn = PWMCommutationFcnCD;
PWMTransition = PWMTransitionVectorCD;
SaveSecondSample = SaveSecondSampleCD;
ZCdetection = ZCdetectionCD;
}
/*****************************************************************************
*
* Function: void PWM_Vector_0CD(void)
*
* Description: Generates the 6-channel complementary bipolar PWM output
* for 3-phase power stages. For voltage vector see function comment
* rotation direction: 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 PWM_Vector_0CD(void)
{
signed int temp;
/* A = +Voltage, B = -Voltage, C off */
TPM1C4SC = 0; // PWM C, TOP
TPM1C5SC = 0; // PWM C, BOT
TPM1C2SC = TPM1C0SC_ELS0B_MASK; // PWM B, TOP
TPM1C3SC = TPM1C0SC_ELS0A_MASK; // PWM B, BOT
TPM1C0SC = TPM1C0SC_ELS0A_MASK; // PWM A, TOP
TPM1C1SC = TPM1C0SC_ELS0B_MASK; // PWM A, BOT
temp = PWM_MODULO/2 - PWM_DEAD_TIME - duty_cycle;
TPM1C1V = temp;
TPM1C2V = temp;
TPM1C4V = temp;
if (temp <= 0)
{
TPM1C0V = 0;
TPM1C3V = 0;
TPM1C5V = 0;
}
else
{
temp = PWM_MODULO/2 + PWM_DEAD_TIME - duty_cycle;
TPM1C0V = temp;
TPM1C3V = temp;
TPM1C5V = temp;
}
}
/*****************************************************************************
*
* Function: void PWM_Vector_1CD(void)
*
* Description: Generates the 6-channel complementary bipolar PWM output
* for 3-phase power stages. For voltage vector see function comment
* rotation direction: 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 PWM_Vector_1CD(void)
{
signed int temp;
/* A off, B = -Voltage, C = +Voltage */
TPM1C0SC = 0; // PWM A, TOP
TPM1C1SC = 0; // PWM A, BOT
TPM1C4SC = TPM1C0SC_ELS0A_MASK; // PWM C, TOP
TPM1C5SC = TPM1C0SC_ELS0B_MASK; // PWM C, BOT
TPM1C2SC = TPM1C0SC_ELS0B_MASK; // PWM B, TOP
TPM1C3SC = TPM1C0SC_ELS0A_MASK; // PWM B, BOT
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: void PWM_Vector_2CD(void)
*
* Description: Generates the 6-channel complementary bipolar PWM output
* for 3-phase power stages. For voltage vector see function comment
* rotation direction: 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 PWM_Vector_2CD(void)
{
signed int temp;
/* A = -Voltage, B off, C = +Voltage */
TPM1C2SC = 0; // PWM B, TOP
TPM1C3SC = 0; // PWM B, BOT
TPM1C0SC = TPM1C0SC_ELS0B_MASK; // PWM A, TOP
TPM1C1SC = TPM1C0SC_ELS0A_MASK; // PWM A, BOT
TPM1C4SC = TPM1C0SC_ELS0A_MASK; // PWM C, TOP
TPM1C5SC = TPM1C0SC_ELS0B_MASK; // PWM C, BOT
temp = PWM_MODULO/2 - PWM_DEAD_TIME - duty_cycle;
TPM1C0V = temp;
TPM1C2V = temp;
TPM1C5V = temp;
if (temp <= 0)
{
TPM1C1V = 0;
TPM1C3V = 0;
TPM1C4V = 0;
}
else
{
temp = PWM_MODULO/2 + PWM_DEAD_TIME - duty_cycle;
TPM1C3V = temp;
TPM1C4V = temp;
TPM1C1V = temp;
}
}
/*****************************************************************************
*
* Function: void PWM_Vector_3CD(void)
*
* Description: Generates the 6-channel complementary bipolar PWM output
* for 3-phase power stages. For voltage vector see function comment
* rotation direction: 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 PWM_Vector_3CD(void)
{
signed int temp;
/* A = -Voltage, B = +Voltage, C off */
TPM1C4SC = 0; // PWM C, TOP
TPM1C5SC = 0; // PWM C, BOT
TPM1C2SC = TPM1C0SC_ELS0A_MASK; // PWM B, TOP
TPM1C3SC = TPM1C0SC_ELS0B_MASK; // PWM B, BOT
TPM1C0SC = TPM1C0SC_ELS0B_MASK; // PWM A, TOP
TPM1C1SC = TPM1C0SC_ELS0A_MASK; // PWM A, BOT
temp = PWM_MODULO/2 - PWM_DEAD_TIME - duty_cycle;
TPM1C0V = temp;
TPM1C3V = temp;
TPM1C5V = temp;
if (temp <= 0)
{
TPM1C1V = 0;
TPM1C2V = 0;
TPM1C4V = 0;
}
else
{
temp = PWM_MODULO/2 + PWM_DEAD_TIME - duty_cycle;
TPM1C1V = temp;
TPM1C2V = temp;
TPM1C4V = temp;
}
}
/*****************************************************************************
*
* Function: void PWM_Vector_4CD(void)
*
* Description: Generates the 6-channel complementary bipolar PWM output
* for 3-phase power stages. For voltage vector see function comment
* rotation direction: 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 PWM_Vector_4CD(void)
{
signed int temp;
/* A off, B = +Voltage, C = -Voltage */
TPM1C0SC = 0; // PWM A, TOP
TPM1C1SC = 0; // PWM A, BOT
TPM1C4SC = TPM1C0SC_ELS0B_MASK; // PWM C, TOP
TPM1C5SC = TPM1C0SC_ELS0A_MASK; // PWM C, BOT
TPM1C2SC = TPM1C0SC_ELS0A_MASK; // PWM B, TOP
TPM1C3SC = TPM1C0SC_ELS0B_MASK; // PWM B, BOT
temp = PWM_MODULO/2 - PWM_DEAD_TIME - duty_cycle;
TPM1C0V = temp;
TPM1C3V = temp;
TPM1C4V = temp;
if (temp <= 0)
{
TPM1C1V = 0;
TPM1C2V = 0;
TPM1C5V = 0;
}
else
{
temp = PWM_MODULO/2 + PWM_DEAD_TIME - duty_cycle;
TPM1C1V = temp;
TPM1C2V = temp;
TPM1C5V = temp;
}
}
/*****************************************************************************
*
* Function: void PWM_Vector_5CD(void)
*
* Description: Generates the 6-channel complementary bipolar PWM output
* for 3-phase power stages. For voltage vector see function comment
* rotation direction: 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 PWM_Vector_5CD(void)
{
signed int temp;
/* A = +Voltage, B off, C = -Voltage */
TPM1C2SC = 0; // PWM B, TOP
TPM1C3SC = 0; // PWM B, BOT
TPM1C0SC = TPM1C0SC_ELS0A_MASK; // PWM A, TOP
TPM1C1SC = TPM1C0SC_ELS0B_MASK; // PWM A, BOT
TPM1C4SC = TPM1C0SC_ELS0B_MASK; // PWM C, TOP
TPM1C5SC = TPM1C0SC_ELS0A_MASK; // PWM C, BOT
temp = PWM_MODULO/2 - PWM_DEAD_TIME - duty_cycle;
TPM1C1V = temp;
TPM1C3V = temp;
TPM1C4V = temp;
if (temp <= 0)
{
TPM1C0V = 0;
TPM1C2V = 0;
TPM1C5V = 0;
}
else
{
temp = PWM_MODULO/2 + PWM_DEAD_TIME - duty_cycle;
TPM1C0V = temp;
TPM1C2V = temp;
TPM1C5V = temp;
}
}
/*****************************************************************************
*
* Function: void PWM_Vector_0CCD(void)
*
* Description: Generates the 6-channel complementary bipolar PWM output
* for 3-phase power stages. For voltage vector see function comment
* 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 PWM_Vector_0CCD(void)
{
signed int temp;
/* A = +Voltage, B = -Voltage, C off */
TPM1C4SC = 0; // PWM C, TOP
TPM1C5SC = 0; // PWM C, BOT
TPM1C2SC = TPM1C0SC_ELS0B_MASK; // PWM B, TOP
TPM1C3SC = TPM1C0SC_ELS0A_MASK; // PWM B, BOT
TPM1C0SC = TPM1C0SC_ELS0A_MASK; // PWM A, TOP
TPM1C1SC = TPM1C0SC_ELS0B_MASK; // PWM A, BOT
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -