Shop Learn
Assembly help needed to modify Rotary Encoder — Parallax Forums

Assembly help needed to modify Rotary Encoder

T ChapT Chap Posts: 4,056
edited 2021-05-05 01:30 in Propeller 1

I'm trying to figure out how to set this object (using only one encoder) to a position after the program is already running. Some time back someone gave me a method to make the encoder count backwards by going backwards from AMask [ - 8] and modifying the opcode that was set to add to become sub.

I was wondering if such a method could be done to send in a position and overwrite the position value of the single encoder already running.

If you look at SetDir you can see the method that works to make the encoder count backwards. I tried to use the same idea in SetEncoder to do Amask[11] := 5000 as a test set the value to a value first just to see if it can be modified. This just makes it go crazy. 12, 13 etc no luck. Can anyone think of a way to modify the position remotely by the main program? Thanks for any ideas.

 :IPos           add     0, Diff  'AMask[-8]   ****Add/Sub to encoder position value

                    wrlong  0, MPosAddr                     'Write new position to main memory
                    add     IPosAddr, #1                    'Increment encoder position addresses
                    movd    :IPos+0, IPosAddr
                    movd    :IPos+1, IPosAddr
                    add     MPosAddr, #4                            

:Next djnz Idx, #:UpdatePos 'Loop for each encoder
jmp #:Sample 'Loop forever

'Define Encoder Reading Cog's constants/variables

AMask long $55555555 'A bit mask
BMask long $AAAAAAAA 'B bit mask
MSB long $80000000 'MSB mask for current bit pair

Pin long 0 'First pin connected to first encoder
TotEnc long 0 'Total number of encoders

'IPosAddr long 16

Idx res 1 'Encoder index
St1 res 1 'Previous state
St2 res 1 'Current state
T1 res 1 'Temp 1
T2 res 1 'Temp 2
Diff res 1 'Difference, ie: -1, 0 or +1
IPosAddr res 1 'Address of current encoder position counter (Internal Memory)
MPosAddr res 1 'Address of current encoder position counter (Main Memory)
IntPos res 16

** sorry I can't figure out how to format this code to look right


  • JonnyMacJonnyMac Posts: 7,571

    If you're open to a simpler object, I have used what's attached in a couple commercial projects (hence, I know it works well).

    If you want to change the value at any time, use the set_value() method to change the encoder's value on-the-fly.

      if (some_condition)

    Hint: Use three back-ticks ( ` ) on the lines before and after your code to make it look like code.

  • T ChapT Chap Posts: 4,056
    edited 2021-05-05 05:27

    Thanks! I’ll try that out. Seems like an easy fix. It will need the option to flip the direction it counts so maybe I can find a way to do that

  • JonnyMacJonnyMac Posts: 7,571
    edited 2021-05-05 14:22

    Put a direction/scaling variable in your program and then do this:

        position := enc.value * delta

    If delta is equal to -1, you'll get the behavior you want.

    I am a big believer in keeping objects simple and to the point; it's your application that wants to modify the output, so let the application do it.

    Another way to do it is to restart the object and reverse the A/B pin order.

    I prefer to use the delta variable; it keeps the object clean, simple, and efficient -- it also makes the intent of the application code very obvious.

  • T ChapT Chap Posts: 4,056

    This is working out really well. Almost done translating from the older one. I have a method from the main program > PID now able to update the encoder position to a stored position. I was going to swap AB pins as you said which is simple and easy to do and restart. The system must restart anyway on a direction change since it will be lost and need to home in the new direction. Direction change is only needed to determine the method the encoder is mounted. On a motor rear shaft it counts one way. On the motor output it might be flipped opposite and count the other way. It needs a software way to correct it when needed which is a one time fix. But I'll add the delta to see how affected the timing is adding the extra math per test. I already am removing the hi lo limits which aren't needed here and require math per read. Much appreciated Jon.

  • JonnyMacJonnyMac Posts: 7,571
    edited 2021-05-05 19:58

    If you republish the object I posted, please take my name off of it.

Sign In or Register to comment.