HEDS5540 optical encoder


Closed Thread
Results 1 to 24 of 24

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    frac= ((posn1+posn2)//100 +10)/10 ;round it up
    is incorrect , it will round the number up always

    frac= ((posn1+posn2)//100 +5)/10 ;round it up
    is the way to round the number up if the last digit is 5 or more
    Warning I'm not a teacher

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,689


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    using n-bit math

    result

    ready v3
    turns 23 f 489
    33609 3664.3145
    turns 23 f 486
    33606 3663.9875
    turns 23 f 484
    33604 3663.7694



    Code:
     Include "N-Bit_MATH.pbp" 
     
    enc_new VAR BYTE   bank0
    enc_old VAR BYTE   bank0
    enc_counter VAR WORD   bank0
    Flag var BYTE        bank0
    tmp         var  byte[4]
    res         var  byte[4]
    tmp1        var  byte[4]
    posn        var WORD
    turns       var word
    frac        var WORD
     
     
     
    ; Set variable value @ startup
    Flag = 1                   ;preset a value    for testing
    enc_new = 0
    enc_old= 0
    enc_counter = 33609      ;preset a value    for testing
    
     
    Main_Loop:     
    if Flag = 1 then
    turns=  enc_counter /1440
    frac=   enc_counter //1440
    serout2 PORTa.0,84, [13,10,"turns ",#turns,9,"f ",#frac ] 
    if frac then
    @  MOVE?CP 157000, _tmp  
    @  MOVE?WP _frac, _tmp1  
    @  MATH_MUL  _tmp1, _tmp, _res 
    @  MOVE?CP 1440000, _tmp
    @  MATH_DIV  _res, _tmp, _tmp1
    @  MOVE?PW _tmp1, _posn
    @  MOVE?CP 144, _tmp
    @  MATH_DIV  REG_Z, _tmp, _tmp1
    @  MOVE?PW _tmp1, _frac
    endif
    posn=posn+turns*157
    serout2 PORTa.0,84, [13,10,#enc_counter,9,dec4 posn,".",dec4 frac       ] 
     
     
     
    Flag = 0
    endif
    goto Main_Loop
    end
    Warning I'm not a teacher

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    I try to compile your code. I downloaded the N-Bit_MATH.pbp include, but the compiler stops with this error message:
    "Bad data type"

    If I try to compile only the N-Bit_MATH.pbp (only for test) and its do not compile. Stops on first line:
    REG_X VAR BYTE[PRECISION] BANK0 SYSTEM
    with the same error message MCU is the same 16F628A, For test I try also 16F688 with the same result. Something I'am doing wrong, I think.
    Last edited by louislouis; - 11th October 2016 at 14:05.

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


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    You need to define the constant PRECISION and assign it a value equal to the number of bytes to use for the values in the math routine. And you need to do this before including N-Bit. In this case it looks like Richard opted for 32bit values (4 bytes)
    Code:
    PRECISION CON 4 SYSTEM    '32bit values
    Include "N-Bit_Math.pbp"
    If you open the N-Bit file you'll find a link to the thread on this forum: http://www.picbasic.co.uk/forum/showthread.php?t=12433

    /Henrik.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: HEDS5540 optical encoder

    Yes, right. I forgot this line. After putting this line as you say the code compiles OK. Now I do some testing and trying to understand the code.

Similar Threads

  1. 2 Beam Optical Pulse Generator
    By WOZZY-2010 in forum Schematics
    Replies: 8
    Last Post: - 6th April 2010, 05:03
  2. USB Optical mouse as motor encoder
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 24th August 2008, 16:09
  3. optical voice link
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th May 2008, 22:11
  4. Using a optical coder
    By debutpic in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 16th October 2007, 05:57
  5. Using the optical sensor from a ball mouse
    By lsteele in forum General
    Replies: 5
    Last Post: - 5th September 2007, 17:45

Members who have read this thread : 0

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