0
0
Power-electronicsProgramBeginner · 2 min read

Embedded C Program for LED Chaser with Delay

An Embedded C program for an LED chaser uses a loop to shift a bit through an output port like PORTB = 1 << i; with a delay between shifts to create a moving light effect.
📋

Examples

InputPORTB initially 0x00, 8 LEDs connected
OutputLEDs light one by one from first to last with delay, then repeat
InputPORTB initially 0x00, 4 LEDs connected
OutputLEDs light one by one from first to fourth with delay, then repeat
InputPORTB initially 0x00, no delay
OutputLEDs appear to light all at once or flicker too fast to see
🧠

How to Think About It

To create an LED chaser, think of turning on one LED at a time in a sequence. Use a variable to represent which LED is on by shifting a 1 bit left each time. After lighting one LED, wait a short time, then move to the next LED by shifting the bit. Repeat this cycle forever to create a chasing effect.
📐

Algorithm

1
Initialize the output port for LEDs as output
2
Start an infinite loop
3
For each LED position from 0 to number_of_LEDs - 1:
4
Set the output port to light only the current LED using bit shifting
5
Wait for a short delay
6
Repeat the loop
💻

Code

embedded_c
#include <xc.h>
#define _XTAL_FREQ 4000000

void delay_ms(unsigned int ms) {
    while(ms--) {
        __delay_ms(1);
    }
}

void main() {
    TRISB = 0x00; // Set PORTB as output
    PORTB = 0x00; // Clear PORTB
    while(1) {
        for(unsigned char i = 0; i < 8; i++) {
            PORTB = 1 << i; // Light one LED at a time
            delay_ms(200); // Delay 200 ms
        }
    }
}
Output
LEDs on PORTB light one by one from RB0 to RB7 repeatedly with 200 ms delay
🔍

Dry Run

Let's trace lighting LEDs one by one on PORTB with delay

1

Initialize PORTB

TRISB = 0x00 (all output), PORTB = 0x00 (all LEDs off)

2

First loop iteration i=0

PORTB = 1 << 0 = 0x01 (only first LED on), delay 200 ms

3

Second loop iteration i=1

PORTB = 1 << 1 = 0x02 (second LED on), delay 200 ms

4

Continue for i=2 to 7

PORTB lights LEDs 3rd to 8th one by one with delay

5

Repeat loop

Cycle repeats from first LED again

Iteration iPORTB Value (Hex)LED On
00x01First LED
10x02Second LED
20x04Third LED
30x08Fourth LED
40x10Fifth LED
50x20Sixth LED
60x40Seventh LED
70x80Eighth LED
💡

Why This Works

Step 1: Setting PORTB as output

We use TRISB = 0x00; to make all PORTB pins outputs so we can control LEDs.

Step 2: Lighting one LED at a time

Using PORTB = 1 << i; shifts a 1 bit to the position of the LED to turn it on alone.

Step 3: Delay for visibility

The delay function pauses the program so the LED stays lit long enough to see before moving on.

🔄

Alternative Approaches

Using a rotating bit mask with bitwise operations
embedded_c
#include <xc.h>
#define _XTAL_FREQ 4000000

void delay_ms(unsigned int ms) {
    while(ms--) {
        __delay_ms(1);
    }
}

void main() {
    TRISB = 0x00;
    PORTB = 0x01;
    while(1) {
        PORTB <<= 1;
        if(PORTB == 0) PORTB = 0x01;
        delay_ms(200);
    }
}
This method shifts the bit left continuously and resets when it goes out of range, making code shorter but less explicit.
Using an array to store LED patterns
embedded_c
#include <xc.h>
#define _XTAL_FREQ 4000000

void delay_ms(unsigned int ms) {
    while(ms--) {
        __delay_ms(1);
    }
}

void main() {
    unsigned char leds[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
    TRISB = 0x00;
    while(1) {
        for(int i=0; i<8; i++) {
            PORTB = leds[i];
            delay_ms(200);
        }
    }
}
Using an array makes it easy to customize LED patterns but uses more memory.

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

Time Complexity

The program loops through each LED once per cycle, so time complexity is O(n) where n is number of LEDs.

Space Complexity

Uses constant extra space for variables and delay, so space complexity is O(1).

Which Approach is Fastest?

Bit shifting is fastest and simplest; using arrays adds memory overhead but allows flexible patterns.

ApproachTimeSpaceBest For
Bit shifting loopO(n)O(1)Simple, fast LED chaser
Rotating bit maskO(n)O(1)Compact code, continuous shift
Array patternO(n)O(n)Custom LED sequences
💡
Use bit shifting to control LEDs one by one efficiently in embedded C.
⚠️
Forgetting to set the port pins as output with TRIS registers causes LEDs not to light.