您的位置 首页 芯闻

STM32单片机怎么经过定时器的操控输出PWM

STM32单片机如何通过定时器的控制输出PWM-STM32F103ZET6里共有8个定时器,其中高级定时器有TIM1-TIM5、TIM8,共6个。 我这里输出PWM的定时器是TIM2,空闲的定时器是TIM3。以TIM2为主定时器,TIM3为从定时器对TIM2的输出脉冲数进行计数。 查表可知,TIM3为从定时器选择TIM2为触发源,需要配置TS=001,即选择ITR1。

STM32F103ZET6里共有8个定时器,其间高档定时器有TIM1-TIM5、TIM8,共6个。

我这儿输出PWM的定时器是TIM2,闲暇的定时器是TIM3。以TIM2为主定时器,TIM3为从定时器对TIM2的输出脉冲数进行计数。

查表可知,TIM3为从定时器挑选TIM2为触发源,需求装备TS=001,即挑选ITR1。

完成经过定时器操控输出PWM个数的功用,能够有如下一种装备方法:

STM32单片机怎么经过定时器的操控输出PWM

void TIM2_Master__TIM3_Slave_Configuration(u32 PulseFrequency)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

u16 nPDTemp ;

/* ———————————————————————–

TIMx Configuration: generate 4 PWM signals with 4 different duty cycles:

TIMxCLK = 72 MHz, Prescaler = 0x0, TIMx counter clock = 72 MHz

TIMx ARR Register = 0 =》 TIMx Frequency = TIMx counter clock/(ARR + 1)

TIMx Frequency = 72MHz.

———————————————————————– */

TIM_Cmd(TIM2, DISABLE);

nPDTemp = 72000000UL/PulseFrequency;

// 时基装备:装备PWM输出定时器——TIM2

/* Time base configuration */

TIM_TimeBaseStructure.TIM_Period = nPDTemp-1;

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

// 输出装备:装备PWM输出定时器——TIM2

/* PWM1 Mode configuration: Channel1 */

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = nPDTemp》》1;//50%

TIM_OC1Init(TIM2, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);

// 时基装备:装备脉冲计数寄存器——TIM3

TIM_TimeBaseStructure.TIM_Period = 0xFFFF;

TIM_TimeBaseStructure.TIM_Prescaler = 1;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

/* Output Compare Active Mode configuration: Channel1 */

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = 0xFFFF; // 这儿的装备值含义不大

TIM_OC1Init(TIM3, &TIM_OCInitStructure);

// 装备TIM2为主定时器

/* Select the Master Slave Mode */

TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);

/* Master Mode selection */

TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);

// 装备TIM3为从定时器

/* Slave Mode selection: TIM3 */

TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated);

TIM_SelectInputTrigger(TIM3, TIM_TS_ITR1);

TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);

TIM_Cmd(TIM2, DISABLE);

TIM_Cmd(TIM3, DISABLE);

}

中止服务程序如下:

u8 TIM2_Pulse_TIM3_Counter_OK = 0;

void TIM3_IRQHandler(void)

{

if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)

{

TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); // 铲除中止标志位

TIM_Cmd(TIM2, DISABLE); // 封闭定时器

TIM_Cmd(TIM3, DISABLE); // 封闭定时器

TIM2_Pulse_TIM3_Counter_OK = 1;

}

}

应用程序为:

u16 pulsecnt = 10000;

void main(void)

{

SystemSetup(); // 初始化内核和外设

TIM2_Master__TIM3_Slave_Configuration(10000);//装备TIM2的脉冲输出为10k

while(1)

{

TIM_ITConfig(TIM3, TIM_IT_CC1, DISABLE); /* TIM enable counter */

TIM_Cmd(TIM3, ENABLE);

TIM3-》CCR1 = pulsecnt;

TIM3-》CNT = 0;

TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);

TIM_Cmd(TIM2, ENABLE); /* TIM enable counter */

while(TIM2_Pulse_TIM3_Counter_OK == 0);

}

}

这种装备方法下,运用的是TIM3的比较中止,我还没实验过其他的方法,想来应该也是能够的,比如用定时器更新中止

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/xinwen/346719.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部