My rotary encoder adventures


Results 1 to 17 of 17

Threaded View

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


    Did you find this post helpful? Yes | No

    Default Re: My rotary encoder adventures

    You have turned off the WDT (for speed I suppose) but forgot to tell PBP about it so no speed gained.

    Anyway, try this as your encoder logic and see if it beats 26us.
    Code:
    New_Encoder 	VAR BYTE
    Old_Encoder     VAR BYTE
    Test            VAR BYTE
    Enc1_counter    VAR WORD
    
    BlinkLED1           VAR LatB.5         ' Switch these ON/OFF to determine time
    BlinkLED2           VAR LatB.4 
    
    MainLoop:    
        BlinkLED1 = 1                       ' Top of LOOP on Logic 2 probe
        BlinkLED2 = 0
    
        New_Encoder = PortA & %00000011                 ' Read encoder signals on RA0 and RA1
        Test = New_Encoder ^ Old_Encoder                ' Bitwise XOR current state with previous state to see if any pins changed.
            
        IF Test.0 = 1 THEN                              ' Edge detected on channel A?
            IF Old_Encoder.0 ^ Old_Encoder.1 THEN       ' If Old_Encoder is 0 or 3 we count up, otherwise we count down.
                Enc1_counter = Enc1_counter + 1
            ELSE
                Enc1_counter = Enc1_counter - 1
            ENDIF
            GOTO MainLoop        
        ENDIF
    
        IF Test.1 = 1 THEN                              ' Edge detected on channel B?
            IF Old_Encoder.0 ^/ Old_Encoder.1 THEN      ' If Old_Encoder is 1 or 2 we count up, otherwise we count down.
                Enc1_counter = Enc1_counter + 1
            ELSE
                Enc1_counter = Enc1_counter - 1
            ENDIF
            Goto MainLoop
        ENDIF
    
        BlinkLED1 = 0                       ' Bottom of IFs on Logic 2 probe
        BlinkLED2 = 1
        
        ' Other stuff here...
        
        Goto MainLoop
    EDIT: Oh, the timing measurment will not work properly since we're jumping back to mainloop before flipping the LEDs but you can fix that.
    Last edited by HenrikOlsson; - 19th August 2024 at 20:19.

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 : 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