STM32F407 PWM无法输出波形的问题

2019-07-20 10:58发布

最近在调试PWM, 用到了PWM输出, 使用STM32F407VG, TIM4 CH1 PB6上输出, 示波器上却看不到波形. 反复看了很多遍代码, 实在是找不到问题在哪里了. 烦请各位帮忙看一下, 不胜感激...

[mw_shl_code=c,true]void TIM4_GPIO_Config()
{
        GPIO_InitTypeDef GPIO_InitStructure;

        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;       
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

        GPIO_Init(GPIOB, &GPIO_InitStructure);

        GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);
}

void TIM4_TIM_Config()
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef TIM_OCInitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

        TIM_TimeBaseStructure.TIM_Period = 10000 - 1;
        TIM_TimeBaseStructure.TIM_Prescaler = 9000 - 1;
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

        TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);


        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OCInitStructure.TIM_Pulse = 2000;

        TIM_OC1Init(TIM4, &TIM_OCInitStructure);
   
        TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
        TIM_ARRPreloadConfig(TIM4, ENABLE);
        TIM_ClearFlag(TIM4, TIM_FLAG_Update);
        TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE );
       
        NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;  
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;  
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);  

        TIM_Cmd(TIM4, ENABLE);
}

void PWMConfig()
{
        TIM4_GPIO_Config();
        TIM4_TIM_Config();
}
[/mw_shl_code]

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。