AnsweredAssumed Answered

Encoder input pulse counting

Question asked by MrStive on Sep 4, 2013
Latest reply on Sep 5, 2013 by Jack Peacock
Hi All,
I have a hall effect sensor outputting 0-5V square wave. This is connected to TIM2 CH1 (PA15) on STM32 M4.
I am having problems converting this output to RPM value.
The output varies from 0Hz to ~200Hz. My MCU is running at 168Mhz.

I have tried IC with CC1 interrupt, TIM_TIxExternalClockConfig to increment count, and TIM_ETRClockMode1Config/TIM_ETRClockMode2Config.

I cant seem to get consistent/meaningful readings. I have a oscilloscope hooked up to the signal and there is a little noise, but not too much.

For instance, the code for TIM_TIxExternalClockConfig is as follows.
//Encoder PA15. TIM2 CH1
// Enable GPIO clock
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
/* TIM2 Configuration */
TIM_DeInit(TIM2);
// PA15 for encoder  
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
 
GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2); //PA15. TIM2 CH1
 
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
 //Configure Timer
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; // TIM2 is 32-bit counter
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//TIM_ARRPreloadConfig(TIM2, ENABLE);
//TIM_SetAutoreload (TIM2, 0xffff);
    //TIM_ETRClockMode1Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x01);
 
  TIM_TIxExternalClockConfig(TIM2,  TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0x1);
 
TIM_Cmd(TIM2, ENABLE);
TIM2->CNT = 0;

Then I poll TIM2->CNT every 0.5s and then reset CNT but cant seem to get consistent readings.
Any reason, or is it noise?
Do I need a specific prescale?

Outcomes