AnsweredAssumed Answered

STM32F4 Timer14 as impulse counter

Question asked by matkar on Jan 7, 2016
Latest reply on Jan 7, 2016 by Clive One
Hi,
I'm trying to capture and count impulses on pin PA7 using timer 14 input capture TIM14_CH1. I have tried with the following code:
Initialization:
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
 
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;       
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOA, &GPIO_InitStructure);
    
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM14); 
 
   TIM_DeInit(TIM14);
 TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
 TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
// TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
 TIM_TimeBaseInitStruct.TIM_Period = 0xFFFF;
 TIM_TimeBaseInitStruct.TIM_Prescaler = 0x0000;
 TIM_TimeBaseInit(TIM14,&TIM_TimeBaseInitStruct);
 
   TIM_ICInitStruct.TIM_Channel=TIM_Channel_1;
   TIM_ICInitStruct.TIM_ICPolarity=TIM_ICPolarity_BothEdge;//TIM_ICPolarity_Rising;//TIM_ICPolarity_BothEdge;
   TIM_ICInitStruct.TIM_ICSelection=TIM_ICSelection_DirectTI;
 TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;
   TIM_ICInitStruct.TIM_ICFilter=0;
 
   TIM_ICInit(TIM14, &TIM_ICInitStruct);
    
   TIM_TIxExternalClockConfig(TIM14,TIM_TIxExternalCLK1Source_TI1,TIM_ICPolarity_Rising,0x0F);
    
   TIM_Cmd(TIM14, ENABLE);

I read and reset the value every second:

Nr_transitions = TIM_GetCounter(TIM14);
TIM_SetCounter(TIM14, 0);
USB_VCP_Putc(Nr_transitions>>8);
USB_VCP_Putc(Nr_transitions);

The problem is I can't get the correct result. I constantly get a number between 0xBCxx and 0xBDxx no matter how many impulses I send to the pin. To be on the safe side I have confirmed the same pin can accept counting impulses through TIM3_CH2 timer using the following code:
Initialization:

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;         
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM3);   
 
  TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 0xFFFF;
TIM_TimeBaseInitStruct.TIM_Prescaler = 0x0000;
TIM_TimeBaseInit(TIM14,&TIM_TimeBaseInitStruct);
 
  TIM_ICInitStruct.TIM_Channel=TIM_Channel_2;
  TIM_ICInitStruct.TIM_ICPolarity=TIM_ICPolarity_BothEdge;//TIM_ICPolarity_Rising;//TIM_ICPolarity_BothEdge;
  TIM_ICInitStruct.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;
  TIM_ICInitStruct.TIM_ICFilter=0;
 
  TIM_ICInit(TIM3, &TIM_ICInitStruct);
   
  TIM_TIxExternalClockConfig(TIM3,TIM_TIxExternalCLK1Source_TI2,TIM_ICPolarity_BothEdge,0x0F);
   
  TIM_Cmd(TIM3, ENABLE);

One second timer:

Nr_transitions = TIM_GetCounter(TIM3);
TIM_SetCounter(TIM3, 0);
USB_VCP_Putc(Nr_transitions>>8);
USB_VCP_Putc(Nr_transitions);

On the F4 discovery board this pin serves as a data pin to MEMS sensor. But I have verified the signal with an oscilloscope and it seems it has no influence to the input.
Any suggestions are much appreciated.

Regards,
Mat


Outcomes