STM32f405 RG tim pwm channel2初始化失败

2019-07-20 12:15发布

void H_LED_Init(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
       
        /* GPIOA, GPIOB and GPIOE Clocks enable */
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
       
        /* GPIOA Configuration: Channel 1, 3 and 4 as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
//        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
//        GPIO_PinAFConfig(GPIOA, GPIO_PinSource8|GPIO_PinSource9, GPIO_AF_TIM1);

        /* TIM1 clock enable */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);

        TimerPeriod = (SystemCoreClock / 100 ) - 1;
       
        // Time base configuration
        TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
        TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
//        TIM_TimeBaseStructure.TIM_Prescaler = 0;//初始值,0不分频 4KHZ
//        TIM_TimeBaseStructure.TIM_Prescaler = 4;//4分频800HZ 2017.6.23
//        TIM_TimeBaseStructure.TIM_Prescaler = 8;//8分频频率为450 hz
        TIM_TimeBaseStructure.TIM_Prescaler = 16;  //基于光耦继电器反应时间跟摄像头拍照帧率设置分频为16分频(237.769HZ)
        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 = 0;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
       

        TIM_OC1Init(TIM1, &TIM_OCInitStructure);
        TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
//        TIM_OC2Init(TIM1, &TIM_OCInitStructure);
//        TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
       
        TIM_ARRPreloadConfig(TIM1, ENABLE); //???????

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

void H_LED_Light_Set(uint8_t Light)
{
        TIM_SetCompare1(TIM1, ((uint32_t)Light)*TimerPeriod/100);        //原版 set/100*计数值,设置占空比
//        TIM_SetCompare1(TIM1, (((uint32_t)Light)*TimerPeriod/2)/100);        //光耦继电器调节周期为0-50%
}
/******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************/
void W_LED_Init(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
       
        /* GPIOA, GPIOB and GPIOE Clocks enable */
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
       
        /* GPIOA Configuration: Channel 1, 3 and 4 as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_TIM1);

        /* TIM1 clock enable */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);

        TimerPeriod = (SystemCoreClock / 100 ) - 1;
       
        // Time base configuration
        TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
        TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
//        TIM_TimeBaseStructure.TIM_Prescaler = 0;//初始值,0不分频 4KHZ
//        TIM_TimeBaseStructure.TIM_Prescaler = 4;//4分频800HZ 2017.6.23
//        TIM_TimeBaseStructure.TIM_Prescaler = 8;//8分频频率为450 hz
        TIM_TimeBaseStructure.TIM_Prescaler = 16;  //基于光耦继电器反应时间跟摄像头拍照帧率设置分频为16分频(237.769HZ)
        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 = 0;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

        TIM_OC2Init(TIM1, &TIM_OCInitStructure);
        TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
       
        TIM_ARRPreloadConfig(TIM1, ENABLE); //???????

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

void W_LED_Light_Set(uint8_t Light)
{
        TIM_SetCompare2(TIM1, ((uint32_t)Light)*TimerPeriod/100);        //原版 set/100*计数值,设置占空比
//        TIM_SetCompare1(TIM1, (((uint32_t)Light)*TimerPeriod/2)/100);        //光耦继电器调节周期为0-50%
}
第一段程序是TIM1,channel1 ,可以正常运行
第一段程序是TIM1,channel2 ,管脚没有输出
求助!!!!

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