我的思路是用TIM5发出脉冲数,TIM2计时,计时结束中断时停止PWM输出。
这是我TIM2的设置
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseStructure.TIM_Period = 60000-1;
TIM_TimeBaseStructure.TIM_Prescaler = 36000-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel =TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInit(TIM2 , &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM2,TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
TIM_Cmd(TIM2 , ENABLE);
TIM_ITConfig(TIM2,TIM_IT_CC2,ENABLE) ;
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM , 0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH , 0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_FLAG_CC2) != RESET)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_0);
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
}
}
大神,帮忙看看我的配置是不是正确的。 |