微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 转两个stm32定时器的使用代码

转两个stm32定时器的使用代码

时间:11-30 来源:互联网 点击:
具体没有测试,先收藏,以后去尝试一下

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = Pluse;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;

TIM_OC1Init(BLDC_TIMER_NUM, &TIM_OCInitStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
Pluse = MotorA.TimerPeriod - Pluse;
TIM_OCInitStructure.TIM_Pulse = Pluse;
TIM_OC2Init(BLDC_TIMER_NUM, &TIM_OCInitStructure);

#define Fsys 72000000ul // system freq 72MHz
#define Fpwm 20000 // PWM freq 20K

void ConfigTimer(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM4,ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,ENABLE);

TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
TIM_TimeBaseStructure.TIM_Period = (Fsys/2) / Fpwm;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = (((Fsys/2) / Fpwm) * 20) / 100;
TIM_OC3Init(TIM4,&TIM_OCInitStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_Pulse = (((Fsys/2) / Fpwm) * (100-20)) / 100;
TIM_OC4Init(TIM4,&TIM_OCInitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

TIM_CtrlPWMOutputs(TIM4, ENABLE);
TIM_Cmd(TIM4,ENABLE);
}

基本设置如下:
1)配置定时器的计数器为中间对齐计数,即先向上计数再向下计数。
2)在该定时器上选择2个通道,并分别配置为输出比较模式,并配置在比较成功时翻转对应的引脚输出。
3)配置自动重装载寄存器TIMx_ARR为要求输出频率的一半。
4)假定CC1为第一个输出信号的通道,再假定第一个信号的正脉冲宽度对应为W1,则配置TIMx_CCR1为TIMx_ARR-W1/2。
5) 同4),假定CC2为第二个输出信号的通道,正脉冲宽度对应为W2,配置TIMx_CCR2为W2/2。

----------------------------------------------
下面以一个例子说明:

假设要求输出的信号频率为10kHz,占空比为1:3。
再假设定时器的输入时钟为72MHz。

输出信号的频率10kHz,换算为计数器的数值为7200。
按照上述3),设置TIMx_ARR=3600

输出信号1的高电平时间W1,换算为计数器的数值为W1=7200/4=1800
按照上述4),设置TIMx_CC1=3600 - W1/2=2700

输出信号2的高电平时间W2,换算为计数器的数值为W2=7200/4=1800
按照上述5),设置TIMx_CC2=2/2=450

参照下图,图中红线表示计数器的数值变化:
①当计数器的数值从0向上计数,达到TIMx_CC1时,CC1匹配成功,CC1的输出电平翻转;
②计数器继续向上计数,达到TIMx_ARR时开始调头向下计数;当计数器的数值下降到TIMx_CC1时,CC1再次匹配成功,CC1的输出电平再次翻转;
③计数器继续向下计数,达到到TIMx_CC2时,CC2匹配成功,CC2的输出电平翻转;
④计数器继续向下计数,减到0时开始调头向上计数;当计数器的数值上升到TIMx_CC2时,CC2再次匹配成功,CC2的输出电平再次翻转;

如此循环,得到连续的相位互为180度的两路输出波形。

注意:上述描述是一个

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top