0
0
Power-electronicsProgramBeginner · 2 min read

Embedded C Program for LED Binary Counter

An Embedded C program for an LED binary counter uses a loop to increment a number and outputs its binary form to LEDs connected to a port, for example: for(count=0; count<16; count++) { PORT = count; delay(); } where PORT controls LEDs.
📋

Examples

InputStart counter from 0
OutputLEDs show binary from 0000 to 1111 repeatedly
InputCount up to 8
OutputLEDs show binary from 0000 to 1000
InputCount up to 1
OutputLEDs show 0000 then 0001
🧠

How to Think About It

To create an LED binary counter, think of the LEDs as bits of a binary number. You start counting from zero and increase the number by one each time. The binary value of the count is sent to the LEDs by setting the output port pins high or low, which turns the LEDs on or off accordingly.
📐

Algorithm

1
Initialize the port connected to LEDs as output
2
Set a counter variable to zero
3
In a loop, output the counter value to the port controlling LEDs
4
Wait for a short delay to see the LED pattern
5
Increment the counter and repeat the loop
💻

Code

embedded_c
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRB = 0xFF; // Set PORTB as output for LEDs
    unsigned char count = 0;
    while(1) {
        PORTB = count; // Output count to LEDs
        _delay_ms(500); // Wait 500 ms
        count++;
    }
    return 0;
}
Output
LEDs connected to PORTB show binary count from 00000000 to 11111111 repeatedly with 500 ms delay
🔍

Dry Run

Let's trace the counter from 0 to 3 through the code

1

Initialize

PORTB set as output, count = 0

2

Output count

PORTB = 0 (binary 00000000), LEDs all off

3

Delay

Wait 500 ms

4

Increment count

count = 1

5

Output count

PORTB = 1 (binary 00000001), LED0 on

6

Delay

Wait 500 ms

7

Increment count

count = 2

8

Output count

PORTB = 2 (binary 00000010), LED1 on

9

Delay

Wait 500 ms

10

Increment count

count = 3

CountPORTB (binary)LEDs
000000000All off
100000001LED0 on
200000010LED1 on
300000011LED0 and LED1 on
💡

Why This Works

Step 1: Set PORT as output

We use DDRB = 0xFF to make all pins of PORTB output pins so they can control LEDs.

Step 2: Output count to LEDs

The variable count holds the number to display. Writing it to PORTB sets LEDs on/off in binary.

Step 3: Delay to see LEDs

A delay like _delay_ms(500) lets us see the LED pattern before changing to the next count.

Step 4: Increment and repeat

Increasing count by one each loop cycles through binary numbers, showing a counting effect on LEDs.

🔄

Alternative Approaches

Using a timer interrupt
embedded_c
#include <avr/io.h>
#include <avr/interrupt.h>

volatile unsigned char count = 0;

ISR(TIMER0_OVF_vect) {
    PORTB = count++;
}

int main(void) {
    DDRB = 0xFF;
    TCCR0 = (1 << CS02) | (1 << CS00); // Prescaler 1024
    TIMSK = (1 << TOIE0); // Enable overflow interrupt
    sei(); // Enable global interrupts
    while(1) {}
    return 0;
}
This method uses hardware timer interrupts for precise timing without blocking CPU.
Counting with a for loop and reset
embedded_c
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRB = 0xFF;
    for(unsigned char count=0; ; count++) {
        PORTB = count;
        _delay_ms(500);
    }
    return 0;
}
A simple for loop counts indefinitely, easier but less flexible for adding other tasks.

Complexity: O(n) time, O(1) space

Time Complexity

The program runs an infinite loop incrementing the count, so time complexity is O(n) where n is the number of counts displayed.

Space Complexity

Uses only a few variables and fixed port registers, so space complexity is O(1).

Which Approach is Fastest?

Using timer interrupts is more efficient for multitasking, while simple loops are easier but block CPU during delay.

ApproachTimeSpaceBest For
Simple loop with delayO(n)O(1)Easy implementation, blocking
Timer interruptO(n)O(1)Efficient multitasking, precise timing
💡
Use a delay between counts so you can clearly see the LED binary pattern change.
⚠️
Forgetting to set the port pins as output causes LEDs not to light up.