STM32 HAL advanced timer orthogonal encoding mode case
STM32 HAL advanced timer orthogonal encoding mode case
- Based on the stm32F030RBT6 microcontroller, it uses advanced timer 1 and encoder mode to test the EC11 encoder.
-
EC11 test results:
-
The STM32 timer encoder has 3 mapping modes:
- ?The above mode 3 is used this time. Every time the EC11 encoder rotates one small division, the TIM1 timer can capture 4 pulse signals. That is, the timer captures 4 pulse signals, which means that the encoder rotates one small division.
- EC11 encoder schematic diagram
STM32CubeMX configuration
-
Enable the advanced timer TIM1 and configure the encoder mode.
-
Parameter configuration
-
Encoder pin configuration:
-
EC11 encoder button pin configuration
-
Interrupt configuration
Function code implementation part
- Interrupt callback part
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim) {<!-- --> if(htim == & amp;htim1) {<!-- --> __HAL_TIM_CLEAR_IT(htim, TIM_FLAG_TRIGGER);//Clear the interrupt flag bit if(__HAL_TIM_GET_FLAG(htim, TIM_CHANNEL_1) != RESET) {<!-- --> Trg + + ; } if(__HAL_TIM_GET_FLAG(htim, TIM_CHANNEL_2) != RESET) {<!-- --> Trg + + ; } if(( + + Trg)==4) {<!-- --> Trg =0; CaptureNum = __HAL_TIM_GET_COUNTER( & amp;htim1) / 4; } } } /** * @brief update overflow callback function * @retval None */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) {<!-- --> __HAL_TIM_CLEAR_IT(htim, TIM_FLAG_UPDATE);//Clear the update interrupt flag bit } void EXTI4_15_IRQHandler(void) {<!-- --> /* USER CODE BEGIN EXTI4_15_IRQn 0 */ /* USER CODE END EXTI4_15_IRQn 0 */ HAL_GPIO_EXTI_IRQHandler(EC11M_Pin); /* USER CODE BEGIN EXTI4_15_IRQn 1 */ //Clear the interrupt flag bit __HAL_GPIO_EXTI_CLEAR_IT(EC11M_Pin); //Call external interrupt callback function HAL_GPIO_EXTI_Callback(EC11M_Pin); /* USER CODE END EXTI4_15_IRQn 1 */ } //Rewrite the external interrupt callback function EC11 key to clear the count value void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {<!-- --> /*Judge whether it is an interrupt pin*/ if(GPIO_Pin == EC11M_Pin) {<!-- --> __HAL_TIM_SET_COUNTER( & amp;htim1, 0); } \t }
- Main function code
int main(void) {<!-- --> /* USER CODE BEGIN 1 */ uint32_t TimerUART; uint32_t Temp; /* USER CODE END 1 */ /* MCU Configuration------------------------------------------------- ----------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_TIM1_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ // HAL_TIM_Encoder_Start( & amp;htim1, TIM_CHANNEL_ALL); // Query method __HAL_TIM_SET_COUNTER( & amp;htim1, 0); __HAL_TIM_ENABLE_IT( & amp;htim1, TIM_IT_UPDATE); /* Clear the interrupt flag bit */ __HAL_TIM_CLEAR_IT( & amp;htim1, TIM_IT_UPDATE); HAL_TIM_Encoder_Start_IT( & amp;htim1, TIM_CHANNEL_ALL);//Interrupt mode TimerUART = HAL_GetTick(); printf("STM32F030 SysClockFreq:%d \r\\ ", HAL_RCC_GetSysClockFreq()); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while(1) {<!-- --> /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ if((HAL_GetTick() - TimerUART) > 1000) {<!-- --> HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); TimerUART = HAL_GetTick(); } if(CaptureNum != Temp) {<!-- --> Temp = CaptureNum; HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin); __HAL_TIM_IS_TIM_COUNTING_DOWN( & amp;htim1)==0?printf("Forward\tCaptureNum:%d\r\\ ",CaptureNum):printf("Reverse\tCaptureNum:%d\r \\ ",CaptureNum); } } /* USER CODE END 3 */ }
Test project (mode three)
Link: https://pan.baidu.com/s/1idS6b4bA5YZXg54joM1WsA Extraction code: a0eg
?Introduction to using mode one and mode two
-
The usage methods of encoding mode 1 and encoding mode 2 are the same, except that the trigger signal channels are different. Every time the EC11 encoder rotates one small division, the trigger timer counts 2 pulses.
-
The timer input capture interrupt callback function can be executed and adjusted.
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim) {<!-- --> if(htim == & amp;htim1) {<!-- --> __HAL_TIM_CLEAR_IT(htim, TIM_FLAG_TRIGGER);//Clear the interrupt flag bit if(__HAL_TIM_GET_FLAG(htim, TIM_CHANNEL_1) != RESET) {<!-- --> Trg + + ; } // if(__HAL_TIM_GET_FLAG(htim, TIM_CHANNEL_2) != RESET) // {<!-- --> // Trg + + ; // } if(( + + Trg)==2) {<!-- --> Trg = 0; CaptureNum = __HAL_TIM_GET_COUNTER( & amp;htim1) / 2; } } }
- This article is only used as a summary of personal study and exploration knowledge, and does not serve as a theoretical basis for others or citations. Due to limited knowledge, errors or omissions will inevitably occur, and you are welcome to correct me.