+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2012
    Posts
    15

    Default Rotary encoder subroutine

    Hello,

    I have the following subroutine which reads a 2bit gray code rotary encoder.
    This encoder has also a push button I need to use for other functions.
    The idea is to built a menu system to change some settings for my project
    and I thought that a rotary encoder is ideal for that.

    I press the button, I get in the menu, I rotate the encoder to select a value and
    then I press the button again to go to next setting, etc...

    I have searched the forum and I got some ideas how to make the rotary encoder work,
    and I ended with the following code which is fine for my setup.

    My problem is that I use the following code as a subroutine and I call this from many
    places in my program.

    Maybe it's something simple but I cannot think of something right now.
    If you look at the code and comments, I want to get out of the loop when the button is pressed
    to go the next menu, but since I'm in a subroutine I cannot use a GOTO, which would be
    fine if it wasn't an overflow and PIC reset.

    The need for the loop is to read the ports for any change, if the rotary encoder has moved and
    wait if the there is no movement.

    So, do you have any ideas how I can get out of this loop ?


    Code:
    ' Rotary encoder is connected to B4 and B5, pull ups enabled.
    ' OLDR and NEWR are variables holding the states of ports B4 and B5
    ' LERI is a variable used to determine CW or CCW rotary encoder movement.
    '
    ROTARY:
    '
    PAUSE 15                                             ' Needed for smooth rotary encoder operation 
    OLDR = PORTB & $30                           ' Mask other and get only Ports B4 and B5
    GOSUB ROTARY1
    RETURN
    '
    ROTARY1:
    '
    NEWR = PORTB & $30                              ' Mask other ports and get only B4 and B5
    
    ' IF BUT = 0 THEN GOTO MENU2               ' This is the wrong way to get out, overflow error !!!
    ' I need to get out of this loop when the button is pressed !!!
     
    IF NEWR = $30 THEN ROTARY1                ' Same value - no movement (need to eliminate this)
    LERI.0 = OLDR.4 ^ NEWR.5                     ' XOR old with new (need to find a simpler way)
    LERI.1 = OLDR.5 ^ NEWR.4                     ' XOR old with new (need to find a simpler way)
    ON LERI GOSUB CLCD, CCW, CW              ' Using CLCD as dummy label for index #0 - CCW=1, CW=2
    RETURN
    '
    CCW:                                                     ' Left turn CounterCLockWise
    '
    X = X - 1
    RETURN
    '
    CW:                                                       ' Right Turn ClockWise
    '
    X = X + 1
    RETURN
    '

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,490

    Default Re: Rotary encoder subroutine

    Code:
    ROTARY:
    '
    PAUSE 15                                             ' Needed for smooth rotary encoder operation 
    OLDR = PORTB & $30                           ' Mask other and get only Ports B4 and B5
    GOSUB ROTARY1
    RETURN
    if you need a pause then you are not doing it right


    Code:
    ;IF BUT = 0 THEN GOTO MENU2               ' This is the wrong way to get out, overflow error !!!
    ' I need to get out of this loop when the button is pressed !!!
    
    
    IF BUT = 0 THEN 
    buttonState=1


    IF NEWR = $30 THEN ROTARY1 ' Same value - no movement (need to eliminate this) ??

    assuming last state was $20 or $10 or 0 how can you tell direction ?
    you need to save the last state and compare that with the newstate



    this thread has re/switch code and is worth a study


    http://www.picbasic.co.uk/forum/showthread.php?t=23840
    This is more entertaining than Free to Air TV

Similar Threads

  1. Using an incremental rotary encoder?
    By keithv in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th January 2016, 23:23
  2. Paralel switches with a Rotary encoder
    By critix in forum Code Examples
    Replies: 5
    Last Post: - 18th June 2014, 13:12
  3. New approach to Rotary Encoder
    By Ioannis in forum Code Examples
    Replies: 90
    Last Post: - 11th July 2013, 21:05
  4. Calculate Distance Using Rotary Encoder
    By guanerrr in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 4th May 2012, 16:40

Members who have read this thread : 23

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