Hi everyone,
I wrote some simple code that handles an encoder. It checks for the encoder pushbutton and to one of the phases with interrupts.
the problem is that for some reason when EXTI2 is fired (encoder pushbutton), ALSO EXTI1 is triggered. And when I try and trigger EXTI1, nothing happends.
What did I do wrong? I really cannot see anithing weird in the code that explains this behavior. All of the tests were conducted in debug mode by phisically connecting the pins to ground. I can see by peeking in the GPIO IDR registers that the signal arrives correctly to the relative pins.
Can you please help me? Thanks!
#include "stm32f303x8.h"
/*
This code is used to manage the encoder knob. It is usage-independent,
meaning that it only sense if it is turned and update a counter variable
accordingly, decreasing or increasing it based on the turn direction.
It also manages the pushbutton embedded into the encoder.
D8 (PF1): Phase A used as interrupt EXTI1 reference
D3 (PB0): Phase B
A7 (PA2): Pushbutton EXTI2
*/
extern int encoderCounter;
void EXTI1Config(){
RCC->AHBENR |= RCC_AHBENR_GPIOFEN; // Enable GPIOF clock
RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // Enable GPIOB clock
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // Enable SysConfig clock
GPIOF->MODER &= ~(1<<2); // GPIOF1 in input mode
GPIOF->MODER &= ~(1<<3);
GPIOF->PUPDR |= 1<<2; // GPIOF1 internal pullup enable
GPIOF->PUPDR &= ~(1<<3);
SYSCFG->EXTICR[0] |= 1<<0 | 1<<2; // EXTI1 assigned to GPIOF0
EXTI->IMR |= 1<<1; // EXTI1 unmasked
EXTI->FTSR |= 1<<1; // EXTI1 sensitive to falling edge
GPIOB->MODER &= ~(1<<0); // GPIOB0 in input mode
GPIOB->MODER &= ~(1<<1);
GPIOB->PUPDR |= 1<<0; // GPIOB0 internal pullup enable
GPIOB->PUPDR &= ~(1<<1);
NVIC_EnableIRQ(EXTI1_IRQn);
//NVIC_SetPriority(EXTI1_IRQn, 2);
}
void EXTI2Config(){
RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // Enable GPIOA clock
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // Enable SysConfig clock
GPIOA->MODER &= ~(1<<4); // GPIOA2 in input mode
GPIOA->MODER &= ~(1<<5);
GPIOA->PUPDR |= 1<<4; // GPIOA2 internal pullup enable
GPIOA->PUPDR &= ~(1<<5);
SYSCFG->EXTICR[0] |= 0; // EXTI2 assigned to GPIOA2 by default
EXTI->IMR |= 1<<2; // EXTI2 unmasked
EXTI->FTSR |= 1<<2; // EXTI2 sensitive to falling edge
NVIC_EnableIRQ(EXTI2_TSC_IRQn);
//NVIC_SetPriority(EXTI2_TSC_IRQn, 3);
}
void EXTI1_IRQHandler(){
EXTI->PR |= 1<<1; // Clear pending EXTI1 interrupt flag
if(GPIOB->IDR & (1<<0)) // Depending on the direction of the rotation
encoderCounter++; // it increments
else encoderCounter--; // or decrements a variable
}
void EXTI2_TSC_IRQHandler(){
EXTI->PR |= 1<<2; // Clear pending EXTI2 interrupt flag
encoderCounter = 0; // Pushing the button resets the variable
}