1
控制/MCU
STM32F103ZET6里共有8个定时器,其中高级定时器有TIM1-TIM5、TIM8,共6个。
我这里输出PWM的定时器是TIM2,空闲的定时器是TIM3。以TIM2为主定时器,TIM3为从定时器对TIM2的输出脉冲数进行计数。
查表可知,TIM3为从定时器选择TIM2为触发源,需要配置TS=001,即选择ITR1。
实现通过定时器控制输出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的比较中断,我还没试验过其他的方式,想来应该也是可以的,比如用定时器更新中断
全部0条评论
快来发表一下你的评论吧 !