time keeping

I've been running this code for a couple days now, uC is a '1284P with 16 MHz crystal and two 22pC caps:

unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedTime;

// Initial time to start, adjust as needed.

byte hundredths;
byte tenths;
byte oldTenths;
byte secondsOnes = 0;
byte oldSecondsOnes;
byte secondsTens = 0;
byte minutesOnes = 2;
byte minutesTens = 3;
byte hoursOnes = 4;
byte hoursTens = 0;

void setup() {

  Serial.begin(115200); // make serial monitor match
  Serial.println ("Setup Done");
}

void loop() {

  currentMicros = micros();

  // how long's it been?
  elapsedTime = currentMicros - previousMicros;
  if ( elapsedTime >= 10000UL) { // 0.01 second passed? Update the timers
    previousMicros  = previousMicros + 10000UL;

    hundredths = hundredths + 1; // increment
    if (hundredths >= 10) {
      hundredths = 0; // else rollover and increment next digit

      tenths = tenths + 1;
      if (tenths >= 10) {
        tenths = 0;
        secondsOnes = secondsOnes + 1;
        if (secondsOnes >= 10) {
          secondsOnes = 0;
          secondsTens = secondsTens + 1;
          if (secondsTens >= 6) {
            secondsTens = 0;
            minutesOnes = minutesOnes + 1;
            if (minutesOnes >= 10) {
              minutesOnes = 0;
              minutesTens = minutesTens + 1;
              if (minutesTens >= 6 ) {
                minutesTens = 0;
                hoursOnes = hoursOnes + 1;
                if ((hoursTens == 2) && (hoursOnes == 4)) {
                  hoursOnes = 0;
                  hoursTens = 0;
                }// hours total rollover check
                if (hoursOnes >= 10) {
                  hoursOnes = 0;
                  hoursTens = hoursTens  + 1;
                } // hoursOnes rollover check
              } // minutesTens rollover check
            } // minutesOnes rollover check
          } // secondsTens rollover check
        } // secondsOnes rollover check
      } // tenths rollover check
    } // hundredths rollover check
  }// hundredths passing check

  if (oldTenths != tenths) { // show the elapsed time
    oldTenths = tenths;
    Serial.print(hoursTens);
    Serial.print(hoursOnes);
    Serial.print(":");
    Serial.print(minutesTens);
    Serial.print(minutesOnes);
    Serial.print(":");
    Serial.print(secondsTens);
    Serial.print(secondsOnes);
    Serial.print(".");
    Serial.println (tenths);

  } // end one second check

} // end loop

Eyeballed the time to within a second to start it running, seems to be tracking my PC's time pretty well.

Hi,

Looks good, how about temperature stability.

(Congrats on using the code tags, you must have read the sticky... +1 Karma.. lol)

Tom... :slight_smile:

It's only been run from 60F to 85F so far.