# MSP430 - Seven-Segment LED driver

Ok, it's time to get back into the MSP430 tutorials. In the next few blogs I will present examples on driving seven-segment LEDs followed by interfacing with the popular alphanumeric LCD modules.

So let's get coding and debugging a seven-segment LED example.

The classic way to drive a common anode seven-segment LED is with a 74LS47 seven-segment decoder/driver.

Another popular interface chip is the CMOS 4511 decoder/driver. This will interface directly to common cathode LED seven-segment displays. An added feature of the 4511 is that it includes a 4-bit latch. This is important for applications such as a frequency counter whereby the previous count can be displayed while the current count is being acquired.

For a microcontroller interface you can choose any one of these decoder chips to suit your needs. Here you will only need to output four bits for each display.

But why use an extra chip if you can avoid it?

The alternative is to allow the mcu to perform its own BCD-to-seven-segment decoding allowing you to display any pattern you choose. This is particularly handy because you can create your own version of 4-bit to hexadecimal display which is absent in the 4511 and idiotic on the 7447. Of course, the one disadvantage is you need to use seven output pins of the mcu instead of four.

Ok, let's get on with the MSP430G2553 example.

Here is the schematic of what I have in mind.

Here is a photo of the layout on a protoboard:

Sample code

This sample program increments the variable n and displays the least significant 4 bits as one hexadecimal digit.

Code:
//-------------------------------------------
// Project #2 - Seven-segment LED interface
// 2013.06.07 - MrChips
//-------------------------------------------

#include "io430.h"

#define DELAY 100000

char seg[16];

// simple software deklay
void delay(unsigned long d)
{
unsigned long i;
for (i = 0; i < d; i++);
}

void init(void)
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P1DIR  = 0xFF;      // enable port outputs

// create 7-segment table
seg[0]  = 0x3F;
seg[1]  = 0x06;
seg[2]  = 0x5B;
seg[3]  = 0x4F;
seg[4]  = 0x66;
seg[5]  = 0x6D;
seg[6]  = 0x7D;
seg[7]  = 0x07;
seg[8]  = 0x7F;
seg[9]  = 0x67;
seg[10] = 0x77;
seg[11] = 0x7C;
seg[12] = 0x39;
seg[13] = 0x5E;
seg[14] = 0x79;
seg[15] = 0x71;
}

void main( void )
{
unsigned int n;

init();

while (1)
{
n++;
delay(DELAY);
// invert for common anode LED
P1OUT = ~seg[n & 0x0F];
}
}
Notes:

1) The schematic shows 220Ω current limiting resistors. The MSP430 LaunchPad provides +3.6V at VCC to power the mcu. I have driven the LED segments directly from the mcu I/O ports at 3.6V without the current limiting resistors. In the next example, we will be multiplexing three LED displays and hence the resistors become even less necessary.

2) This code can be used with either common anode or common cathode LED displays. The seg[ ] table uses positive logic. Thus to use common anode displays, the data is inverted with the statement:

P1OUT = ~seg[n & 0x0F];

To use the code with common cathode displays, simply use the statement:

P1OUT = seg[n & 0x0F];

Remember to connect the common cathode to GND.

3) For the letter "B" the shape "b" is used. This means that the digit "6" has been modified from the normal shape in order to distinguish it from "b".

If you do not wish to display hexadeximal values you can revert to the usual "6" by using:

seg[6] = 0x7C;

Next example: How to multiplex three or four LED digits.
Aizaz_Ali

Author
MrChips
Views
6,689