I'm trying to interface a rotary encoder with my pic18f4550. Just trying to get it to increase/decrease the variable output and then display that value through the LATD pins.
The code does not seem to be working at all, and was just wondering if I could get some help from you guys as to where I'm going wrong.
#include <xc.h> #include "config.h" #define _XTAL_FREQ 8000000 unsigned int output; void main(void) { OSCCON = 0b01110010; // Oscillator setup TRISB = 0b00110000; //Set RB4 & RB5 as input TRISD = 0x00; //All D pins set as output //Port B interrupts INTCONbits.RBIE = 1; //Enable port B interrupts; interrupt flag is not cleared so interrupt is triggered instantly, allowing us to record initial value INTCONbits.GIE = 1; //Enable general interrupts //Port B pull-ups INTCON2bits.RBPU = 0; while(1) { if(INTCONbits.RBIF==1) { static unsigned char prevState = 0xFF; unsigned char state = (PORTBbits.RB4 | PORTBbits.RB5 << 1); //Get value 0-3 from rotary encoder bits if(prevState != 0xFF)//If this is not the first time we enter the interrupt, process the gray codes { if(((prevState == 0b00) && (state == 0b01)) //Turn counterclockwise || ((prevState == 0b01) && (state == 0b11)) || ((prevState == 0b11) && (state == 0b10)) || ((prevState == 0b10) && (state == 0b00))) { output--; } else if(((prevState == 0b00) && (state == 0b10)) //Turn clockwise || ((prevState == 0b10 && state == 0b11)) || ((prevState == 0b11 && state == 0b01)) || ((prevState == 0b01 && state == 0b00))) { output++; } } prevState = state; //Save previous port b state. INTCONbits.RBIF=0; //Clear port B interrupts flag } LATD = output; //Display value on LED's on D pins } } 