Mathematics and remainder for PLL MC145158


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default still not Ok

    Sorry Darrel, can you have a look again ?
    In the code A = (A/1000*1000) does not do anything
    So the A counter value is not calculated correctly


    Is it possible to use for freq1 only the 3 first digits
    for freq2 4 digits, it makes more sense and easyer for the serin2 command
    to enter the completer frequency

    433.6875 Mhz so we can split 433 and 6875 into 2 variables

    What you think ?

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    In the code A = (A/1000*1000) does not do anything
    Yes it does! Since PBP only works with integers, dividing by 1000 then multiplying by 1000 will isolate any digits over 1000. With Freq2 only being 3 digits, anything over 1000 gets added to the N value.


    Is it possible to use for freq1 only the 3 first digits for freq2 4 digits
    No. Due to overflow and accuracy problems, Freq2 must only be 3 digits. Since, the numbers are being parsed from ascii text, it shouldn't matter how many digits are in each parameter.

    Here are some sample results of actually running the routine on a PIC16F877.
    Scroll to the right for the PIC results.
    Code:
    <table BORDER="1" CELLSPACING="0" BORDERCOLOR="#000000" CELLPADDING="2" WIDTH="789" height="279">
      <tr>
        <td WIDTH="11%" HEIGHT="23">
          <p></p>
        </td>
        <td WIDTH="9%" HEIGHT="23">
          <p></p>
        </td>
        <td WIDTH="9%" HEIGHT="23">
          <p></p>
        </td>
        <td WIDTH="3%" HEIGHT="213" rowspan="11">
          <p></p>
        </td>
        <td WIDTH="48%" HEIGHT="23" colspan="5">
          <p ALIGN="CENTER">Calculated</td>
        <td WIDTH="2%" HEIGHT="213" rowspan="11">
          <p>&nbsp;</p>
          <p>&nbsp;</p>
          <p>&nbsp;</p>
          <p>&nbsp;</p>
          <p>&nbsp;</p>
          <p>&nbsp;</p>
        </td>
        <td WIDTH="19%" COLSPAN="2" HEIGHT="23">
          <p ALIGN="CENTER">Results from PIC</td>
      </tr>
      <tr>
        <td WIDTH="11%" align="center" ><b>
          <p >Freq</b></td>
        <td WIDTH="9%" align="center" ><b>
          <p >Freq1</b></td>
        <td WIDTH="9%" align="center" ><b>
          <p >Freq2</b></td>
        <td WIDTH="11%" ><b>
          <p >C Result N</b></td>
        <td WIDTH="8%" ><b>
          <p >INT N</font></b></td>
        <td WIDTH="11%" ><b>
          <p >Remainder</font></b></td>
        <td WIDTH="11%" ><b>
          <p >C Result A</font></b></td>
        <td WIDTH="7%" ><b>
          <p >INT A</font></b></td>
        <td WIDTH="10%" align="center" ><b>
          <p >N</font></b></td>
        <td WIDTH="9%" align="center" ><b>
          <p >&nbsp;A</font></b></td>
      </tr>
      <tr>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">430000.0</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">4300</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">0</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">897.5</font></td>
        <td WIDTH="8%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">897</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">0.5</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">32</font></td>
        <td WIDTH="7%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">32</font></td>
        <td WIDTH="10%" >
          <p ALIGN="RIGHT">897</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">32</font></td>
      </tr>
      <tr>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">430816.5</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">4308</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">165</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">887.29375</font></td>
        <td WIDTH="8%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">887</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">0.29375</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">18.8</font></td>
        <td WIDTH="7%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">19</font></td>
        <td WIDTH="10%" >
          <p ALIGN="RIGHT">887</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">19</font></td>
      </tr>
      <tr>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">431123.3</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">4311</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">233</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">883.45875</font></td>
        <td WIDTH="8%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">883</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">0.45875</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">29.36</font></td>
        <td WIDTH="7%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">29</font></td>
        <td WIDTH="10%" >
          <p ALIGN="RIGHT">883</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">29</font></td>
      </tr>
      <tr>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">432000.0</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">4320</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">0</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">872.5</font></td>
        <td WIDTH="8%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">872</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">0.5</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">32</font></td>
        <td WIDTH="7%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">32</font></td>
        <td WIDTH="10%" >
          <p ALIGN="RIGHT">872</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">32</font></td>
      </tr>
      <tr>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">432086.1</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">4320</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">861</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">871.42375</font></td>
        <td WIDTH="8%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">871</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">0.42375</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">27.12</font></td>
        <td WIDTH="7%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">27</font></td>
        <td WIDTH="10%" >
          <p ALIGN="RIGHT">871</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">27</font></td>
      </tr>
      <tr>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">433010.7</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">4330</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">107</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">859.86625</font></td>
        <td WIDTH="8%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">859</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">0.86625</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">55.44</font></td>
        <td WIDTH="7%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">55</font></td>
        <td WIDTH="10%" >
          <p ALIGN="RIGHT">859</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">55</font></td>
      </tr>
      <tr>
        <td WIDTH="11%" ><b>
          <p ALIGN="RIGHT">433687.5</font></b></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">4336</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">875</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">851.40625</font></td>
        <td WIDTH="8%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">851</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">0.40625</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">26</font></td>
        <td WIDTH="7%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">26</font></td>
        <td WIDTH="10%" >
          <p ALIGN="RIGHT">851</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">26</font></td>
      </tr>
      <tr>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">438214.0</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">4382</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">140</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">794.825</font></td>
        <td WIDTH="8%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">794</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">0.825</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">52.8</font></td>
        <td WIDTH="7%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">53</font></td>
        <td WIDTH="10%" >
          <p ALIGN="RIGHT">794</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">53</font></td>
      </tr>
      <tr>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">440000.0</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">4400</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">0</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">772.5</font></td>
        <td WIDTH="8%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">772</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">0.5</font></td>
        <td WIDTH="11%" >
          <p ALIGN="RIGHT">32</font></td>
        <td WIDTH="7%" BGCOLOR="#ffff00" >
          <p ALIGN="RIGHT">32</font></td>
        <td WIDTH="10%" >
          <p ALIGN="RIGHT">772</font></td>
        <td WIDTH="9%" >
          <p ALIGN="RIGHT">32</font></td>
      </tr>
    </table>
    The only possibility I can see for getting the wrong answer, is that I am rounding the A value in the last line of the routine. You had only requested the integer. If you don't want it to be rounded, simply comment out the ...

    IF Remainder >= 5000 THEN A = A + 1

    If you still get the wrong numbers, let me know.

    <br>
    Last edited by Darrel Taylor; - 22nd August 2005 at 19:21.
    DT

  3. #3


    Did you find this post helpful? Yes | No

    Default oeps

    Hi Darrel

    Oeps, I changed A var word to A var byte to save some place
    But as A is only 127, but you use the word space to calculate

    Thanks again, it looks that it works

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Just curious where the "R2" comes from?

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    R2 is one of PBP's system variables. They are defined in the PBPPICxx.RAM files, located in the PBP folder.

    In this case R2 holds the modulas (remainder) of the DIV32 command.

    For more info ... http://www.picbasic.co.uk/forum/showthread.php?t=48

    <br>
    DT

  6. #6


    Did you find this post helpful? Yes | No

    Default

    So this is basically the same as the // but only for DIV32?

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Correct!<br><br>
    DT

  8. #8
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Question

    Quote Originally Posted by Darrel Taylor
    R2 is one of PBP's system variables. They are defined in the PBPPICxx.RAM files, located in the PBP folder.

    In this case R2 holds the modulas (remainder) of the DIV32 command.

    <br>
    I was looking through this file, hoping to learn some of that voodoo that Darrel pops on us time to time... but all the definition said was "System Register". Does anyone know just what exactly these other variables mean or do? Is it documented somewhere that I have missed?

  9. #9
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I've been found out. I guess I can take those pins out of my Rhinoceros doll now.

    While the PBPPICxx.RAM declares the system variables, the PBPPICxx.LIB and PBPPICxx.MAC files are where you'll find out how they are used.

    For instance, with the DIV32 command on an 18F part... Open up the PBPPIC18.LIB file and search for DIV32. You'll find this header in front of the code for the DIV32 command.
    Code:
    ;************************************************
    ;* DIV32      : 31 x 15 divide
    ;*                                    
    ;* Input      : R0, R2 = 32 bits 
    ;*            : R1 + 1, W = divisor
    ;* Output     : R0 = quotient    
    ;*            : R2 = remainder      
    ;*                                        
    ;* Notes      : Uses result from MUL. 
    ;************************************************
    From that you can see that R2 is the remainder of a DIV32, even though it's not mentioned anywhere in the manual.

    Most of the PBP macros have a header showing the input and output variables, but not necessarily all the system variables that are used by that routine. For that you have to digest the code and follow subroutine calls.

    As far as I know, there is no list of macro's or the system variables that they use. My guess is that it would create a tech support nightmare for them if they put that kind of stuff in the manual. Can't blame them.

    <br>
    DT

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