AnsweredAssumed Answered

The code for STM32F301 encoder interface  does not work

Question asked by Jiannong Zhou on May 28, 2017
Latest reply on May 29, 2017 by Jiannong Zhou

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

Outcomes