cancel
Showing results for 
Search instead for 
Did you mean: 

Encoder input pulse counting

stefanskos9
Associate II
Posted on September 04, 2013 at 06:10

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 PA 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); //PA 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?
3 REPLIES 3
stefanskos9
Associate II
Posted on September 05, 2013 at 01:47

Perhaps I need to use EXTI?

I am getting readings from the methods listed, however they seem to be very inconsistent. Perhaps something to do with prescale?

Anyone have any thoughts?

Posted on September 05, 2013 at 02:25

Doesn't look unreasonable, no you don't need a prescaler.

I'd probably not reset the counter, and just compute a delta measurement between two sample periods.

Try driving the pin from another clock source to confirm it functions as expected.

You could try using EXTI and an interrupt too, although 200 KHz is more appropriate for the timer.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jpeacock2399
Associate II
Posted on September 05, 2013 at 16:01

What's your definition of ''consistent''?  What's the variation in the timer count between intervals?  How is the motor controlled, do you know it's at speed with no variation?

Is this a 3 phase BLDC motor?  There's a special hall effect mode for timers configured for shaft angle encoders on a 3 phase BLDC.

  Jack Peacock