Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Please provide a sample code for this rotary encoder.

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
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.


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
           }
     }
}

 

Easyrider83

Advanced Member level 5
Joined
Oct 11, 2011
Messages
1,608
Helped
373
Reputation
746
Reaction score
361
Trophy points
1,363
Location
Tallinn, Estonia
Activity points
8,577
ENCA and ENCB to GPIO with internal/external pull-up. Also, some capacitors 100p-1n will be needed to prevent noise. Interrupt by ENCA fall down, reading state of ENCB. If 1, rotating cw, if 0, rotating ccw. That's all. Very easy.
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
Thank you EasyRider but in the above mentioned encoder what pins I have to use for signals ? DT and CLK ? I am using them SW is I think for press detection. But neither is working. The module has the required resistors and capacitors.
 

Easyrider83

Advanced Member level 5
Joined
Oct 11, 2011
Messages
1,608
Helped
373
Reputation
746
Reaction score
361
Trophy points
1,363
Location
Tallinn, Estonia
Activity points
8,577
9017255000_1432278306.png
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top