码迷,mamicode.com
首页 > 其他好文 > 详细

STM32 输出3路同频不同占空比先后顺序不同的PWM

时间:2015-06-16 21:01:08      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

方法一:用同一个定时器的3路通道能输出同频不同占空比的pwm;但是他们输出的顺序一定是一样的,因为他们用的是同一个计数器

如果要输出3路不同顺序的PWM必须使用3个定时器

 

例子:

void Tim1_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

/* PA9设置为功能脚(PWM) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);


RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);


TIM_DeInit(TIM1);

/*TIM1时钟配置*/
TIM_TimeBaseStructure.TIM_Prescaler = 71; //预分频(时钟分频)72M/72=1000K
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseStructure.TIM_Period = 660;       //装载值 1000k/660=1515hz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);

/* Channel 2 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;//反向通道无效
TIM_OCInitStructure.TIM_Pulse = 430;        //占空时间
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性           有效电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补端的极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

TIM_OC2Init(TIM1,&TIM_OCInitStructure); //通道1

/* TIM1 counter enable */
// TIM_Cmd(TIM1,ENABLE);

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

 

PWM占空比高低电平设置:PWM模式1时:CNT(计数器)<TIM_Pulse为有效电平,否则为无效电平;PWM模式2, CNT<TIM_Pulse为无效电平,否则为有效电平

方法二:当定时器资源不足时,用一个定时器,在中断中通过3个IO口模拟出3路PWM

STM32 输出3路同频不同占空比先后顺序不同的PWM

标签:

原文地址:http://www.cnblogs.com/child123/p/4581533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!