我用定时器4配置成PWM输出,同时做为主模式,定时器5配置为输出比较模式,作为从模式。为什么定时器5没有进输出比较的中断里面
void Timer4_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* GPIOD clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
//ÅäÖÃTIM4μÄDÅoÅêä3ö1ü½ÅÎaPD12£¬OC1í¨μ࣬êä3ö
SINCDÅoÅ
/* TIM4 channel 1 pin (PD12) configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Connect TIM pins to AF2 */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 4-1; //84M/4=21Mhz
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 700; //21M/700=30Khz.
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* Channel 1 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 350; //50%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
/* Master Mode selection: TIM1 */
TIM_SelectOutputTrigger(TIM4, TIM_TRGOSource_Update);//
/* Select the Master Slave Mode */
TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM4, ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM4,ENABLE);
}
void Timer5_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* TIM9 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
/* GPIOE clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
/* Configure PD11 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_ResetBits(GPIOD, GPIO_Pin_11);
TIM_TimeBaseStructure.TIM_Period = 1000;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
/* Slave Mode selection: TIM4 */
TIM_SelectInputTrigger(TIM5, TIM_TS_ITR2);
TIM_SelectSlaveMode(TIM5, TIM_SlaveMode_Trigger);
TIM_SetCompare1(TIM5,20);
TIM_SetCompare2(TIM5,30);
TIM_ITConfig(TIM5,TIM_IT_CC1,ENABLE);
TIM_ITConfig(TIM5,TIM_IT_CC2,ENABLE);
/* TIM enable counter */
TIM_Cmd(TIM5, ENABLE);
}
void TIM5_IRQHandler(void)
{
if(TIM_GetFlagStatus(TIM5,TIM_FLAG_CC1) == SET)
{
GPIO_SetBits(GPIOD, GPIO_Pin_11);
TIM_ClearITPendingBit(TIM5,TIM_IT_CC1);
}
if(TIM_GetFlagStatus(TIM5,TIM_FLAG_CC2) == SET)
{
GPIO_ResetBits(GPIOD, GPIO_Pin_11);
TIM_ClearITPendingBit(TIM5,TIM_IT_CC2);
}
}
一周热门 更多>