New approach to Rotary Encoder


Closed Thread
Results 1 to 40 of 91

Hybrid View

  1. #1
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    579


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    So far...no good ...
    Complete "hardware" it's like in picture . I intend to use an remote with some "rotary encoder" ...
    The remote controller has six wires . They are connected to three rows and three columns. This allows maximum of 3*3=9 functions. Roller takes three functions because it must be possible to know which way it is rotated (how doing this in PBP ?!?). Rest of the functions are assigned to six buttons.
    I use great pice of code (keypad matrix routine) of Mr. Mister-e and the "regular" buttons works verry fine...but I want to use the function of roller too.
    I can't figure out how doing this, so any "clue" will be appreciated !
    Attached Images Attached Images  

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,523


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Hi,
    Without analyzing your code in detail one possible problem might be this line
    Code:
    Q_New = PortB.7 + PortB.6 + PortB.5         ' get port status
    In this case you'll get Q_New = 1 no matter WHICH of the three pins are high. I'm not sure but I guess that's not what you want.

    You could try something like Q_New = PortB & %11100000 >> 5 instead.

    Here's another approach (untested) which is smaller in terms of program space and RAM but like I said it's untested so it might not even work.
    Code:
    oldState VAR BYTE
    newState VAR BYTE
    Q_Count VAR WORD
    DIR VAR BIT
    
    UP CON 1
    DN CON 0
    
    newState = (PortB & %11100000)      ' Isolate top three bits, newState will be 32, 64 or 128 
    
    If newState <> 0 THEN
    If newState <> oldState THEN        ' Changed from last time?
    
      Select Case oldState
        Case 32
        If NewState = 128 THEN          ' Was 1 now 4 = Up
          Q_Count = Q_Count + 1
          DIR = UP
        ELSE                          ' Was 1 now 2 = Down
          Q_Count = Q_Count - 1
          DIR = DN
        ENDIF
    
      
        Case 64
        If NewState = 32 THEN        ' Was 2 now 1 = Up
          Q_Count = Q_Count + 1
          DIR = UP
        ELSE                           ' Was 2 now 4 = Down
          Q_Count = Q_Count - 1
          DIR = DN
        ENDIF
    
      
        Case 128
        If NewState = 64 THEN        ' Was 4 now 2 = Up
          Q_Count = Q_Count + 1
          DIR = UP
        ELSE                       ' Was 4 now 1 = Down
          Q_Count = Q_Count - 1
          DIR = DN
        ENDIF
    
      END SELECT
    
      oldState = NewState
    
    ENDIF
    ENDIF

  3. #3
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    579


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    It a real pleasure to see how generous some people can be ... Thank You, AGAIN, Mr. Henrik !
    ...I think I missing something ... If UP do something, If Down do something else...dont ?!
    But nothing happens.
    Code:
    '****************************************************************
     @ DEVICE pic16F628A, XT_OSC, WDT_OFF, PWRT_ON, BOD_OFF, MCLR_ON, LVP_OFF
    
       Define   OSC 4           ' 4MHz 
       CMCON = 7                ' Disable on-chip comparator, PORTA in digital mode
    
    include "alldigital.pbp"
    
    TrisA = 000000
    PortB = 010000
    TrisB = 000000
    
    oldState VAR BYTE
    newState VAR BYTE
    Q_Count VAR WORD
    DIR VAR BIT
    
    UP CON 1
    DN CON 0
    
    
    Main_Loop:  
    newState = (PortB & 100000)    
    goto encoder
    if dir = up then portb.0 = 1                           
    if dir = dn then portb.1 = 1 
    goto Main_Loop 
    goto encoder
    if dir = up then 
    portb.4 = 1
    portb.0 = 1
    endif                           
    if dir = dn then 
    portb.4 = 0
    portb.1 = 1
    endif
    goto Main_Loop
    
    
    encoder:
    
    If newState <> 0 THEN
    If newState <> oldState THEN        ' Changed from last time?
    
      Select Case oldState
        Case 32
        If NewState = 128 THEN          ' Was 1 now 4 = Up
          Q_Count = Q_Count + 1
          DIR = UP
        ELSE                          ' Was 1 now 2 = Down
          Q_Count = Q_Count - 1
          DIR = DN
        ENDIF
    
      
        Case 64
        If NewState = 32 THEN        ' Was 2 now 1 = Up
          Q_Count = Q_Count + 1
          DIR = UP
        ELSE                           ' Was 2 now 4 = Down
          Q_Count = Q_Count - 1
          DIR = DN
        ENDIF
    
      
        Case 128
        If NewState = 64 THEN        ' Was 4 now 2 = Up
          Q_Count = Q_Count + 1
          DIR = UP
        ELSE                       ' Was 4 now 1 = Down
          Q_Count = Q_Count - 1
          DIR = DN
        ENDIF
    
      END SELECT
    
      oldState = NewState 
    
    ENDIF
    ENDIF
    Or I wired the roller wrong ?
    Here ( http://www.angelfire.com/nd/maza/kenwood.html ) it's a working variant, but in other "way" , using Atmega ...
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by fratello; - 13th April 2012 at 09:46.

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,523


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Try GOSUB Encoder and put a RETURN at the end of the subroutine. Currently you GOTO Encoder and then you run off into never never land. I'm not saying that will make it work but that's one possible problem.

    Also, make sure that have the "wiper" of the roller connected to 5V, verify that you get signals on the actuall pins of the PIC when you rotate the encoder/roller.

  6. #6
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    579


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    I think the hardware must be build in this way (since the rest of buttons are allready connected as matrix). I wonder if "KeyPad.bas" of Mr.Mister-e can be addapted to read the increment/decrement of roller, by "including" somehow the code for encoder ....
    Brrrrrr...it's beyond of my intellectual capacities .
    Attached Images Attached Images  

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,523


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Hi,
    I don't have any experience with that routine but provided everything else works the way you want why don't you just set the pin which is common to the encoder (RB5 ?) high, read the port, and return the pin low.

    GOSUB ScanMatrix 'Or whatever
    PortB.5 = 1
    GOSUB Encoder
    PortB.5 = 0

    Finally, in the first code you posted you had the encoder to RB7-5 so I based my code on that. Now, in your latest schematic, you have it on RB2-0 which means that my code (if that's what you're trying) definitely won't work without modifying it since it expects the three "switches" in the encoder to be on RB7-5.

Members who have read this thread : 1

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