// Test Program to evaluate T1 Capture performance // Input configured for falling edge detection // Application Requirement Range is 1ms-10s // T1 clocked at 16MHz // Capture input (coupled with 4n7 and 1K Pullup) // Input using D8 (PB ) pin // Calibrated TTL Test Generator with // 0.1Hz-100kHz Output range // // 24-01-24 V1.02 const unsigned long period = 1000; // RESULTS INTERVALL const unsigned long period2 = 25; // ADC READ RATE unsigned long startMillis; unsigned long startMillis2; unsigned long currentMillis; #define ICP_INPUT 8 void setup() { pinMode(ICP_INPUT, INPUT_PULLUP); startMillis = millis(); //initial start time init_InputCapture(); Serial.begin(115200); Serial.println("COUNT,RAW,OVF,FREQ,DEGC,STAT"); } void loop() { InputCapture_out(); } volatile unsigned long ICP_val = 0; volatile unsigned long T1OVF_Ctr = 0; volatile uint16_t ovf_Copy = 0; bool bMEASUREMENT_READY = false; #define NUM_AVG 192UL #define LM335_TRIM 1.2 #define LM335_KELVIN_OFFSET 2.732 // Correct Pro-Mini Board XTAL trimming as measured against high accuracy standard // FSG7 (0.1Hz - 100kHz) // PM board runs at 15.999608 MHz, 23.5 DEGC #define TIMEBASE_TRIM 1.0000245 #define TIMEBASE_TRIM_INV 0.9999755006 #define XTAL_CALIBRATED 16000000.0 * TIMEBASE_TRIM float scale = (float) NUM_AVG * 1024.0; /****************************************************************************** *Function: void InputCapture() *Description: *Parameters: None *Return Value: None ******************************************************************************/ void InputCapture_out() { static long cnt=0; double Freq = 0.0; static uint8_t i=0; uint8_t statusbyte = 0; static uint32_t Accumulator = 0; static uint32_t sensorValue = 610 * NUM_AVG ; char buff[10]; double ftemp = 16000000; currentMillis = millis(); if ( (currentMillis - startMillis2) >= period2) { // LM335 Sensor acquisition with averaging Accumulator += analogRead(A0); if(++i == NUM_AVG) { i=0; Accumulator += NUM_AVG / 2; sensorValue = Accumulator; Accumulator = 0; } startMillis2 = currentMillis; } // Run printouts at defined intervalls //if ( (currentMillis - startMillis) >= period) if( (bMEASUREMENT_READY == true) && ( (currentMillis - startMillis) >= period) ) { bMEASUREMENT_READY = false; float degc = sensorValue * (5.0 / scale); degc = degc - LM335_KELVIN_OFFSET; // Subtract 273.2 DEG from result degc = degc * 100.0; // Multiplier to get DEGC degc = degc - LM335_TRIM; // Instead of LM335 Trim Pot if(ICP_val > 0){ ftemp = (double) ICP_val; Freq = XTAL_CALIBRATED / ftemp; // Correct for AVR XTAL tolerance at 23.5 DEGC ftemp = ftemp * TIMEBASE_TRIM_INV; // Correct for AVR XTAL tolerance } // Flag a missed OVF in 1 Hz range if(Freq > 1.000001) { statusbyte = 1; }else{ statusbyte = 0; } Serial.print(cnt++); Serial.print(","); Serial.print(ftemp,0); Serial.print(","); Serial.print(T1OVF_Ctr); Serial.print(","); Serial.print(Freq, 7); Serial.print(","); Serial.print(degc, 1); Serial.print(","); Serial.println(statusbyte); startMillis = currentMillis; } } /****************************************************************************** *Function: void init_ICP() *Description: setup for ICP *Parameters: None *Return Value: None ******************************************************************************/ void init_InputCapture() { TCCR1A = 0; TCCR1B = 0; TIMSK1 = 0; // ICNC1 ICES1 – WGM13 WGM12 CS12 CS11 CS10 TCCR1B = (1<