aboutsummaryrefslogtreecommitdiffstats
path: root/freqplan.py
blob: 8ad520fe34871d824c63b7954a30232827090d44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3

from fractions import Fraction
from math import floor

xtal = 25

print()
print("# 160MHz and 4.915 MHz on PLLa")

PLLa = xtal * 32
print(f"PLLa = xtal * 32 = {PLLa} MHz")

# x/y = a + b/y
# a = floor(x/y)
# b = x - (a * y)

f1 = 116
a = floor(PLLa / f1)
b = PLLa - (a * f1)
div1 = Fraction(b, f1)
print(f"CLK1: f1 = PLLa / ({a} + {div1.numerator} / {div1.denominator}) = {f1}")

f2 = 4.91521
a = floor(PLLa / f2)
b = PLLa - (a * f2)
div1 = Fraction(round(b * 10000), round(f2 * 10000))
print(f"CLK2: f2 = PLLa / ({a} + {div1.numerator} / {div1.denominator}) = {f2}")


print()
print("# VFO between 23 and 25 MHz on PLLb")

def calculate(frequency):
    divider = floor(900000000 / frequency) # Calculate the division ratio. 900,000,000 is the maximum internal

    if divider % 2:
        divider -= 1 # Ensure an even integer division ratio

    pllFreq = round(divider * frequency) # Calculate the pllFrequency: the divider * desired output frequency

    xtalFreq = xtal * 1000000
    mult = floor(pllFreq / xtalFreq) # Determine the multiplier to get to the required pllFrequency

    l = pllFreq % xtalFreq # It has three parts:

    f = floor(l) # mult is an integer that must be in the range 15..90
    denom = 1048575
    f *= denom # num and denom are the fractional parts, the numerator and denominator
    f /= xtalFreq # each is 20 bits (range 0..1048575)

    num = floor(f) # the actual multiplier is  mult + num / denom
    return (divider, mult, num, denom)

vfo = 23000000
for i in range(10):
    divider, mult, num, denom = calculate(vfo)

    PLLb = xtal * 1000000 * (mult + num/denom)

    freq = PLLb / divider

    delta = vfo - freq

    print(f"CLK0: VFO {vfo}, PLLb = xtal * ({mult} + {num} / {denom}) = {PLLb}, PLLb / {divider} = {freq}, delta = {delta:.03} Hz")
    vfo += 100000