+ Reply to Thread
Results 1 to 6 of 6

Thread: Rotary encoders

  1. #1
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,896

    Default Rotary encoders

    Hi,
    is there anybody who have a snippet of code to increase/decrease value of a variable by a rotary encoder.

    I did one who works perfectly one a gray code based rotary encoder but the rotary encoder was so simple to use. 1 gray code change by shaft step.

    The new one i have now do half of gray code change by shaft step.

    example in clockwise turning:

    initial state : 00
    01
    one step later: 11
    10
    one step later: 00

    any idea ??

    regards


    Hi, we are pleased to see that you are using our forum.

    We'd like you to become a member of our community. Membership is FREE, please REGISTER and view our forums without these annoying reminders!

    Once registered you may post on the forums, download from the file areas and use the WIKI without interruption.




    Steve

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

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

    Default

    O.K. after reading my post i realize how stupid i'm now ;(

    encoder is connect to PORTD.0 and PORTD.1

    here's the code:

    Code:
    value=127
    
    GetInitial: 
    OldPos = PORTD & $3
    NewPos=OldPos
    
    while (NewPos==0) or (NewPos==3)
          NewPos = portd & $3
    wend
    
    select case NewPos
           case 1
                select case OldPos
                       case 0
                            value=value+1
                       case 3
                            value=value-1
                end select
           case 2
                select case OldPos
                       case 3
                            value=value+1
                       case 0
                            value=value-1
                end select
    end select
    
    lcdout $fe,$c0,dec value
    goto GetInitial
    Any improvement will be apreciate. But it works really well

    sorry for that

    regards
    Last edited by mister_e; - 12th October 2004 at 04:42.
    Steve

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

  3. #3
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511

    Default

    Hello Mister_e,

    M>>O.K. after reading my post i realize how stupid i'm now ;(


    Well, it works like this Mister_e... I have done this so many times, and I know it won't be the last time I do it <g>. Welcome to the Titanic... we can all have fun together laughing at what we do as we sink.

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,896

    Default

    Hi Dwayne,
    yes you're right. Since i program thos PIC i don't know how many times it happen to me and of course it will happen again since every project are different too.

    But this time, i was really disapointed of me. Usualy i figure out every conditions on paper... but not this time.

    So when i read back my post, i realize how simple and clear it was to do. After a few minute of crying... i finally laugh of me....

    regards
    Steve

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

  5. #5
    Join Date
    Feb 2004
    Posts
    330

    Default

    Steve,

    Could you explain this code for me as I'm a whole lot green on these things. I have one connected and it works great but I have to connect 5 of them and need to know what everything actually does first before I can adjust the code properly.

    Thanks,
    Paul

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

    Default

    a blast from the past

    Well, i thought it was self explained but anyways. The actual monitor the encoder initial state, store them in a variable (OldPos) and then spin in round until the value change. Most encoder use the gray code.
    http://en.wikipedia.org/wiki/Gray_code

    now you need to know the direction, this is done by when you compare the NewPos and OldPos.

    here's another version with a littlt bit more comments 'round.
    Code:
        '
        '    ////////////////////////////////|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\            
        '
                                        GetEncoder:
        '                               ===========
        '
        '    Procedure to get value change from rotary encoder
        '        1. Read variant of rotary encoder (move, enter pushbutton)
        '        2. Store result in 'MenuChoice' & 'MenuPointer' variables
        '
        '    Resume conditions:
        '    ------------------
        '        1. Encoder Move
        '        2. PBEnter button is pressed
        '        3. User move to previous menu
        '
        '    ////////////////////////////////|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\            
        '
        OldPos = (PORTB & %11000000)>>6 ' get only PORTB<7:6> bits
        NewPos = OldPos 
            '   
            '    -----------------------------------------------------
            '    see if encoder has move or if ENTER button is pressed
            '    -----------------------------------------------------
        while ((NewPos==0) or (NewPos==3)) and PBEnter
           NewPos = (PORTB & %11000000)>>6 ' get only PORTB<7:6> bits
        wend
        pause 20 ' debounce delay
            '  
            '    --------------------
            '    Get encoder movement
            '    --------------------
            '
            '        Rotary encoder output table (clockWise rotation):
            '        -------------------------------------------------
            '               00
            '               01 between move (half detent)
            '               11
            '               10 between move (half detent)
            '
            '        Using XOR
            '
            '               OldPOS   : 0     0     3     3
            '               NewPos   : 1     2     1     2
            '               movement : CW   CCW   CCW    CW
            '           XOR -------------------------------
            '                          1     2     2     1
            '
            '    So, if NewPos XOR OldPos = 1 => increment
            '           Newpos XOR OldPos = 2 => Decrement    
            '
        Newpos=newpos ^ oldpos 
        select case newpos
            '
               case 1
                   menuchoice=menuchoice+1
                   Menupointer=menupointer+1
            '
               case 2
                   menuchoice=menuchoice-1
                   Menupointer=menupointer-1
            '
         end select
            '
            '    ---------------------------
            '    test the actual menu choice
            '    ---------------------------
            '        if higher than the maximum menu item... stay there
            '
        If menuchoice > Item then 
           menuchoice = Item
           menupointer = thisSCREENLINES
        endif
        return
    For 5 different encoder, you'll have to read ALL related encoder i/o in oneshot, determine which one has moved, and in which direction. mmm

    depending what else your program need to do, make sure you never miss any movements. you have to read and react really fast. Assembler could be handy here. For a background process, as you need 10 interrupts source, it's not going to be really easy.

    Sometimes it's handy to have few different dedicated controller for that.
    Steve

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

Similar Threads

  1. Calculate Distance Using Rotary Encoder
    By guanerrr in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 4th May 2012, 16:40
  2. Quick thoughts: DT Ints with encoders
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 7th January 2010, 02:01
  3. Rotary encoder help (not 'counting steps)
    By Elnino in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 26th October 2009, 23:03
  4. quad encoders
    By cpayne in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th March 2007, 18:49
  5. Farnell's Rotary Encoder 733-726
    By crematory in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th October 2005, 13:51

Members who have read this thread : 17

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