 } 
 
 
-
+

// TICC.ino - master sketch file

// TICC Time interval Counter based on TICC Shield using TDC7200
// version 0.60 -- 30 July 2016

// Copyright John Ackermann N8UR 2016
// Portions Copyright George Byrkit K9TRV 2016

  Serial.print(" cal2Result="), Serial.println(cal2Result);
*/
  
  long ringticks;            // time1Result - time2Result, unitless
  long ringps;               // ps per ring cycle, nominal 55ps 
  long ringtime;             // ringticks * ringperiod, picoseconds
  unsigned long cal_tmp;
  unsigned long clocktime;   // clock1Result * CLOCK_PERIOD, picoseconds
  unsigned long tof;         // clocktime - ringtime, picoseconds; "time of flight" per datasheet

 // Datasheet saysring period =  (cal2Result - cal1Result)/(cal2Periods - 1)
  
 //ringps = CLOCK_PERIOD_PS / ((cal2Result - cal1Result)/(CALIBRATION2_PERIODS -1));
 // ringticks = time1Result - time2Result;
 // ringtime = ringticks * ringps;
 
 // But the steps above truncate ringps at 1ps. But ringps is multiplied
 // by up to a few thousand ringticks, so the truncation error is 
 // multiplied as well.  So we use mult/div by 1e2 to improve resolution 

  ringticks = time1Result - time2Result;

  // separated steps because I'm a math moron
  cal_tmp = ((cal2Result - cal1Result) * 100L) /(CALIBRATION2_PERIODS - 1 );
  ringps = ((CLOCK_PERIOD_PS) * 10000L) / cal_tmp; // ring period * 100
  ringtime = (ringticks * ringps) / 100L; 

  /*
  Serial.print("ringticks: "); Serial.print(ringticks);Serial.print(" ");
  Serial.print("ringps: "); Serial.print(ringps);Serial.print(" ");
  Serial.print("ringtime: "); Serial.print(ringtime);Serial.print(" ");
  */
  
  clocktime = clock1Result * CLOCK_PERIOD_PS;
  tof = clocktime - ringtime - FUDGE0_PS; // compensates for silicon delays
    
  /*  
  Serial.print(" ringps: ");Serial.print(ringps);
