Shop OBEX P1 Docs P2 Docs Learn Events
Quadrature Encoder Example Problem — Parallax Forums

Quadrature Encoder Example Problem

BPM-WorkBPM-Work Posts: 2
edited 2007-01-10 16:28 in General Discussion
This may be due to the quadrature encoder I am using but here it goes...· :\

When using the below code from the help file (I removed the display code) when I WATCH the encValue using
debug, or even putting encValue out to a port with leds, the count always increments and decrements by 4 decimals. When I turn the encoder VERY slowly, between clicks(my encoder has 32 lock positions) it will show the first two bits momentarily, but they dissappear...
So I turn my encoder one click, and it counts 4, and if I turn it again, 8.· Then I turn it back, and it counts back to 4, turn it once more back,·then counts·0.

Is this code somehow counting too many pulses from my quadrature encoder?
The encoder I am using is a·HRPG-AD32-#13C http://www.avagotech.com/assets/downloadDocument.do?id=610

Any code suggestions on how to deal with this?

' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------
DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ            4_000_000
ID              "ENCODER"
' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------
EncPort         VAR     RA                      ' encoder port
TRIS_Enc        VAR     TRIS_A
' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------
MaxVal          CON     100
' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------
encCheck        VAR     Byte
encOld          VAR     Byte                    ' previous encoder bits
encNew          VAR     Byte                    ' new encoder bits
encValue        VAR     Byte                    ' encoder value
tmpB3           VAR     Byte
' -------------------------------------------------------------------------
  INTERRUPT
' -------------------------------------------------------------------------
' Runs every 64 uS @ 4 MHz (no prescaler)
ISR_Start:
  encNew = EncPort & %00000011                  ' get econder bits
  tmpB3 = encOld XOR encNew                     ' test for change
  IF tmpB3 > 0 THEN                             ' change?
    encOld = encOld << 1                        ' adjust old bits
    encOld = encOld XOR encNew                  ' test direction
    IF encOld.1 = 1 THEN
      IF encValue < MaxVal THEN                 ' if max, no change
        INC encValue                            ' increase value
      ENDIF
    ELSE
      IF encValue > 0 THEN                      ' if 0, no change
        DEC encValue                            ' decrease value
      ENDIF
    ENDIF
    encOld = encNew                             ' save last input
  ENDIF
 
ISR_Exit:
  RETURNINT
' =========================================================================
  PROGRAM Start
' =========================================================================
' -------------------------------------------------------------------------
' Subroutine Declarations
' -------------------------------------------------------------------------

' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------
Start:
  encNew = EncPort & %00000011                  ' read encoder pos
  encOld = encNew                               ' copy
  encValue = 0                                  ' clear value
  OPTION = $88                                  ' interrupt, no prescaler
Main:
  
GoTo Main
' -------------------------------------------------------------------------
' Subroutine Code
' -------------------------------------------------------------------------

Comments

Sign In or Register to comment.