milan.rajik
Banned
Please provide a sample code for this rotary encoder. You can edit the below code. Also tell me if my connections are correct.
https://www.banggood.com/5Pcs-5V-KY-040-Rotary-Encoder-Module-For-Arduino-AVR-PIC-p-951151.html
I have connected DT and CLK pins to RB0 and RB1 and have connected GND to GND and + to 5V. But the code is not working.
Here is my code.
https://www.banggood.com/5Pcs-5V-KY-040-Rotary-Encoder-Module-For-Arduino-AVR-PIC-p-951151.html
I have connected DT and CLK pins to RB0 and RB1 and have connected GND to GND and + to 5V. But the code is not working.
Here is my code.
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 76 77 // LCD module connections sbit LCD_RS at LATB4_bit; sbit LCD_EN at LATB5_bit; sbit LCD_D4 at LATB0_bit; sbit LCD_D5 at LATB1_bit; sbit LCD_D6 at LATB2_bit; sbit LCD_D7 at LATB3_bit; sbit LCD_RS_Direction at TRISB4_bit; sbit LCD_EN_Direction at TRISB5_bit; sbit LCD_D4_Direction at TRISB0_bit; sbit LCD_D5_Direction at TRISB1_bit; sbit LCD_D6_Direction at TRISB2_bit; sbit LCD_D7_Direction at TRISB3_bit; // End LCD module connections char value = 0; char new_encoder = 0; char last_encoder = 0; char ledSelect = 0; void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ANSELA = 0x01; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC0; TRISB = 0x03; TRISC = 0x00; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; Delay_ms(200); LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,"Rotary Encoder"); while(1) { new_encoder = (PORTB & 0x03); Delay_ms(50); // keep only 2 bits if(new_encoder != last_encoder) // if encoder has changed { if(new_encoder == 0x00)LCD_Out(1,1,"Zero "); else if(new_encoder == 0x01)LCD_Out(1,1,"One "); else if(new_encoder == 0x03)LCD_Out(1,1,"Three "); else if(new_encoder == 0x02)LCD_Out(1,1,"Two "); last_encoder = new_encoder; // save for next time } } }