/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 168 MHz */ SystemClock_Config(); /* Configure LED1 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED3); /* Initialize TIM3 to emulate a quadrature encoder outputs */ Init_TIM_Emulator(&EmulatorHandle); /* -1- Initialize TIM1 to handle the encoder sensor */ /* Initialize TIM1 peripheral as follow: + Period = 65535 + Prescaler = 0 + ClockDivision = 0 + Counter direction = Up */ Encoder_Handle.Instance = TIM1; Encoder_Handle.Init.Period = 65535; Encoder_Handle.Init.Prescaler = 0; Encoder_Handle.Init.ClockDivision = 0; Encoder_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; Encoder_Handle.Init.RepetitionCounter = 0; sEncoderConfig.EncoderMode = TIM_ENCODERMODE_TI12; sEncoderConfig.IC1Polarity = TIM_ICPOLARITY_RISING; sEncoderConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; sEncoderConfig.IC1Prescaler = TIM_ICPSC_DIV1; sEncoderConfig.IC1Filter = 0; sEncoderConfig.IC2Polarity = TIM_ICPOLARITY_RISING; sEncoderConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; sEncoderConfig.IC2Prescaler = TIM_ICPSC_DIV1; sEncoderConfig.IC2Filter = 0; if(HAL_TIM_Encoder_Init(&Encoder_Handle, &sEncoderConfig) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Start the encoder interface */ HAL_TIM_Encoder_Start(&Encoder_Handle, TIM_CHANNEL_ALL); /* Infinite loop */ while (1) { /* Step 1: */ /* Emulate a Forward direction */ Emulate_Forward_Direction(&EmulatorHandle); /* Insert 1s delay */ HAL_Delay(1000); /* Get the current direction */ uwDirection = __HAL_TIM_IS_TIM_COUNTING_DOWN(&Encoder_Handle); /* Step 2: */ /* Emulate a Backward direction */ Emulate_Backward_Direction(&EmulatorHandle); /* Insert 1s delay */ HAL_Delay(1000); /* Get the current direction */ uwDirection = __HAL_TIM_IS_TIM_COUNTING_DOWN(&Encoder_Handle); } }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); MX_TIM1_Init(); MX_TIM3_Init(); HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL); HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_1); HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_2); cnt1 = __HAL_TIM_GET_COUNTER(&htim3); tick = HAL_GetTick(); while (1) { if (HAL_GetTick() - tick > 1000L) { cnt2 = __HAL_TIM_GET_COUNTER(&htim3); if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&htim3)) { if (cnt2 < cnt1) /* Check for counter underflow */ diff = cnt1 - cnt2; else diff = (65535 - cnt2) + cnt1; } else { if (cnt2 > cnt1) /* Check for counter overflow */ diff = cnt2 - cnt1; else diff = (65535 - cnt1) + cnt2; } sprintf(msg, "Difference: %d\r\n", diff); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); speed = ((diff / PULSES_PER_REVOLUTION) / 60); /* If the first three bits of SMCR register are set to 0x3 * then the timer is set in X4 mode (TIM_ENCODERMODE_TI12) * and we need to divide the pulses counter by two, because * they include the pulses for both the channels */ if ((TIM3->SMCR & 0x3) == 0x3) speed /= 2; sprintf(msg, "Speed: %d RPM\r\n", speed); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); dir = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim3); sprintf(msg, "Direction: %d\r\n", dir); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); tick = HAL_GetTick(); cnt1 = __HAL_TIM_GET_COUNTER(&htim3); } if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_RESET) { /* Invert rotation by swapping CH1 and CH2 CCR value */ tim1_ch1_pulse = __HAL_TIM_GET_COMPARE(&htim1, TIM_CHANNEL_1); tim1_ch2_pulse = __HAL_TIM_GET_COMPARE(&htim1, TIM_CHANNEL_2); __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, tim1_ch2_pulse); __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, tim1_ch1_pulse); } } }