How to Drive a 7-segment Display

Last modified by Microchip on 2025/01/07 11:36

Overview

7-segment displays are widely used in applications like digital clocks, temperature displays and basic user interfaces where numeric or limited alphanumeric data needs to be visually represented. This page demonstrates how to use the Configurable Logic Block (CLB) to drive a 7-segment display, reducing external components and freeing up the CPU for other operations.

Each segment (Segment A, Segment B, etc.) of the 7-segment display requires its own control signal, which is generated by the microcontroller’s I/O pins. The CLB processes a 4-bit Binary Coded Decimal (BCD) input, representing a single digit (0–9), and decodes it into the seven control signals. This ensures the appropriate segments light up to display the desired output.

7 Segment LED Display Schematic

The CLB can be utilized to drive a 7-segment display using a 4-bit Binary Coded Decimal (BCD) input. A primary design challenge is that a 4-bit BCD input cannot directly drive the individual segments of the display. The CLB overcomes this by implementing custom hardware logic that seamlessly converts the BCD input into the precise control signals needed for each segment.

MPLAB Melody CLB BDC to 7 Segment Display Verilog Block

Information

Note: The bcd7seg block represents custom Verilog code, not a drag-and-drop module. Users must write or import the Verilog code into the CLB configuration. See the "How to Use Verilog Sheets with the Configurable Logic Block Synthesizer Tool" page to learn how to create a Verilog module in the CLB Synthesizer and integrate it into your design.

Verilog Code for bcd7Seg Module:

module bcd7seg(
  bcd0, bcd1, bcd2, bcd3,
  seg0, seg1, seg2, seg3, seg4, seg5, seg6
);

input bcd0, bcd1, bcd2, bcd3; // initializing bcd as a 4-bit input signal
output seg0, seg1, seg2, seg3, seg4, seg5, seg6; // initializing seg as a 7-bit output signal

assign seg0 = ~(((~bcd3)&(~bcd2)&(~bcd1)&bcd0) | ((~bcd3)&bcd2&(~bcd1)&(~bcd0))); // Logical expression for segment A
assign seg1 = ~(((~bcd3)&bcd2&(~bcd1)&bcd0) | ((~bcd3)&bcd2&bcd1&(~bcd0)));       // Logical expression for segment B
assign seg2 = ~(((~bcd3)&(~bcd2)&bcd1&(~bcd0)));                                  // Logical expression for segment C
assign seg3 = ~(((~bcd3)&(~bcd2)&(~bcd1)&bcd0) | ((~bcd3)&bcd2&(~bcd1)&(~bcd0)) | ((~bcd3)&bcd2&bcd1&bcd0)); // Logical expression for segment D
assign seg4 = ~(((~bcd3)&bcd0) | ((~bcd3)&bcd2&(~bcd1)) | (~(bcd2)&(~bcd1)&bcd0)); // Logical expression for segment E
assign seg5 = ~(((~bcd3)&(~bcd2)&bcd0) | ((~bcd3)&(~bcd2)&bcd1) | ((~bcd3)&bcd1&bcd0)); // Logical expression for segment F
assign seg6 = ~(((~bcd3)&(~bcd2)&(~bcd1)) | ((~bcd3)&bcd2&bcd1&bcd0));          // Logical expression for segment G

endmodule

Requirements

Procedure

Implement the CLB logic by creating a Verilog sheet in the CLB Synthesizer tool to define the logic for driving the 7-segment display. This Verilog code decodes the 4-bit BCD input into the seven control signals required to drive the display segments. Refer to the block diagram above for guidance.

Information

Note: If using MPLAB® Code Configurator, use the CLB Synthesizer tool or download the pre-configured CLB file.

Back to Top


Assign pins by mapping the 4-bit BCD input signals to the appropriate input connections. Route the seven decoded output signals from the CLB to the corresponding connections for the segments (Segment A, Segment B, etc.) of the 7-segment display.

Back to Top


To ensure the 7-segment display functions correctly, the source code in the main application must provide input values to the CLB. This can be done by cycling through values dynamically, such as 0–9 for the 7-segment display.

Here is a code example of software-based input to the CLB:

while(1) {
 for(uint8_t i = 0; i < 10; i++) {
   CLB1_SWIN_Write(i); // Send BCD input to CLB
   _delay_ms(250);     // Add a delay to make the output visible
    }
  }

Back to Top


Verify the design by testing the 7-segment display to ensure each BCD input (0–9) activates the correct segments. Use a debugging tool or simulation to confirm that the CLB outputs match the expected segment patterns and that the display operates smoothly without errors.

Back to Top

Results

The GIF demonstrates the 7-segment display cycling through digits 0–9, with each digit accurately represented by the correct combination of illuminated segments. This confirms that the CLB logic successfully decodes the 4-bit BCD inputs into segment control signals. The smooth transitions and proper representation validate the design and its intended functionality.

LED Counter Circuit

Back to Top

Learn More

Back to Top