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的比較中斷,我還沒試驗過其他的方式,想來應該也是可以的,比如用定時器更新中斷
評論
查看更多