cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 HAL for TIM PWM and Encoder Input

metzlermatt
Associate
Posted on November 09, 2015 at 05:13

Hello,

I am trying to use the STM32F4 HAL Drivers to read a 50 Hz PWM input signal and an A quad B encoder signal on an STM32F4 Discovery board, but the TIMx_CNT register is NOT incrementing on the external triggers. 

I used the STM32CubeMX program to configure TIM2 for encoder input on Channels 1 and 2, and tried to start reading using the HAL_TIM_Encoder_Start_IT(&huart2, TIM_CHANNEL_ALL).  I have confirmed that the timer is recognizing the signal edges because I can successfully generate interrupts off of the edges.  However, I would like to use the SysTick interrupt to read the change in encoder position periodically (In this case, I start the timer using HAL_TIM_Encoder_Start();), but the TIM2_CNT register always reads 0.

I have the same issue when trying to read the pulse width of a PWM input signal using TIM9.  I can successfully generate interrupts by starting the timer using HAL_TIM_IC_Start_IT(&huart9, TIM_CHANNEL_1), but when I read the TIM9_CCR1 register, nothing is there.  Debugging has shown me that the TIM9_CNT never starts incrementing, even though it must have recognized the pulse edge in order to generate an interrupt.

I have stepped through the TIM2_Init() and TIM9_Init() functions (which were generated by the STM32CubeMX program), and as far as I can tell they line up with the instructions for PWN input capture and encoder modes listed in the STM32F4 Reference Manual.

There are no specific functions for reading PWM input or Encoder input in the HAL library, but since the counter never starts incrementing, I suspect reading the registers is not the issue.  Any guidance would be much appreciated!

Thanks,

Matt

2 REPLIES 2
metzlermatt
Associate
Posted on November 09, 2015 at 05:19

Here is the initialization code generated by the Cube (with one edit in TIM9):

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 = 0;

  htim2.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(&htim2, &sConfig);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);

}

void MX_TIM9_Init(void)

{

  TIM_ClockConfigTypeDef sClockSourceConfig;

  TIM_SlaveConfigTypeDef sSlaveConfig;

  TIM_IC_InitTypeDef sConfigIC;

  htim9.Instance = TIM9;

  htim9.Init.Prescaler = 160;

  htim9.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim9.Init.Period = 0;

  htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  HAL_TIM_Base_Init(&htim9);

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

  HAL_TIM_ConfigClockSource(&htim9, &sClockSourceConfig);

  HAL_TIM_IC_Init(&htim9);

  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;

  sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;

  sSlaveConfig.TriggerPolarity = TIM_INPUTCHANNELPOLARITY_RISING;

  sSlaveConfig.TriggerPrescaler = TIM_ICPSC_DIV1;

  sSlaveConfig.TriggerFilter = 0;

  HAL_TIM_SlaveConfigSynchronization(&htim9, &sSlaveConfig);

  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;

  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;

  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;

  sConfigIC.ICFilter = 0;

  HAL_TIM_IC_ConfigChannel(&htim9, &sConfigIC, TIM_CHANNEL_1);

  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_BOTHEDGE;    /* User edited from Falling to Both */

  sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;

  HAL_TIM_IC_ConfigChannel(&htim9, &sConfigIC, TIM_CHANNEL_2);

}

Posted on August 22, 2017 at 09:15

Did you find the reason why? I think i've got the same problem