这篇文章主要为大家展示了“如何解决STM32普通定时器配置PWM的问题”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何解决STM32普通定时器配置PWM的问题”这篇文章吧。
oid TIM3_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* PWM信号电平跳变值 */ // u16 CCR1= 36000; // u16 CCR2= 36000; /*PCLK1经过2倍频后作为TIM3的时钟源等于72MHz*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period =1599; //计数周期1600 TIM_TimeBaseStructure.TIM_Prescaler = 1; //设置预分频:预分频=2,即为72/3=24MHz TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分频系数:不分频 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数溢出模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse =600; //设置跳变值,当计数器计数到这个值时,电平发生跳变 TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_Low; //当定时器计数值小于CCR1时为高电平 TIM_OC1Init(TIM3, &TIM_OCInitStructure); //使能通道1 TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 300; //设置通道2的电平跳变值,输出另外一个占空比的PWM TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR2时为低电平 TIM_OC2Init(TIM3, &TIM_OCInitStructure); //使能通道2 TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIM3重载寄存器ARR /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE); Delay_8ms(); TIM_Cmd(TIM3, DISABLE); }
以上是“如何解决STM32普通定时器配置PWM的问题”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/208481.html