STM32F103ZET6 用定時(shí)器級(jí)聯(lián)方式輸出特定數(shù)目的PWM
掃描二維碼
隨時(shí)隨地手機(jī)看文章
STM32F103ZET6里共有8個(gè)定時(shí)器,其中高級(jí)定時(shí)器有TIM1-TIM5、TIM8,共6個(gè)。
這里需要使用定時(shí)器的級(jí)聯(lián)功能,ST的RM0008 REV12的P388和P399頁(yè)上有說(shuō)明對(duì)于特定的定時(shí)器,怎么去選擇級(jí)聯(lián)功能,參見(jiàn)表86。
我這里輸出PWM的定時(shí)器是TIM2,空閑的定時(shí)器是TIM3。以TIM2為主定時(shí)器,TIM3為從定時(shí)器對(duì)TIM2的輸出脈沖數(shù)進(jìn)行計(jì)數(shù)。
查表可知,TIM3為從定時(shí)器選擇TIM2為觸發(fā)源,需要配置TS=001,即選擇ITR1。
實(shí)現(xiàn)通過(guò)定時(shí)器控制輸出PWM個(gè)數(shù)的功能,可以有如下一種配置方式:
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;
// 時(shí)基配置:配置PWM輸出定時(shí)器——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輸出定時(shí)器——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);
// 時(shí)基配置:配置脈沖計(jì)數(shù)寄存器——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為主定時(shí)器
/* Select the Master Slave Mode */
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
/* Master Mode selection */
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
// 配置TIM3為從定時(shí)器
/* 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);
}
中斷服務(wù)程序如下:
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); // 清除中斷標(biāo)志位
TIM_Cmd(TIM2, DISABLE); // 關(guān)閉定時(shí)器
TIM_Cmd(TIM3, DISABLE); // 關(guān)閉定時(shí)器
TIM2_Pulse_TIM3_Counter_OK = 1;
}
}
應(yīng)用程序?yàn)椋?/p>
u16 pulsecnt = 10000;
void main(void)
{
SystemSetup(); // 初始化內(nèi)核和外設(shè)
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的比較中斷,我還沒(méi)試驗(yàn)過(guò)其他的方式,想來(lái)應(yīng)該也是可以的,比如用定時(shí)器更新中斷……