My rotary encoder adventures


+ Reply to Thread
Results 1 to 17 of 17

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    169


    Did you find this post helpful? Yes | No

    Default Re: My rotary encoder adventures

    Quote Originally Posted by HenrikOlsson View Post
    Very good, then there's plenty of margin.
    Next challenge: Velocity sensing, ie a way to detect that the user wants the value to change by a lot and not have to turn the knob 600 turns. Getting that just right seems to be tricky, even the big boys don't always... (I've never tried).
    I'm in the middle of a project requiring a rotary encoder to set a weight target for a loadcell. I plan on using Richard's graphical LCD driver for a dot matrix display (not superfast update refreshing), so I actually needed something like what you've described there Henrik. Using Demon's code, I twiddled it a bit to utilise (elapsed) Timer0 on an 18F26K22 to provide the screen refreshing. This is what I basically came up with (which I'm happy with):

    Code:
    'REMOVED ALL THE INIALIZATION STUFF FOR THIS PASTING
    
    Enc1_counter = 1000
    
    SETFONT FONT5x7
    'SETFONT bignum5
    LATC.4 = 0 'using a pic pin output as a temporary ground rail for the rotary encoder
    gosub grf_clr
    
    T0CON.7 = 1 'enable timer 0
    T0CON.6 = 0 'timer 0 in 16 bit mode
    T0CON.5 = 0 'timer 0 clock source = internal instruction clock
    'T0CON.4 
    T0CON.3 = 0 'timer prescaler assigned
    T0CON.2 = 0 ' prescaler assigned to 1:4
    T0CON.1 = 0 ' "
    T0CON.0 = 1 ' "
    
    Timer_Counter = 0
    Change = 0
    
    MainLoop:
        Timer_Counter.lowbyte = TMR0L
        Timer_Counter.highbyte = TMR0H    
        New_Encoder = (PortB.4 << 1) + PortB.5
    
        IF New_Encoder != Old_Encoder THEN
            IF New_Encoder = %00000011 THEN
                IF Old_Encoder = %00000010 THEN
                    Change = Change+1
                    Enc1_counter = Enc1_counter + Change 
                ELSE
                    IF Old_Encoder = %00000001 THEN
                        Change = Change+1
                        Enc1_counter = Enc1_counter - Change      
                    ENDIF
                ENDIF
            ELSE
                IF New_Encoder = %00000000 THEN
                    IF Old_Encoder = %00000001 THEN
                        Change = Change+1
                        Enc1_counter = Enc1_counter + Change  
                    ELSE
                        IF Old_Encoder = %00000010 THEN
                            Change = Change+1
                            Enc1_counter = Enc1_counter - Change    
                        ENDIF
                    ENDIF
                ENDIF
            ENDIF
        ENDIF
        
        if change >= 1 and Timer_Counter>60000 then
            TMR0H=0
            TMR0L=0 
            ARRAYWRITE BUFF,[sdec Enc1_counter,0]
            gosub grf_clr
            'DMDSTR 10,10, BUFFp,1
            DMDSTR 10,10, BUFF,1
            gosub show
            Change = 0
      else
            pause 5
        endif
        
        Old_Encoder = New_Encoder
    
    goto MainLoop:
    Troy
    Last edited by rocket_troy; - 4th October 2024 at 07:18.

Similar Threads

  1. Rotary encoder with DT interrupts
    By louislouis in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 21st March 2021, 15:45
  2. Rotary encoder subroutine
    By Fanias in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th October 2018, 13:13
  3. Using an incremental rotary encoder?
    By keithv in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th January 2016, 22:23
  4. New approach to Rotary Encoder
    By Ioannis in forum Code Examples
    Replies: 90
    Last Post: - 11th July 2013, 21:05

Members who have read this thread : 11

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