cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Timer2 in Encoder Mode and CaptureCompare

dominik
Senior
Posted on February 02, 2015 at 10:21

Hi

I use the Timer2 in Encoder Mode, works perfect. Now I need an Interrupt on a Compare Value if the Encoder rech this value, but the Timer should count normally.

Eyample:

Encoder Position = 1000 (Timer value)

Encoder turning to 10000 (Timer value)

Interrupt if the Timer Value reaches 5000.

void PositionEncoder_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_5; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2); TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 0xffff; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* Configure the timer */ TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Falling); TIM_SetCounter (TIM2, (uint32_t) Encoder_Position_Default); //set the Timer to the default init value (middle) /* TIM2 counter enable */ TIM_Cmd(TIM2, ENABLE); //TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); ///////////////////// // configure PWM mode and duration TIM_OCStructInit (&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; //TIM_OCMode_Timing TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; //TIM_OutputState_Enable //TIM_OCInitStructure.TIM_Pulse = PulseDurationInMicroSeconds; // set the duty cycle / pulse here! TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC2Init(TIM2, &TIM_OCInitStructure); }

Whats wrong in this configuration?

Regards, Dominik
2 REPLIES 2
Posted on February 02, 2015 at 13:04

Well that's difficult to read, consider using the Format Code Block tool (Paintbrush [<>] icon, upper left)

Use Inactive mode?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
dominik
Senior