由于之前是使用PB9、PB7引脚即TIM4_ch3\TIM4_ch4,由于项目更改为c8t6的PB、PB7引脚(TIM4_ch3\TIM4_ch4) 改为配置后发现只有一边的轮子可以转到,明明配置没什么问题,编译也没有报错,最后将pwm的调制模式更改才可以, 一个改为TIM_OCMode_PWM2、另一个改TIM_OCMode_PWM1,发现电机转动了。……………^_^大无语事件,知道的大神可以告诉我原因。
void TB6612_PWM_Init(u32 arr, int psc)
{
TIM_OCInitTypeDef TIM_OCInitSructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//配置pwm输出端口
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//初始化定时器
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseStructure.TIM_Period = arr-1; //自动重新装载寄存器周期的值澹ㄥ计数值澹)
TIM_TimeBaseStructure.TIM_Prescaler = psc-1; //时钟分频系数
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //对外部时钟进行采样的时钟分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure); //参数初始化
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
//设置通道2 pwm参数
TIM_OCInitSructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitSructure.TIM_OutputState= TIM_OutputState_Enable;
TIM_OCInitSructure.TIM_Pulse = 0; //占空比=
TIM_OCInitSructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC1Init(TIM4, &TIM_OCInitSructure); //参数初始化
TIM_OC1PolarityConfig(TIM4, TIM_OCPreload_Enable); //开始输出pwm
//设置通道4 pwm参数
TIM_OCInitSructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitSructure.TIM_OutputState= TIM_OutputState_Enable;
TIM_OCInitSructure.TIM_Pulse = 0; //占空比=
TIM_OCInitSructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC2Init(TIM4, &TIM_OCInitSructure); //参数初始化
TIM_OC2PolarityConfig(TIM4, TIM_OCPreload_Enable); //开始输出pwm
TIM_ARRPreloadConfig(TIM4, ENABLE); //启动自动重装
TIM_Cmd(TIM4, ENABLE); //启动定时
}
附一下电机接口说明图: