Mathematics and remainder for PLL MC145158


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1

    Default Mathematics and remainder for PLL MC145158

    Hi All

    I need some help with some mathematics and remainders, which might not be that difficult if you know how to handle it

    Here is what I want to do and calculate in pbp:

    I want to determine the A and N counters from a dual modulus PLL MC145158 for who might be familiar with it, if not:
    I receive with serin a value which is 6 digits with 1 decimal point a frequency in Khz with 1 decimal
    ex: 433687,5 which is Freq as variable or if easyer 433 (in one variable) , and 6875 in another

    So:

    Freq = var ??? (because it is bigger than 16bit

    I have 3 variable, one is fixed which needs to be calculated as followed:

    R = var word (14bit)
    R = 800 '(fix value untill now)
    N = var word (10bit) 'to be calculated
    A = var word (7bit) 'to be calculated
    temp var word


    here is what I need to do in PBP comming from a original C source:

    unsigned long temp,nlong,along
    Temp = (501800 - 433687,5) * 800
    Nlong = temp/64000
    Along = (temp-(Nlong*64000))/1000
    N = (unsigned int) Nlong
    A= (unsigned int)Along




    example: freq is 433,6875 Mhz
    temp = (501800 - 433687,5) * 800 = 54490000
    Nlong = temp/64000 = 54490000/64000 = 851,40625000 (64000 is a fixed divider ratio)
    Along = ((Nlong*64000))/1000 = (0,406250*64000))/1000 = 26 '0,406250 is remainder from 851,406250

    result should be:
    N = 851 without remainder
    A = 26

    Anyone who can help me

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


    Did you find this post helpful? Yes | No

    Default

    HI RFsolution,

    I was trying to work thru this calculation, but I have a question. There's seems to be 2 different ways of calculating "Along".

    In the "here is what I need to do " section, it's...
    Along = (temp-(Nlong*64000))/1000

    Then in the example, it's
    Along = ((Nlong*64000))/1000

    For now, I'm assuming the second one is correct, but just wanted to verify it with you.<br><br>
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Well, nevermind, I got it figured out anyway.

    Give this a try.
    Code:
    <font color="#000000"><b>Freq1      </b><font color="#008000"><b>VAR WORD
    </b></font><b>Freq2      </b><font color="#008000"><b>VAR WORD
    
    </b></font><b>N          </b><font color="#008000"><b>VAR WORD
    </b></font><b>A          </b><font color="#008000"><b>VAR WORD
    </b></font><b>Remainder  </b><font color="#008000"><b>VAR WORD
    
    
    </b></font><font color="#0000FF"><b><i>; --- Frequency must be split into 2 variables ---
    </i></b></font><b>Freq1 </b>= <b>4336   </b><font color="#0000FF"><b><i>; 433687.5
    </i></b></font><b>Freq2 </b>= <b>875
    </b><font color="#008000"><b>GOSUB </b></font><b>PLL_Calc
    
    </b><font color="#008000"><b>STOP
    
    </b></font><b>PLL_Calc</b>:
        <b>N </b>= (<b>5018 </b>- <b>Freq1</b>)     
        <font color="#008000"><b>IF </b></font><b>Freq2 </b>&gt; <b>0 </b><font 
    
    color="#008000"><b>THEN          
            </b></font><b>A </b>= <b>1000 </b>- <b>Freq2   
            N </b>= <b>N </b>- <b>1      
        </b><font color="#008000"><b>ELSE                       
            </b></font><b>A </b>= <b>0              
        </b><font color="#008000"><b>ENDIF                      
        </b></font><b>N </b>= <b>N </b>* <b>8          
        A </b>= <b>A </b>* <b>8          
    
        N </b>= <b>N </b>+ (<b>A</b>/<b>1000</b>)       
        <b>A </b>= <b>A </b>- (<b>A</b>/<b>1000</b>*<b>1000</b>)  
            
        <b>N </b>= <b>N</b>*<b>10                   
        N </b>= <font color="#008000"><b>DIV32 </b></font><b>64                   
        Remainder </b>= <b>R2                     
        Remainder </b>= <b>Remainder </b>* <b>10000      
        Remainder </b>= <font color="#008000"><b>DIV32 </b></font><b>64               
        
        A </b>= <b>A </b>* <b>10000              
        A </b>= <font color="#008000"><b>DIV32 </b></font><b>64                   
        
        A </b>= <b>A </b>+ <b>Remainder
        A </b>= <b>A </b>* <b>64
        A </b>= <font color="#008000"><b>DIV32 </b></font><b>10000
        Remainder </b>= <b>R2
        </b><font color="#008000"><b>IF </b></font><b>Remainder </b>&gt;= <b>5000 </b><font 
    
    color="#008000"><b>THEN </b></font><b>A </b>= <b>A </b>+ <b>1
    </b><font color="#008000"><b>RETURN</b></font>
    Last edited by Darrel Taylor; - 18th August 2005 at 07:45.
    DT

  4. #4


    Did you find this post helpful? Yes | No

    Default not yet OK

    Hi darrel

    I gave it a try but I think there is still a problem

    As I told you it is a part of the C routine, but if i take the remainder from
    :
    Nlong = temp/64000 = 54490000/64000 = 851,40625000
    so the remainder is 0,40625000 and multiply by 64000 divide by 1000
    is giving me the correct A value

    Tis is not the case with your example

    Can you have a look ?, you can try any example of freq (in the range 430000.0 til 440000.0 following my example

    Thanks again

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


    Did you find this post helpful? Yes | No

    Default

    OOPS, you're right.

    Somehow, in the process of posting it, I lost two 0's in the third DIV32.

    This works better.
    Code:
    <font color="#000000"><b>Freq1      </b><font color="#008000"><b>VAR WORD
    </b></font><b>Freq2      </b><font color="#008000"><b>VAR WORD
    
    </b></font><b>N          </b><font color="#008000"><b>VAR WORD
    </b></font><b>A          </b><font color="#008000"><b>VAR WORD
    </b></font><b>Remainder  </b><font color="#008000"><b>VAR WORD
    
    
    </b></font><font color="#0000FF"><b><i>; --- Frequency must be split into 2 variables ---
    </i></b></font><b>Freq1 </b>= <b>4336   </b><font color="#0000FF"><b><i>; 433687.5
    </i></b></font><b>Freq2 </b>= <b>875
    </b><font color="#008000"><b>GOSUB </b></font><b>PLL_Calc
    
    </b><font color="#008000"><b>STOP
    
    </b></font><b>PLL_Calc</b>:
        <b>N </b>= (<b>5018 </b>- <b>Freq1</b>)     
        <font color="#008000"><b>IF </b></font><b>Freq2 </b>&gt; <b>0 </b><font 
    
    color="#008000"><b>THEN          
            </b></font><b>A </b>= <b>1000 </b>- <b>Freq2   
            N </b>= <b>N </b>- <b>1      
        </b><font color="#008000"><b>ELSE                       
            </b></font><b>A </b>= <b>0              
        </b><font color="#008000"><b>ENDIF                      
        </b></font><b>N </b>= <b>N </b>* <b>8          
        A </b>= <b>A </b>* <b>8          
    
        N </b>= <b>N </b>+ (<b>A</b>/<b>1000</b>)       
        <b>A </b>= <b>A </b>- (<b>A</b>/<b>1000</b>*<b>1000</b>)  
            
        <b>N </b>= <b>N</b>*<b>10                   
        N </b>= <font color="#008000"><b>DIV32 </b></font><b>64                   
        Remainder </b>= <b>R2                     
        Remainder </b>= <b>Remainder </b>* <b>10000      
        Remainder </b>= <font color="#008000"><b>DIV32 </b></font><b>64               
        
        A </b>= <b>A </b>* <b>10000              
        A </b>= <font color="#008000"><b>DIV32 </b></font><b>6400       <font color="#0000FF">; <-- Changed, was 64</font>           
        
        A </b>= <b>A </b>+ <b>Remainder
        A </b>= <b>A </b>* <b>64
        A </b>= <font color="#008000"><b>DIV32 </b></font><b>10000
        Remainder </b>= <b>R2
        </b><font color="#008000"><b>IF </b></font><b>Remainder </b>&gt;= <b>5000 </b><font 
    
    color="#008000"><b>THEN </b></font><b>A </b>= <b>A </b>+ <b>1
    </b><font color="#008000"><b>RETURN</b></font>
    Last edited by Darrel Taylor; - 21st August 2005 at 23:10.
    DT

  6. #6


    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 ?

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