2015-02-09 06:24 AM
Hello,
I'm learing to use STM32Cube and HAL.Then I tried to set TIM encoder mode,but it can't work.This is my code of initialization and functionCan someone tell me ,what mistake I made or what important step I missed.Thank you for your reading and forgive my poor poor English./* TIM4 init function */void TIM4_Init(void){ TIM_Encoder_InitTypeDef sConfig; TIM_MasterConfigTypeDef sMasterConfig; GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); htim4.Instance = TIM4; htim4.Init.Prescaler = 65535; htim4.Init.CounterMode = TIM_COUNTERMODE_UP; htim4.Init.Period = 0; htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; sConfig.EncoderMode = TIM_ENCODERMODE_TI1; sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; sConfig.IC1Prescaler = TIM_ICPSC_DIV1; sConfig.IC1Filter = 0; sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; sConfig.IC2Prescaler = TIM_ICPSC_DIV1; sConfig.IC2Filter = 0; HAL_TIM_Encoder_Init(&htim4, &sConfig); HAL_TIM_Base_Init(&htim4); sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim4,&sMasterConfig); HAL_TIM_Encoder_MspInit(&htim4); HAL_TIM_Encoder_Start(&htim4,TIM_CHANNEL_1); HAL_TIM_Encoder_Start(&htim4,TIM_CHANNEL_2); }/*QEI Get Value Function*/#define QEI_R TIM4->CNTint32_t Encoder_R;int16_t E_r_V;void read_encoder (void){ Encoder_R=QEI_R;}2015-02-09 06:57 AM
> htim4.Init.Prescaler = 65535;
> htim4.Init.Period = 0; I don't speak nor understand the Cube gobbledygook, but if this translates directly to TIMx_PSC and TIMx_ARR settings respectively, it is incorrect and should be the other way round. Read the TIm2-TIM5 chapter of RM0090. JW2015-02-10 10:47 PM
hello waclawek:
You're right. My setting Prescaler and Period ,It is incorrect and should be the other way round. I revise this mistake and it can work. Thank you.