在F4驱动TB6612时出现PWM无法控制电机速度的问题,就是改变占空比转速不变,但是还是转的
代码如下:
void IO_Config(){
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE);
// 右后
// PG13,14
// 正转
// PG13 0
// PG14 1
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_SetBits(GPIOG,GPIO_Pin_13 | GPIO_Pin_14);
GPIO_ResetBits(GPIOG,GPIO_Pin_13);
////////////////////////////////////////////////// 后左
//PE2,3
//正转
//PE2 0
//PE3 1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_2 | GPIO_Pin_3);
GPIO_ResetBits(GPIOE,GPIO_Pin_2);
////////////////////////////////////////////////// 前左
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PE6,5
GPIO_Init(GPIOE, &GPIO_InitStructure); //正转
//PE5 0
//PE6 1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_5 | GPIO_Pin_6);
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
/////////////////////////////////////////////////// 前右
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PG12,11
GPIO_Init(GPIOG, &GPIO_InitStructure); //正转
//PG11 0
//PG12 1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_SetBits(GPIOG,GPIO_Pin_12 | GPIO_Pin_11);
GPIO_ResetBits(GPIOG,GPIO_Pin_11);
}
static void TIMx_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource14,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource13,GPIO_AF_TIM1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
static void TIM_PWMOUTPUT_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 8400-1;
TIM_TimeBaseStructure.TIM_Prescaler = 840-1;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 5000-1;
// speed=7000-1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //使能通道4
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //使能通道2
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //使能通道1
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //使能通道3
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_Cmd(TIM1,ENABLE);
}
void TIMx_Configuration(void)
{
TIMx_GPIO_Config();
TIM_PWMOUTPUT_Config();
}
PWM与TB6612FNG驱动电机,你没配置完成?
https://miaowlabs.com/book/lite/A25.html