cancel
Showing results for 
Search instead for 
Did you mean: 

help! - I couldnot get stm32f4 timer work in encoder mode

mehmet.karakaya
Associate III
Posted on October 20, 2013 at 19:17

hello dear forum,

I am trying to read an AB encoder with stm32f4 timer_1 I got the encoder signals through 75175 to timer_1_ch1 and ch2 pins I can see square waves of A and B with osciloscope the codes are below - any help appreciated - thank you

void
Encoderinit(
void
) { 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; 
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
GPIO_Init(GPIOE, &GPIO_InitStructure); 
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1); 
GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_TIM1); 
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 0xffff; 
TIM_TimeBaseStructure.TIM_Prescaler = 0; 
TIM_TimeBaseStructure.TIM_ClockDivision = 0; 
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); 
TIM_EncoderInterfaceConfig(TIM1,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising ); 
TIM_ITConfig(TIM1,TIM_IT_Update , ENABLE); 
TIM_Cmd(TIM1, ENABLE); 
TIM1->CNT=0; 
}

and in the main loop I read enkoder 100 times a second but always value 0

main{ 
.................. 
Encoderinit() 
.................. 
while
(1){ 
.................. 
enkoder=TIM1->CNT; 
}

#tim-encoder #tim-encoder #stm32f4-timer-encoder
7 REPLIES 7
mehmet.karakaya
Associate III
Posted on October 20, 2013 at 21:07

hello - the problem solved

Posted on October 20, 2013 at 21:53

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

??

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mehmet.karakaya
Associate III
Posted on October 21, 2013 at 14:23

hello - the problem is solved

it was a mistake of me while reading the TIM1->CNT

the above initialization code is correct for anyone who is searching for help in this issue

with this occasion I want to ask

if I can program the input capture filter IC1F , IC2F 

(TIMx_CCMR1)

 

while the timer is in encoder mode ?

I mean is the input capture filter active in encoder mode ?

thank you  

jpeacock2399
Associate II
Posted on October 21, 2013 at 15:47

Yes you can enable captures in encoder mode.  I use capture on CC1 to detect when the encoder count changes and on CC3 to detect the index.

  Jack Peacock
mehmet.karakaya
Associate III
Posted on October 21, 2013 at 18:26

hello thank you for the answers

what I want ask is ,

there is filter at the input stage of timer inputs to eliminate fals triggering

it is sampling the timer input and if it is always ''1''  for example for a period of programmed filter time the timer only then captures or counts ( sorry this is my best english hope you understand )

what I want ask is is this filter active when in encoder mode ? ( IC1F IC2F ) CCMR register

the manual says:

IC1F[3:0]

 

: Input capture 1 filter

 

This bit-field defines the frequency used to sample TI1 input and the length of the digital filter applied

to TI1. The digital filter is made of an event counter in which N events are needed to validate a

transition on the output

 

Yes you can enable captures in encoder mode.  I use capture on CC1 to detect when the encoder count changes and on CC3 to detect the index.

  Jack Peacock
Posted on October 21, 2013 at 18:35

The pins are feed though a synchronizer, likely at the pin by the AHB clocks, and then by the TIM at it's APB/TIM clock. You'd have to digest the docs.

Filter elements of the TIM are controlled at a rate specified by

TIM_TimeBaseStructure.TIM_ClockDivision

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 October 21, 2013 at 21:16

Yes the digital filter is used in encoder mode, and it does make a difference.  My setup is a single-ended Avago HEDS-5540 optical encoder on the end of a NEMA 17 or 23 stepper, with 800 count wheels (2x sampling for 400 half setps on a 1.8 degree motor). 

Some ad hoc experimenting gave a good reading at digital filter set to 5, not so good at 7.  This is on an STM32F405 at 168MHz, using TIM4 for a rotary encoder and TIM5 for linear encoder.

  Jack Peacock