Shop OBEX P1 Docs P2 Docs Learn Events
PropBasic: Quadrature Encoder Help Please — Parallax Forums

PropBasic: Quadrature Encoder Help Please

skynuggetskynugget Posts: 172
edited 2011-01-18 13:27 in Propeller 1
hey all, im trying to decode a standard quadrature encoder, and im soooo close.

the code below seems to only count in a negative direction no matter which way i turn the encoder.
i suspect im not masking the bits right before i check for the direction.

does anyone see the mistake in the following test program? thanks so much!
DEVICE          P8X32A, XTAL1, PLL16X
XIN             5_000_000

Baud 		CON 		"T9600"

Encoder   PIN   10..11   INPUT
TX           PIN   30         HIGH

LOAD "serial.lib"

	idx			var		long
	tmp			var		long
	tmp1			var		long
	tmp2			var		long
	
	encNew		var		long
	encOld		var		long
	encVal		var		long	
	
' ======================================================================
  PROGRAM Start
' ======================================================================

Start:
  ' init

Main:
  encNew = Encoder					'get incoder bits
  
  tmp = encOld ^ encNew				'did it change?
  IF tmp = 0 THEN skip				'no so skip
  	
  encOLD = encOLD << 1				'align
  encOLD = encOLD ^ encNew		        'check direction
  tmp1 = encOLD & %00000010		        'iso bit1 
 
  IF tmp1 = 1 THEN					'going up
  	INC encVal					'add to value
  ELSE							'going down	
  	DEC encVal					'subtract from value
  ENDIF
  	
  encOLD = encNew					'save last input
  
  skip:	
  TX_DEC encVal					'debug value
  TX_BYTE 1						'debug home

  
GOTO Main
END

Comments

  • skynuggetskynugget Posts: 172
    edited 2011-01-17 18:17
    got it! it seems to work real nice. thanks all
    Main:
    	
      encNew = Encoder						'get incoder bits
      encChk = encOld ^ encNew				'did it change?
      
      IF encChk > 0 THEN 					'yes, accumulate
      	encOLD = encOLD & %00000001		'iso bit.0 of old value
      	encChk = encNew >> 1				'iso bit.1 of new value
      	encDir = encold ^ encChk			        'xor magic
    		IF encDir = 1 THEN				'going up
      		INC encVal					'add to value
      	ELSE							'going down	
      		DEC encVal					'subtract from value
      	ENDIF
      ENDIF
      
      encOLD = encNew						'save last input
      TX_DEC encVal						'debug value
      TX_BYTE 1							'debug home
    	
    GOTO Main
    END
    
  • BeanBean Posts: 8,129
    edited 2011-01-18 06:08
    Oops, Sorry I missed this thread.
    Glad you got it working and thanks for posting the working code. So many forget to do this and really helps others out.

    Bean
  • SapiehaSapieha Posts: 2,964
    edited 2011-01-18 07:33
    Hi skynugget
    Thanks

    @Bean. Its my opinion to: At every time anyone POST (Solved) --- Them can Post how them solved it.

    That can help many Beginners to found theirs problems.

    And thanks for PBasic.


    Bean wrote: »
    Oops, Sorry I missed this thread.
    Glad you got it working and thanks for posting the working code. So many forget to do this and really helps others out.

    Bean
  • skynuggetskynugget Posts: 172
    edited 2011-01-18 10:28
    Bean wrote: »
    Oops, Sorry I missed this thread.
    Glad you got it working and thanks for posting the working code. So many forget to do this and really helps others out.

    Bean

    no worries boss, there are lots of threads to manage :). without people posting problems, solutions and snippets, i never would have been able to get started with microcontrollers, i try to post everything i can so that someday i may return the favor.
  • $WMc%$WMc% Posts: 1,884
    edited 2011-01-18 13:27
    skynugget wrote: »
    no worries boss, there are lots of threads to manage :). without people posting problems, solutions and snippets, i never would have been able to get started with microcontrollers, i try to post everything i can so that someday i may return the favor.
    '
    Thanks skynugget:
    '
    I just got a Quadrature encoder set-up last night. I planed to write some code for it this evening in PropBasic.Thanks for the code.
    '
    I hope you post the Quad-code in the OBEX under PropBasic!
    '
    Thanks again.
Sign In or Register to comment.