cancel
Showing results for 
Search instead for 
Did you mean: 

The code for STM32F301 encoder interface does not work

Jiannong Zhou
Associate II
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
2 REPLIES 2
Imen.D
ST Employee
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

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Jiannong Zhou
Associate II
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