New approach to Rotary Encoder


Closed Thread
Results 1 to 40 of 91

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Yes it will for the time that either is in LOW state. If there is a possibility that this condition is likely to happn pemanently, then you have other options to choose.

    As stated on he first post, that is an interesting experiment of another way (more simple) to do the same task. No-one is forced to accept it. Or use it. Just forget it and you will be just fine.

    Ioannis

  2. #2
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Please forgive me if it seemed that I was complaining. I was just trying to understand how your method worked.

    If you're using a rotary encoder with detents as a control, detecting a single AB transition (out of four) between detents is probably preferable. That's how I do it but I use a switch state latch and simple parallel switch state logic to filter out the unwanted switch states.

    Your method is simple and elegant. Thank you for sharing with us...

    Cheerful regards, Mike

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    No problem Mike. I have to admit that I was a little touchy.

    Yes, the idea was to read a mechanical rotary encoder with detents. Sure is better to buffer the port states and then do the processing.

    Even better, use interrups to grab the port change and then do whatever you want. More, make a debounce based on interrupts since mechanical switch of the encoder do bounce a little. But this is for more advanced programmers. Maybe later when my related project allows.

    Ioannis

  4. #4
    Join Date
    Nov 2011
    Posts
    2


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Hello world. I have learnt a lot from lurking these threads so i thought i would try and help someone else out a bit.




    All your encoder worries can be over for about 50 pence and if i can get a picture in here i will show you how...........

    Name:  encoder interface to pic.bmp
Views: 21541
Size:  442.0 KB

    the thing to realise is that moving along waveform A from left to right the negative edge of A is always when B is low. moving back the other way the negative edge is always when B is high.

    the jk flip flop clock input is negative edge triggered and when triggered it feeds the input on J through to Q -- but only if j and k inputs are opposite hence section 1 of the nor gate to invert the signal. j is always opposite to k now. and the clock input always gets triggered right in the middle of the high or low so it never misses.

    so, when moving clockwise Q will be high and anticlockwise it will go low.

    two other parts of the quad nor gate (2 and 3 above) to produce a hardware XOR on output C and producing the waveform at bottom of screen.

    Now all you need to do is feed C into an input that generates interrupts on a negative edge trigger. when the interrupt fires all it has to do is look at the level of D and either add 1 to clockwise total or subtract 1. Thats it. Your programming days are over.

    And, all the mushy signal edges and bounce are tidied up too. Lovely solid latched outputs.
    And you are going to have to go some to bother them with speed or with slowness.

    Not bad for 50p I hope you will agree.

  5. #5
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Hey podgycode...
    I have not tried your circuit. I would guess it works as you describe.

    What I can't understand is why is this so difficult to do using two PIC pins and some code. Are these rotary encoders so difficult to read using code??

    I am struggling with this very thing myself. I have two rotary encoders... one that I have wired up and done some basic testing... one that I have not tried yet.

    I have also done quite a bit of searching over on the Parallax Basic Stamp forum, since the basic stamp uses VERY similar language to our PICbasic.

    I really would like to use interrupts (DT instant INT's, if possible)

    My real problem is TIME. If I only had more of it, I'm sure you all can relate.

    Any way, if anyone has some good sample code (that used interrupts) for one to try... that they might be willing to share
    Last edited by Heckler; - 4th November 2011 at 15:43.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  6. #6
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Here the code for a rotary optical encoder, using quadrature.
    Just connect the Channel A to portB.6 and channel B to portB.7, ground portB.5 and portB.4

    Connection for LCD can be found to page 96 of PBP user manual.



    Code:
    include "ALLDIGITAL.pbp"
    INCLUDE "DT_INTS-14.bas"        ; Base Interrupt System
    'INCLUDE "ReEnterPBP.bas"        ; Include if using PBP interrupts
    
    DEFINE OSC 20
    
    '                       PIC 16F628
    ' PicBasic program to demonstrate operation of an LCD in 4-bit mode
    '          and reading an optical encoder in quadrature
    '
    ' LCD should be connected as follows:
    '       LCD     PIC
    '       DB4     PortA.0
    '       DB5     PortA.1
    '       DB6     PortA.2
    '       DB7     PortA.3
    '       RS      PortA.4 (add 4.7K pullup resistor to 5 volts)
    '       E       PortB.0
    '       RW      Ground
    '       Vdd     5 volts
    '       Vss     Ground
    '       Vo      20K potentiometer (or ground)
    '       DB0-3   No connect
    
    
    TrisA = 000000
    TrisB = 110000
    PortB=0
    
    Define LCD_DREG PORTA
    Define LCD_DBIT 0
    Define LCD_RSREG PORTA
    define LCD_RSBIT 4
    define LCD_EREG PORTB
    define LCD_EBIT 0
    define LCD_BITS 4
    define LCD_LINES 2
    define LCD_COMMANDUS 2000
    define LCD_DATAUS 50
    
    Flag                var bit
    wsave               VAR BYTE    $70     SYSTEM  ' alternate save location for W 
    Q_New               var Byte
    Q_Old               var byte
    Q_Count             var word
    
    
    ' Set variable value @ startup
              Flag = 0          
             Q_New = 0
             Q_Old = 0
           Q_Count = 0
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, Resetflag?
            INT_Handler    RBC_INT,  _Encoder,   ASM,  yes
        endm
        INT_CREATE                      ; Creates the interrupt processor
    ENDASM
    
    @ INT_ENABLE RBC_INT            ; enable external (INT) interrupts
    
    
    Lcdout $fe, 1                       ' Clear LCD screen
    Lcdout "System Ready"               ' Display message
    Pause 500                           ' Wait .5 second
    
    Goto Main_Loop
    
    Encoder:
    Q_New = PortB.7 + PortB.7 + PortB.6
    
    if Q_Old = 0 then
    if Q_New = 2 then Minus_Count
    if Q_New = 1 then Plus_Count
    endif
    
    if Q_Old = 1 then
    if Q_New = 0 then Minus_Count
    if Q_New = 3 then Plus_Count
    endif
    
    if Q_Old = 3 then
    if Q_New = 1 then Minus_Count
    if Q_New = 2 then Plus_Count
    endif
    
    if Q_Old = 2 then
    if Q_New = 3 then Minus_Count
    if Q_New = 0 then Plus_Count
    endif
    
    goto Q_Skip
    
    Minus_Count:
    Q_Count = Q_Count - 1
       Flag = 1
    goto Q_Skip
    
    Plus_Count:
    Q_Count = Q_Count + 1
       Flag = 1
    Q_Skip:
    Q_Old = Q_New
     
     
    @ INT_RETURN
    
    Main_Loop:   
    
    if Flag = 1 then
    Lcdout $fe, 1                    
    Lcdout Dec Q_Count
    Flag = 0
    endif
    
    goto Main_Loop
    
    end
    Enjoy

    Cheers

    Al.
    All progress began with an idea

  7. #7
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Quote Originally Posted by aratti View Post
    Connection for LCD can be found to page 96 of PBP user manual.
    Do you have a link the that manual, please? I've looked at two different PBP Compiler Manuals so far and neither has a schematic for an LCD interface on page 96...
    Last edited by Mike, K8LH; - 5th November 2011 at 02:26.

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