How to Multiplex LEDs
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.
This 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 | ||||||||
---|---|---|---|---|---|---|---|---|---|
2 | 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
Z | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | |
Z | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | |
1 | 0 | Z | 0 | 0 | 1 | 0 | 0 | 0 | |
0 | 1 | Z | 0 | 0 | 0 | 1 | 0 | 0 | |
1 | Z | 0 | 0 | 0 | 0 | 0 | 1 | 0 | |
0 | Z | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
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
- PIC18F56Q71 Curiosity Nano Evaluation Board (EV01G21A)
- One USB Type-A male to Micro-B male cable for programming and debugging
- Six through-hole LEDs
- Three 470 Ohm resistors
- Small solderless breadboard
- Six male-to-male jumper wires
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.
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.
Connect PIC18F56Q71 Hardware
Assemble LED circuit using breadboard, LEDs, resistors and jumper wires.
Connect the computer to PIC18F56Q71 Curiosity Nano Evaluation Board using a Type-A male to Micro-B male USB cable.
Create and Set Up MPLAB X IDE Project for PIC18F56Q71
if it’s not already installed.
Install MPLAB XC8 Compilerif it’s not already installed or if your current version does not support PIC18F56Q71 .
Launch MCC Melody
Open and initialize MCC by clicking on the MCC button.
After initialization, click on the Select MCC Melody button.
Then, click Finish and wait for MCC to initialize and load the required contents.
Configure MCC Melody
Open the Pin Manager (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.
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".
Add the Delay driver from the Device Resources panel under the Timer dropdown menu by selecting its green plus symbol.
Click Generate to create and insert MCC Melody driver code into your MPLAB X IDE project.
Verify code was successfully generated.
Update main.c
Click on the Projects tab, open the Source Files folder and double-click on main.c to open it.
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.
- 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
Add the following function declaration above the main() function:
Replace main() code with the following:
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;
}
}
Build and Program PIC18F56Q71
Build the project by clicking the Build for Debugging Main Project icon.
Program MCU
Program the MCU by clicking the Make and Program Device Main Project icon.
Test Application
Results
You will see all six LEDs light up sequentially for 500 ms.
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.