Continue to Site

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.

[SOLVED] Rotary Encoder Interfacing with PIC18F27j53 Micro-Controller??

Status
Not open for further replies.

xpress_embedo

Advanced Member level 4
Advanced Member level 4
Joined
Jul 5, 2011
Messages
1,154
Helped
161
Reputation
396
Reaction score
189
Trophy points
1,353
Location
India
embeddedlaboratory.blogspot.in
Activity points
10,591
Hello!! Everyone i am using this rotary encoder

https://in.element14.com/bourns/3315c-001-016l/encoder-16cycles/dp/9354026?Ntt=9354026

But it is not working properly
I am using PIC18F27j53 Micro-Controller

My RC6 pin is connected to Channel A and RC7 to Channel-B

Here is my Code, please help me what to do


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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Programmer :- Arun Sharma
 
Audio Controller Firmware for PIC18F27j53 Micro-Controller
 
Firmware Version -> v0.0
This version includes basic start up of project from roots.
First phase of development process
*/
#include <p18F27J53.h>
#include <delays.h>
 
/******************CONFIGURATION BITS****************/
#pragma config WDTEN = OFF          //WDT disabled (enabled by SWDTEN bit)
#pragma config PLLDIV = 5           //Divide by 5 (20 MHz oscillator input)
#pragma config STVREN = ON          //stack overflow/underflow reset enabled
#pragma config XINST = OFF          //Extended instruction set disabled
#pragma config CPUDIV = OSC1        //No CPU system clock divide
#pragma config CP0 = OFF            //Program memory is not code-protected
#pragma config OSC = HSPLL          //HS oscillator, PLL enabled, HSPLL used by USB
#pragma config FCMEN = OFF          //Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF           //Two-Speed Start-up disabled
#pragma config WDTPS = 32768        //1:32768
#pragma config DSWDTOSC = INTOSCREF //DSWDT uses INTOSC/INTRC as clock
#pragma config RTCOSC = T1OSCREF    //RTCC uses T1OSC/T1CKI as clock
#pragma config DSBOREN = OFF        //Zero-Power BOR disabled in Deep Sleep
#pragma config DSWDTEN = OFF        //Disabled
#pragma config DSWDTPS = 8192       //1:8,192 (8.5 seconds)
#pragma config IOL1WAY = OFF        //IOLOCK bit can be set and cleared
#pragma config MSSP7B_EN = MSK7     //7 Bit address masking
#pragma config WPFP = PAGE_1        //Write Protect Program Flash Page 0
#pragma config WPEND = PAGE_0       //Start protection at page 0
#pragma config WPCFG = OFF          //Write/Erase last page protect Disabled
#pragma config WPDIS = OFF          //WPFP[5:0], WPEND, and WPCFG bits ignored
#pragma config CFGPLLEN = OFF
/****************************************************/
 
 
/****************PIN DETAILS****************/
#define LCD_RS  LATAbits.LATA2
#define LCD_EN  LATAbits.LATA3
#define LCD_DATA LATB
#define LCD_CLEAR 0x01
#define LCD_CURSOR_OFF 0x06
#define FIRST_ROW 0x80
#define SECOND_ROW 0xC0
#define ENCODER_A PORTCbits.RC6
#define ENCODER_B PORTCbits.RC7
/********************************************/
 
 
/******************Function Prototypes*****************/
void Delay_ms(unsigned int time);
void Lcd4_Init(void);
void Lcd4_Cmd(unsigned char value);
void Lcd4_Write(unsigned char value);
void Lcd4_Write_Text(unsigned char msg[]);
unsigned char * CopyConstToRAM(unsigned char *dest, const rom unsigned char * src);
void Clear_Destination_Buffer(void);
void HIGH_ISR(void);
void LOW_ISR(void);
/***********************************************/
 
const rom unsigned char text[] = "ESSEL SHYAM!";
const rom unsigned char love[] = "PIC";
unsigned char ram_msg[16];
unsigned char character[] = {0,10,21,17,17,10,4,0};
 
unsigned int count = 0;
unsigned char flag = 0;
unsigned char time_A = 0, time_B = 0;
/******************ISR CODE*********************/
#pragma code high_vector = 0x08
void interrupt_at_high_vector(void)
{
    _asm
        GOTO HIGH_ISR
    _endasm
}
#pragma code
 
#pragma interrupt HIGH_ISR
void HIGH_ISR(void)
{
    if(INTCON3bits.INT1E & INTCON3bits.INT1IF)
    {
        if(ENCODER_B == 1)
        {
            if(count <= 999)
                count++;
                flag = 1;
        }
        else if(ENCODER_B == 0)
        {
            if(count > 0)
                count--;
                flag = 1;
        }
        //while(ENCODER_A == 0);    //Stay Here
        INTCON3bits.INT1F = 0;
    }
}
 
void main()
{
    unsigned char i;
    unsigned char ones,tens,hundreds;
    unsigned int temp;
    
    EECON2 = 0x55;
    EECON2 = 0xAA;
    PPSCONbits.IOLOCK = 0;      //Allow Remapping
    
    RPINR1 = 0x11;             //RP17 as External_Interrupt Pin
        
    EECON2 = 0x55;
    EECON2 = 0xAA;
    PPSCONbits.IOLOCK = 1;    
    
    
    Lcd4_Init();
    Lcd4_Cmd(LCD_CLEAR);
    Lcd4_Cmd(LCD_CURSOR_OFF);
    Lcd4_Cmd(LCD_CLEAR);
    CopyConstToRAM(ram_msg,text);
    Lcd4_Write_Text(ram_msg);
    Delay_ms(10);
    
    //Store Heart in CGRAM of LCD
    Lcd4_Cmd(64);
    for (i=0;i<=7;i++)
    {
        Lcd4_Write(character[i]);
    }
    
    Lcd4_Cmd(SECOND_ROW);
    Lcd4_Write('I');
    Lcd4_Write(' ');    
    Lcd4_Write(0);      //Custom Character
    Lcd4_Write(' ');
    CopyConstToRAM(ram_msg,love);
    Lcd4_Write_Text(ram_msg);
    
    TRISCbits.TRISC6 = 1;       //Rotary Encoder_A as Input
    TRISCbits.TRISC7 = 1;       //Rotary Encoder_B as Input
    
    //Enable Interrupts
    INTCON3bits.INT1E = 1;
    INTCON3bits.INT1IP = 1;     //High Priority
    INTCON2bits.INTEDG1 = 0;    //Interrupt on Falling Edge
    INTCON3bits.INT1F = 0;    
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;
    INTCON3bits.INT1F = 0;
    while(1)
    {
        if(flag != 0)
        {
            Lcd4_Cmd(LCD_CLEAR);
            
            temp = count;
            
            ones = temp%10;
            temp = temp/10;
            tens = temp%10;
            temp = temp/10;
            hundreds = temp%10;
            
            Lcd4_Write(hundreds | 0x30);
            Lcd4_Write(tens | 0x30);
            Lcd4_Write(ones | 0x30);
                        
            flag = 0;
        }
    }
}
 
/**************Function Definitions*************/
void Delay_ms(unsigned int time)
{
    unsigned int first;
    unsigned int second;
    for(first=0;first<time;first++)
    {
        for(second=0;second<650;second++)
        {
            Delay1TCY();
        }
    }
}
void Lcd4_Cmd(unsigned char value)
{
    LCD_RS = 0;
    LCD_DATA = ((value>>4) & 0x0F);
    LCD_EN = 1;
    Delay_ms(1);
    LCD_EN = 0;
    LCD_DATA = (value & 0x0F);
    LCD_EN = 1;
    Delay_ms(1);
    LCD_EN = 0;
    Delay_ms(1);
}
void Lcd4_Write(unsigned char value)
{
    LCD_RS = 1;
    LCD_DATA = ((value>>4) & 0x0F);
    LCD_EN = 1;
    Delay_ms(1);
    LCD_EN = 0;
    LCD_DATA = (value & 0x0F);
    LCD_EN = 1;
    Delay_ms(1);
    LCD_EN = 0;
    Delay_ms(1);
}
 
void Lcd4_Write_Text(unsigned char msg[])
{
    while(*msg)
    {
        Lcd4_Write(*msg);
        msg++;
    }
}
 
void Lcd4_Init(void)
{
    TRISB = 0x00;
    TRISAbits.TRISA2 = 0;
    TRISAbits.TRISA3 = 0;
        
    LCD_RS = 0;     //Command Register
    LCD_EN = 0;
    
    LCD_DATA = 0x03;
    
    LCD_EN = 1;
    Delay_ms(1);
    LCD_EN = 0;
    Delay_ms(1);
    
    LCD_EN = 1;
    Delay_ms(1);
    LCD_EN = 0;
    Delay_ms(1);
    
    LCD_EN = 1;
    Delay_ms(1);
    LCD_EN = 0;
    Delay_ms(1);
    
    Delay_ms(1);
    LCD_DATA = 0x02;    // Four bit mode
    LCD_EN = 1;
    Delay_ms(1);
    LCD_EN = 0;
    Delay_ms(1);   
    
    Lcd4_Cmd(0x28);
    Lcd4_Cmd(0x0C);
    Lcd4_Cmd(0x01);
    Lcd4_Cmd(0x06);
}
unsigned char * CopyConstToRAM(unsigned char *dest, const rom unsigned char * src)
{
    Clear_Destination_Buffer();
    for(;*dest++ = *src++;)
        ;
    return dest;
}
void Clear_Destination_Buffer()
{
    unsigned char i;
    for(i=0;i<16;i++)
    {
        ram_msg[i] = 0;
    }
}
/***********************************************/




On oscilloscope pulses are perfect,
Both lines are pulled up by 10k resistor

Please help.
Waiting for reply
 

To develop better debugging skills, start with a simpler program. Get rid of the RAM writing stuff and get rid of the LCD stuff. You want the program down to the simplest form where it works or does not work.

Then connect an LED to one pin, turn it off in your initialize routine, and turn it on in your interrupt routine. This will tell you if your interrupts are working. Once this works you can add in the additional things one and a time.
 

It's a 2 bit quadrature code,output encoder

I used to have a working code for this type of encoder but it was for PIC16F87xA series, if you are interested I 'll post the code once I get back home.

You could modify it for 18F series, but do understand it will be useful only if you understand pic interrupts and make the transition from16F to 18F
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top