New approach to Rotary Encoder


Closed Thread
Results 1 to 40 of 91

Hybrid View

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

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

  3. #3
    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 : 2

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