0
0
Power-electronicsProgramBeginner · 2 min read

Embedded C Program for Digital Clock with Time Display

An Embedded C program for a digital clock uses variables for hours, minutes, and seconds, increments seconds every loop, and resets values using if conditions; for example, if(seconds == 60) { seconds = 0; minutes++; } to keep time correctly.
📋

Examples

InputStart time 00:00:00
Output00:00:01 00:00:02 ... up to 00:00:05
InputStart time 23:59:58
Output23:59:59 00:00:00 00:00:01
InputStart time 12:34:59
Output12:35:00 12:35:01 12:35:02
🧠

How to Think About It

To build a digital clock in Embedded C, think of three counters: seconds, minutes, and hours. Increase seconds every time unit, and when seconds reach 60, reset to 0 and increase minutes. Repeat similarly for minutes and hours, resetting hours after 23 to 0. Display the time in HH:MM:SS format continuously.
📐

Algorithm

1
Initialize hours, minutes, and seconds to zero or a start time
2
Create an infinite loop to keep the clock running
3
Increment seconds by one every cycle
4
If seconds reach 60, reset seconds to 0 and increment minutes
5
If minutes reach 60, reset minutes to 0 and increment hours
6
If hours reach 24, reset hours to 0
7
Display the current time in HH:MM:SS format
💻

Code

embedded_c
#include <stdio.h>
#include <unistd.h>

int main() {
    int hours = 0, minutes = 0, seconds = 0;
    while(1) {
        printf("%02d:%02d:%02d\n", hours, minutes, seconds);
        sleep(1); // wait for 1 second
        seconds++;
        if(seconds == 60) {
            seconds = 0;
            minutes++;
        }
        if(minutes == 60) {
            minutes = 0;
            hours++;
        }
        if(hours == 24) {
            hours = 0;
        }
    }
    return 0;
}
🔍

Dry Run

Let's trace the clock starting at 00:00:58 through the code

1

Initial time

hours=0, minutes=0, seconds=58

2

Print time

Prints 00:00:58

3

Increment seconds

seconds = 59

4

Next loop print

Prints 00:00:59

5

Increment seconds and reset

seconds = 60 triggers reset: seconds=0, minutes=1

6

Print updated time

Prints 00:01:00

hoursminutesseconds
0058
0059
010
💡

Why This Works

Step 1: Time variables

We use hours, minutes, and seconds to store the current time values.

Step 2: Increment seconds

Each loop increases seconds by 1 to simulate time passing.

Step 3: Reset and carry over

When seconds reach 60, reset to 0 and increase minutes. Similarly for minutes and hours.

Step 4: Display time

Print the time in HH:MM:SS format using printf with zero padding for clarity.

🔄

Alternative Approaches

Using timer interrupts
embedded_c
#include <stdio.h>
volatile int hours=0, minutes=0, seconds=0;
void timer_interrupt() {
    seconds++;
    if(seconds == 60) { seconds=0; minutes++; }
    if(minutes == 60) { minutes=0; hours++; }
    if(hours == 24) { hours=0; }
}
int main() {
    while(1) {
        printf("%02d:%02d:%02d\n", hours, minutes, seconds);
        // timer_interrupt would be called by hardware timer
    }
    return 0;
}
This approach uses hardware timer interrupts for precise timing but is more complex to set up.
Using delay loops instead of sleep
embedded_c
#include <stdio.h>
void delay() {
    for(long i=0; i<1000000; i++); // simple delay loop
}
int main() {
    int h=0,m=0,s=0;
    while(1) {
        printf("%02d:%02d:%02d\n", h, m, s);
        delay();
        s++;
        if(s==60) { s=0; m++; }
        if(m==60) { m=0; h++; }
        if(h==24) { h=0; }
    }
    return 0;
}
Delay loops are less accurate and CPU-intensive but useful when no OS or timer is available.

Complexity: O(1) per loop iteration time, O(1) space

Time Complexity

The program runs in an infinite loop with constant time operations each cycle, so each iteration is O(1).

Space Complexity

Only a few integer variables are used, so space complexity is O(1).

Which Approach is Fastest?

Using hardware timer interrupts is more efficient and accurate than delay loops or sleep, but requires more setup.

ApproachTimeSpaceBest For
Simple loop with sleepO(1)O(1)Basic clocks on OS
Timer interruptsO(1)O(1)Precise timing on microcontrollers
Delay loopsO(1)O(1)Simple hardware without OS
💡
Use zero-padded formatting like %02d in printf to keep the clock display neat and consistent.
⚠️
Beginners often forget to reset seconds, minutes, or hours after reaching their limits, causing incorrect time display.