--[[ Eugeniy Mikhailov 1 June 2021 ]] --------- FREQ TO BITS --------- function freq2regestersValues(freq) local F_COMP = 5000000 -- magic number local N = freq / F_COMP local RF_N = math.floor(N) local frac = math.abs(N - RF_N) local RF_FD = 4000000 --magic number local RF_FN = math.floor( (frac * RF_FD) + 0.5) return freq, RF_N, RF_FN, RF_FD end -- test freq2regestersValues fIn = 6.834e9 freq, RF_N, RF_FN, RF_FD = freq2regestersValues(fIn) if freq ~= fIn and RF_N ~= 1366 and RF_FN ~= 3200000 and 4000000 then print("ERROR in freq2regestersValues") end fIn=nil freq=nil RF_N=nil RF_FN=nil RF_FD=nil -- end of freq2regestersValues test ------ INITIAL SETTINGS (send to chip)------- function initSettings( ) local setting = { } -- prefill with magic numbers & pad empty bits with 0's setting["R0"] = pad2NumBitsInPlace( toBits(11197502), 24) setting["R1"] = pad2NumBitsInPlace( toBits(4352515), 24) setting["R2"] = pad2NumBitsInPlace( toBits(8560613), 24) setting["R3"] = pad2NumBitsInPlace( toBits(15925287), 24) setting["R4"] = pad2NumBitsInPlace( toBits(10618633), 24) setting["R5"] = pad2NumBitsInPlace( toBits(393579), 24) setting["R6"] = pad2NumBitsInPlace( toBits(8126541), 24) setting["R7"] = pad2NumBitsInPlace( toBits(10639), 24) return setting end ---------- COPY TABLE WITH DIFFERNT INDEX NAME ----------- function copySettings(old) local dst={} dst["R0"]= copyTableArray(old["R0"]) dst["R1"]= copyTableArray(old["R1"]) dst["R2"]= copyTableArray(old["R2"]) dst["R3"]= copyTableArray(old["R3"]) dst["R4"]= copyTableArray(old["R4"]) dst["R5"]= copyTableArray(old["R5"]) dst["R6"]= copyTableArray(old["R6"]) dst["R7"]= copyTableArray(old["R7"]) return dst end --------- SLICE AND REPLACE ------------ function FreqToSettings(freq, settings) --create table with new values of R0, R1, and R5 local freq, RF_N, RF_FN, RF_FD = freq2regestersValues(freq) local bRF_N=toBits(RF_N) local bRF_FN=toBits(RF_FN) local bRF_FD=toBits(RF_FD) pad2NumBitsInPlace(bRF_N, 11) pad2NumBitsInPlace(bRF_FN, 22) pad2NumBitsInPlace(bRF_FD, 22) -- print("bRF_N = "..bin2string(bRF_N, " ")) -- print("bRF_FN = "..bin2string(bRF_FN, " ")) -- print("bRF_FD = "..bin2string(bRF_FD, " ")) --print(bin2string(settings["R0"], " ")) settings["R0"]=tableReplace(settings["R0"], bRF_N, 14, 24) --print(bin2string(settings["R0"], " ")) settings["R0"]=tableReplace(settings["R0"], bitSlice(bRF_FN, 1, 12), 2, 13) --print(bin2string(settings["R0"], " ")) --print(bin2string(settings["R1"], " ")) settings["R1"]=tableReplace(settings["R1"], bitSlice(bRF_FD, 1, 12), 5, 16) --print(bin2string(settings["R1"], " ")) --print(bin2string(settings["R5"], " ")) settings["R5"]=tableReplace(settings["R5"], bitSlice(bRF_FN, 13, 22), 5, 14) --print(bin2string(settings["R5"], " ")) settings["R5"]=tableReplace(settings["R5"], bitSlice(bRF_FD, 13, 22), 15, 24) --print(bin2string(settings["R5"], " ")) return settings end -- test sinit= initSettings() snew = FreqToSettings(6.834e9, sinit) if bin2string(snew["R0"]) ~= "101010101100100000000000" then print("Frequency to R0 register value conversion is broken") end if bin2string(snew["R1"]) ~= "010000101001000000000011" then print("Frequency to R1 register value conversion is broken") end if bin2string(snew["R5"]) ~= "111101000011000011011011" then print("Frequency to R5 register value conversion is broken") end sinit=nil snew=nil ------- no changes to previous code ------------- -- send new register binary strings to chip function set_lmx2487_board_to_new_state(settings) set = {"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7"} for i = 1, #set do set_lmx2487register(toNum(settings[(set[i])]), (i-1)) end update_lmx2487state() end function setFreq(freq) settings = FreqToSettings(freq, settings) set_lmx2487_board_to_new_state(settings) end