Quadrature Decoder With Up/Down Counter — Use Case for CLB Using the PIC16F13276 Microcontroller With MCC Melody
This repository provides an MPLAB® X project that implements a quadrature decoder circuit using the Configurable Logic Block (CLB) peripheral.
The CLB peripheral is a collection of logic elements that can be programmed to perform a wide variety of digital logic functions. The logic function may be completely combinatorial, sequential, or a combination of the two, enabling users to incorporate hardware-based custom logic into their applications.
Quadrature signals are standard output waveforms of incremental encoders that come with motors. They provide information about the motor direction and shaft position. There are two types of encoders: linear and rotary, with two types of measurement each: absolute and incremental. The different technologies used for such encoders can be one of the following: magnetic, optical, inductive, capacitive, and laser.
A rotary absolute encoder outputs a unique code for each position of the motor's shaft. If the power of this encoder is lost and the shaft is rotated, the encoder can provide the correct position of the shaft. The rotary incremental encoder generates pulses while the shaft is rotated. If any pulse is missed, the correct position of the shaft cannot be determined any longer. This application uses a rotary incremental encoder to show how to decode the quadrature signals.
The following image highlights the Clockwise (CW) and Counterclockwise (CCW) conditions on the quadrature signals A and B. Signal A leading signal B (either on rising or falling edge) results in a clockwise detection, whereas signal B leading signal A (either on rising or falling edge) results in a counterclockwise detection.
More details and code examples on the PIC16F13276 can be found at the following links:
- MPLAB X IDE v6.30 or newer or MPLAB® Tools for VS Code®
- MPLAB® XC8 v3.10 or newer
- PIC16F1xxxx_DFP v1.29.444 or newer
-
Oscilloscope/Logic analyzer
Note: The setup can be made with a rotary encoder or with a motor with encoder. In this example, the setup uses a rotary encoder.
To program the Curiosity Nano board with this MPLAB X project, follow the steps provided in the How to Program the Curiosity Nano Board chapter.
This application showcases the capabilities of the CLB peripheral by creating a logic circuit that decodes the waveforms from a rotary encoder and a 4-bit up/down counter circuit used to track the ticks. The circuit comprises: two inputs, filtered phase signals from the encoder (the filtering circuit was implemented with a combination of the Configurable Logic Cell (CLC) modules), and four outputs corresponding to the 4-bit up/down counter. Additional logic is used to detect the overflow/underflow transitions of the up/down counter and to route them as interrupts.
To compute the total number of ticks, the counter value and the total number of overflow and underflow transitions are read in software. The total number of encoder increments is obtained using the following formula:
The decoder circuit implementation is displayed in the image below:
The phase signals from the rotary encoder have glitches when transitioning, so a debouncing circuit is used to avoid these false detections. The debouncing circuit is implemented using two CLC modules, as presented in the following figures:
To detect a clockwise turn of the rotary encoder, the phase A signal has to lead the phase B signal either on a rising or falling edge.
To detect a counterclowise turn of the rotary encoder, the phase B signal has to lead the phase A signal either on a rising or falling edge.
The logic mechanism used to decode the clockwise and counterclockwise ticks from the encoder consists of a 4-input Look-Up Table (LUT), with the following inputs: phase A, delayed phase A, phase B, and delayed phase B. The following diagrams present the CLB circuit implementation and the detection principle by using two graphs.
The implementation for the 4-bit up/down counter circuit can be found below:
To detect an overflow or an underflow event from the 4-bit up/down counter, the two Most Significant output bits (MSb) from the counter are used as input for a detector implemented within CLB. The overflow/underflow detector circuit outputs an interrupt everytime an overflow or an underflow event is detected. The implementation and working principle are presented in the figures below:
The following peripheral and clock configurations are set up using MPLAB® Code Configurator (MCC) Melody for the PIC16F13276:
-
Configuration Bits:
-
Clock Control:
-
Interrupt Manager:
-
CLB Synthesizer Library:
-
CLB1:
-
CRC:
- Auto-configured by CLB
-
UART2:
-
EUSART2:
-
CLC1:
-
CLC2:
-
CLC3:
-
CLC4:
Note: For details on the CLC configurations, check the diagrams displayed in the MCC Melody figures.
- Pin Grid View and Pins:
- EUSART2 RX2: RD7
- EUSART2 TX2: RD6
- CLBPPSOUT0: RC0 (4-bit counter bit 0)
- CLBPPSOUT1: RC1 (4-bit counter bit 1)
- CLBPPSOUT2: RC2 (4-bit counter bit 2)
- CLBPPSOUT3: RC3 (4-bit counter bit 3)
- CLC4 CLCIN1: RC6
- CLC3 CLCIN1: RC6
- CLC2 CLCIN0: RC7
- CLC1 CLCIN0: RC7

The following pin configurations must be made for this project:
| Pin | Configuration | Function |
|---|---|---|
| RC7 | Input | Encoder phase A (CLCIN0PPS) |
| RC6 | Input | Encoder phase B (CLCIN1PPS) |
| RD6 | Output | EUSART2 TX2 |
| RD7 | Input | EUSART2 RX2 |
| RC0 | Output | 4-bit up/down counter bit 0 (CLBPPSOUT0) |
| RC1 | Output | 4-bit up/down counter bit 1 (CLBPPSOUT1) |
| RC2 | Output | 4-bit up/down counter bit 2 (CLBPPSOUT2) |
| RC3 | Output | 4-bit up/down counter bit 3 (CLBPPSOUT3) |
The setup for the demo is presented in the diagram below.
In this demo, every clockwise or counterclockwise pulse detection results in a printed message on the serial terminal.
The figure below presents a logic analyzer capture that contains the waveforms corresponding to the messages sent to the terminal in the above figure.
Note: Follow the steps in the How to use MPLAB® Data Visualizer section to set up the Data Visualizer so that the text messages through UART2 are displayed correctly.
This application showcases how to create a quadrature decoder circuit that can detect and count clockwise and counterclockwise pulses on rising and falling edges of the quadrature signals using the CLB peripheral.
This section illustrates how to use the MPLAB® X Data Visualizer; this can be applied to any other projects, too.
-
Open the software terminal in MPLAB® X IDE. Click the Data Visualizer button.

-
Select the Input Source corresponding to the PIC16F13276 on the right side..
-
Identify the COMx port corresponding to the PIC16F13276 Curiosity Nano board and select it from the Connections tab on the left-side menu.
-
Set the specific Baud Rate, in this case 115200.
-
Press the Start streaming COMx button. The text messages will appear in the Terminal tab.

This chapter demonstrates how to use the MPLAB X IDE to program a PIC® device with an Example_Project.X. This is applicable to other projects, too.
-
Connect the board to the PC.
-
Open the
Example_Project.Xproject in MPLAB X IDE. -
Set the
Example_Project.Xproject as main project.
Right click the project in the Projects tab and click Set as Main Project.
-
Clean and build the
Example_Project.Xproject.
Right click theExample_Project.Xproject and select Clean and Build.
-
Select PICxxxxx Curiosity Nano in the Connected Hardware Tool section of the project settings:
Right click the project and click Properties.
Click the arrow under the Connected Hardware Tool.
Select PICxxxxx Curiosity Nano (click the SN), click Apply and then click OK:
-
Program the project to the board.
Right click the project and click Make and Program Device.




























