void test_Encoders(void) { ssd1306Init(0); ssd1306ClearScreen(); ssd1306Refresh(); Encoders_Init(); while(1) { ssd1306ClearScreen(); // ssd1306PrintInt(0, 6, "REV = ", toto, &Font_3x6); // ssd1306PrintInt(0, 13, "CNT = ", (&htim1)->Instance->CNT, &Font_3x6); ssd1306PrintInt(0, 7, "L_REV = ", left_encoder.nb_revolutions, &Font_3x6); ssd1306PrintInt(0, 14, "L_CNT = ", __HAL_TIM_GetCounter(&htim1), &Font_3x6); ssd1306PrintInt(0, 21, "L_DIR = ", __HAL_TIM_DIRECTION_STATUS(&htim1), &Font_3x6); ssd1306PrintInt(0, 35, "R_REV = ", right_encoder.nb_revolutions, &Font_3x6); ssd1306PrintInt(0, 42, "R_CNT = ", __HAL_TIM_GetCounter(&htim3), &Font_3x6); ssd1306PrintInt(0, 49, "R_DIR = ", __HAL_TIM_DIRECTION_STATUS(&htim3), &Font_3x6); ssd1306Refresh(); HAL_Delay(10); } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 180 MHz */ SystemClock_Config(); /* Initialize LED1 & 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_DIRECTION_STATUS(&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_DIRECTION_STATUS(&Encoder_Handle); } }