New approach to Rotary Encoder


Results 1 to 40 of 91

Threaded View

  1. #33
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Step-by-step I think I begin to understand ...
    The correct hardware it's this.
    The software can be this :
    Code:
    @ DEVICE pic16F628A, XT_OSC, WDT_OFF, PWRT_ON, BOD_OFF, MCLR_ON, LVP_OFF
    
       Define   OSC 4           ' 4MHz 
       CMCON = 7                ' Disable on-chip comparator, PORTA in digital mode
    
    include "alldigital.pbp"
    include "C:\PBP\enc_KEY.BAS" 
    
    TRISB = 000000
    PORTB = 000000 
    
    oldState VAR BYTE
    newState VAR BYTE
    DIR VAR BIT
    
    UP CON 1
    DN CON 0
    
    Main_Loop: 
    portb.5 = 1
    gosub keypadscan
    gosub check
    goto main_loop
    
    Check :
    if dir = up then 
    portb.0 = 1
    pause 1000
    portb.0 = 0
    endif
    if dir = dn then
    portb.1 = 1
    pause 1000
    portb.1 = 0
    endif
    return
    
    encoder:
    portb.5 = 0
        newState = (porta & 000111)
    If newState <> oldState THEN        ' Changed from last time?
      Select Case oldState
        Case 2
        If NewState = 4 THEN          ' Was 1 now 4 = Up
          DIR = up
        ELSE                          ' Was 1 now 2 = Down
          DIR = dn
        ENDIF
      
        Case 4
        If NewState = 1  THEN        ' Was 2 now 1 = Up
          DIR = up
        ELSE                           ' Was 2 now 4 = Down
          DIR = dn
        ENDIF
      
        Case 1
        If NewState = 2 THEN        ' Was 4 now 2 = Up
          DIR = up
        ELSE                       ' Was 4 now 1 = Down
          DIR = dn
        ENDIF
      END SELECT
    
      oldState = NewState 
    endif
    portb.5 = 1
    Return
    The question is : where to put "gosub encoder" ? Inside the "enc_key.bas" (it's keypad.bas addapted to 2 row/3 cols) ???
    Attached Images Attached Images  

Members who have read this thread : 3

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