使用stm32f103的pwm控制m3508电机的代码
时间: 2023-10-06 18:11:36 浏览: 197
由于m3508电机是四个电机,每个电机需要两个PWM信号来进行控制,因此需要八个PWM输出口。以下是使用STM32F103的PWM控制M3508电机的代码:
首先需要初始化PWM输出口:
```c
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
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_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 19999;
TIM_TimeBaseStructure.TIM_Prescaler = 719;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1000;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStructure.TIM_DeadTime = 50;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
TIM_Cmd(TIM1, ENABLE);
```
然后可以通过以下函数来控制电机的转速和方向:
```c
void m3508_set_speed(uint16_t speed1, uint16_t speed2, uint16_t speed3, uint16_t speed4)
{
if (speed1 > 2000) speed1 = 2000;
if (speed2 > 2000) speed2 = 2000;
if (speed3 > 2000) speed3 = 2000;
if (speed4 > 2000) speed4 = 2000;
TIM_SetCompare1(TIM1, speed1);
TIM_SetCompare2(TIM1, speed2);
TIM_SetCompare3(TIM1, speed3);
TIM_SetCompare4(TIM1, speed4);
}
void m3508_set_direction(uint8_t dir1, uint8_t dir2, uint8_t dir3, uint8_t dir4)
{
if (dir1 == 0) GPIO_ResetBits(GPIOA, GPIO_Pin_8);
else GPIO_SetBits(GPIOA, GPIO_Pin_8);
if (dir2 == 0) GPIO_ResetBits(GPIOA, GPIO_Pin_9);
else GPIO_SetBits(GPIOA, GPIO_Pin_9);
if (dir3 == 0) GPIO_ResetBits(GPIOA, GPIO_Pin_10);
else GPIO_SetBits(GPIOA, GPIO_Pin_10);
if (dir4 == 0) GPIO_ResetBits(GPIOA, GPIO_Pin_11);
else GPIO_SetBits(GPIOA, GPIO_Pin_11);
if (dir1 == 0) GPIO_SetBits(GPIOA, GPIO_Pin_12);
else GPIO_ResetBits(GPIOA, GPIO_Pin_12);
if (dir2 == 0) GPIO_SetBits(GPIOA, GPIO_Pin_13);
else GPIO_ResetBits(GPIOA, GPIO_Pin_13);
if (dir3 == 0) GPIO_SetBits(GPIOA, GPIO_Pin_14);
else GPIO_ResetBits(GPIOA, GPIO_Pin_14);
if (dir4 == 0) GPIO_SetBits(GPIOA, GPIO_Pin_15);
else GPIO_ResetBits(GPIOA, GPIO_Pin_15);
}
```
其中,`m3508_set_speed`函数用于设置电机的转速,取值范围为0-2000,而`m3508_set_direction`函数用于设置电机的方向,其中dir1、dir2、dir3、dir4为0或1,表示电机的转向。
可以在主函数中调用以上两个函数来控制电机。例如:
```c
m3508_set_speed(1500, 1500, 1500, 1500);
m3508_set_direction(1, 1, 0, 0);
```
以上代码表示设置电机1和2的转速为1500,电机3和4的转速为0,电机1和2的方向为正转,电机3和4的方向为反转。
阅读全文