Using TIMER in Encoder Mode with interrupts on value change
Hi all,
I'm quite new to the embedded stm32 environment so if I'm asking something stupid please forgive me.
First of all, my setup.
I'm using a NUCLEO-32bit with STM32L432KC, and we need to test some performances on encoders because we have some problems.One of these tests is the one in which I'm stuck.
Basically I want to use one of the available TIMx (TIM2) in Encoder Mode with interrupts, and I've successfully managed to do let's say 80% of the work. The only part that I'm stuck with is the interrupt part: I want to fire an interrupt each time the CNT value changes, and also know the direction of the change (i.e. if my encoder rotates CW, increase a variable, if it rotates ACW i want the variable to decrement).Actually the interrupt fires regularly also when the CNT value of encoder does not changes, so I do not know what I'm doing wrong. And I also experience a strange behaviour of the encoder: also when it is not moved, sometimes the CNT value decrements itself until 0.
Here my actual code:/* TIM2 init function */
static void MX_TIM2_Init(void){TIM_Encoder_InitTypeDef sConfig;
TIM_MasterConfigTypeDef sMasterConfig;htim2.Instance = TIM2;
htim2.Init.Prescaler = 0; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 200; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; sConfig.EncoderMode = TIM_ENCODERMODE_TI12; sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; sConfig.IC1Prescaler = TIM_ICPSC_DIV1; sConfig.IC1Filter = 15; sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; sConfig.IC2Prescaler = TIM_ICPSC_DIV1; sConfig.IC2Filter = 15; if (HAL_TIM_Encoder_Init(&htim2, &sConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }}
/* MAIN */
int main(void)
{ /* USER CODE BEGIN 1 *//* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init(); MX_TIM2_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 *//* USER CODE END 2 */
HAL_TIM_Encoder_Start_IT(&htim2,htim2.Channel);/* Infinite loop */
/* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE *//* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */}
/* the last part is the IRQ handler: */
void TIM2_IRQHandler(void)
{ /* USER CODE BEGIN TIM2_IRQn 0 */ char asd[50]; sprintf(asd,'%d\n',htim2.Instance->CNT);HAL_UART_Transmit(&huart2,(uint8_t *)asd,sizeof(asd),20); /* USER CODE END TIM2_IRQn 0 */ HAL_TIM_IRQHandler(&htim2); /* USER CODE BEGIN TIM2_IRQn 1 *//* USER CODE END TIM2_IRQn 1 */
}The part regarding init functions has been automatically generated by CubeMX.
has Anyone an Idea on what's the problem?
Thanks!
#quadrature-encoder #nucleo-l432kc #timer-interrupts #st-cube