Charles Young's development fork. Going forward I only want to push mature code to main repository.
Fork of GEO_COUNTER_L432KC by
Diff: main.cpp
- Revision:
- 61:00601fb0a3b9
- Parent:
- 60:6ae1a2390600
- Child:
- 62:9e5cb78d25e4
diff -r 6ae1a2390600 -r 00601fb0a3b9 main.cpp --- a/main.cpp Sat Sep 08 21:37:35 2018 -0700 +++ b/main.cpp Sun Sep 09 06:03:33 2018 -0700 @@ -49,6 +49,12 @@ I2C i2c(D4, D5); // I2C port Ticker SecTenth_Beat; // .1 second ticker Ticker Sec_Beat; // 1 second ticker + + +int begin; +int end; +double secCorrection; + Serial PC(USBTX, USBRX); // Virtual COM via USB (PC connection) Serial GPS(D1, D0); // Serial port for GPS module @@ -56,11 +62,15 @@ time_t seconds; // Real-Time Clock (RTC) timestamp unsigned int value = 0; // displayed value on the 6-digits of the display -uint32_t Count1, Count2; // pulse counters (32-bit) +uint32_t Count1, Count2; // pulse counters (32-bit) +uint32_t Count1Save; +uint32_t Count2Save; + int32_t TickerCorrection = 0; uint32_t TickerPeriod = 100000; uint32_t TickerPeriodCount = 0; const uint32_t TickerPeriodsPerSecond = 1000000/TickerPeriod; +const uint32_t TickerTicksPerSecond = TickerPeriod*TickerPeriodsPerSecond; const int16_t TickerCorrectionMax = 99; const int16_t TickerCorrectionMin = -99; @@ -102,7 +112,7 @@ DigitsDisplay.Display_6D_write(0x543210); DigitsDisplay.Display_2D_write(0); - // RTC is supposed to be loose time at power down (no backup battery) + // RTC is supposed to lose time at power down (no backup battery) // An initialization is performed anyway set_time(0); // Set time @@ -117,6 +127,8 @@ SecTenth_Beat.attach_us(&UpdateIO, TickerPeriod); //RTC::attach(&Update, RTC::Second); //RTC::detach(RTC::Second); + time.start(); + begin = timer.read_us(); TRIG1.rise(&Count1_up); TRIG2.rise(&Count2_up); @@ -151,7 +163,21 @@ { if (++TickerPeriodCount >= TickerPeriodsPerSecond) { + // Capture the counts early so they will be more accurate + Count1Save = Count1; + Count2Save = Count2; + Count1 = 0; + Count2 = 0; + + // Correct for ticker inaccuracy + end = timer.read_us(); + int diff = end - begin; + secCorrection = diff/TickerTicksPerSecond; + Count1Save = Count1Save/secCorrection; + Count2Save = Count2Save/secCorrection; + TickerPeriodCount = 0; + UpdateOutput(); } UpdateInput(); @@ -159,12 +185,6 @@ void UpdateOutput() { - // Capture the counts early so they will be more accurate - uint32_t Count1Save = Count1; - uint32_t Count2Save = Count2; - Count1 = 0; - Count2 = 0; - if(Stopped) { // disable interrupts on TRIG1 and TRIG2