OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3945|回复: 2

407VE做PWM无输出,找不到问题

[复制链接]

1

主题

1

帖子

0

精华

新手入门

积分
3
金钱
3
注册时间
2017-7-21
在线时间
1 小时
发表于 2017-7-21 13:08:06 | 显示全部楼层 |阅读模式
3金钱
本帖最后由 rensongwang 于 2017-7-21 13:10 编辑

下面是我的代码,不管怎么弄都没波形输出,有谁能帮我看看怎么回事吗?这里面TIM2可以不管,就用了TIM4.原来用的的TIM4_CH1, PB6输出的,现在改到TIM4_CH3, PB8输出,一点反应也没有。
:[mw_shl_code=c,true]
#include "TIM_PWM_Config.h"

void TIM4_GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;


  //NVIC_Configuration(1);

  /* GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//open SYSCFG clock for EXTI input
        
  /*GPIOB Configuration: TIM4 channel 1 and 2 as alternate function push-pull */
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM4);
        
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
  //GPIO_ResetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7);
        
        //EXTI input configure
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}


void Tim2_Slave_Init(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_DeInit(TIM2);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    //NVIC_Configuration(8);
    TIM_TimeBaseStructure.TIM_Period= 40000;
    TIM_TimeBaseStructure.TIM_Prescaler= 0;         //ê±ÖóÔ¤·ÖÆμ
    TIM_TimeBaseStructure.TIM_ClockDivision=0;  //2éÑù·ÖÆμ
    TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);

    TIM_SelectInputTrigger(TIM2,TIM_TS_ITR3);//TIMx internal trigger connection±í
    TIM_InternalClockConfig(TIM2);
    //TIM2->SMCR |=  0x0007;//é趨′óģ꽣¬¼ÆêyÄ£ê½
    TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_External1);
        
    //TIM_UpdateDisableConfig(TIM4,ENABLE);
                TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Global);
    TIM_ARRPreloadConfig(TIM2, ENABLE);
    TIM_ClearFlag(TIM2,TIM_FLAG_Update);    //Çå3yòç3öÖD¶Ï±êÖ¾
    //TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
    TIM_Cmd(TIM2,ENABLE);//¿aÆôê±Öó(¿aÆôoóÿ·¢Ëíò»¸öÂö3壬¶¨ê±Æ÷¼ó1)


}
/*
void Tim3_Slave_Init(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_DeInit(TIM3);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    NVIC_Configuration(9);

        TIM_TimeBaseStructure.TIM_Period= XBUF[3] - 1;
    TIM_TimeBaseStructure.TIM_Prescaler= 0;         //??????
    TIM_TimeBaseStructure.TIM_ClockDivision=0;  //????
    TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//????
    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);

    TIM_SelectInputTrigger(TIM3,TIM_TS_ITR3);//??TIM2??????  ????3  TIM4
    TIM_InternalClockConfig(TIM3);
    TIM3->SMCR |=  0x0007;//??????????,??????1 ???????
     
    //TIM_UpdateDisableConfig(TIM4,ENABLE);
    TIM_ARRPreloadConfig(TIM3, ENABLE);
    TIM_ClearFlag(TIM3,TIM_FLAG_Update);    //????????
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
    TIM_Cmd(TIM3,ENABLE);//??????(??????????,?????)


}*/


/*
* ???:TIM4_Mode_Config
* ??  :??TIM4???PWM?????,??????????
* ??  :?
* ??  :?
* ??  :????
*/
void TIM4_PWM_Init(u16 T)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;

    //u16 CCR1_Val = T/2;
    //u16 CCR2_Val = (T+1)/2;
    //u16 CCR3_Val = (T+1)/2;
    //u16 CCR4_Val = (T+1)/2;
          /* TIM4 clock enable */
    //TIM4ê±ÖóÔ′μèóú84MHz

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
    TIM_DeInit(TIM4);
  /* -----------------------------------------------------------------------
    TIM1 Configuration: generate 4 PWM signals with 4 different duty cycles:
    TIM1CLK = 36 MHz, Prescaler = 0x0, TIM1 counter clock = 36 MHz
    TIM1 ARR Register = 999 => TIM1 Frequency = TIM1 counter clock/(ARR + 1)
    TIM1 Frequency = 36 KHz.
    TIM1 Channel1 duty cycle = (TIM1_CCR1/ TIM1_ARR)* 100 = 50%
    TIM1 Channel2 duty cycle = (TIM1_CCR2/ TIM1_ARR)* 100 = 50%
    TIM1 Channel3 duty cycle = (TIM1_CCR3/ TIM1_ARR)* 100 = 50%
    TIM1 Channel4 duty cycle = (TIM1_CCR4/ TIM1_ARR)* 100 = 50%
  ----------------------------------------------------------------------- */

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = T-1;//T us
  TIM_TimeBaseStructure.TIM_Prescaler = 84-1;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  //TIM4->CR1 &= ((u16)0x03FD);// UDIS enable
  //TIM_UpdateDisableConfig(TIM4,DISABLE);
        
  /* PWM1 Mode configuration: Channel1 */
        TIM_OCStructInit(&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 = T/2;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
        
  TIM_OC3Init(TIM4, &TIM_OCInitStructure);

  TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel2
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

  TIM_OC2Init(TIM4, &TIM_OCInitStructure);

  TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);*/
/*

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
  TIM_OC3Init(TIM4, &TIM_OCInitStructure);
  TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
  TIM_OC4Init(TIM4, &TIM_OCInitStructure);
  TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);*/

  TIM_ARRPreloadConfig(TIM4, ENABLE);

  //TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_OC1Ref);
  TIM_ClearFlag(TIM4, TIM_FLAG_Update);
  /* TIM1 enable counter */
  TIM_Cmd(TIM4, ENABLE);

  /* Main Output Enable */
  TIM_CtrlPWMOutputs(TIM4, ENABLE);

}[/mw_shl_code]
下面是main函数里面的相关调用:


        TIM4_GPIO_Config();
        Tim2_Slave_Init();
        TIM4_PWM_Init(2500);

        while(1) {}


最佳答案

查看完整内容[请看2#楼]

RCC_APB2PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE); 不想多说什么.这种错 自己都发现不出来 我就呵呵了 还是从自身找问题吧, 论坛大佬说的对, 走路还不会呢,就想跑. 当心扯着淡 ps:不一定是这个错导致的不输出
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

7

主题

59

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
461
金钱
461
注册时间
2015-4-6
在线时间
57 小时
发表于 2017-7-21 13:08:07 | 显示全部楼层
  RCC_APB2PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE);
不想多说什么.这种错 自己都发现不出来
我就呵呵了
还是从自身找问题吧,
论坛大佬说的对,
走路还不会呢,就想跑.
当心扯着淡
ps:不一定是这个错导致的不输出
回复

使用道具 举报

9

主题

1385

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
11560
金钱
11560
注册时间
2013-3-8
在线时间
1233 小时
发表于 2017-7-21 13:20:41 | 显示全部楼层
//TIM4 PWM部分初始化
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM4_PWM_Init(u32 arr,u32 psc)
{                                                          
        //此部分需手动修改IO口设置
       
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);          //TIM3时钟使能   
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);         //使能PORTF时钟       
       
        GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4);
        GPIO_PinAFConfig(GPIOD,GPIO_PinSource13,GPIO_AF_TIM4);
        GPIO_PinAFConfig(GPIOD,GPIO_PinSource14,GPIO_AF_TIM4);
        GPIO_PinAFConfig(GPIOD,GPIO_PinSource15,GPIO_AF_TIM4);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;         
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        //复用功能
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;        //速度2MHz
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //推挽复用输出
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        //上拉
        GPIO_Init(GPIOD,&GPIO_InitStructure);              //初始化
          
        TIM_TimeBaseStructure.TIM_Prescaler=psc;  //定时器分频
        TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
        TIM_TimeBaseStructure.TIM_Period=arr;   //自动重装载值
        TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
       
        TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);//初始化定时器4
       
        //初始化TIM4 Channel1 PWM模式         
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性低
       
        TIM_OC1Init(TIM4, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM1 4OC1
        TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);  //使能TIM3在CCR1上的预装载寄存器
       
        TIM_OC2Init(TIM4, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM1 4OC1
        TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);  //使能TIM3在CCR1上的预装载寄存器
       
        TIM_OC3Init(TIM4, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM1 4OC1
        TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);  //使能TIM3在CCR1上的预装载寄存器
       
        TIM_OC4Init(TIM4, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM1 4OC1
        TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);  //使能TIM3在CCR1上的预装载寄存器
  
    TIM_ARRPreloadConfig(TIM4,ENABLE);//ARPE使能
       
        TIM_Cmd(TIM4, ENABLE);  //使能TIM3
        TIM_SetCompare1(TIM4,0);        //修改比较值,修改占空比       
        TIM_SetCompare2(TIM4,0);        //修改比较值,修改占空比       
        TIM_SetCompare3(TIM4,0);        //修改比较值,修改占空比       
        TIM_SetCompare4(TIM4,0);        //修改比较值,修改占空比       
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-12-23 03:17

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表