Electronics - MSP430 - Using P1OUT pins and LEDs to count in binary

Maybe you've always dreamed to understand how to count in binary.

What do you think about learning it with a MSP430 LaunchPad and LEDs?

If you are still interested, that's what we are going to see with this tutorial and a video.

Let's get started.

What do we need?

If you would like to reproduce exactly this example, let's see which electronic stuff we need:

  • 1 MSP430 LaunchPad with a M430G2553 microcontroller;
  • 9 LEDs (1 green, 4 red and 4 orange);
  • 9 resistors of 330 ohm;
  • 4 breadboards;
  • 28 wires (10 red, 4 tiny and 4 long orange, 8 yellow, 2 black);
  • 1 USB mini cable.

Of course, it's possible to do the same with less parts.

I'm sure you can, you're a creative person, aren't you?

Binary explanation

Binary counting is just playing with 0 and 1.

So every time we add 1 to 1, it becomes 10.

For example, we could have:

  00000010
+ 00000001
---------------
= 00000011

And:

  00000001
+ 00000001
---------------
= 00000010

The same way:

  00000010
+ 00000010
---------------
= 00000100

And:

  00000111
+ 00000001
---------------
= 00001000

and so on.

That's what we can see in the video.
Each time we add 1, we go from the right to the left like if LEDs were counting in binary.

Electronics explanation

In this tutorial, we are going to see how to set all P1OUT pins to high in order to turn on the LEDs.

It's so also a good exercise to practise GPIO.

With the example below we will be able to play with it.

The MSP430 LaunchPad has 8 P1OUT pins, respectively:

  1. P1.0
  2. P1.1
  3. P1.2
  4. P1.3
  5. P1.4
  6. P1.5
  7. P1.6
  8. P1.7

You had certainly seen that the pins P1.0 to P1.5 were on the left side of the board while the two others, P1.6 and P1.7, were on the right side.

If you didn't already see it, have a look to the board.

So, it is necessary to put 6 resistors on the left and 2 on the right of the board.

All resistors have a value of 330 ohm.

Why?

According to the datasheet of our LEDs, they need at least 10 mA to have a liminous intensity.

So as we know that:

  • U =RI
  • I = U/R
  • R = U/I

Then if:

  • U = 3.55 V
  • R = 330 ohm
  • I = 3.55 / 330 = 0.01075757575 A

It means approximatively that I = 10 mA.

We use a ninth LED (a green one) to check that the current is going through our circuit.
If we removed it, it, of course, would work as well.

We have a breadboard with a GND line.

It easiest to use it because we can plug every wire from the cathode of each LED to this GND line.

Each red wire are used to connect each resistor to a LED (even the green one).

Long-orange wires are used to show that they are from the right side of the board (P1.6 and P1.7 pins).
Notice that the tiny-orange wires are here to extend the VCC line.

Yellow wires are connected from red and orange LEDs to GND line.

Black wires are connected from GND line to a GND pin on the board.

Now that the electronics is explained, let's see how the software works.

The code

The code is quite short, but also a bit complex if you have never programmed.

For the delay, as a TI employee said, we use our own function to manipulate delays.

After declaring our variables, we stop the watchdog timer.

We reset the PIDIR registers to 0 and the P1OUT as well.

Then we put all P1OUT pins to high with 0xFF (equals to 11111111), so each LED we be turned on.

Finally the algorithm will turn on each LED as if we wanted to count in binary from 0 to 255.

  • 0 = 0x00;
  • 255 = 0xFF.

So we add 1 up the iterator variable is equal to 0xFF (or 255 in decimal).

Once every LED is switched on, we stop the loop and we ask to blink all LEDs at the same time, and this 11 times (6 times off and 5 on).

Let's see this in details.

#include <msp430.h>

void delay_ms(unsigned int delay);

#define TIME_START 7
#define TIME_END 4

/**
 * The main function.
 * Notice that we don't need a return value.
 * The function is thus of void type.
 */
void main(void) {
    unsigned char iterator = 0x00;        // set iterator to 0
    unsigned int myTime = TIME_START;    // set myTime to 7
    int ledsOK = 0;                        // set ledsOK to 0
    int end = 11;                        // set end to 11

    WDTCTL = WDTPW | WDTHOLD;             // Stop watchdog timer
    P1DIR &= 0x00;                        // resetting the P1DIR register
    P1OUT &= 0x00;                        // resetting the P1OUT register
    P1DIR = 0xFF;                        // setting all the P1DIR registers to 1 (high)

    while (1337) {                        // infinite loop
        if (iterator == 0xFF) {
            ledsOK = 1;
            P1OUT = ++iterator;            // P1OUT = iterator + 1
        }
        if (ledsOK) {                    // we enter in the loop if all LEDs are turned on
            if (myTime == TIME_START)
                myTime *= TIME_END;        // multiply TIME_START by 4
            while (end != 0) {
                P1OUT = ~P1OUT;            // blink all LEDs at the same time
                delay_ms(myTime);
                --end;                    // remove 1 to end, 11 times
            }
        } else {
            P1OUT = ++iterator;            // add +1 to P1OUT
        }
        delay_ms(myTime);                // delay
    }
}

/**
 * Takes a value in paramterer in order to have a delay.
 * 16,000 is equal to 16,000,000 / 1,000.
 * -> 16,000,0000 = 16 Mhz (the CPU clock speed).
 */
void delay_ms(unsigned int delay)
{
    while (delay--)
    {
        __delay_cycles(16000);
    }
}

Conclusion

This tutorial is an original way to learn counting in binary.
Especially with LEDs.
Don't be afraid by the algorithm, try to test it with several values, to understand how it works.
Anyway congratulations, you've just learnt it. laugh

Comments

Comment: 

what if you want it to light the led for 2 times than delay and light 7 times than delay than light for 4 times. But just for one led

Comment: 

Hello,

I have a quetison about the "11 times (6 times off and 5 on)", isn't that 5 times off and 6 times on, or why does it work like that, I can't fully understand this delay loop, I mean, the ''off and on''...

Thanks for answering in advance!

Comment: 

Hello yuan,

The 11 times (off and on) were to make an effect at the end of the binary counting.

So there is nothing special to understand with that. cool

Comment: 

Hello,

Ah, that's very nice! Great thanks to you, with your tutorial, I can easily start the MSP430 coding!

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.