Hello all I just working on project using encoder interface but as much as search for example for encoder interface for stm32 I find less I want a sample that use interrupt and micro encoder capability not software solution that can give me direction count and speed but there nothing use full that I can find in reference manual or timers application note or even micro manual the micro I just using is stm32l152rbt6 I find confining alternate function for encoder interface but there no evidence of interrupt handler for using that Please help me if you can
Part of code that I just use till now
void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct;
/** Configure pins as * Analog * Input * Output * EVENT_OUT * EXTI PA8 ------> SYS_MCO */
/*Enable or disable the AHB peripheral clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOA, ENABLE);
/*Configure GPIO pin : PB */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_40MHz; GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : PA */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_400KHz; GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin alternate function */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO);
/** I2C1 GPIO Configuration PB6 ------> I2C1_SCL PB7 ------> I2C1_SDA */
/*Enable or disable the AHB peripheral clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/*Configure GPIO pin : PB */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_400KHz; GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin alternate function */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
/*Configure GPIO pin alternate function */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
/** TIM2 GPIO Configuration PA0-WKUP1 ------> TIM2_CH1_ETR PA1 ------> TIM2_CH2 */
/*Enable or disable the AHB peripheral clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/*Configure GPIO pin : PA */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_400KHz; GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin alternate function */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);
/*Configure GPIO pin alternate function */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);
/** USART3 GPIO Configuration PB10 ------> USART3_TX PB11 ------> USART3_RX */
/*Configure GPIO pin : PB */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_400KHz; GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin alternate function */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
/*Configure GPIO pin alternate function */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
}
void RCC_Configuration(void) { /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* GPIOA clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); }
void TIM2_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; // Maximal TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge depending on TI2FP2 level. TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_Cmd(TIM2, ENABLE);
Wall of code, non-sense, ...
If you just want to read encoder position, read TIMx->CNT, ideally use a 32-bit counter, and channel 1 and 2 via pins that map there, see the Data Sheet for options/availability.
Use Update, or assorted CC Channel Compare (CC) interrupts to catch, or advance/retard counts you wish to get signalled.
Tnx man but still you just don't answer my question which interrupt is happening while it change state Timer or EXT and it enabled automatically or it must manually enabled and which flag must check for it
Because I'm not sure what you're asking. What does "change state" mean in this context? The timer can interrupt into the TIMx_IRQHandler() based on an UPDATE event (ie crossing ZERO count), or one of the CC1, CC2, CC3, CC4 events as a comparator matches a COUNT.
It's not naturally going to interrupt at each up/down tick. You'd be better checking it periodically (1ms or whatever)
tnx man