cancel
Showing results for 
Search instead for 
Did you mean: 

Timer Encoder Interface

adriano
Associate II
Posted on December 12, 2014 at 15:54

Hi, 

I'm trying to read an encoder using the encoder interface provided by the STM32F3Discovery board. I've set up the peripherals using CubeMX and connected the encoder to the input pins (tried with timer 1 and 2), but the CNT register doesn't seem to get updated.

I tried declaring the same pins as GPIO (Input mode) and blinking leds when the input is 1, and they seem to work properly (so I guess the encoder is fine).

Can anybody tell me what's going on? Do I need to specificate other parameters/enable interrupts/DMA/other?

Thanks

#cubemx #i-can''t-help-you-with-that
3 REPLIES 3
Posted on December 12, 2014 at 17:00

STM32F3-Disco Encoder example using the Standard Library at the end of [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F3%20Discovery%20Counter&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=525]this thread

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
adriano
Associate II
Posted on December 13, 2014 at 13:01

Thank you Clive but sadly I couldn't get a solution out of that. It looks like the parameters are set correctly in my code (except for a different mapping of the pins made automatically by STCubeMX) but nevertheless it doesn't count.

I'll make some other tests and see if I can figure out the problem; in the meanwhile, I'll paste here the init code:

 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_TI12;

  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 HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef* htim_encoder)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(htim_encoder->Instance==TIM1)

  {

  [...]

  }

  else if(htim_encoder->Instance==TIM2)

  {

  /* USER CODE BEGIN TIM2_MspInit 0 */

  /* USER CODE END TIM2_MspInit 0 */

    /* Peripheral clock enable */

    __TIM2_CLK_ENABLE();

  

    /**TIM2 GPIO Configuration    

    PA1     ------> TIM2_CH2

    PA5     ------> TIM2_CH1 

    */

    GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_5;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

[...]

}

And in main function:

 int main(void)

{

  /* USER CODE BEGIN 1 */

extern USBD_HandleTypeDef hUsbDeviceFS;

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  

  MX_TIM2_Init();

  USB_reset();

  MX_USB_DEVICE_Init();

  /* USER CODE BEGIN 2 */

char outbuf[40]; //buffer for output communication

int count2 = 0;

  /* USER CODE END 2 */

  /* USER CODE BEGIN 3 */

while(UserButton_GetState() != 1); //wait until the button is pressed

sprintf(outbuf, ''test message\n'');

VCP_PutStr(outbuf);

  /* Infinite loop */

  while (1)

  {

count2 = COUNT2;  //COUNT 2 has been defined as TIM2->CNT

sprintf(outbuf, ''\nEncoder 2: %d'', count2);

VCP_PutStr(outbuf);

HAL_GPIO_TogglePin (GPIOE, GPIO_PIN_9); 

HAL_Delay(250);

}

  /* USER CODE END 3 */

}

adriano
Associate II
Posted on December 13, 2014 at 13:13

Probably I found something: I tried to read the pins (PA1, PA5) as inputs and to light up leds depending on their state, and nothing happens. Do you know if that's normal in encoder mode?