/************ Based on a Simple Frequency Counter ver 1.2 6 May 2014 D.Kidder W6DQ and J.Purdum, W9NMT. Modified by KG7TR for use in my OctalMania 80 meter receiver. The VFO frequency is measured by the Arduino routine, and then the 455 KHz IF frequency is subtracted from the measured VFO frequency, and displayed on a 6 digit, 7 segment LED. This frequency counter is based on the work of: Martin Nawrath KHM LAB3 Kunsthochschule f¸r Medien Kˆln Academy of Media Arts http://www.khm.de http://interface.khm.de/index.php/labor/experimente/ and uses the FreqCounter.h library from the same source. Pulses are counted on Digital Pin 5 during the gate time and then converted into the frequency in Hz, KHz, or MHz, depending on settings in the code used during compile. Precision is set to 4 or 5 decimal places (100 Hz or 10 Hz resolution respectively). An Arduino module using a quartz crystal for the Atmega microcontroller clock is preferred. Arduino modules using ceramic resonators for the clock drift too much to achieve accurate and stable resolution to 100 Hz or better. By using a quartz clock and by tweaking the CALIBRATION value in the sketch, it should be possible to achieve an accuracy of less than 50 Hz or so. A 6 digit LED display is used to display frequency. 100 Hz or 10 Hz rewsolution (i.e., 4 or 5 decimal places) is selected by a front panel switch on the radio. The display driver parts of the sketch are based on Henrik Jensen's libraries: http://lygte-info.dk/project/DisplayDriver%20UK.html Find the necessary LEDDisplayDriver.cpp and LEDDisplayDriver.h files at that site. This is version 1.01 from 2019-8-1 By HKJ from lygte-info.dk * *************/ #include #include #include "LEDDisplayDriver.h" #define DEFAULTPRECISION 4 // Default decimal places #define VALUEWIDTH 6 // Numeric field width #define STARTUPDELAY 100 // Delay used for splash screen at startup #define COUNTDELAY 20 // Delay for normal reading #define CALIBRATION 19 // Integer only, no decimal point, typically 15 to 20 // Tweaks gate time for variation in Arduino clock rate to calibrate #define GATETIME 100 // Gate time in milliseconds, affects CALIBRATION value if tweaked #define MEGAHERTZ 1000000 // Let's define a couple of constants #define MILLISECONDS 1000 // Number of milliseconds in a second #define SDA_PIN A0//Sets pin A0 for LED Display data #define SCL_PIN A1//Sets pin A0 for LED Display clock #define BFO 0.455020 // Frequency of xtal controlled BFO (Radio's BFO actually set to exactly 0.455000; // +20 Hz added here to make final calibration) LEDDisplayDriver display(SDA_PIN, SCL_PIN, true, 6);//Sets up LED Display for 6 digits float scaleFactor = 1.0; // Scale factor used to adjust units. //Default is 1.0 for Hz, 0.001 for KHz and .000001 for MHz int ValueDigits = HIGH; //Setup a variable for the number of digits to display. void setup() { display.setBrightness(7);// Set the brightness level from 0(darkest) to 7(brightest) pinMode(A3, INPUT_PULLUP); // High = 4 decimal places, Low = 5 decimal places } void loop() { unsigned long dataIn; float val; int i; int ValueDigits = digitalRead(A3); //pin A3 grounded for 10 Hz resolution; open for 100 Hz resolution FreqCounter::f_comp = CALIBRATION; // Calibrate with known source FreqCounter::start(GATETIME); // Count pulses for specified gating period while (FreqCounter::f_ready == 0) dataIn = FreqCounter::f_freq;; delay(COUNTDELAY); val = (float) dataIn * scaleFactor * ( MILLISECONDS / GATETIME) / MEGAHERTZ ; // Scale the input reading... {val = val - BFO;} // Subtract BFO from VFO frequency to get signal frequency if (ValueDigits == LOW) {display.showNum(val,5);} else {display.showNum(val,4);} }