Hi All,
I am using a PIC16F887 with a rotary encoder to display the selected menu option on the LCD as well as to print what option was selected by the user when they press on the encoder button. The interrupt is triggering when I rotate the encoder and the menu options show up properly, but the button does not do anything. It is not the wiring at fault, because if I move the switch case into the main loop, the button works. It is only when the switch case for the button is inside the interrupt handler it doesn't do anything.
I hooked up the oscilloscope to the RB2 pin and I see a good signal when the button is pressed. To note that I have the signal from the button coming from an output of a Schmitt trigger for de-bouncing. Is that a problem somehow when using interrupts? (I'd think not since it's working in the main loop)
What am I missing here?
#include <xc.h>
#define ENC_A PORTBbits.RB0
#define ENC_B PORTBbits.RB1
#define ENC_C PORTBbits.RB2
#define NUM_OPTIONS 4
const char* OPTIONS[NUM_OPTIONS] = {
"Option 1",
"Option 2",
"Option 3",
"Option 4"
};
int menu_index = 0;
int menu_changed = 0;
void main() {
TRISB = 0b00000111;
ANSEL = 0x0;
ANSELH = 0x0;
INTCONbits.INTE = 1;
OPTION_REGbits.INTEDG = 0;
INTCONbits.GIE = 1;
while (1) {
if (menu_changed) {
LCD_Cmd(LCD_CLEAR);
LCD_Goto(1, 1);
LCD_Print(OPTIONS[menu_index]);
menu_changed = 0;
}
}
}
void __interrupt() isr() {
if (INTCONbits.INT0IF) {
int dir = 0;
if (ENC_A == ENC_B) {
dir = 1;
} else {
dir = -1;
}
menu_index += dir;
if (menu_index < 0) {
menu_index = NUM_OPTIONS - 1;
} else if (menu_index >= NUM_OPTIONS) {
menu_index = 0;
}
if (ENC_C) { // button connected to a Schmitt trigger
switch (menu_index) {
case 0:
LCD_Goto(1, 2);
LCD_Print("Option 1 selected");
break;
case 1:
LCD_Goto(1, 2);
LCD_Print("Option 2 selected");
break;
case 2:
LCD_Goto(1, 2);
LCD_Print("Option 3 selected");
break;
case 3:
LCD_Goto(1, 2);
LCD_Print("Option 4 selected");
break;
}
}
menu_changed = 1;
INTCONbits.INT0IF = 0;
}
}