Add an LED display | Kickoff

Add an LED display

9 May

Add an LED display
For our last memory-mapped device we will implement an LED display. If the liquid crystal display was not good enough for you, let’s try implementing a four-digit 7-segment LED display for variety. There are a number of multiple-digit, 7-segment, LED display driver chips available. Fairchild Semiconductor makes the MM74C911 and MM74C91215 and e-labs make the EDE707.16 Both of these work with common anode or common cathode 7-segment LED displays. Again, we will be using one of the chip selects from our address decoder to enable the LED driver. The schematic (Figure 8-21) shows the circuit we will be using. Note that the LED driver needs the 2N3904 transistors to drive the common anode LED displays. The circuit will be slightly different for common cathode LEDs. The class provided here, LEDport, supports sending messages to a four character array of 7-segment LED displays. Each of the four characters of the display are then selected by using the address lines A0 and A1; the class implements this as four consecutive dataports starting with the base address as determined by our chip select logic.

Because we are  only using seven segments, not all ASCII characters are possible. We can display all numbers and a reasonable representation for these letters and symbols:

•  AbCcdEFGHhIiJLnOoPrStUuYZ
•  :?.[]|-_

This means you need to use peculiar case and spell certain words oddly (or not at all). Some examples of this oddness are: tHE HErE tHAt tini Hello hi HI dALLAS StAtUS AlArm Error StOP HALt HELP rESEt rESTArt rEbOOt UP dn LEFT rIGHt UnDEr OvEr. But you can see that it is still possible to get the message across to the user.

Listing 8-14: LEDport.java

import com.dalsemi.system.*;
// Class LEDport defines a set of methods for accessing an LED
display.
public class LEDport
{
DataPort led0;
DataPort led1;
DataPort led2;
DataPort led3;
int timedelay = 150;
int flashcount = 4;
public LEDport( int baseaddr )
{
// the LED driver datasheet says that address to write enable
// time
// is 50 ns and the write enable width is 250 ns
led0 =   new DataPort( baseaddr );
led0.setStretchCycles(DataPort.STRETCH10);
led1 =   new DataPort( baseaddr+1 );
led1.setStretchCycles(DataPort.STRETCH10);
led2 =   new DataPort( baseaddr+2 );
led2.setStretchCycles(DataPort.STRETCH10);
led3 =   new DataPort( baseaddr+3 );
led3.setStretchCycles(DataPort.STRETCH10);
}
public void init()
{
clear();
}
public void clear()
{
try {
led0.write( 0×00 );
led1.write( 0×00 );
led2.write( 0×00 );
led3.write( 0×00 );
}
catch(IllegalAddressException e)
System.out.println( “Error in INIT” );
System.out.println( e );
}
}
public void setDigit( int digit, int value )
{
try {
switch (digit) {
case 0:                                                         led0.write( value );
break;
case 1:                                                         led1.write( value );
break;
case 2:                                                         led2.write( value );
break;
case 3:                                                         led3.write( value );
break;
default:  break;

}
}
catch(IllegalAddressException e)
System.out.println( “Error in SET” );
System.out.println( e );
}
}
public int toSegment( char c)
{
// convert ASCII characters (as in INT into 7 Segments representation
return ( toSegment((int)c) );
}
public int toSegment( int x )
{
int i=0;
// We map SOME letters into an equivalent form (different case).
// You can add mor
e as you like.
switch (x) {

case  ((int)         ‘0’): i=0×3F; break;
case  ((int)         ‘1’): i=0×06; break;
case  ((int)         ‘2’): i=0×58; break;
case  ((int)         ‘3’): i=0×4F; break;
case  ((int)         ‘4’): i=0×66; break;
case  ((int)         ‘5’): i=0×6D; break;
case  ((int)         ‘6’): i=0×7D; break;
case  ((int)         ‘7’): i=0×07; break;
case  ((int)         ‘8’): i=0×7F; break;
case  ((int)         ‘9’): i=0×6F; break;
case  ((int) ‘A’):
case  ((int)         ‘a’): i=0×77; break;
case  ((int) ‘B’):
case  ((int)         ‘b’): i=0×7C; break;
case  ((int)         ‘C’): i=0×39; break;
case  ((int)         ‘c’): i=0×58; break;
case  ((int) ‘D’):
case  ((int)         ‘d’): i=0×5E; break;
case  ((int) ‘E’):
case  ((int)         ‘e’): i=0×79; break;
case  ((int) ‘F’):
case  ((int)         ‘f’): i=0×71; break;
case  ((int) ‘G’):
case  ((int)         ‘g’): i=0×7D; break;
case  ((int)         ‘H’): i=0×76; break;
case  ((int)         ‘h’): i=0×74; break;
case  ((int)         ‘I’): i=0×06; break;
case  ((int)         ‘i’): i=0×04; break;
case  ((int) ‘J’):
case  ((int)         ‘j’): i=0×1E; break;

case ((int) ‘L’): i=0×38; break;
case ((int) ‘l’): i=0×06; break;
case ((int) ‘N’):
case ((int) ‘n’): i=0×54; break;
case ((int) ‘O’): i=0×3F; break;
case ((int) ‘o’): i=0×5C; break;
case ((int) ‘P’):
case ((int) ‘p’): i=0×73; break;
case ((int) ‘R’):
case ((int) ‘r’): i=0×50; break;
case ((int) ‘S’):
case ((int) ‘s’): i=0×6D; break;
case ((int) ‘T’):
case ((int) ‘t’): i=0×78; break;
case ((int) ‘U’): i=0×3E; break;
case ((int) ‘u’): i=0×1C; break;
case ((int) ‘Y’):
case ((int) ‘y’): i=0×66; break;
case ((int) ‘Z’):
case ((int) ‘z’): i=0×5B; break;
case ((int) ‘?’): i=0×53; break;
case ((int) ‘.’): i=0×80; break;
case ((int) ‘[‘): i=0x39; break;
case ((int) ‘]’): i=0×0F; break;
case ((int) ‘-’): i=0×40; break;
case ((int) ‘_’): i=0×08; break;
case ((int) ‘|’): i=0×30; break;
case ((int) ‘ ‘): i=0×00; break;
case ((int) ‘:’): i=0×09; break;

default:
// other characters get mapped into a ? sort of thing
i = 0×53;
System.out.println( “Bad character: “ + x);
break;
}
return( i );
}
// Format the string for the LED display, look for “.” and set
// this as
// the decimal point then convert all characters to 7seg
// representation.
public byte[] format( String text )
{
String message = new String( text );
byte[] databytes = message.getBytes();
// scan data looking for “.”
for(int i=0; i<databytes.length; i++) {
if (databytes[i]==’.’) {
// put the decimal on previous character
databytes[i-1] = (byte) (databytes[i-1] | 0×80);
// now delete it by shifting all characters left by 1
for (int j=i; j<databytes.length-1; j++) {
databytes[j] = databytes[j+1];
}
// slap a space on the end
databytes[databytes.length-1]=(char)’ ‘;
// convert it to a segment
databytes[i] = (byte) toSegment(databytes[i]);
}
return( databytes );
}
// Make a long text message scoll across the 4 character display.
// pad the left with 4 spaces for it to scroll onto the display.
// pad the right with 4 spaces for it to scroll off the display.
public void scrollMessage( String text )
{
byte[] databytes = format( text );
// Now scroll the message
for(int i=0; i<databytes.length-3; i++) {
for(int j=0;j<4;j++) {
if ((i+j)>databytes.length-1) {
this.setDigit(j, 0×00);
}
else {
this.setDigit(j, databytes[i+j]);
}
}
TINIOS.sleepProcess(timedelay);
}
}
// Flash a short message on the display to get attention
public void flashMessage( String text )
{
byte[] databytes = format( text );
for( int x=0; x<flashcount; x++) {
// Now flash the first 4 characters
for(int i=0; i<4; i++) {
if (i>databytes.length-1) {
this.setDigit(i, 0×00);
}
else {
this.setDigit(i, databytes[i]);
}
}
TINIOS.sleepProcess(timedelay);
clear();
TINIOS.sleepProcess(timedelay);
}
}
public void setDelay( int delay)
{
timedelay = delay;
}
public void setFlashCount( int count )
{
flashcount = count;
}
}
Here is a summary of the methods in this class:

• LEDport(  int  ) is the constructor. It takes one parameter, the base address of the dataport.

• init() initializes the LED display.

• clear() clears the display (no digits on).

•setDigit( int, int )  sets a digit (0..3) to a particular value. The value is the character that has been encoded as an integer to turn on the proper segments.

• toSegment(  char  ) takes a character and returns an integer that represents the proper segments on the 7-segment display.

• toSegment  (  int  ) takes integer (ASCII value) and returns an integer that represents the proper segments on the 7-segment display

• format(  String  ) takes a character string and returns an array of bytes that have been properly encoded for display.

• scrollMessage(  String  )  takes a character string and scrolls it across the LED display from right to left.

•flashMessage(  String  )  takes a character string and flashes it on the LED display.

• setDelay(  int  ) sets the delay amount in ms that the scrollMessage will delay between each character or the time between flashes, depending on which method is used.

•setFlashCount(  int  ) sets the number of times to flash a message using the flashMessage() method.

The 7-segment encoding is quite simple. You select the segment you want turned on for a particular character. This character then requires those segments to be on, or a logic 1. These binary segments are then ordered: dp, g, f, e, d, c, b, a, according to the diagram in Figure 8-22. For example, “C” requires segments a, d, e and f. This is 00111001 in binary or 39 hex. Writing a 0×39 to the LED dataport for one of the characters will display a “C” on that LED.

The following program demonstrates the capabilities of the LEDport class and this LED display circuit.

Listing 8-15: myLED.java

import com.dalsemi.comm.*;
import com.dalsemi.system.*;
// Class myLED demonstrates the methods in our LEDport class
public class myLED
{
public static void main(String[] args)
{
final LEDport myled = new LEDport(0×0080000C);
myled.init();
// milliseconds to wait bewteen scroll steps and flashes
myled.setDelay( 150 );
// our version of “hello world”
myled.scrollMessage( “                                                                       HELLO tini   “);
// number of times to flash

myled.setFlashCount( 4 );
myled.flashMessage( “ThIS” );
myled.flashMessage( “ IS “ );
myled.flashMessage( “CooL” );
}
}

Compile this program:
C:\> cd src
C:\> javac -bootclasspath       %TINI_HOME%\bin\tiniclasses.jar
-d ..\bin myLED.java
C:\> cd ..
C:\> java                       -classpath %TINI_HOME%\bin\tini.jar;. BuildDependency
-p                              %TINI_HOME%\bin\owapi_dependencies_TINI.jar
-f bin
-x                              %TINI_HOME%\bin\owapi_dep.txt
-o bin\myLED.tini
-d %TINI_HOME%\bin\tini.db

Summary
In this chapter we discussed adding components and interfacing user input/output devices to TINI. There are certainly many other types of displays or I/O devices that you may wish to add. We hope that this chapter has provided you with the tools necessary to add these devices. In following chapters we will be discussing some of the other features of the Dallas Semiconductor E10/20 socketboards, such as the serial ports, parallel I/O, CAN, and I2C interface.

References
1.  Computer/Microcontroller Interfacing,

http://www.ffldusoe.edu/faculty/Denenberg/courses/GK415/Computer-

Microcontroller_Interfacing.htm
2.  Beyond Logic,

http://www.beyondlogic.org/

3.  L.O.S.A – List of Stamp Applications,

http://www.hth.com/losa/

4.  How to use Intelligent LCDs,

http://www.epemag.wimborne.co.uk/resources.htm

5.  The Extended Concise LCD Data Sheet,

http://www.electronic-engineering.ch/microchip/datasheets/lcd/

the_lcd_data_sheet.pdf
6.  How to Control a HD44780-based Character-LCD,

http://home.iae.nl/users/pouweha/lcd/lcd.shtml

Random Posts

Comments are closed.