Pond Electronics - Home of the µFlash876 & µFlash876B embedded controllers, µStack & µConnect Bus.

A flashing LED - The "Hello World" of embedded systems

Introduction

When Brian Kernigahan and Dennis Ritchie introduced C to the world in their book "The C programming language", they did so with the now famous "hello world" program. This simple program printed the words "hello world" on the screen and exited. Embedded systems often do not have any form of text display so it has become traditional to introduce embedded C programming with an example more suited to the embedded environment, the flashing LED.

 

An introduction to LEDs

A LED or "Light Emitting Diode" is probably the simplest output device, requiring only a current limiting resistor to effect an interface. A LED (as its name suggests) is a diode which emits light when a small current is passed through it. Because it is a diode it will only pass current in one direction, so it is important to connect it the right way around. Don't worry, so long as you don't exceed the reverse breakdown voltage (usually 5-8V) you can't damage it by connecting it the wrong way, it simply wont light.

The value of the resistor depends on the required drive current of the LED. Most LEDs work well with a current of between 10mA and 20mA, since the uFlash I/O port works at 5V a 330 Ohm resistor will result in an appropriate current. You should be aware that there are several kinds of LEDs available including "low current" requiring only 1mA to 2mA for operation (use a 2K2 resistor) , LEDs with built in resistors, bi and tri-colour LEDs and even flashing leds (though these are comparatively rare).

 

LEDs are available in many shapes, sizes & colours

 

A typical LED and its schematic symbol

When connecting a LED to an I/O line you have two possible configurations, current sourcing and current sinking.

In the current sourcing configuration when the I/O pin is low (at 0V) the LED is off, when the I/O pin is switched high (5V) the pin sources current and so the LED lights. With current sinking, when the I/O pin is high, no current flows and so the LED is off, you must switch the pin low to sink current and turn the LED on.

While the current sourcing configuration seems more sensible, the current sinking form is much more common. This is because many microcontrollers (in particular those of the 8051 family) have very poor current sourcing capability and cannot drive a LED in this mode. Though this is not a restriction with the µFlash (which can easily source the required current) we will employ the current sinking method as it is more widely applicable.

 

Microcontroller current sourcing LED driver

Microcontroller current sinking LED driver

Building the Hardware

As building a LED circuit on a prototype board just to demonstrate it flashing seems a bit extravagant we will take a simpler approach. By simply wiring the resistor and LED together and inserting the free pins directly into the uStack header between +5V and P15 we can achieve a zero cost interface.

 

The Software

The software is simplicity itself, by using the built in functions of the CCS compiler for I/O control and delay generation the code is reduced to a few easy to understand lines:

 

void main(void)
	{
	while(true)                   // loop forever
		{
		delay_ms(500);           // wait for half a second
		output_low(P15);         // turn the LED on by bringing P15 low
		delay_ms(500);           // wait another half second
		output_high(P15);        // turn the LED off by making P15 high
		}
	}

 

If you have any questions or comments regarding this application note, please contact us.

Copyright (c) Paul O Neill & Pond Electronics 2003

Home | Products | Technical Info | How to Order | Contact Us | Links