本帖最后由 toneon 于 2022-10-27 10:21 编辑
代码如下:#include "motor_pwm_output.h"
/*!
* [url=home.php?mod=space&uid=247401]@brief[/url] PWMx Fault Test CallBack Function. When hardware over-current fault occurs, PWM fault interrupt is triggered and this function is called.
*
* @param[in] device: device
* @param[in] wparam: callback parameter
* @param[in] lpara: callback parameter
* [url=home.php?mod=space&uid=266161]@return[/url] none
*/
void FOC_PWM_FaultCallback(void *device, uint32_t wpara, uint32_t lpara)
{
if (PWM_GetOverflowFlag(PWM1))
{
PWM_ClearOverflowFlag(PWM1);
}
if (PWM_GetFaultChannelFlag(PWM1, (uint16_t)PWM_FAULT_CH_1))
{
PWM_ClearFaultChannelFlag(PWM1, (uint16_t)PWM_FAULT_CH_1);
}
}
/*!
* [url=home.php?mod=space&uid=247401]@brief[/url] FOC PWM Module Initialize.
*
* @param[in] none
* [url=home.php?mod=space&uid=266161]@return[/url] none
*/
void FOC_PWM_Initialize(void)
{
// PWM_DeInit(PWM1);
PWM_CombineChConfig combineChConfig[4];
PWM_ModulationConfigType pwmConfig;
PWM_ConfigType config;
PWM_FaultConfig faultConfig;
PWM_SyncConfigType syncConfig;
/*
* GPIO Configuration for PWM1
* PB5 --> PWM1_IN0
* PB4 --> PWM1_IN1
* PB8 --> PWM1_IN2
* PB7 --> PWM1_IN3
* PB10 --> PWM1_IN4
* PB9 --> PWM1_IN5
* PB6 --> PWM1_IN6
*/
/* PWM output channel 0 */
GPIO_SetFunc(GPIOB, GPIO_PIN5, GPIO_FUN1);
/* PWM output channel 1 */
GPIO_SetFunc(GPIOB, GPIO_PIN4, GPIO_FUN1);
/* PWM output channel 2 */
GPIO_SetFunc(GPIOB, GPIO_PIN8, GPIO_FUN1);
/* PWM output channel 3 */
GPIO_SetFunc(GPIOB, GPIO_PIN7, GPIO_FUN1);
/* PWM output channel 4 */
GPIO_SetFunc(GPIOB, GPIO_PIN10, GPIO_FUN1);
/* PWM output channel 5 */
GPIO_SetFunc(GPIOB, GPIO_PIN9, GPIO_FUN1);
/* PWM output channel 6,Trigger I_bus sample */
GPIO_SetFunc(GPIOB, GPIO_PIN6, GPIO_FUN1);
/* PWM initial output
PWM_SetChannelOutputInitLevel(PWM1, PWM_CH_0, PWM_LOW_LEVEL);
PWM_SetChannelOutputInitLevel(PWM1, PWM_CH_1, PWM_LOW_LEVEL);
PWM_SetChannelOutputInitLevel(PWM1, PWM_CH_2, PWM_LOW_LEVEL);
PWM_SetChannelOutputInitLevel(PWM1, PWM_CH_3, PWM_LOW_LEVEL);
PWM_SetChannelOutputInitLevel(PWM1, PWM_CH_4, PWM_LOW_LEVEL);
PWM_SetChannelOutputInitLevel(PWM1, PWM_CH_5, PWM_LOW_LEVEL);
*/
// PWM_SetInitChannelOutput(PWM1, ENABLE);
/* channel 0/1 */
combineChConfig[0].pairChannel = PWM_CH_0;
combineChConfig[0].ch1stValue = PWM_PERIOD_VALUE >> 2;
combineChConfig[0].ch2ndValue = PWM_PERIOD_VALUE >> 1;
combineChConfig[0].levelMode = PWM_HIGH_TRUE;
combineChConfig[0].complementEn = ENABLE;
combineChConfig[0].deadtimeEn = ENABLE;
combineChConfig[0].ch1stMatchDir = PWM_MATCH_DIR_UP;
combineChConfig[0].ch2ndMatchDir = PWM_MATCH_DIR_DOWN;
//combineChConfig[0].ch1stPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
//combineChConfig[0].ch2ndPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
/* channel 2/3 */
combineChConfig[1].pairChannel = PWM_CH_2;
combineChConfig[1].ch1stValue = PWM_PERIOD_VALUE >> 3;
combineChConfig[1].ch2ndValue = PWM_PERIOD_VALUE >> 1;
combineChConfig[1].levelMode = PWM_HIGH_TRUE;
combineChConfig[1].complementEn = ENABLE;
combineChConfig[1].deadtimeEn = ENABLE;
combineChConfig[1].ch1stMatchDir = PWM_MATCH_DIR_UP;
combineChConfig[1].ch2ndMatchDir = PWM_MATCH_DIR_DOWN;
//combineChConfig[1].ch1stPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
//combineChConfig[1].ch2ndPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
/* channel 4/5 */
combineChConfig[2].pairChannel = PWM_CH_4;
combineChConfig[2].ch1stValue = PWM_PERIOD_VALUE >> 4;
combineChConfig[2].ch2ndValue = PWM_PERIOD_VALUE >> 1;
combineChConfig[2].levelMode = PWM_HIGH_TRUE;
combineChConfig[2].complementEn = ENABLE;
combineChConfig[2].deadtimeEn = ENABLE;
combineChConfig[2].ch1stMatchDir = PWM_MATCH_DIR_UP;
combineChConfig[2].ch2ndMatchDir = PWM_MATCH_DIR_DOWN;
//combineChConfig[2].ch1stPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
//combineChConfig[2].ch2ndPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
/* channel 6/7 */
combineChConfig[3].pairChannel = PWM_CH_6;
/* Down edge sample */
combineChConfig[3].ch1stValue = PWM_PERIOD_VALUE >> 1;
combineChConfig[3].ch2ndValue = (PWM_PERIOD_VALUE >> 4) + (PWM_PERIOD_VALUE >> 1);
combineChConfig[3].levelMode = PWM_HIGH_TRUE;
combineChConfig[3].deadtimeEn = DISABLE;
combineChConfig[3].complementEn = DISABLE;
combineChConfig[3].ch1stMatchDir = PWM_MATCH_DIR_UP;
combineChConfig[3].ch2ndMatchDir = PWM_MATCH_DIR_UP;
combineChConfig[3].ch1stTriggerEn = ENABLE;
combineChConfig[3].ch2ndTriggerEn = ENABLE;
/* modulation mode config */
pwmConfig.initTriggerEn = DISABLE;
pwmConfig.countMode = PWM_UP_DOWN_COUNT;
pwmConfig.combineChannelNum = 4;
pwmConfig.combineChConfig = combineChConfig;
pwmConfig.deadtime = PWM_DEAD_TIME;
pwmConfig.deadtimePsc = PWM_DEADTIME_DIVID_4;
pwmConfig.combineChConfig->ch1stPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
pwmConfig.combineChConfig->ch2ndPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
/* pwm config */
config.mode = PWM_MODE_MODULATION;
config.initModeStruct = &pwmConfig;
config.clkSource = PWM_CLK_SOURCE_NONE; //why not APB
config.clkPsc = PWM_PRSC;
// config.initValue = 0;
config.maxValue = PWM_PERIOD_VALUE;
config.interruptEn = ENABLE;
config.callBack = FOC_PWM_FaultCallback;
PWM_Init(PWM1, &config);
/* pwm sync config */
syncConfig.syncTriggerMode = PWM_SYNC_TRIGGER_SOFTWARE;
syncConfig.hardwareSync0En = DISABLE;
syncConfig.hardwareSync1En = DISABLE;
syncConfig.hardwareSync2En = DISABLE;
syncConfig.autoClearHWTriggerEn = DISABLE;
syncConfig.counterInitSyncEn = ENABLE;
syncConfig.outCtrlSyncEn = DISABLE;
syncConfig.inverterSyncEn = DISABLE;
syncConfig.outmaskSyncEn = DISABLE;
syncConfig.polaritySyncEn = DISABLE;
syncConfig.chValueSyncEn[0] = ENABLE;
syncConfig.chValueSyncEn[1] = ENABLE;
syncConfig.chValueSyncEn[2] = ENABLE;
syncConfig.chValueSyncEn[3] = ENABLE;
syncConfig.maxLoadingPointEn = DISABLE;
syncConfig.minLoadingPointEn = ENABLE;
syncConfig.syncEn = ENABLE;
PWM_InitSyncControl(PWM1, &syncConfig);
/* Set interrupt priority */
NVIC_SetPriority(PWM1_IRQn, 0);
/* PWM clock source */
PWM_SetClockSource(PWM1, PWM_CLK_SOURCE_APB);
}
上述代码根据小圆板官方资料修改,生成的三相PWM上桥臂波形如下,是边沿对齐,我已配置了向上向下计数模式,请教还需要如何配置才能输出三相PWM为中心对齐模式?
|