打印

stm32 tim1 4 PWM 关闭问题

[复制链接]
12662|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
dcp|  楼主 | 2011-10-13 03:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 dcp 于 2011-10-13 03:38 编辑

芯片是STM32F103RCT6
IAR6.21
2.01库


tim1 tim4 开PWM和溢出中断,tim1 tim4溢出中断正常工作,PWM波正常,但tim4对应的PWM波不能关闭


TIM_CtrlPWMOutputs(TIM1, ENABLE);    打开PWM
TIM_CtrlPWMOutputs(TIM1, DISABLE);  可正常关闭PWM

TIM_CtrlPWMOutputs(TIM4, ENABLE);   打开PWM
TIM_CtrlPWMOutputs(TIM4, DISABLE);  不能正常关闭PWM,这句有没有都有一样有PWM波形

请教是哪里设置出错?


void tim1_PWM_init(u16 PWM_frequency)//  PWM+定时中断
{
         
       GPIO_InitTypeDef GPIO_InitStructure;
       /* Timer1 Channel 2, PA8 */
       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_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
   
   
    TIM_DeInit(TIM1);
   
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    //TIM_TimeBaseStructure.TIM_Period = 7;   ////1us*(499+1)=500us
    //TIM_TimeBaseStructure.TIM_Prescaler = 71; //(71+1)/72000000=1us
   
    TIM_TimeBaseStructure.TIM_Period = (72000 / PWM_frequency - 1); //
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
   
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
   //-------
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    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 = CCR1_Val;//占空比
   
    TIM_OCInitStructure.TIM_Pulse = (72000 / PWM_frequency - 1) / 2; //
   
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
      
      
   // TIM_OC1Init(TIM1,&TIM_OCInitStructure);
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
   
   
   //========
   
   
   
    NVIC_InitTypeDef NVIC_InitStructure;
   //Enable the TIM1 gloabal Interrupt TIM1_UP_IRQChannel
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel;//TIM1_UP_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
   
    TIM_ClearFlag(TIM1, TIM_FLAG_Update);
    TIM_ARRPreloadConfig(TIM1, DISABLE);
    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM1, ENABLE);
  
}




void tim4_PWM_init(u16 PWM_frequency)//  PWM+定时中断
{
         
       GPIO_InitTypeDef GPIO_InitStructure;
        
       GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8; //M2CY
       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_DeInit(TIM4);
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    //TIM_TimeBaseStructure.TIM_Period = 7;   ////1us*(499+1)=500us
    //TIM_TimeBaseStructure.TIM_Prescaler = 71; //(71+1)/72000000=1us
   
    TIM_TimeBaseStructure.TIM_Period = (72000 / PWM_frequency - 1); //
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
   
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
   //-------
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    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 = CCR1_Val;//占空比
   
    TIM_OCInitStructure.TIM_Pulse = (72000 / PWM_frequency - 1) / 2; //
   
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
    TIM_OC3Init(TIM4, &TIM_OCInitStructure);
      
      
    //TIM_OC3Init(TIM4,&TIM_OCInitStructure);
    TIM_CtrlPWMOutputs(TIM4, ENABLE);
   
   
   //========
   
   
   
    NVIC_InitTypeDef NVIC_InitStructure;
   //Enable the TIM1 gloabal Interrupt TIM1_UP_IRQChannel
    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
   
    TIM_ClearFlag(TIM4, TIM_FLAG_Update);
    TIM_ARRPreloadConfig(TIM4, DISABLE);
    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
   
     
   
    TIM_Cmd(TIM4, ENABLE);
  
}


void TIM1_UP_IRQHandler(void)
{
   
    if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
         LED9B();
        
    }
     
}




void TIM4_IRQHandler(void)
{
    if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
         LED8B();
        
     }
}
沙发
airwill| | 2011-10-13 09:02 | 只看该作者
本帖最后由 airwill 于 2011-10-13 09:06 编辑

看样子, 楼主是没有仔细看手册咯.
固件手册这么写的:

void TIM_CtrlPWMOutputs  ( TIM_TypeDef *  TIMx,  
  FunctionalState  NewState  
)   

Enables or disables the TIM peripheral Main Outputs.

参数:
TIMx,: where x can be 1, 8, 15, 16 or 17 to select the TIMx peripheral.  
NewState,: new state of the TIM peripheral Main Outputs. This parameter can be: ENABLE or DISABLE.  

返回值:
None  

也就是说, TIM_CtrlPWMOutputs() 的  TIMx: where x can be 1, 8, 15, 16 or 17
Tim4 是没有作用的.

仔细再看函数的实现:

TIMx->BDTR |= BDTR_MOE_Set;
(TIMx->BDTR &= BDTR_MOE_Reset;)

Tim4 没有 BDTR, 就没有 MOE 位, 所以这个函数无效

使用特权

评论回复
板凳
dcp|  楼主 | 2011-10-13 10:21 | 只看该作者
谢谢,原来这个函数只对TIM1和8有效

使用特权

评论回复
地板
zmj2008| | 2011-10-13 11:20 | 只看该作者
你可以结贴了,呵呵

使用特权

评论回复
5
dcp|  楼主 | 2011-10-13 11:21 | 只看该作者
还没有找到关闭TIM4 PWM的方法呢

使用特权

评论回复
6
airwill| | 2011-10-13 11:46 | 只看该作者
控制 CCER 的 CCxE 就可以了

使用特权

评论回复
7
dcp|  楼主 | 2011-10-13 12:52 | 只看该作者
谢谢,分已送

TIM4 -> CCER  |= 0X0100; //开
TIM4 -> CCER &= 0XFEFF; //关

使用特权

评论回复
8
zhihong0105| | 2013-10-11 16:11 | 只看该作者
学到东西了!

使用特权

评论回复
9
zeluo| | 2013-10-11 20:19 | 只看该作者
问题解决了就好     感觉还是版主给力   相当强悍

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

dcp

71

主题

720

帖子

1

粉丝