How to Charlieplex LEDs
Overview
Charlieplexing is a technique that multiplexes LEDs with tri-state to save on I/O pins. For N pins used, charlieplexing allows for (N2 - N) LEDs to be controlled. In this example, three pins drive six LEDs in the following arrangement.
Since the three LEDs have three states (HIGH, LOW, High-Z [tri-state]), each pin has two outputs – an Output Enable (OE) and the digital output. This creates a truth table for each LED as such:
Output Enable | Digital Output | I/O State |
0 | X | High-Z |
1 | 0 | 0 |
1 | 1 | 1 |
For this example, the LEDs turn on according to the number the microcontroller sets in the CLBSWINx Register (CLB Software Input):
CLBWIN2 | CLBWIN1 | CLBWIN01 | Active LED |
0 | 0 | 0 | None |
0 | 0 | 1 | LED1 |
0 | 1 | 0 | LED2 |
0 | 1 | 1 | LED3 |
1 | 0 | 0 | LED4 |
1 | 0 | 1 | LED5 |
1 | 1 | 0 | LED6 |
1 | 1 | 1 | None |
Here’s how the schematic looks in MPLAB® Code Configurator (MCC) Melody Configurable Logic Block (CLB) tool:
Requirements
Procedure
Implement the CLB Logic
The clock source used was HFINTOSC, but other sources like LFINTOSC, shouldn’t cause issues.
Configure the Look-Up Tables (LUTs)
Click on each LUT and look at properties.
At the bottom of the table, there is a text field for entering a hexadecimal number corresponding to the truth table.
- PPS_OE0 = 001E
- PPS_OE1 = 0078
- PPS_OE2 = 0066
- PPS_OUT0 = 0012
- PPS_OUT1 = 0048
- PPS_OUT2 = 0024
Assign the pins to the desired locations.
Generate MCC code.
Set up the main function as follows:
{
SYSTEM_Initialize();
while(1)
{
NOP();
NOP();
NOP();
CLB1_SWIN_Write16(0);
NOP(); __delay_ms(250u);
CLB1_SWIN_Write16(1);
NOP(); __delay_ms(250u);
CLB1_SWIN_Write16(2);
NOP(); __delay_ms(250u);
CLB1_SWIN_Write16(3);
NOP(); __delay_ms(250u);
CLB1_SWIN_Write16(4);
NOP(); __delay_ms(250u);
CLB1_SWIN_Write16(5);
NOP(); __delay_ms(250u);
CLB1_SWIN_Write16(6);
NOP(); __delay_ms(250u);
}
}
Build the circuit on a breadboard. Refer to Figure 1 for guidance.
Verify the design.
Results
In this screenshot from a logic analyzer, the seven states can be seen clearly. The top three traces represent the digital value the pin is in, while the three traces below are the analog values of those signals. Each marker pair (1, 2, 3, etc…) represents which LED is enabled at that time. The LEDs are off before and after the markers show as the I/O is tri-stated.
Video 1 demonstrates charlieplexing in action, showcasing the LEDs cycling through the states as observed in the logic analyzer.