Skip to main content
Jiannong Zhou
Associate III
May 28, 2017
Question

The code for STM32F301 encoder interface does not work

  • May 28, 2017
  • 2 replies
  • 683 views
Posted on May 28, 2017 at 11:25

Dear All friends,

I am a new developer to code the encoder interface of STM32F301 TIM2, based on STM32CubeMX. the code generated by STM32CubeMx does not work. I followed the instruction of TIM2 encoder mode. Please see the code below,

void SystemClock_Config(void);

int16_t counter;

int main(void)

{

/* 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_TIM1_Init();

MX_TIM2_Init();

MX_TIM16_Init();

MX_TIM17_Init();

MX_USART1_UART_Init();

HAL_TIM_Encoder_Start_IT(&htim2,TIM_CHANNEL_1 + TIM_CHANNEL_2);

TIM2->CNT = 0;

   while (1)

   {

      int16_t count1;

      count1 = TIM2->CNT & 0x0000ffff;

   }

}

I got some problems,

1. After complied, got error message 'Warning[Pe550]: variable 'count1' was set but never used'. I saw some samples code use TIM2->CNT to read the number of encoder. but in my application, got the compiler warning and the variable is not available.

2. the interrupt of the code is working. How to read the counter of encoder?

3. There is no sample code for TIMER encoder mode that is generated by STM32CubeMX. Do any experts have any sample code with STM32CubeMX?

Thanks in advance!

Jiannong

#stm32f3-timer-encoder-mode
This topic has been closed for replies.

2 replies

Technical Moderator
May 29, 2017
Posted on May 29, 2017 at 11:11

Hi

Jiannong.Zhou

,

Please make sure that you configure the GPIO settings correctly and

provide your

Timer

configuration for more check.

The STM32CubeF3 does not contains an encoder example.

But,

you can inspire from anotherexample with STM32Cube package to develop your application, like STM32CubeF4 which has the 'TIM_Encoder' example

available at this path:

STM32Cube_FW_F4_V1.0\Projects\STM324x9I_EVAL\Examples\TIM\TIM_Encoder

Imen

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
Jiannong Zhou
Associate III
May 29, 2017
Posted on May 29, 2017 at 15:56

Hi Imen,

thank you very much for your comment and suggestion. I will try it out.

regards,

Jiannong