How do you convert an algorithm to code?


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231

    Default How do you convert an algorithm to code?

    HI all,

    After overworking the few functional brain cells that I have left. I was able to come up with a formula in Excel that accomplishes getting measurement numbers from a 16 bit timer into a value from 1 to 1000 for use in a Power Control PWM module.
    The 16 bit upper and lower limits can be changed to adjust the PWM to different situations.

    Now, I'm at a bit of a blank stare about how to convert it into code. Anyone think of search terms that might get me started on my homework?

    formula: ((Echo-Min)/(Max-Min))*1000
    Echo= captured value from 16 bit TMR1
    Min= low setting for "0" PWM, (can be changed ) 16 bit
    Max= high setting for 100% PWM (can also be changed) 16 bit

    Thanks
    Bo

  2. #2
    Join Date
    Jan 2009
    Location
    Ankara,TURKEY
    Posts
    45


    Did you find this post helpful? Yes | No

    Wink start from somewhere

    If you have an algorithm,you can write it in PicBasic easily. Decide what you need how you do processes and use logic comparison statement (if then etc.)
    and create neccessary labels to minimize your code.

    Pic Basic Forum community will help you, if you start writing your code
    Electrical & Electronic Engineering
    Undergraduate Student

  3. #3
    timmers's Avatar
    timmers Guest


    Did you find this post helpful? Yes | No

    Talking

    How about..

    RESULT VAR WORD
    ECHO VAR WORD
    MIN VAR WORD
    MAX VAR WORD

    RESULT = ((ECHO - MIN) / (MAX - MIN)) * 1000

    Its hardly rocket science.

    Tim.

  4. #4
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Exclamation

    Quote Originally Posted by timmers View Post
    RESULT = ((ECHO - MIN) / (MAX - MIN)) * 1000
    I think, it won't work:

    (ECHO - MIN) / (MAX - MIN) will be between 0 and 1 and will be truncated to 0.

    Try:

    ZW VAR WORD
    ZW=MAX-MIN:RESULT= (ECHO-MIN)*1000:RESULT=DIV32 ZW

    That will work with 16 Bit-Variables and don't use to big numbers.
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    MIN & MAX are PBP keywords. You'll want to rename these.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231


    Did you find this post helpful? Yes | No

    Default

    Thanks for all of the input. After working on it a while, I ran into the same problems that you guys pointed out. Here is where I ended up. It required LONGs for me to get it to work right. When I started to look at it, I had no idea how to do it. Why is it that some projects seem to make you need every function or process that you are new at?

    Code:
    <font color="#000000"><b>DEFINE </b>OSC 8 
    <b>DEFINE </b>DEBUG_REG PORTA 
    <b>DEFINE </b>DEBUG_BIT 0
    <b>DEFINE </b>DEBUG_BAUD 2400 
    <b>DEFINE </b>DEBUG_MODE 0
    
    TRISA   = %00100000      <font color="#0000FF"><i>' PortA : All outputs except RA.1 for INT_INT
    </i></font>TRISB   = %01000100      <font color="#0000FF"><i>' PWM1,3,5 outputs,RB2=INT, RB0,2,3: DIO
    </i></font>ADCON1  = %00001111      <font color="#0000FF"><i>' Set up ADCON1 
    </i></font>OSCCON  = %11111110      <font color="#0000FF"><i>' INTOSC, 8MHz
    </i></font>EchoTime     <b>VAR    WORD
    </b>Term1        <b>VAR    WORD
    </b>Term2        <b>VAR    WORD
    </b>Term3        <b>VAR    LONG      </b><font color="#0000FF"><i>' making this LONG solved the problem
    </i></font>Duty0        <b>VAR    WORD
    </b>Near0        <b>VAR    WORD
    </b>Far0         <b>VAR    WORD
    </b>EchoTime = 28750              <font color="#0000FF"><i>' test value for the measurement
    </i></font>Near0     = 6500              <font color="#0000FF"><i>' test setting for the minimum reading
    </i></font>Far0     = 51000              <font color="#0000FF"><i>' test setting for the maximum
    </i></font><b>DEBUG </b><font color="#FF0000">&quot;1330-2l&quot;</font>,13,10          <font color="#0000FF"><i>' splash
    
    </i></font>Main:
    Term1 =    Far0 - Near0           <font color="#0000FF"><i>'51000 - 6500 = 44500
    </i></font>Term2 =    EchoTime-Near0         <font color="#0000FF"><i>'25500 - 6500 = 19000
    </i></font>Term3 =    Term2*1000             <font color="#0000FF"><i>'19000 * 1000 = 19000000
    </i></font>Duty0 =    Term3/Term1            <font color="#0000FF"><i>'19000000/44500 = 426.996
    '                ' Word won't work: divisor&gt;32767, Need Long
    
    ' Duty0 = (EchoTime-Near0)/(Far0-Near0)*1000 
     </i></font><b>DEBUG </b>#Term1,<font color="#FF0000">&quot; &quot;</font>,#Term2,<font color="#FF0000">&quot; &quot;</font>,#Duty0,  13,10
     <b>PAUSE </b>700
     <b>GOTO </b>Main
    Thank you all for looking at it.
    Bo

  7. #7
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Wink

    Ähhh....

    What's about the DIV32-command ?
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

  8. #8
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231


    Did you find this post helpful? Yes | No

    Default

    Thought about the DIV32 way, but I hadn't done either before and figured that the LONGs were the newest, so that was where I would concentrate my efforts unless pointed otherwise.

    I'm sure it would work, but I had to have a reason to justify my recent upgrade :-)

    Bo

Similar Threads

  1. Convert code 12C508 to 12F508
    By Olivier in forum General
    Replies: 2
    Last Post: - 25th October 2009, 16:19
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. convert code "c" lcd graphic to pbp
    By nicolasronan in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 19th June 2006, 15:49

Members who have read this thread : 1

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