From e0c9f4dcee8672866fa0f6df0ef8eec6a148da02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Sat, 13 Nov 2021 19:32:58 +0100 Subject: [PATCH] Fix fractional calculation for frequencies close to integer multiples --- Software/VNA_embedded/Application/Drivers/max2871.cpp | 8 ++++++++ Software/VNA_embedded/Application/VNA.cpp | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Software/VNA_embedded/Application/Drivers/max2871.cpp b/Software/VNA_embedded/Application/Drivers/max2871.cpp index 9781898..54fa29b 100644 --- a/Software/VNA_embedded/Application/Drivers/max2871.cpp +++ b/Software/VNA_embedded/Application/Drivers/max2871.cpp @@ -190,6 +190,14 @@ bool MAX2871::SetFrequency(uint64_t f) { auto approx = Algorithm::BestRationalApproximation(fraction, 4095); + if (approx.denom == approx.num) { + // got an impossible result due to floating point limitations(?) + // Set fractional part to zero, increase integer part instead + approx.num = 0; + approx.denom = 2; + N++; + } + if(approx.denom == 1) { // M value must be at least 2 approx.denom = 2; diff --git a/Software/VNA_embedded/Application/VNA.cpp b/Software/VNA_embedded/Application/VNA.cpp index 9e0b4c3..65b0973 100644 --- a/Software/VNA_embedded/Application/VNA.cpp +++ b/Software/VNA_embedded/Application/VNA.cpp @@ -180,9 +180,11 @@ bool VNA::Setup(Protocol::SweepSettings s) { // Configure LO2 for the changed IF1. This is not necessary right now but it will generate // the correct clock settings last_LO2 = actualFirstIF - HW::IF2; - LOG_INFO("Changing 2.LO to %lu at point %lu (%lu%06luHz) to reach correct 2.IF frequency", + LOG_INFO("Changing 2.LO to %lu at point %lu (%lu%06luHz) to reach correct 2.IF frequency (1.LO: %lu%06luHz, 1.IF: %lu%06luHz)", last_LO2, i, (uint32_t ) (freq / 1000000), - (uint32_t ) (freq % 1000000)); + (uint32_t ) (freq % 1000000), (uint32_t ) (actualLO1 / 1000000), + (uint32_t ) (actualLO1 % 1000000), (uint32_t ) (actualFirstIF / 1000000), + (uint32_t ) (actualFirstIF % 1000000)); } else { // last entry in IF table, revert LO2 to default last_LO2 = HW::IF1 - HW::IF2;