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
    582


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Step-by-step I think I begin to understand ...
    The correct hardware it's this.
    The software can be this :
    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"
    include "C:\PBP\enc_KEY.BAS" 
    
    TRISB = 000000
    PORTB = 000000 
    
    oldState VAR BYTE
    newState VAR BYTE
    DIR VAR BIT
    
    UP CON 1
    DN CON 0
    
    Main_Loop: 
    portb.5 = 1
    gosub keypadscan
    gosub check
    goto main_loop
    
    Check :
    if dir = up then 
    portb.0 = 1
    pause 1000
    portb.0 = 0
    endif
    if dir = dn then
    portb.1 = 1
    pause 1000
    portb.1 = 0
    endif
    return
    
    encoder:
    portb.5 = 0
        newState = (porta & 000111)
    If newState <> oldState THEN        ' Changed from last time?
      Select Case oldState
        Case 2
        If NewState = 4 THEN          ' Was 1 now 4 = Up
          DIR = up
        ELSE                          ' Was 1 now 2 = Down
          DIR = dn
        ENDIF
      
        Case 4
        If NewState = 1  THEN        ' Was 2 now 1 = Up
          DIR = up
        ELSE                           ' Was 2 now 4 = Down
          DIR = dn
        ENDIF
      
        Case 1
        If NewState = 2 THEN        ' Was 4 now 2 = Up
          DIR = up
        ELSE                       ' Was 4 now 1 = Down
          DIR = dn
        ENDIF
      END SELECT
    
      oldState = NewState 
    endif
    portb.5 = 1
    Return
    The question is : where to put "gosub encoder" ? Inside the "enc_key.bas" (it's keypad.bas addapted to 2 row/3 cols) ???
    Attached Images Attached Images  

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


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Hi,
    Now we're getting somewhere - that looks like it might work.
    The keypad routine has nothing to do with reading the encoder. Put the GOSUB Encoder in the mainloop, before GOSUB check obviously....

    And again, if it doesn't work then remove the keypad routine untill you get the encoder routine working - THEN try to make the two coexist. There's no guarantee that the encoder code works, I just posted it as one possible idea of how to read that kind of "encoder".

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


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    No matter what I do, portb.1 goes high and low .... portb.0 don't change ....
    Code:
    TRISB = 000000
    PORTB = 000000 
    
    oldState VAR BYTE
    newState VAR BYTE
    DIR VAR BIT
    
    UP CON 1
    DN CON 0
    
    oldstate = (porta & 000111)
    
    Main_Loop: 
    
    gosub Encoder
    gosub check
    goto main_loop
    
    Check :
    if dir = up then 
    portb.0 = 1
    pause 1000
    portb.0 = 0
    endif
    if dir = dn then
    portb.1 = 1
    pause 1000
    portb.1 = 0
    endif
    return
    
    encoder:
    portb.5 = 1
        pause 100
        newState = (porta & 000111)
    portb.5 = 0
    If newState <> oldState THEN        ' Changed from last time?
      Select Case oldState
        Case 1
        If NewState = 4 THEN dir=up         
        If NewState = 2 THEN dir=dn
      
        Case 2
        If NewState = 4 THEN dir=up        
        If NewState = 1 THEN dir=dn
      
        Case 4
        If NewState = 1 THEN dir=up        
        If NewState = 2 THEN dir=dn
        
      END SELECT
    
    oldstate=newstate
    endif
    Return
    Attached Files Attached Files

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Hi,
    Since you have the inputs pulled high and the wiper "grounding" each input as it cycles thru them you can't use 1, 2 and 4 as the states since that "looks for" a '1' on the individual pins. You need to use 3, 5 and 6 instead.

  5. #5
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Twillight zone ....
    Code:
      Select Case oldState
        Case 3
        If NewState = 6 THEN dir=up         
        If NewState = 5 THEN dir=dn
      
        Case 5
        If NewState = 3 THEN dir=up        
        If NewState = 6 THEN dir=dn
      
        Case 6
        If NewState = 5 THEN dir=up        
        If NewState = 3 THEN dir=dn
      END SELECT
    No matter what I do, portb.1 goes high and low .... portb.0 don't change ....

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Don't know what to tell you but the code works just fine here.
    Code:
    HSEROUT ["Program start",13]
    
    Main:
        PortB.5 = 0
        newState = PortA & %00000111
        PortB.5 = 1
        
        If newState <> 7 THEN
            If newState <> oldState THEN
                  
                Select Case oldState
                    Case 3
                    If NewState = 6 THEN dir=up         
                    If NewState = 5 THEN dir=dn
      
                    Case 5
                    If NewState = 3 THEN dir=up        
                    If NewState = 6 THEN dir=dn
      
                    Case 6
                    If NewState = 5 THEN dir=up        
                    If NewState = 3 THEN dir=dn
                END SELECT
    
                HSEROUT["oldState: ", DEC oldState, "   newState: ", DEC newState, "   DIR: ", DEC DIR, 13]        
            
            oldState = newState
            Pause 1000
            ENDIF
        ENDIF
        
    Goto Main
    This was executed on an AMICUS18 board with a PIC18F25K20. A wire from RB5 to the pulled up inputs was used to simulate the "encoder", the results are here:

    Name:  Fratello_encoder.jpg
Views: 8814
Size:  69.7 KB

    As you can see, it works.

    Have you verified that PortB.5 does go LOW?
    Have you verified that you DO get a LOW on RA0,1 and 2 as you rotate the encoder?
    Have you checked the datasheet to see if any other peripherals are multiplexed onto the pins you're trying to use? ADC? Comparator?

  7. #7
    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

    Been said before and I will say it again.

    NEVER TRUST A SIM!
    Dave
    Always wear safety glasses while programming.

Members who have read this thread : 3

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