// based on PaulS's FreqMeasure for Teensy example
// Digital Input: 1 PPS signal : on Teensy LC: Pin 16 on Teensy 3/3.1: Pin 3
// Analog Input: 10 mV/degC temp from LM35 : Analog 1
#include <FreqMeasure.h>
#define TEMP_IN A1 // analog input with 10 mV/deg.C from LM35 temp sensor
#define AVERAGES 30 // how many input pulses to integrate over
#define VREF (3.316) // Teensy LC ADC reference voltage (= Vdd power supply)
//#define VREF (1.200) // Teensy 3.0/3.1 ADC internal reference voltage
#define ADCMAX (65535) // maximum possible reading from ADC
#define NOMCOUNT (48000000) // nominal oscillator frequency count
#define NOMCM (48) // nominal count divided by 1E6 (for ppm calculation)
void setup() {
pinMode(TEMP_IN,INPUT);
analogReference(INTERNAL); // set analog reference to internal ref
// analogReference(EXTERNAL); // set analog reference to external
analogReadRes(16); // Teensy: set ADC resolution to this many bits
analogReadAveraging(16); // average this many readings
Serial.begin(115200);
FreqMeasure.begin();
}
double sum=0;
double ppm; // frequency error in ppm
int count=0;
unsigned long lastT = 0;
unsigned long deltaT;
unsigned long newT;
unsigned long reading = 0; // how many measurements we've reported
boolean firstTime = true;
double x=0; // analog temperature reading (LM35)
unsigned long tsamp=0; // how many temperature readings we've done
long fcount; // raw frequency count value
void loop() {
if (FreqMeasure.available()) {
newT = micros();
fcount = FreqMeasure.read();
sum = sum + (fcount - (NOMCOUNT));
count = count + 1;
if (count >= AVERAGES) {
deltaT = newT - lastT;
lastT = newT;
if (firstTime) {
firstTime = false;
Serial.println("Rdg , degC , ppm");
} else {
ppm = sum / (NOMCM * AVERAGES);
x /= tsamp;
float degC = 100 * (VREF/ADCMAX)*x; // LM35 temp sensor = 10 mV/C
reading++;
Serial.print(reading); Serial.print(" , ");
Serial.print(degC,4); Serial.print(" , ");
Serial.print(ppm,3);
Serial.println();
}
sum = 0;
count = 0;
x = 0;
tsamp = 0;
}
} else {
x += analogRead(TEMP_IN);
tsamp++;
}
} // end loop()