【国民技术M4内核热销款N32G45XVL评估】第四篇 三相BLDC电机”六步”控制波形生成
[复制链接]
本实验是使用TIM1产生三相BLDC电机控制控制波形,也就是所谓的六步换向控制波形。
N32G45的TIM1输出信号及影像关系如下表。在N32G45XVL-STB板上选择最后一组端口映射来输出PWM信号。
下面是实验代码:
端口配置
void GPIO_Conf(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_GPIOE, ENABLE);
AFIO->RMP_CFG |= 0xC0;
GPIO_InitStructure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
/* GPIOE Configuration: BKIN pin */
GPIO_InitStructure.Pin = GPIO_PIN_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
}
TIM1 配置
/* Time Base configuration */
TIM_TimeBaseStructure.Prescaler = 0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_TimeBaseStructure.Period = 4095;
TIM_TimeBaseStructure.ClkDiv = 0;
TIM_TimeBaseStructure.RepetCnt = 0;
TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.OcMode = TIM_OCMODE_TIMING;
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_ENABLE;
TIM_OCInitStructure.Pulse = 2047;
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;
TIM_OCInitStructure.OcNPolarity = TIM_OCN_POLARITY_HIGH;
TIM_OCInitStructure.OcIdleState = TIM_OC_IDLE_STATE_SET;
TIM_OCInitStructure.OcNIdleState = TIM_OCN_IDLE_STATE_SET;
TIM_InitOc1(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.Pulse = 1023;
TIM_InitOc2(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.Pulse = 511;
TIM_InitOc3(TIM1, &TIM_OCInitStructure);
/* Automatic Output enable, Break, dead time and lock configuration*/
TIM_BDTRInitStructure.OssrState = TIM_OSSR_STATE_ENABLE;
TIM_BDTRInitStructure.OssiState = TIM_OSSI_STATE_ENABLE;
TIM_BDTRInitStructure.LockLevel = TIM_LOCK_LEVEL_OFF;
TIM_BDTRInitStructure.DeadTime = 1;
TIM_BDTRInitStructure.Break = TIM_BREAK_IN_DISABLE; // TIM_BREAK_IN_ENABLE;
TIM_BDTRInitStructure.BreakPolarity = TIM_BREAK_POLARITY_HIGH;
TIM_BDTRInitStructure.AutomaticOutput = TIM_AUTO_OUTPUT_ENABLE;
TIM_ConfigBkdt(TIM1, &TIM_BDTRInitStructure);
TIM_EnableCapCmpPreloadControl(TIM1, ENABLE);
TIM_ConfigInt(TIM1, TIM_INT_COM, ENABLE);
/* TIM1 counter enable */
TIM_Enable(TIM1, ENABLE);
/* Main Output Enable */
TIM_EnableCtrlPwmOutputs(TIM1, ENABLE);
运行后结果:
CH1/2/3波形
CH1/2N/3N波形
波形满足要求,可以作为六步换向控制信号。
|