decoding quadrature encoders


Closed Thread
Results 1 to 40 of 94

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    Hello ICE,

    From the PDF file page 3:
    http://www.parallax.com/dl/docs/cols...l1/col/nv8.pdf

    In the case of the encoder sequence, it turns out that for
    any given sequence, XORing the righthand bit of the old value
    with the lefthand bit of the new value tells you the direction
    of rotation.
    For example, take the clockwise sequence 01 00: 1 XOR 0 = 1.
    Now the counter-clockwise sequence 01 11: 1 XOR 1 = 0.
    This relationship holds for any pair of numbers in either direction.

    * * *
    The code for the above is:

    direction_bit = old.bit0 ^ new.bit1 ' 1 if CW, 0 if CCW

    * * *
    In your code:

    Pause 10 '= 10 milliseconds

    * * *
    This will help you to debug your code.

    Remove the line
    LCDOut $fe,1,"no movement"

    Replace the lines
    LCDOut $fe,1,"one dir" with LCDOut $fe,1,"Last direction was CCW"
    LCDOut $fe,1,"2nd dir" with LCDOut $fe,1,"Last direction was CW"


    Luciano

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    be sure you have pull-up or pull down attach to both output of your encoder.

    depending of you rotary encoder model, you can try this thread and monitor the 'value' variable. or modify it easily to have CW and CCW flag in one shot....
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    File attachment = Incremental quadrature encoder direction drawing.

    Luciano
    Attached Files Attached Files
    Last edited by Luciano; - 9th April 2005 at 15:03.

  4. #4
    ice's Avatar
    ice Guest


    Did you find this post helpful? Yes | No

    Default

    Luciano>>
    Thanks for the file..i figured out the logic of the "XOR"..

    i included this in my code...[the counting up and counting down lines]
    Code:
    
    quad:
    	new= PORTC & %00000011
    	
    start:
    	old=new & %00000011 
    
    again:
    	new= PORTC & %00000011
    	IF new=old Then again
    	direction_bit=old.bit1 ^ new.bit0
    	IF direct=0 Then  cw
    	LCDOut $fe,1,"Last was CCW"
    	counter=counter+1
    	LCDOut $fe,$c0,DEC counter
    	Pause 5
    	GoTo start
    		
    cw:
    	LCDOut $fe,1,"Last was CW"
    	counter=counter-1
    	LCDOut $fe,$c0,DEC counter
    	Pause 5
    	GoTo start
    ...I am able to see the counter going up and down as the motor changes it's direction..however the LCD line "last was cw/ccw"..keeps on flipping and never remains the same..even if the motor has moved in the same direction..

    Im still working on it..and will keep u all posted...

    monitor the 'value' variable. or modify it easily to have CW and CCW flag in one shot....
    Thanks for the link M_e
    ..i'll be doing tht to take care of the above problem

  5. #5
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    Try this code. (Not tested).

    (Problem in your code: you use 'direction_bit' but also 'direct').

    Luciano


    Code:
    	old_val VAR BYTE
    	new_val VAR BYTE
    	direction_bit VAR BIT
    	enc_counter VAR WORD
    
    	enc_counter = 10000
    	new_val = PORTC & %00000011
    start:
    	old_val = new_val
    again:
    	new_val = PORTC & %00000011
    	
    	IF new_val = old_val Then again
    
    	direction_bit = old_val.bit0 ^ new_val.bit1
    	
    	IF direction_bit = 1 Then  
    	
    		LCDOut $fe,1,"Last was CW"
    		enc_counter = enc_counter + 1
    		LCDOut $fe,$c0,DEC enc_counter
    
    	else
    		LCDOut $fe,1,"Last was CCW"
    		enc_counter = enc_counter - 1
    		LCDOut $fe,$c0,DEC enc_counter	
    	
    	endif
    
    	GoTo start
    Last edited by Luciano; - 12th April 2005 at 11:04.

  6. #6
    Join Date
    May 2007
    Posts
    65


    Did you find this post helpful? Yes | No

    Thumbs up

    Quote Originally Posted by Luciano View Post
    File attachment = Incremental quadrature encoder direction drawing.

    Luciano
    Luciano: five stars!

Similar Threads

  1. Quick thoughts: DT Ints with encoders
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 7th January 2010, 01:01
  2. PMDC SERVO MOTOR WITH quadrature encoder DRIVE ?
    By phoenix_1 in forum Schematics
    Replies: 37
    Last Post: - 22nd November 2009, 19:45
  3. 32-bit Quadrature Counter With Serial Interface
    By precision in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 10th June 2008, 02:49
  4. quad encoders
    By cpayne in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th March 2007, 17:49
  5. "momentary" IR decoding
    By sporker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 20th June 2005, 01:53

Members who have read this thread : 4

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts