cancel
Showing results for 
Search instead for 
Did you mean: 

Confused about STM32F103 Encoder Interrupt

Hrishikesh
Senior
Posted on July 05, 2018 at 23:44

I'm trying to set an interrupt to occur each time my rotary encoder detents, however this doesn't seem to be working.What am I missing?

This is how my timer function is setup:

#define ENCODER_PPR 20
#define ENCODER_STEP 2
#define ENCODER_COUNT ENCODER_PPR / ENCODER_STEP
#define ENCODER_FILTER 15
void MX_TIM1_Init(void)
{
 TIM_Encoder_InitTypeDef sConfig;
 TIM_MasterConfigTypeDef sMasterConfig;
 htim1.Instance = TIM1;
 htim1.Init.Prescaler = 0;
 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim1.Init.Period = ENCODER_COUNT-1;
 htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim1.Init.RepetitionCounter = 0;
 htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
 sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
 sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
 sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
 sConfig.IC1Filter = ENCODER_FILTER;
 sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
 sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
 sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
 sConfig.IC2Filter = ENCODER_FILTER;
 if (HAL_TIM_Encoder_Init(&htim1, &sConfig) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

This is what the main looks like:

int main(void)
{
 /* USER CODE BEGIN 1 */
 /* 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();
 /* USER CODE BEGIN 2 */
 HD44780_Init(20, 4);
 HD44780_CreateChar(0, &g_custom_char[0]);
 HD44780_Clear();
 HAL_TIM_Encoder_Start_IT(&htim1, TIM_CHANNEL_ALL);
 __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_CC1);
 HAL_TIM_Base_Start_IT(&htim1);
 /* USER CODE END 2 */
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */
 /* USER CODE BEGIN 3 */

 }
 /* USER CODE END 3 */
}
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

And this is the callback function. For now its supposed to write to the LCD every time its called.

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if (htim == &htim1) {
HD44780_Puts(0, 0, 'Triggered!');
 }
}�?�?�?�?�?�?

#stm32-interrupt #timer-interrupts #quadrature-encoder #interrupts
0 REPLIES 0