How to Make 3-bit Number Comparisons

Last modified by Microchip on 2025/01/07 16:25

Overview

In many cases, detecting when a number is greater than, less than, and/or equal to is desirable. These comparisons only require two Look-Up Tables (LUTs) to implement for a 3-bit number. Greater than and less than are the same operation, but with the look-up values inverted.

MPLAB Melody CLB 3 Number Comparator

Information

Note: TMR2_Postscaled is routed through the Configurable Logic Block (CLB) circuit for the logic analyzer to decode the bus as parallel. It is not required for the comparison operation.

Information

Note: The LUT values vary depending on the desired comparison.

Requirements

Verilog Code for “Counter3” module

module Counter3 (CLK, out0, out1, out2);
   input CLK;
   output out0, out1, out2;
   reg [2:0] counter;

   assign out0 = counter[0];
   assign out1 = counter[1];
   assign out2 = counter[2];

   always @(posedge CLK) begin
       counter <= counter + 1;
   end
endmodule

Back to Top

Procedure

Create the necessary Counter3 Verilog files in the CLB tool. Refer to the Verilog Code for “Counter 3” module section for the needed Verilog code.

Information

Note: The “How to Use Verilog Sheets with the Configurable Logic Block (CLB) Synthesizer Tool” page provides more information on how to implement Verilog in the CLB tool.

Back to Top


Implement the CLB logic.

Configure the CLB to implement the breathing LED logic. Refer to the block diagram for guidance.

Information

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

Back to Top


Configure the Look-Up Tables (LUTs).

Click on each LUT and look at the properties.

At the bottom of the table, there is a text field for entering a hexadecimal number corresponding to the truth table.

The LUT values vary depending on the configuration. See the list below:

  • For Less Than (Reference Value):
LSB LUT = 0CEE
MSB LUT = 00B2
  • For Equal To (Reference Value):
LSB LUT = 8001
MSB LUT = 0081
  • For Less Than or Equal To (Reference Value):
LSB LUT = 8CEF
MSB LUT = 00B2
  • For Greater Than, invert the output of the MSB LUT or swap the inputs
00B2 --> FF4D

Back to Top


Assign the outputs as desired.

Back to Top

Results

Less Than

The output is HIGH if the counter is greater than, but not equal to, the reference value of 4.

Less Than 3 Number Comparison Waveform

Equal To

The output is HIGH if the counter is equal to 4.

Equal To 3 Number Comparison Waveform

Less Than or Equal To

The output is HIGH if the counter is less than OR equal to the reference value of 4.

Less Than Or Equal To 3 Number Comparison Waveform

Back to Top

Learn More

Back to Top