BusOut

Example 1

A LED light bar is connected from p11 to p20 of a mbed, The program moves a light bar from the 1st position to the 10th position.

/media/uploads/yoonghm/ledbar.jpg

#include "mbed.h"

#define WAIT_SECOND 0.1

            //  LSB                                          MSB
BusOut      Bar(p20, p19, p18, p17, p16, p15, p14, p13, p12, p11);
DigitalOut  Led(LED1);

int main() {
    Led = 0;
    while(1) {
        // Shift from LSB to MSB
        for (int i = 0; i < 10; i++) {
            Led = !Led;
            Bar = 1 << i;
            wait(WAIT_SECOND);
        }
    }
}

Example 2

Let's try to make the light bar bound back from the edges.

#include "mbed.h"

#define WAIT_SECOND 0.1

            //  LSB                                          MSB
BusOut      Bar(p20, p19, p18, p17, p16, p15, p14, p13, p12, p11);
DigitalOut  Led(LED1);

int main() {
    Led = 0;
    while(1) {
        // Shift from LSB to MSB
        for (int i = 0; i < 10; i++) {
            Led = !Led;
            Bar = 1 << i;
            wait(WAIT_SECOND);
        }
        
        // Shift from MSB to LSB
        for (int i = 0; i < 10; i++) {
            Led = !Led;
            Bar = 0x0200 >> i;
            wait(WAIT_SECOND);
        }
    }
}

However, the light bar stops at the both edges for an extra time each. Let change the index in the for loop to start from 1 instead of 0.

#include "mbed.h"

#define WAIT_SECOND 0.1

            //  LSB                                          MSB
BusOut      Bar(p20, p19, p18, p17, p16, p15, p14, p13, p12, p11);
DigitalOut  Led(LED1);

int main() {
    Led = 0;
    while(1) {
        // Shift from LSB to MSB
        for (int i = 1; i < 10; i++) {
            Led = !Led;
            Bar = 1 << i;
            wait(WAIT_SECOND);
        }
        
        // Shift from MSB to LSB
        for (int i = 1; i < 10; i++) {
            Led = !Led;
            Bar = 0x0200 >> i;
            wait(WAIT_SECOND);
        }
    }
}

However, during the first start, the LSB pin (p20) is not switched on. Let's fix this:

#include "mbed.h"

#define WAIT_SECOND 0.1

            //  LSB                                          MSB
BusOut      Bar(p20, p19, p18, p17, p16, p15, p14, p13, p12, p11);
DigitalOut  Led(LED1);

int main() {
    Led = 0;
    Bar = 1; wait(WAIT_SECOND); // Start from LSB first
    while(1) {
        // Shift from LSB to MSB
        for (int i = 1; i < 10; i++) {
            Led = !Led;
            Bar = 1 << i;
            wait(WAIT_SECOND);
        }
        
        // Shift from MSB to LSB
        for (int i = 1; i < 10; i++) {
            Led = !Led;
            Bar = 0x0200 >> i;
            wait(WAIT_SECOND);
        }
    }
}

Example 3

Let's modify the program in Example 3 to have the so-called Cyclon or Knight Rider effect

#include "mbed.h"

#define WAIT_SECOND 0.1

            //  LSB                                          MSB
BusOut      Bar(p20, p19, p18, p17, p16, p15, p14, p13, p12, p11);
DigitalOut  Led(LED1);

int main() {
    Led = 0;
    Bar = 3; wait(WAIT_SECOND); // Start from LSB first
    while(1) {
        // Shift from LSB to MSB
        for (int i = 1; i < 10; i++) {
            Led = !Led;
            Bar = 3 << i;
            wait(WAIT_SECOND);
        }
        
        // Shift from MSB to LSB
        for (int i = 1; i < 10; i++) {
            Led = !Led;
            Bar = 0x0300 >> i;
            wait(WAIT_SECOND);
        }
    }
}


4 comments on BusOut:

12 Dec 2012

Nice program, you could also preset a list of patterns in an array and 'dump' each array element to the bus in sequence. Note: since you use a single resistor the brightness will vary with the number of leds you have 'on' at the same time. Better use a resistor for each output.

12 Dec 2012

Thank for the tip. The LED bar that I used give quite consistent brightness.

13 Jan 2015

Can you upload a gif or stills of the bar cycling through?

15 Aug 2016

why the BusOut format is different from in the BusOut.h file?

Please log in to post comments.