How to Multiplex LEDs

Last modified by Microchip on 2024/03/28 13:46

Objective

This tutorial shows how to multiplex LEDs to control more LEDs with fewer general-purpose input/output (GPIO) microcontroller (MCU) pins. It is based on an PIC18F56Q71 8-bit MCU although this technique can be generalized for any MCU. It uses Microchip’s MPLAB® Code Configurator (MCC) Melody to generate software to configure GPIO and other system functions.

Overview

This tutorial makes use of Microchip’s PIC18F56Q71 Curiosity Nano platform to simplify hardware integration. It also showcases the capability of MCC Melody Code Generator to create the required drivers and MCC Melody API (Application Protocol Interface) functions to configure and control the GPIO as needed.

MCC Melody is a free graphical programming environment that generates seamless, easy-to-understand C code that is inserted into your project. It enables and configures a rich set of Core-Independent Peripherals (CIPs), GPIOs and functions using an intuitive interface. It is integrated into MPLAB X IDE to provide a powerful and easy-to-use development platform.

In this training, the PIC18F56Q71 will be configured to run a state machine that will repeatedly illuminate each of the six LEDs sequentially for 0.5s.

PIC18F56Q71 Curiosity Nano Evaluation Board is available from Microchip Purchasing and Client Services. The breadboard, jumper wires and LEDs are readily available from third-party suppliers such as Amazon or DigiKey.

The application begins with a 500 ms delay, then sequentially lights each LED individually based on the state counter, which is incremented after each step and reset after the last LED is activated. This process is repeated indefinitely.

Note: This solution is only capable of lighting one LED at a time or turning all LEDs off. This limitation permits multiplexing to minimize GPIO requirements.

Multiplex LEDs Software Flow ChartThis application utilizes three GPIOs configured as either high-impedance inputs or output driven either logic level 1 or 0 to control the LEDs.

GPx LEDs
210 123456
000 000000
Z10 100000
Z01 010000
10Z 001000
01Z 000100
1Z0 000010
0Z1 000001

Legend: 0 = Output driven low, 1 = Output driven high, Z = High-impedance input

Two Ways to Use This Tutorial

  • Create the projects from scratch:
    • Follow the step-by-step instructions to create the required software
  • Use the solution projects as an example:
    • Build the solution project and program it into the MCU demonstration board to observe the expected behavior

Lab Objectives

  • Create an MPLAB X IDE project for a PIC18F56Q71 MCU from scratch
  • Use MCC Melody to configure and generate the GPIO driver code

Materials

Connection Diagram

The PIC18F56Q71 Evaluation Board is designed to plug in directly to a breadboard. The USB connection to the evaluation board should be sufficient to power the entire circuit. The jumper wires and LEDs will be used to complete the circuit as shown. Software and drivers will be configured to light the LEDs in a sequential manner.

Multiplexed LEDs Connection Diagram

Note: This project has been verified to work with the following versions of software tools:

Because we regularly update our tools, occasionally you may discover an issue while using newer versions. If you suspect that to be the case, we recommend that you double-check and use the same versions that the project was tested with.

Lab Solutions

This ZIP file contains the completed solution projects for this lab. The contents of this ZIP file can be placed in a folder of your choice. This file is stored in a GitHub repository. You will need to create a free account to download the files.

GitHub Download

Note: Because MCC generates source, header files and libraries under the project folder, the contents of this ZIP file can be placed in any folder of your choice.

Note: Steps 1 – 5 must be completed before you are ready to build, download, and run the application.

Connect PIC18F56Q71 Hardware

Assemble LED circuit using breadboard, LEDs, resistors and jumper wires.

Multiplexed LEDs Connection Diagram

Note: Each LED will illuminate when GPIO voltage is applied to the anode connection (long side of the triangle of the diode symbol) and ground is applied to the cathode connection (line on the front of the triangle). Through-hole diodes will have a longer lead indicating the anode and a shorter lead for the cathode. They typically also have a flat side indicating the cathode side. Through-hole LED Orientation

Connect the computer to PIC18F56Q71 Curiosity Nano Evaluation Board using a Type-A male to Micro-B male USB cable.

Note: The USB connection provides power and debug communication to the PicKit™ On-Board (PKOB) circuitry on the evaluation kit. No additional power connections should be required.

Back to Top


Create and Set Up MPLAB X IDE Project for PIC18F56Q71

Install MPLAB X IDE

if it’s not already installed.

Install MPLAB XC8 Compilerif it’s not already installed or if your current version does not support PIC18F56Q71 .

Note: MPLAB code configurators such as MCC Melody are pre-installed into MPLAB X IDE therefore, a separate installation is not required.

Back to Top


Launch MCC Melody

Open and initialize MCC by clicking on the MCC button.

MCC Icon

After initialization, click on the Select MCC Melody button.

MCC Content Manager Wizard

Then, click Finish and wait for MCC to initialize and load the required contents.

MCC Content Manager Wizard Finish Button

Back to Top


Configure MCC Melody

Open the Pin Manager (Project Resources > Pins).

MCC Melody Project Resources Pins

Assign Port B pins 0, 1 and 2 to be GPIO inputs. The assignment can be made to any three available pins. These assignments configure the GPIOs as high-impedance inputs to protect the MCU during start-up.

MCC Melody Pin Grid View

Locate the Pins tab Easy View (typically in the top right corner of the screen). In the Custom Name column, rename the pins as "GP0", "GP1" and "GP2".

MCC Melody Pins Easy View

Add the Delay driver from the Device Resources panel under the Timer dropdown menu by selecting its green plus symbol.

MCC Melody Device Resources Timer

Click Generate to create and insert MCC Melody driver code into your MPLAB X IDE project.

MCC Generator Button

Verify code was successfully generated.

MCC Generate Success

Back to Top


Update main.c

Click on the Projects tab, open the Source Files folder and double-click on main.c to open it.

MPLAB X IDE Open main.c

The following code will be inserted into the main() function in main.c. The code implements a state machine to light each LED individually by changing each GPIO from a high-impedance input, output low or output high to light each LED sequentially for 500 ms. This algorithm refers to each GPIO by the custom name assigned in MCC Melody’s Pin Manager. These macros are described in the MCC Melody API Reference for the selected device architecture. 

  • GPx_SetDigitalOutput():
    • Configures the pin associated with GPx as an output pin that can be driven HIGH or LOW.
  • GPx_SetHigh():
    • Sets the associated digital output pin HIGH.
  • GPx_SetLow():
    • Sets the associated digital output pin LOW.

Note: Pins must be configured as a DigitalOutput before GPx_SetHigh or GPX_SetLow macros are called.

  • GPx_SetDigitalInput():
    • Configures the pin associated with GPx as a high-impedance input pin 
  • DELAY_milliseconds()
    • Delays the execution of the program for a certain number of milliseconds

Note: This is a blocking function and is presented here for demonstration purposes.

Add the following function declaration above the main() function:

void DELAY_milliseconds(uint16_t);

Replace main() code with the following:

uint8_t state = 0;

int main(void)
{
   SYSTEM_Initialize();
   while(1)
    {
       DELAY_milliseconds(500); //Sit here and wait for 500 ms
       switch(state)
        {
           case 0:
               GP2_SetDigitalOutput();
               GP1_SetDigitalOutput();
               GP0_SetDigitalOutput();
               GP2_SetLow();
               GP1_SetLow();
               GP0_SetLow();               
               break;
           case 1:
               GP2_SetDigitalInput(); //sets the GP2 pin as High Impedance(Z)input
               GP1_SetDigitalOutput();
               GP0_SetDigitalOutput();
               GP2_SetLow();
               GP1_SetHigh(); //drive GP1 High
               GP0_SetLow();                     
               break;
           case 2:
               GP2_SetDigitalInput();
               GP1_SetDigitalOutput();
               GP0_SetDigitalOutput();
               GP2_SetLow();
               GP1_SetLow();
               GP0_SetHigh();
               break;
           case 3:
               GP2_SetDigitalOutput();
               GP1_SetDigitalOutput();
               GP0_SetDigitalInput();
               GP2_SetHigh();
               GP1_SetLow();
               GP0_SetLow();
               break;
           case 4:
               GP2_SetDigitalOutput();
               GP1_SetDigitalOutput();
               GP0_SetDigitalInput();
               GP2_SetLow();
               GP1_SetHigh();
               GP0_SetLow();
               break;
           case 5:
               GP2_SetDigitalOutput();
               GP1_SetDigitalInput();
               GP0_SetDigitalOutput();
               GP2_SetHigh();
               GP1_SetLow();
               GP0_SetLow();
               break;
           case 6:
               GP2_SetDigitalOutput();
               GP1_SetDigitalInput();
               GP0_SetDigitalOutput();
               GP2_SetLow();
               GP1_SetLow();
               GP0_SetHigh();
               state = 0;
               break;
           default:
               GP2_SetDigitalInput();
               GP1_SetDigitalInput();
               GP0_SetDigitalInput();
               GP2_SetLow();
               GP1_SetLow();
               GP0_SetLow();    
               state = 0;
        }
       ++state;
    }
}

Back to Top


Build and Program PIC18F56Q71

Build the project by clicking the Build for Debugging ​Main Project icon.

MPLAB X IDE Build Icon

MPLAB X IDE Build Successful

Program MCU

Program the MCU by clicking the Make and Program Device Main Project icon.

MPLAB X IDE Make and Program Project Icon

MPLAB X IDE Programming Complete

Back to Top


Test Application

Run the project by clicking the Run Main Project icon. MPLAB X IDE Run Project Icon

Back to Top


Results

You will see all six LEDs light up sequentially for 500 ms.

Multiplexed LEDs Demo Success

Analysis

You successfully demonstrated GPIO and Delay functionality using MCC Melody. Your project used fundamental elements to configure GPIO as high-impedance inputs and outputs driven both high and low. This demonstration continuously runs a software state machine to sequentially light each of the 6 LEDs using only 3 GPIOs.

You used MCC Melody to configure a delay routine and GPIOs and accessed fundamental GPIO functions in MCC Melody to successfully complete this training.

Back to Top