Can PBP read optical encoder damn fast? :)


Closed Thread
Results 1 to 40 of 47

Hybrid View

  1. #1
    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? :)

    Absolutely agree. Code works as expected and is fast enough for manual rotation. Have not tested for faster resposne.

    The encoder indeed has a detent that produces 4x pulses so yeah. It is not appropriate for that code.

    Ioannis

  2. #2
    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? :)

    Not the best programming techniques but uses interrupts and is fast enough for x4 encoders like ALPS with push switch used to reset the position counter.

    Based on Henrik's example and tested on 16F886.

    Code:
    intcon=%10001000
    iocb=7
    
    Encoder VAR BYTE
    Old_Enc VAR BYTE
    Test VAR BYTE
    Dir VAR WORD          ' Must be same size as Position variable
    position    var word
    ch_a var portb.0
    ch_b var portb.1
    detent  var byte
    flag  var bit
    
    clear
    
    INCLUDE "DT_INTS-14.bas"     ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas"     ' Include if using PBP interrupts
    
    wsave   var byte $70 system
    
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler     RBC_INT,    _rotary,   PBP,    yes
        endm
        INT_CREATE 
    ENDASM
    
    @ INT_ENABLE  RBC_INT        ; Enable PortB.0 interrupt for signal reception
    
    goto main
    
    rotary:
    '-------------------------------------------------------------
    '--------------Poll encoder and keep count -------------------
    '-------------Alternative routine with 4x decoding------------
    '-------------------------------------------------------------
    
        Encoder.0 = Ch_A
        Encoder.1 = Ch_B
    
        Test = Encoder ^ Old_Enc
    
        Dir = 0
    
        If Test = 1 THEN
            If Old_Enc = 0 THEN Dir = 1
            IF Old_Enc = 1 THEN DIR = -1
            IF Old_Enc = 2 THEN Dir = -1
            If Old_Enc = 3 Then Dir = 1
        ENDIF
    
        IF Test = 2 THEN
            IF Old_Enc = 0 THEN DIR = -1
            IF Old_Enc = 1 THEN Dir = 1
            IF Old_Enc = 2 THEN Dir = 1
            IF Old_Enc = 3 THEN Dir = -1
        ENDIF
        if !portb.2 then             'Reset counter
            position=0
            flag=1
        endif
        if Old_Enc <> Encoder then
            Old_Enc = Encoder
            detent=detent+1      'Increment position after 4 pulses (one detent on ALPS encoder)
            if detent = 4 then
                Position = Position + Dir
                detent=0
                flag=1
            endif
        endif
    @ INT_RETURN
    
    
    main:
    while 1
    if flag then
        hserout [#position,13,10]
        flag=0
    endif
    
    'other code stuff here
    
    wend
    
    END
    Ioannis

Similar Threads

  1. HEDS5540 optical encoder
    By louislouis in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 12th October 2016, 00:23
  2. how to read ky40 mechanical encoder ?
    By iw2fvo in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 25th October 2015, 17: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, 21:43
  4. USB Optical mouse as motor encoder
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 24th August 2008, 16:09
  5. Damn these low power PIC and other Microcontrollers
    By keithdoxey in forum Off Topic
    Replies: 8
    Last Post: - 12th November 2006, 22: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