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

    This is schematic and code...Maybe I miss something important ...
    Code:
    @ DEVICE pic16F628A, XT_OSC, WDT_OFF, PWRT_ON, BOD_OFF, MCLR_ON, LVP_OFF, PROTECT_ON
    
    include "alldigital.pbp"
       Define   OSC 4           ' 4MHz 
       CMCON = 7                ' Disable on-chip comparator, PORTA in digital mode
    
    
    TrisA = %00011100           ; coloane la RA2, 3, 4 ; randuri la RA1, 0 ,7 
    porta = %00011100
    TRISB = %00000000
    PORTB = %00010000 
    
    oldState VAR BYTE
    newState VAR BYTE
    
    UP CON 1
    DN CON 0
    
    
    loop:
        PortA.1 = 0
        newState = PortA & %00011100         ; this is for my hw : 11100 = 28
        PortA.1 = 1
    
        If newState <> 28 THEN
            If newState <> oldState THEN
                  
                Select Case oldState
                    Case 12
                    If NewState = 20 THEN dir=up       
                    If NewState = 24 THEn dir=dn
                    
                    Case 20
                    If NewState = 24 THEN dir=up               
                    If NewState = 12 THEN dir=dn
                   
                    Case 24
                    If NewState = 12 THEN dir=up                       
                    If NewState = 20 THEN dir=dn
                END SELECT
                oldState = newstate
                GOSUB UpOrDn 
           pause 15                    ; time for UpOrDn     
           endif          
        ENDIF
    ;============
    UpOrDn:
    if dir=DN then
        PortB.4 = 0
        pauseus 3850
        PortB.4 = 1
        pauseus 3850
        PortB.4 = 0
        pauseus 660
        PortB.4 = 1
    endif
    
    if dir=UP then
        PortB.4 = 0
        pauseus 4450
        PortB.4 = 1
        pauseus 4400
        PortB.4 = 0
        pauseus 660
        PortB.4 = 1
    endif    
    return
    
    end
    Attached Images Attached Images

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


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    1) I don't understand why you keep putting those PAUSE statements in there, now you have a PAUSE 15 in there....
    2) There's no GOTO LOOP anywhere so the program will "fall thru" into the UpOrDn routine then hit the RETURN and bad things will happpen. (BTW, Loop is a reserved word in newer versions of PBP, best to get used to not using it.
    3) Try resampling the pins and set oldState when you return from the subroutine, ie (changes in red):
    Code:
    Main:
        PortA.1 = 0
        newState = PortA & %00011100         ; this is for my hw : 11100 = 28
        PortA.1 = 1
        If newState <> 28 THEN
            If newState <> oldState THEN
                  
                Select Case oldState
                    Case 12
                    If NewState = 20 THEN dir=up       
                    If NewState = 24 THEn dir=dn
                    
                    Case 20
                    If NewState = 24 THEN dir=up               
                    If NewState = 12 THEN dir=dn
                   
                    Case 24
                    If NewState = 12 THEN dir=up                       
                    If NewState = 20 THEN dir=dn
                END SELECT
                GOSUB UpOrDn 
                
                PAUSE 15                    ; time for UpOrDn   
             
         ' Now resample the encoder and keep the state as oldState
              PortA.1 = 0
             oldState = PortA & %00011100         ; this is for my hw : 11100 = 28
             PortA.1 = 1     
           
        ENDIF          
      ENDIF
    GOTO MAIN
    ;============
    UpOrDn:
    if dir=DN then
        PortB.4 = 0
        pauseus 3850
        PortB.4 = 1
        pauseus 3850
        PortB.4 = 0
        pauseus 660
        PortB.4 = 1
    endif
    if dir=UP then
        PortB.4 = 0
        pauseus 4450
        PortB.4 = 1
        pauseus 4400
        PortB.4 = 0
        pauseus 660
        PortB.4 = 1
    endif    
    RETURN
    END

  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

    Mr. Henrik I must get my hat in front of you ! (Hope G.translate work well !).
    NOW, the code work verry good !!! Only sometimes the command are inversed , but in 9 case of 10 it's ok, so I can live with this...
    Thanks for lost so much time with me ! I am verry grateful !

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


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Great, finally... Now try removing the PAUSE 15 and see if that improves it further.

  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

    Thanks to Mr.Henrik Olsson this code work PERFECT !!! I'm sure I would not have done it without his help !
    Code:
    Main:
        PortA.1 = 0
        newState = PortA & 011100        
        PortA.1 = 1
    
        If newState <> 28 THEN
            If newState <> oldState THEN
                  
                Select Case oldState
                    Case 12
                    If NewState = 20 THEN dir=up       
                    If NewState = 24 THEn dir=dn
                    
                    Case 20
                    If NewState = 24 THEN dir=up               
                    If NewState = 12 THEN dir=dn
                   
                    Case 24
                    If NewState = 12 THEN dir=up                       
                    If NewState = 20 THEN dir=dn
                END SELECT
    
                GOSUB UpOrDn 
    
        PortA.1 = 0
        OldState = PortA & 011100         
        PortA.1 = 1
                   
           endif          
        ENDIF
    Goto Main

  6. #6
    Join Date
    Sep 2011
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Hi to all. I want to use a rotary encoder with a 18f252, and i have a lot of troubles. I want to use interupts, so my only solution PORTB or I can use Portc also?

    I made a pcb and I made the connections to PORTC, and I also use a debounce circuit, which I think causes problems. Anyone has a snipset code cause I do not have any expieriance with encoders?
    5v
    __|__
    | |
    / /
    \ \
    /10k / 10K
    \ \
    | A | B ENC
    - -
    | |-------------------------------
    \ |
    /10K \
    \ /10K
    / \
    |--------------------- |--------------------
    PORTC.2 | PORTC.3 |
    ---- ----- 100nF
    ---- -----
    | |
    --------- -----------
    ----- -----
    - -
    Last edited by kzeoprf; - 21st August 2012 at 10:49.

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


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Hello,
    On the 18F252 there's no interrupt capabillity on PortC.

    Your schematic really doesn't show up that good here but it LOOKS like you have a 10k from 5V to the "top" of each of the switches in the encoders, then the other side of each switch thru another 10k to the input with a 100nF cap to GND, is that correct?

    If the above is not correct please post a proper schematic for us to see.
    If the above IS correct then it's not a very good hardware design IMHO. When the switch in the ecoder is closed you have 2*10k in series to the input, decoupled by a 100nF cap, this may be fine. But when the switch is open there's no path for the capacitor to discharge except thru the fairly high input impedance of the pin. In effect the pin floats which is not a good thing for inputs.

    /Henrik.

Members who have read this thread : 0

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