STM32 TIM8 PWM

和普通TIM不一样, 使用TIM8 产生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);            
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Fred_Wu/article/details/51544170
个人分类: STM32
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

STM32 TIM8 PWM

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭