主控Stm32f103C8T6
电机驱动DRV8833
电机:就是普通平衡小车编码电机
DRV8833具有双通道,AIN,BIN各作为两个电机的控制输入端。
H—逻辑桥驱动:
Xin1 | Xin2 | Xo1 | Xo2 | 功能 |
---|---|---|---|---|
0 | 0 | Z | Z | 慢速运行\快速衰减 |
0 | 1 | L | H | 反转 |
1 | 0 | H | L | 正转 |
1 | 1 | L | L | 制动/慢速衰减 |
PWM控制电机速度:
Xin1 | Xin2 | 功能 |
---|---|---|
PWM | 0 | 正转PWM,快速衰减 |
1 | PWM | 正转PWM,慢速衰减 |
0 | PWM | 反转PWM,快速衰减 |
PWM | 1 | 反转PWM,慢速衰减 |
要注意的是,一般电机的调速频率为:5-20MHz,而且在控制电机的时候我们一般用两路PWM输出,
目的:实现电机的正转,根据上面电机驱动图:AIN1输出PWM,AIN2输出0,使电机进行正转,快速衰减。
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM3,ENABLE);
TIM_CtrlPWMOutputs(TIM2,ENABLE);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM2, ENABLE);
TIM1_PWM_Init(8999,0);
TIM_SetCompare4(TIM2,0);
TIM_SetCompare2(TIM3,5999); //AIN1