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.

Help me fix codes for PIC16F877A rotary encoder with interrupt

Status
Not open for further replies.

aduri

Newbie level 4
Joined
Jul 9, 2006
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,329
Hello,
I tried these 2 programs but they don't work very well.
They miss some pulses during the count.
Can anybody help me?

Thanks



Code:
'* 
' * Project name: Rotary encoder 
' * Description: Rotary encoder with interrupt 
' * Test configuration: 
'     board: My Board Aduri 
'     MCU:             PIC16F877A 
'     Oscillator:      HS, 20.000 MHz 
'     Ingressi   :     A su RB4, B su RB1 
'     SW:              mikroBasic v5.0 or higher 
' * NOTES: 
'     LCD su PORTD, trigger with CD4093 
' * 

program Rotary_encoder 
dim temp as byte 
dim txt as string[3] 

sub procedure interrupt     ' Interrupt service routine 
  If PORTB.4 = 1 then 

    else 
      If PORTB.1 = 1 then 
      temp = temp - 1 
        else 
      temp = temp + 1 
      end if 
  end if 
delay_ms(25)                     'delay 
INTCON.RBIF = 0             ' interrupt flag disable 
end sub 

main: 
  LCD_init(PORTD)        'LCD on portD 
  TRISB = %11111111      'PORTB.1 and .4 are inputs 
  INTCON = %10001000     ' Enable interrupt on change 

while 1=1            ' loop 
delay_ms(100) 
bytetostr(temp,txt) 
 LCD_out(1,1,"Nr pulses:") 
LCD_out(2,1,txt) 
wend 

end.


and the follow:


Code:
program encoder_A 
'     MCU:             PIC16F877A 
'     Dev.Board:       Myboard 
'     Oscillator:      HS, 20.000 MHz 
'     Ext. Modules:    trigger with CD4093, Lcd_2x16 
'PROGRAM TO DETERMINE DIRECTION OF QUADRATURE ENCODER 

DIM PORTB_OLD AS BYTE 
DIM PORTB_NEW AS BYTE 
dim count as byte 
dim txt as string[3] 

'USING A 2 BIT (QUADRATURE SYSTEM) THIS IS THE RULE: 
'LOW BIT OF OLD VALUE IS ALWAYS EQUAL TO HIGH BIT OF NEW VALUE 
'IF MOVING CLOCKWISE 
'LOW BIT OF OLD VALUE DOES NOT EQUAL HIGH BIT OF NEW VALUE 
'IF MOVING COUNTERCLOCKWISE 

main: 
'TRISA = 0              'RA0 LED WILL INDICATE CW; RA1 LED WILL INDICATE CCW 
TRISB = %00000011      'RB0 and RB1 Inputs 
TRISC = 0 
PORTA = 0 
PORTB = 0 
 LCD_init(PORTD)        'LCD su portD 
 LCD_out(1,1,"Numero impulsi:") 
 LCD_out(2,1,"Dir:") 
PORTB_OLD = PORTB 

my_direction: 

PORTB_NEW = PORTB 

IF PORTB_NEW XOR PORTB_OLD <> 0 THEN     'test for encoder change 
   IF (PORTB_NEW >> 1) = (PORTB_OLD AND 1) THEN 
      PORTC = 1        'CW DIRECTION 
       LCD_out(2,6,"I") 
       count = count-1 
   ELSE 
      PORTC = 0       'CCW DIRECTION 
       count = count+1 
       LCD_out(2,6,"A") 

   END IF 
END IF 
 bytetostr(count,txt) 
     LCD_out(1,18,txt) 


PORTB_OLD = PORTB_NEW 

GOTO my_direction 

end.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top