I’ve been running this simple time keeping code for several days now,
The uC is a '1284P with 16 MHz xtal and 2 22pF caps, just one that happened to be readily at hand.
Started it by giving it an initial time, then opening the Serial Monitor ~ 2 seconds before that time, as that seemed about the right time to reset and open start the program.
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
Still tracking pretty good sitting in my 60F to 85F living room after a couple of days.
Summer time, no AC, cool nights.
Would be easy to read a couple of buttons to bump the time up & down for setting the time,
and to add a time to check against for an alarm function.
I use the individual digits method to make it easy to drive 7 segment displays.