TIM3和TIM4的通道都是低电平,电机无法转动,但如果不设pwm,用GPIO_SetBits设置高电平是可以转的。
代码如下:
#include "stm32f10x.h"
#include "Motor.h"
#define Moto_PwmMax 999
int16_t MOTO1_PWM = 0;
int16_t MOTO2_PWM = 0;
int16_t MOTO3_PWM = 0;
int16_t MOTO4_PWM = 0;
void Moto_PwmRflash(int16_t MOTO1_PWM,int16_t MOTO2_PWM,int16_t MOTO3_PWM,int16_t MOTO4_PWM)
{
if(MOTO1_PWM>Moto_PwmMax) MOTO1_PWM = Moto_PwmMax;
if(MOTO2_PWM>Moto_PwmMax) MOTO2_PWM = Moto_PwmMax;
if(MOTO3_PWM>Moto_PwmMax) MOTO3_PWM = Moto_PwmMax;
if(MOTO4_PWM>Moto_PwmMax) MOTO4_PWM = Moto_PwmMax;
if(MOTO1_PWM<0) MOTO1_PWM = 0;
if(MOTO2_PWM<0) MOTO2_PWM = 0;
if(MOTO3_PWM<0) MOTO3_PWM = 0;
if(MOTO4_PWM<0) MOTO4_PWM = 0;
TIM4->CCR3 = MOTO4_PWM;//motor4 L U
TIM4->CCR4 = MOTO3_PWM;//motor1 R U
TIM3->CCR3 = MOTO2_PWM;//motor2 R D
TIM3->CCR4 = MOTO1_PWM;//motor3 L D
}
void Time3_Mode_Config()
{
uint16_t CCR_Val=800;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStruct.TIM_Period=999;
TIM_TimeBaseStruct.TIM_Prescaler=(uint16_t) (SystemCoreClock / 24000000) - 1;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStruct.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OCMode=TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse=CCR_Val;
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC3Init(TIM3, &TIM_OCInitStruct);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC4Init(TIM3, &TIM_OCInitStruct);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
void Time4_Mode_Config()
{
uint16_t CCR_Val=800;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseStruct.TIM_Period=999;
TIM_TimeBaseStruct.TIM_Prescaler=(uint16_t) (SystemCoreClock / 24000000) - 1;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStruct.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStruct);
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OCMode=TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse=CCR_Val;
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC3Init(TIM4, &TIM_OCInitStruct);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4Init(TIM4, &TIM_OCInitStruct);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4,ENABLE);
TIM_Cmd(TIM4,ENABLE);
}
void Motor_GPIO_Config()
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP ;
GPIO_Init(GPIOB,&GPIO_InitStruct);
}
void Motor_Init(void)
{
Motor_GPIO_Config();
Time3_Mode_Config();
Time4_Mode_Config();
}
main函数如下:
int main()
{
Motor_Init();
while(1)
Moto_PwmRflash(800,800,800,800);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
手误
一周热门 更多>