Can PBP read optical encoder damn fast? :)


Closed Thread
Results 1 to 40 of 47

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    For optical encoder I use this:
    Code:
    ; PIC18F4520 
    
    DEFINE OSC 4
    
    INCLUDE "DT_INTS-14.bas"        ; Base Interrupt System
    INCLUDE "ReEnterPBP.bas"        ; Include if using PBP interrupts
    
    TRISA    = %00000000 ' Make all PortA pins output
    TRISB    = %11001000
    
    Define LCD_DREG PORTA
    Define LCD_DBIT 0
    Define LCD_RSREG PORTA
    define LCD_RSBIT 4
    define LCD_EREG PORTA 
    define LCD_EBIT 6
    define LCD_BITS 4
    define LCD_LINES 2
    define LCD_COMMANDUS 2000
    define LCD_DATAUS 50
    
    ; encoder ch A PortB.6
    ; encoder ch B PortB.7
    
    wsave       VAR BYTE $70     SYSTEM  ; alternate save location for W 
    enc_new     VAR BYTE bank0
    enc_old     VAR BYTE bank0
    enc_counter VAR word bank0
    Flag        var BYTE bank0
    
    asm
    INT_LIST  macro    ; IntSource,        Label,  Type, Resetflag?
            INT_Handler    RBC_INT, _enc,  ASM,  yes
        endm
        INT_CREATE     ; Creates the interrupt processor
    endasm
    
    ; Set variable value @ startup       
    enc_new = 0
    enc_old= 0
    enc_counter = 0
    Flag = 0
    @ INT_ENABLE RBC_INT  ; enable external (INT) interrupts 
    
    Main_Loop:                                         
    Lcdout $fe, 1
    lcdout  Dec enc_counter  
    goto Main_Loop
    
    enc:
    asm    
        ;Read latest input from PORTB & put the value in _enc_new.
             MOVE?CB 1,_Flag
             movf    PORTB,W
             movwf  _enc_new
                                   ;Strip off all but the 2 MSBs in _enc_new.
             movlw    B'11000000'  ;Create bit mask (bits 7 & 6).
             andwf   _enc_new,F    ;Zero bits 5 thru 0.
                                   ;Determine the direction of the Rotary encoder.  
             rlcf     _enc_old,F   ;left shift it into _enc_old to align bit 6 of 
                                   ;_enc_old with bit 7 of _enc_new.
             movf    _enc_new,W    ;Move the contents of _enc_new to W in order to XOR.
             xorwf   _enc_old,F    ;XOR previous inputs (in _enc_old) with latest
                                   ;inputs (in W) to determine CW or CCW.
     
             btfsc   _enc_old,7    ;Test bit 7 of result (in _enc_old).  Skip next line
                                   ;if it is 0 (direction is CCW).
             goto    Up            ;Bit is 1 (direction is CW).  Go around Down
                                   ;and increment counter.
    
    Down
         ;Decrements _enc_counter because the rotary encoder moved CCW.
         ;Decrements _enc_counter (16 bit value), sets Z on exit.
                  
            decf    _enc_counter,F      ; Decrement low byte
            incfsz  _enc_counter,W      ; Check for underflow
            incf    _enc_counter+1,F    ; Update
            decf    _enc_counter+1,F    ; Fixup
            movf    _enc_counter,W
            iorwf   _enc_counter+1,W    ; Set Z bit
            
        ;Add here code for the CCW LED if needed.
             
             goto    Continue          ;Branch around UP.
    Up
        ;Increments _enc_counter because the rotary encoder moved CW.
        ;Increments _enc_counter (16 bit value), sets Z on exit.
            incfsz  _enc_counter,W      ; Add one to low byte
            decf    _enc_counter+1,F    ; No carry (negates next step)
            incf    _enc_counter+1,F    ; Add one to high byte
            movwf   _enc_counter        ; Store updated low byte back.
            iorwf   _enc_counter+1,W    ; Set Z flag
            
        ;Add here code for the CW LED if needed.
        
    Continue 
             
        ;Assign the latest encoder inputs (in _enc_new) to _enc_old.
             movf     _enc_new,W
             movwf   _enc_old
             INT_RETURN
        ;============ END OF THE ROTARY ENCODER CODE =====
        endasm

  2. #2
    Join Date
    Feb 2013
    Posts
    1,153


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    Yes I checked that PIC datasheet already, but I don't see any ways to implement it in PBP, we don't have statement for encoder reading, like we have for LCDOUT or OWIN.

  3. #3
    Join Date
    Feb 2013
    Posts
    1,153


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    I've modified my code and it works ok, but sometimes I get false readings too - say for 20 pulses of CW, I will have 2-3 pulses of CCW and vise versa

    Code:
    STATE: 'WAIT FOR ENCODER TO BE MOVED (STATE CHANGE)
    'Lcdout $Fe, $1, "WAIT..." , DEC ENX, " ", DEC ENY
    X=ENX
    Y=ENY
    PAUSE 1
    X1=ENX
    Y1=ENY
    IF X<>X1 OR Y<>Y1 THEN GOTO SOMELOOP
    GOTO STATE
    
    
    SOMELOOP: 'COUNTER LOOP FOR PULSE LENGTH COUNTING
    IF ENX=1 THEN X1IN=X1IN+1 'X INCREMENT
    IF ENY=1 THEN Y1IN=Y1IN+1 'Y INCREMENT
    IF ENX=0 and ENY=0 THEN GOTO ANALYZE 'EXIT AND COMPARE LENGTHS
    PAUSE 1
    GOTO SOMELOOP
    
    
    ANALYZE: 'DETERMINE DIRECTION AND INCREMENT CORRESPONDING VARIABLE
    IF X1IN>Y1IN THEN 
    CCW=CCW+1
    ELSE
    CW=CW+1
    ENDIF
    LCDOUT $FE, $1, "RAW", DEC Y1IN, "  ", DEC X1IN, "  "
    LCDOUT $FE, $C0, "VAL ", DEC CCW, "  ", DEC CW, "  "
    PAUSE 1
    X1IN=0 'RESET VARIABLES
    Y1IN=0 
    GOTO STATE

  4. #4
    Join Date
    Feb 2013
    Posts
    1,153


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    This is simple 3 pin incremental encoder, middle pin is tied to VCC, A and B are tied to PORTB.3 PORTB.4, which are also pull down with 10K resistors. If I rotate shaft slowly, then no false readings. What can be reason, some wiring issue or code inefficiency?

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    Quote Originally Posted by CuriousOne View Post
    This is simple 3 pin incremental encoder, middle pin is tied to VCC, A and B are tied to PORTB.3 PORTB.4, which are also pull down with 10K resistors. If I rotate shaft slowly, then no false readings. What can be reason, some wiring issue or code inefficiency?
    Well, your LCD may be slowing down the response. And since you use PAUSE and LCDOUT it is expected that program is not the most efficient. The encoder is best done in ISR and even better using hardware module when available.

    Ioannis

  6. #6
    Join Date
    Feb 2013
    Posts
    1,153


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    I don't see how to avoid use of pause statement. We have time changing, things are happening within time, so how not to consider time? In final device, there will be no LCDOUT, if that is the problem.

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    ISR then.

    Ioannis

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default Re: Can PBP read optical encoder damn fast? :)

    Quote Originally Posted by CuriousOne View Post
    Yes I checked that PIC datasheet already, but I don't see any ways to implement it in PBP, we don't have statement for encoder reading, like we have for LCDOUT or OWIN.
    What stops you from writing directly to the registers?

    Ioannis

Similar Threads

  1. HEDS5540 optical encoder
    By louislouis in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 11th October 2016, 23:23
  2. how to read ky40 mechanical encoder ?
    By iw2fvo in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 25th October 2015, 16:22
  3. Probe to read quadrature encoder with DT_INT need help
    By phoenix_1 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 31st August 2009, 20:43
  4. USB Optical mouse as motor encoder
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 24th August 2008, 15:09
  5. Damn these low power PIC and other Microcontrollers
    By keithdoxey in forum Off Topic
    Replies: 8
    Last Post: - 12th November 2006, 21:52

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