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:
- 71:fe799147584b
- Parent:
- 70:a07ab0091d5a
- Child:
- 72:4838d419f33c
--- a/main.cpp Sun Sep 09 12:43:17 2018 -0700 +++ b/main.cpp Thu Sep 13 15:05:08 2018 -0700 @@ -51,13 +51,6 @@ Ticker SecTenth_Beat; // .1 second ticker Ticker Sec_Beat; // 1 second ticker -// Trying to use this to compensate for Ticker inacurracy but -// just enabling this code makes the Count1 and Count2 low -// Timer timer; -// 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 @@ -66,16 +59,16 @@ unsigned int value = 0; // displayed value on the 6-digits of the display uint32_t Count1, Count2; // pulse counters (32-bit) +#define CountAvg 4 +uint32_t Count1Avg[CountAvg], Count2Avg[CountAvg]; // pulse counters (32-bit) +uint8_t CountAvgIndex = 0 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; char Text[40]=""; // used to send messages over the serial port uint8_t Disp_mode = 0x01, Disp_unit = 0xA0; // status of 1st row and 2nd rows of LEDs @@ -131,14 +124,15 @@ //RTC::attach(&Update, RTC::Second); //RTC::detach(RTC::Second); - // Trying to use this to compensate for Ticker inacurracy but - // just enabling this code makes the Count1 and Count2 low - // timer.start(); - // begin = timer.read_us(); - TRIG1.rise(&Count1_up); TRIG2.rise(&Count2_up); + for (uint8_t i=0;i<CountAvg;i++) + { + Count1Avg[i] = 0; + Count2Avg[i] = 0; + } + // main loop does nothing as all activities are interrupt driven while(1) { @@ -175,20 +169,21 @@ if (++TickerPeriodCount >= TickerPeriodsPerSecond) { // Capture the counts early so they will be more accurate - Count1Save = Count1; - Count2Save = Count2; + Count1Avg[CountAvgIndex] = Count1; + Count2Avg[CountAvgIndex] = Count2; + CountAvgIndex = ++CountAvgIndex % CountAvg; + + uint32_t Count1Sum = 0, Count2Sum = 0; + for (uint8_t i=0;i<CountAvg;i++) + { + Count1Sum += Count1Avg[i] + Count2Sum += Count2Avg[i]; + } + Count1Save = Count1Sum / CountAvg; + Count2Save = Count2Sum / CountAvg; Count1 = 0; Count2 = 0; - // Trying to use this to compensate for Ticker inacurracy but - // just enabling this code makes the Count1 and Count2 low - // Correct for ticker inaccuracy - // end = timer.read_us(); - // int diff = end - begin; - // secCorrection = diff/TickerTicksPerSecond; - // Count1Save = Count1Save/secCorrection; - // Count2Save = Count2Save/secCorrection; - TickerPeriodCount = 0; UpdateOutput(); @@ -254,20 +249,12 @@ case CNT2: currentModeToDisplay = currentMode; - if ( (direction > 0) - && (TickerCorrection < TickerCorrectionMax)) + if (direction > 0) { - TickerCorrection++; - Sec_Beat.attach_us(&UpdateOutput, 1000000 + 1000*TickerCorrection); - DigitsDisplay.Display_2D_write(TickerCorrection); } else - if ( (direction < 0) - && (TickerCorrection > TickerCorrectionMin)) + if (direction < 0) { - TickerCorrection--; - Sec_Beat.attach_us(&UpdateOutput, 1000000 + 1000*TickerCorrection); - DigitsDisplay.Display_2D_write(TickerCorrection); } break; case PROSPECT: