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
1楼-- · 2019-07-21 01:42
下面是中断程序:

u32 TIM1CH1_COUNTER_VAL1,TIM1CH1_COUNTER_VAL2,TIM1CH1_COUNTER_VAL;
u32 usefulbyte;
u32 TIM4CH2_COUNTER_VAL1,TIM4CH2_COUNTER_VAL2,TIM4CH2_COUNTER_VAL;

u32 TIM4CH2_CAPTURE_VAL;  //??????????
void TIM4_IRQHandler(void)
{  
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)//????2·??ú????????
{
if(TIM4CH2_CAPTURE_STA&0X40) //????????????????
{  
TIM4CH2_COUNTER_VAL1 = TIM_GetCounter(TIM4);
if(TIM4CH2_COUNTER_VAL2 >= TIM4CH2_COUNTER_VAL1)
TIM4CH2_COUNTER_VAL = TIM4CH2_COUNTER_VAL2 - TIM4CH2_COUNTER_VAL1;
else 
TIM4CH2_COUNTER_VAL = 65535 - TIM4CH2_COUNTER_VAL1 + TIM4CH2_COUNTER_VAL2;
if(TIM4CH2_COUNTER_VAL >= 9240)
{
//usefulbyte = usefulbyte + 1;
databyte = 1;
}
TIM4CH2_CAPTURE_STA=0X00; //±ê??????????????????????
   TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=0 ?è??????????????
}
else   //??????????????????????????
{
TIM4CH2_CAPTURE_STA=0; //????
TIM4CH2_CAPTURE_VAL=0;
TIM_SetCounter(TIM4,0);
TIM4CH2_CAPTURE_STA|=0X40; //±ê????????????????
TIM4CH2_COUNTER_VAL2 = TIM_GetCounter(TIM4);
  TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=1 ?è??????????????
}    
}           
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); //????????±ê????
}
正点原子
2楼-- · 2019-07-21 05:48
帮顶....
qianyuyu
3楼-- · 2019-07-21 11:23
回复【3楼】正点原子:
---------------------------------
哇咔咔,这个配置是可以正常使用的,之前操作失误。
不过还有一个问题,TIM_GetCapture2(TIM4);跟TIM_GetCounter(TIM4);两个有什么区别。
我的目的是获取低电平的宽度。
qianyuyu
4楼-- · 2019-07-21 14:10
 精彩回答 2  元偷偷看……
正点原子
5楼-- · 2019-07-21 16:37
回复【5楼】qianyuyu:
---------------------------------
是的
qianyuyu
6楼-- · 2019-07-21 22:28
 精彩回答 2  元偷偷看……

一周热门 更多>