venkates2218
Full Member level 6

Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Try this example (...) This code is for 3 pin rotary encoder.
Here is an example for OP's Rotary Encoder. I will also try it.
https://circuitdigest.com/microcontroller-projects/avr-rotary-encoder-interfacing
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 sbit ENCA at RC0_bit; sbit ENCB at RA2_bit; sbit ENC_SW at RB0_bit; sbit ENCA_direction at TRISC0_bit; sbit ENCB_direction at TRISA2_bit; sbit ENC_SW_direction at TRISB0_bit; unsigned int Led_State = 0x01; char ENCA_bit = 0,ENCB_bit = 0; char ENC_Button_State = 0; // ISR void Interrupt(){ ENC_Button_State++; // set new ENC button state if (ENC_Button_State == 4) ENC_Button_State = 0; // check for interrupt if(INT0IF_bit){ // clear Interrupt flag // check ENC button state switch (ENC_Button_State){ case 0 : LATD = Led_State = 0b00000001; // One LED are ON break; case 1 : LATD = Led_State = 0b00000101; // Two LEDs are ON break; case 2 : LATD = Led_State = 0b00010101; // Four LEDs are ON break; case 3 : LATD = Led_State = 0b01010101; // Eight LEDs are ON break; default : break; } INT0IF_bit = 0; } } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ANSELA = 0; // Configure PORTA pins as digital ANSELB = 0; // Configure PORTB pins as digital ANSELC = 0; ANSELD = 0; // Configure PORTC pins as digital TRISD = 0x00; LATD = 0x00; ENCA_direction = 1; // Configure RC0 as input ENCB_direction = 1; // Configure RA2 as input ENC_SW_direction = 1; // Configure RB0 as input INT0IE_bit = 1 ; // Enable INTE interrupt on RB0 GIE_bit = 1 ; // Enable Global Interrupt while(1) { if((ENCA_bit != RC0_bit)||(ENCB_bit != RA2_bit)){ // If logic state on pin A or pin B change // Compare last logic state of pin B and new logick state on pin A if(ENCB_bit|RC0_bit==1){ // If ENCB_old OR ENCA_new is 1 Led_State = (Led_State << 1) | (Led_State >> 7); } if(ENCA_bit|RA2_bit==1){ // If ENCA_old OR ENCB_new is 1 Led_State = (Led_State >> 1) | (Led_State << 7); } LATD = Led_State; ENCA_bit = RC0_bit ; // Set new value of ENCA_bit ENCB_bit = RA2_bit ; // Set new value of ENCB_bit } } }
For a simlified design this will work, but not for a reliable industrial design.A rising edge as count clock and B as up/down signal.
I experimented a little and this code works fine
mikroC PRO PIC Code
What is sequential port reading ? Polling ?
if(ENCB_bit|RC0_bit==1){ // If ENCB_old OR ENCA_new is 1
// count up
}
if(ENCA_bit|RA2_bit==1){ // If ENCA_old OR ENCB_new is 1
// count down
}
if(ENCB_bit^RC0_bit){ // If ENCB_old != ENCA_new
// count up
}
if(ENCA_bit^RA2_bit){ // If ENCA_old != ENCB_new
// count down
}
if(ENCB_bit|RC0_bit==1){ // If ENCB_old OR ENCA_new is 1
if((ENCB_bit|RC0_bit)==1){ // If ENCB_old OR ENCA_new is 1