Tutorial 8
Multi digit LED display

Multiplexed display

External device library works with 7-segment (and point) LED displays with multiple digits. This displays should be multiplexed. Multiplexed displays are discribed, for example, in Wikipedia - Multiplexed display. Displays can have any number of digits and be with common anode or cathode. All 8 segments (with point) are used. Display's segment pins can be connected to one port of MCU (segment a - to bit 0, b - bit 1, ..., h - bit 7) or to any port lines available in microcontroller. In this tutorial display with 4 digits and common cathode is used. This is a schematic:


Schematic of board with LED display

Download schematic of board with LED display in TinyCad format (.dsn)

Test program

Number of digits is determined by the program from the number of common pins. First common pin should be pin of less significant digit, last - most significant. Multiplexing itself is performed in processMultiplexing() method. So, it must be called with frequency from 120 Hz * (number of digits) to 500 Hz * (number of digits). For 4 digits, it will be from 480 to 2000 Hz. In this tutorial, 8-bit timer overflowes with frequency 1 MHz/8/256 ≈ 488 Hz. Every time timer overflowes, processMultiplexing() method is called.

import mcujavasource.mcu.*;
import mcujavasource.mcu.external.led.MultiDigitLedDisplay;

/** Tutorial 8: multi digit LED display.
 * At the beginning, number 0000 is displayed.
 * First digit increments, then second after delay, third, fourth. First digit
 * increments again. Program is repeated discontiniously.
 */
public class Main extends Microcontroller
{
  private MultiDigitLedDisplay ledDisplay;
  private Port segmentPort = getHardware().getPort("D");
  private Port commonPort = getHardware().getPort("C");
  private Pin common0 = commonPort.getPin(1);
  private Pin common1 = commonPort.getPin(2);
  private Pin common2 = commonPort.getPin(3);
  private Pin common3 = commonPort.getPin(4);
  
  private Timer ledDisplayTimer;
  
  public void init()
  { getHardware().setAllPortsDirection(Pin.IN);
    getHardware().setAllPortsPullUp(true);
    ledDisplay = new MultiDigitLedDisplay(true, //common anode
        segmentPort, common0, common1, common2, common3);
    ledDisplayTimer = getHardware().getDefaultTimer(8);
    ledDisplayTimer.setMode(TimerMode.NORMAL);
    ledDisplayTimer.setPrescaling(8);
    ledDisplayTimer.setEnabled(true);
    LedDisplayDigitMultiplexer multiplexer = new LedDisplayDigitMultiplexer();
    ledDisplayTimer.addTimerListener(multiplexer);
    ledDisplayTimer.setTimerOverflowedFired(true);
  }
  
  public void start()
  { ledDisplay.setDigitValue(0, 0);
    ledDisplay.setDigitValue(1, 0);
    ledDisplay.setDigitValue(2, 0);
    ledDisplay.setDigitValue(3, 0);
    getHardware().setInterruptsEnabled(true);
    for(;;)
    { for(int digit = 0; digit < 4; digit++)
      { int value = ledDisplay.getDigitValue(digit);
        if(++value > 10) value = 0;
        ledDisplay.setDigitValue(digit, value);
        getHardware().getDelay().sleep(250);
      }
    }
  }
  
  private class LedDisplayDigitMultiplexer implements TimerListener
  {
    public void timerOverflowed()
    { ledDisplay.processMultiplexing();
    }
   
  }
  
  
}

Download java source file