STM32F4 TIM4 第二通道 配置成输入捕获

2019-07-21 00:34发布

STM32F4 TIM4 第二通道 配置成输入捕获,但已经进不了中断,求助。
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_ICInitTypeDef  TIM4_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
uint16_t PrescalerValue = 0;
  NVIC_InitTypeDef NVIC_InitStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);

GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_13;  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //????100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //???ì????????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //????
GPIO_Init(GPIOD, &GPIO_InitStructure);
//GPIO_ResetBits(GPIOD,GPIO_Pin_13);

GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;   //?????÷
  TIM_TimeBaseStructure.TIM_Prescaler = 0;  //???±??·????ó?????????÷
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

TIM_ARRPreloadConfig(TIM4,ENABLE);   //????×???×°??

  TIM4_ICInitStructure.TIM_Channel = TIM_Channel_2; //CC1S=01 ????
  TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //??????????
  TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //??????TI1????????
  TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //??????????·???
  TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000???????????¨?÷?????¨
  TIM_ICInit(TIM4, &TIM4_ICInitStructure);

  /* Enable the TIM3 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
   
  /* TIM Interrupts enable */
//  TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);

  /* TIM3 enable counter */
  TIM_Cmd(TIM4, ENABLE);
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
7条回答
qianyuyu
2019-07-21 23:13
回复【7楼】qianyuyu:
---------------------------------
在看了一下之前的代码,一团乱。。。新改了一个。
u8 TIM4CH2_CAPTURE_STA=0; //????????×???
//bit7:?????ê??±ê??
//bit6:????????????±ê??
//bit5~0:?????????????ó?¨?±?÷??????????

u32 TIM1CH1_COUNTER_VAL1,TIM1CH1_COUNTER_VAL2,TIM1CH1_COUNTER_VAL;

void TIM4_IRQHandler(void)
{   
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)//????2·??ú????????
{
if(TIM4CH2_CAPTURE_STA&0X40) //????????????????
{   
TIM1CH1_COUNTER_VAL1 = TIM_GetCounter(TIM4);
if(TIM1CH1_COUNTER_VAL2 >= TIM1CH1_COUNTER_VAL1)
TIM1CH1_COUNTER_VAL = TIM1CH1_COUNTER_VAL2 - TIM1CH1_COUNTER_VAL1;
else 
TIM1CH1_COUNTER_VAL = 65535 - TIM1CH1_COUNTER_VAL1 + TIM1CH1_COUNTER_VAL2;
TIM4CH2_CAPTURE_STA=0X00; //±ê??????????????????????
//TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=0 ?è??????????????
TIM4_ICConfig(TIM_ICPolarity_Falling);

}
else   //??????????????????????????
{
if(TIM1CH1_COUNTER_VAL >= 9240) //
{
fDMX_buf_right = 1;
}
TIM4CH2_CAPTURE_STA=0X40; //±ê????????????????
TIM_SetCounter(TIM4,0);
TIM1CH1_COUNTER_VAL2 = TIM_GetCounter(TIM4);
//TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=1 ?è??????????????
TIM4_ICConfig(TIM_ICPolarity_Rising);
}     
}               
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); //????????±ê????
}


下面是TIM4的配置:
void TIM4_ICConfig(u16 pola)
{
TIM_ICInitTypeDef  TIM4_ICInitStructure;
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_2; //CC1S=01 
TIM4_ICInitStructure.TIM_ICPolarity = pola;//TIM_ICPolarity_Falling; //??????????
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //??????TI1??
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  //??????????·???
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000???????????¨?÷?????¨
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
}

//???????±??±???????
void TIM4_Cinit(void)//CCR??16??????
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
uint16_t rescalerValue = 0;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);

GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_13;  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //????100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //???ì????????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //????
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_13);

//GPIO_PinAFConfig(GPIOD, GPIO_Pin_13, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;   //?????÷
TIM_TimeBaseStructure.TIM_Prescaler = 0;  //???±??·????ó?????????÷
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

TIM_ARRPreloadConfig(TIM4,ENABLE);   //????×???×°??

TIM4_ICConfig(TIM_ICPolarity_Falling);

/* Enable the TIM3 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
   
/* TIM Interrupts enable */
//TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);

/* TIM3 enable counter */
TIM_Cmd(TIM4, ENABLE);
}

一周热门 更多>