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

    @ aratti... one more question

    wsave VAR BYTE $70 SYSTEM ' alternate save location for W
    could you help me with this line? Is it correct or did the forum software mess it up??

    thanks

    P.S. Oh yea... disregard this question... I think I see how it works..
    Q_New = PortB.7 + PortB.7 + PortB.6 What does this line of code do??
    Last edited by Heckler; - 5th November 2011 at 06:33.
    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

    Heckler, yes trisA and trisB need "%" (don't know Why have been removed)

    PortB.4 and portB.5 are set as input and never leave an input pin floating.

    Wsave is a variable used by DT INT-14.


    @Mike K8LH

    The PBP manual they give me with PBP ver 2.47. But you can Find it also at page 168 of PBP3 manual.

    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
    @Mike K8LH

    The PBP manual they give me with PBP ver 2.47. But you can Find it also at page 168 of PBP3 manual.
    I wonder if there may be a problem with the PBP3 manual at http://www.pbp3.com/downloads/PBP_Reference_Manual.pdf? It seems to stop downloading at 731 kB (consistently)... Argh!!!

  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

    Works fine here. I just opened it in my browser.
    Dave
    Always wear safety glasses while programming.

  5. #5
    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 mackrackit View Post
    Works fine here. I just opened it in my browser.
    Ok, it worked today... I've got it...

  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 I post an update of the code posted in #18. This new code has a more efficient ISR so it can read at higher frequency. I don't know the maximum frequency that the system can acheive, so if someone with some spare time will give a try and post the result, could be useful.

    As the previuos code, the mainloop doesn't do much, it simply update the display.

    Here the code revised:
    Code:
    ' PicBasic program to demonstrate how to read an optical encoder
    ' in quadrature. The code uses a 2 lines LCD in 4-bits mode 
    '                 **** MCU USED: PIC 16F628 ***
    '
    ' ------------------ LCD should be connected as follows -------------------
    '       LCD     PIC Port
    '       ---     --------
    '       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 connected
    
    '-------------------------------------------------------------------------
    ' Encoder channel A     PortB.6  (use pullups resistor if necessary)
    ' Encoder channel B     PortB.7  (use pullups resistor if necessary)
    ' N/U                   PortB.0  (output)
    ' N/U                   PortB.1  (output)
    ' N/U                   PortB.2  (output)
    ' N/U                   PortB.3  (output)
    ' N/U                   PortB.4  (input - to be grounded)
    ' N/U                   PortB.5  (input - to be grounded)
    '
    '-------------------------------------------------------------------------
    
    INCLUDE "DT_INTS-14.bas"    ' Base Interrupt System  (by Darrel Taylor)
    
    DEFINE OSC 20
    
    eeprom 0,[2,0,3,1,1,3,0,2]
    
    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
    
    CMCON = %00000111
    TrisA = %00000000
    TrisB = %11110000
    PortB = 0
    
    Flag                var bit
    wsave               VAR BYTE    $70     SYSTEM  ' alternate save location for W 
    Q_New               var Byte
    Q_Old               var byte
    M_Count             var byte [4]
    P_Count             var byte [4]
    Q_Count             var word
    
    ' ---------------------- Set variable value @ startup ----------------------  
         
    For Q_Old = 0 to 3
    Read Q_Old,M_Count[Q_Old]
    Read Q_Old + 4,P_Count[Q_Old]
    Next Q_Old
         
      Q_Count = 0   
        Q_New = 0
        Q_Old = 0
         Flag = 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
    
    '---------------------------- Initialize LCD ----------------------------
    
    Lcdout $fe, 1                               ' Clear LCD screen
    Lcdout "System Ready"                       ' Display message
    Pause 500                                   ' Wait .5 second
    
    Goto Main_Loop
    
    Encoder:                                    ' ISR 
    
    Q_New = PortB.7 + PortB.7 + PortB.6         ' get port status
     
    If M_Count[Q_Old] = Q_New then              ' if M_Count code satisfied then minus
              Q_Count = Q_Count - 1
                 Flag = 1                       ' set flag to true to update display
    goto Q_Skip
    endif
    
    If P_Count[Q_Old] = Q_New then              ' if M_Count code satisfied then plus
              Q_Count = Q_Count + 1
                 Flag = 1                       ' set flag to true to update display
    endif
    
    Q_Skip:
    
    Q_Old = Q_New                               ' update Q_Old Byte
     
    @ INT_RETURN
    
    Main_Loop:   
    
    if Flag = 1 then
    Lcdout $fe, 1                              ' Clear LCD screen
    lcdout dec Q_Count                         ' Display encoder position (counts)
    Flag = 0                                   ' reset flag to false, display updated
    endif
    
    goto Main_Loop
    
    end
    Cheers

    Al.
    All progress began with an idea

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


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Thanks! Aratti...

    I had just finished adapting your earlier example for a 16F690 pic. It took me a while to figure out why my LCD was not working... Dang MCLR pin tripped me up untill I rememberd that it could not act as an output... then I had to move my LCD to PortC... then I had to figure out that my OLD 4 line LCD (bought it about 20 Years ago from AllElectronics) needed more time on the LCD_COMMANDUS.

    Anyway it is working GREAT...
    I will give your latest code a spin this weekend.

    As far as update time to the LCD... it seems to be limited to any Pause or other code that is in the Main Loop. I had been blinking an LED in the main loop using a Pause statement and therefore this limited the LCD update frequency. I think I'll try and impliment another DT-Int using a timer to run the blinking LED in the main loop. This should remove any excessive delay to the LCD update.

    PS. I can post Aratti's modified working code for the 16F690 if anybody is interested...
    Last edited by Heckler; - 9th November 2011 at 15:14.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

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