STM32的TIM1和TIM3只是输出普通PWM的话,使用上有区别吗?

2019-12-20 21:33发布

第二个函数只是在第一个函数上把TIM3改成TIM1和更改了一些变量的名称可以说是一样的,但第一个函数使用正常能够实现可变的占空比的PWM输出,但第二个函数就不能工作的,这个是什么原因呢?

void SetBackLight(uint8_t _bright)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);

        if (_bright == 0)
        {
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOB, &GPIO_InitStructure);
                return;
        }
        else if (_bright == BRIGHT_MAX)
        {
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOB, &GPIO_InitStructure);
                GPIO_SetBits(GPIOB, GPIO_Pin_1);
                return;
        }

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

        TIM_TimeBaseStructure.TIM_Period = 3000;       
        TIM_TimeBaseStructure.TIM_Prescaler = 0;
        TIM_TimeBaseStructure.TIM_ClockDivision = 0;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = (TIM_TimeBaseStructure.TIM_Period * _bright) / BRIGHT_MAX;

        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;       
        TIM_OC4Init(TIM3, &TIM_OCInitStructure);
        TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);       
        TIM_ARRPreloadConfig(TIM3, ENABLE);
        TIM_Cmd(TIM3, ENABLE);
}


void SetMotor(uint8_t Value)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

        if (Value == 0)
        {
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOB, &GPIO_InitStructure);
                return;
        }
        else if (Value == MOTOR_MAX)
        {
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOA, &GPIO_InitStructure);

                GPIO_SetBits(GPIOA, GPIO_Pin_1);
                return;
        }

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

        TIM_TimeBaseStructure.TIM_Period = 3000;       
        TIM_TimeBaseStructure.TIM_Prescaler = 0;
        TIM_TimeBaseStructure.TIM_ClockDivision = 0;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

        TIM_OCInitStructure.TIM_Pulse = (TIM_TimeBaseStructure.TIM_Period * Value) / MOTOR_MAX;       

        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
       
        TIM_OC1Init(TIM1, &TIM_OCInitStructure);
        TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
       
        TIM_ARRPreloadConfig(TIM1, ENABLE);

        TIM_Cmd(TIM1, ENABLE);
        TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
13条回答
changlyou77
1楼-- · 2019-12-21 00:59
void SetMotor(uint8_t Value)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

if (Value == 0)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
return;
}
else if (Value == MOTOR_MAX)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_SetBits(GPIOA, GPIO_Pin_1);
return;
}

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

TIM_TimeBaseStructure.TIM_Period = 3000;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = (TIM_TimeBaseStructure.TIM_Period * Value) / MOTOR_MAX;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

TIM_ARRPreloadConfig(TIM1, ENABLE);

TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}


哥们看看我给你用双斜线屏蔽掉的语句,下一行是我给你改的,你试试吧
changlyou77
2楼-- · 2019-12-21 01:32
哥们看看我给你用双斜线屏蔽掉的语句,就是后面有一行“*”号的那行,下一行是我给你改的,你试试吧

void SetMotor(uint8_t Value)  
{  
GPIO_InitTypeDef GPIO_InitStructure;  
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
TIM_OCInitTypeDef  TIM_OCInitStructure;  

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);  

if (Value == 0)  
{  
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
GPIO_Init(GPIOB, &GPIO_InitStructure);  
return;  
}  
else if (Value == MOTOR_MAX)  
{  
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
GPIO_Init(GPIOA, &GPIO_InitStructure);  

GPIO_SetBits(GPIOA, GPIO_Pin_1);  
return;  
}  

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
GPIO_Init(GPIOA, &GPIO_InitStructure);  

//RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);  *******************************************
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

TIM_TimeBaseStructure.TIM_Period = 3000;  
TIM_TimeBaseStructure.TIM_Prescaler = 0;  
TIM_TimeBaseStructure.TIM_ClockDivision = 0;  
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);  

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  

TIM_OCInitStructure.TIM_Pulse = (TIM_TimeBaseStructure.TIM_Period * Value) / MOTOR_MAX;  

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  

TIM_OC1Init(TIM1, &TIM_OCInitStructure);  
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  

TIM_ARRPreloadConfig(TIM1, ENABLE);  

TIM_Cmd(TIM1, ENABLE);  
TIM_CtrlPWMOutputs(TIM1, ENABLE);  
}
cemi
3楼-- · 2019-12-21 06:00
你说的那个地方我的确写错了, 不过TIM1的时钟我在RCC初始化已经打开了,所以那一句没有影响。后来我改过来后或者把那一句注释掉也一样不正常。但是我把TIM1改成TIM4,同时把TIM_CtrlPWMOutputs(TIM1, ENABLE); 删除后,也能正常的生成可调的占空比的PWM。也就是说同样的代码TIM3,TIM4都没问题,改成TIM1就不行了,究竟TIM1使用上有什么区别呢?
changlyou77
4楼-- · 2019-12-21 06:13
这个是我调试完全好使的程序,至少我自己用是好使,你用这个试试吧,这个函数是初始化TIM1或TIM8用的,带形参的你看一下就知道怎么用了
void TIM_1_8_Configuration(TIM_TypeDef* TIMx,u16 Prescaler,u16 Period,u16 Ch1_Pulse,u16 Ch2_Pulse)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  /* Time Base configuration */
  
  TIM_TimeBaseStructure.TIM_Period = Period;
  TIM_TimeBaseStructure.TIM_Prescaler = Prescaler;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);

  /* Channel 1and 2  Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = Ch1_Pulse;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OC1Init(TIMx, &TIM_OCInitStructure);
  TIM_OC1PreloadConfig(TIMx, TIM_OCPreload_Enable);

  TIM_OCInitStructure.TIM_Pulse = Ch2_Pulse;
  TIM_OC2Init(TIMx, &TIM_OCInitStructure);
  TIM_OC2PreloadConfig(TIMx, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIMx, ENABLE);
  /* TIM1 counter enable */
  TIM_Cmd(TIMx, ENABLE);
  /* TIM1 Main Output Enable */
  TIM_CtrlPWMOutputs(TIMx, ENABLE);
}

其次在检查一下你的GPIO初始化等等
Clinging
5楼-- · 2019-12-21 07:06
 精彩回答 2  元偷偷看……
daicp
6楼-- · 2019-12-21 08:39
同问

TIM_CtrlPWMOutputs(TIM4, DISABLE);并不能关闭对应的PWM

TIM_CtrlPWMOutputs(TIM5, DISABLE); 并不能关闭对应的PWM



TIM_CtrlPWMOutputs(TIM1, DISABLE); 正常关闭

一周热门 更多>