项目需要:计算PWM脉冲的个数解决办法:采用定时器级联:TIM3做master,TIM4做slave.
具体配置如下:
void TIM3_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //TIM14ê±Öóê1Äü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //ê1ÄüPORTFê±Öó
GPIO_PinAFConfig(GPIOC,GPIO_PinSource8,GPIO_AF_TIM3); //GPIOF9¸′óÃÎa¶¨ê±Æ÷14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //GPIOF9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Ëù¶è100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOC,&GPIO_InitStructure); //3õê¼»ˉPF9
TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨ê±Æ÷·ÖÆμ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseStructure.TIM_Period=arr; //×Ô¶ˉÖØ×°ÔØÖμ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);//3õê¼»ˉ¶¨ê±Æ÷14
//TIM_UpdateRequestConfig(TIM3,TIM_UpdateSource_Regular);
//3õê¼»ˉTIM14 Channel1 PWMÄ£ê½
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔμí
TIM_OCInitStructure.TIM_Pulse = 0xFF;
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM1 4OC1
// TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //ê1ÄüTIM14ÔúCCR1éÏμÄԤװÔؼÄ′æÆ÷
TIM_ARRPreloadConfig(TIM3,ENABLE);//ARPEê1Äü
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //¶¨ê±Æ÷4ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x02; //ÇàÕ¼óÅÏ輶1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x04; //×óóÅÏ輶4
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3, ENABLE); //ê1ÄüTIM14
}
void TIM4_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); ///ê1ÄüTIM3ê±Öó
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_External1);
TIM_SelectInputTrigger(TIM4, TIM_TS_ITR2);
TIM_TimeBaseInitStructure.TIM_Period = arr; //×Ô¶ˉÖØ×°ÔØÖμ
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //¶¨ê±Æ÷·ÖÆμ
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);//3õê¼»ˉTIM4
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn; //¶¨ê±Æ÷4ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x02; //ÇàÕ¼óÅÏ輶1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //×óóÅÏ輶4
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM4,ENABLE); //ê1Äü¶¨ê±Æ÷4
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=RESET) //òç3öÖD¶Ï
{
x=x+1;
}
TIM_ClearITPendingBit(TIM4,TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
}
一周热门 更多>