STM 32 TIM 8 PWM

2726 ワード

普通のTIMと違って、TIM 8を使ってPWMを生成するには、使用が必要です.
TIM_CtrlPWMOutputs(TIM8, ENABLE); 
PWMは発生できない
完全なプロセスは以下の通りです
//gpioc-9  is tim8ch4
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8|RCC_APB2Periph_GPIOC, ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOC, &GPIO_InitStructure);

        TIM_DeInit(TIM8);

        //2.7K
        TIM_BaseInitStructure.TIM_Period =370;
        TIM_BaseInitStructure.TIM_Prescaler = 71;
        TIM_BaseInitStructure.TIM_ClockDivision = 0;
        TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInit(TIM8, &TIM_BaseInitStructure);

        /* Output Compare Active Mode configuration: Channel4 */

        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 185;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OC4Init(TIM8, &TIM_OCInitStructure);
        TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);

        TIM_ARRPreloadConfig(TIM8, ENABLE);             
        TIM_CtrlPWMOutputs(TIM8, ENABLE); 
        TIM_Cmd(TIM8,ENABLE);