Quadrature Decoder — Use Case for Configurable Logic Block (CLB) Using the PIC16F13145 Microcontroller With MCC Melody
This repository provides an MPLAB® X project that implements a quadrature decoder circuit using the 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 the standard output waveforms of incremental encoders that come with motors. They provide information about motor direction and shaft position. There are two types of encoders - linear and rotary - and each can have two types of measurement: absolute and incremental. There are also different technologies used for encoders: magnetic, optical, inductive, capacitive, and laser.
The main categories of encoders are absolute and incremental. 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 return the correct position of the shaft. The rotary incremental encoder generates pulses while the shaft is rotated. If any pulse is missed, then the shaft position cannot be determined correctly anymore. This application will use a rotary incremental encoder to show how to decode quadrature signals.
The following image highlights the clockwise (CW) and counterclockwise (CCW) conditions on the quadrature signals A and B. When signal A leads signal B (on rising or falling edge), it will be a clockwise detection, otherwise, when signal B leads signal A (on rising or falling edge), it will be a counterclockwise detection.
More details and code examples on the PIC16F13145 can be found at the following links:
-
The PIC16F13145 Curiosity Nano Development board is used as a test platform:
-
Oscilloscope/Logic analyzer
Note: The setup can be made using a rotary encoder or a motor with encoder. In this example we will focus on the setup that 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. Each time a clockwise or counterclockwise condition is detected, one clock cycle pulse is generated on the corresponding output line (clockwise or counterclockwise). The output signals are routed as clock sources for TMR0 and TMR1, the timers being used to count the ticks. The total numbers of encoder increments is obtained by subtracting the values read from timers. By using timers as counters to store the rotation information, the CPU does not need to monitor the decoder signal and can simply calculate the total rotation since its last check when a new rotation value is needed. The decoder circuit implementation is figured in the picture below:
The phase signals from the rotary encoder have glitches when transitioning, so a debouncing circuit is used to avoid false detections. The debouncing circuit is presented in the figure below:
To detect a clockwise turn of the rotary encoder the phase A signal has to lead the phase B signal on either rising or falling edge. There are two scenarios:
- rising edge detected on phase B and the phase A high
- falling edge detected on phase B and the phase A low
To detect a counterclowise turn of the rotary encoder the phase B signal has to lead the phase A signal on either rising or falling edge. There are two scenarios:
- rising edge detected on phase A and the phase B high
- falling edge detected on phase A and the phase B low
To implement edge detection for the phase signals the following edge detector circuits have been used:
The following peripheral and clock configurations are set up using MPLAB® Code Configurator (MCC) Melody for the PIC16F13145:
-
Configurations Bits:
-
Clock Control:
-
CLB1:
-
CRC:
- Auto-configured by CLB
-
NVM:
- Auto-configured by CLB
-
TMR0:
-
TMR1:
-
TMR2:
-
UART1:
-
Pin Grid View:
- EUSART1 TX: RC4
- EUSART1 RX: RC5
- CLBPPSOUT0: RC0 (CW)
- CLBPPSOUT2: RC1 (CCW)
- CLBIN0PPS: RA2 (phase A)
- CLBIN1PPS: RC7 (phase B)
The following pin configurations must be made for this project:
Pin | Configuration | Function |
---|---|---|
RA2 | Input | Encoder Phase A |
RC7 | Input | Encoder Phase B |
RC0 | Output | CW detection |
RC1 | Output | CCW detection |
RC4 | Output | EUSART1 TX |
RC5 | Input | EUSART1 RX |
The setup for the demo is presented in the diagram below.
In this demo, the total number of pulses detected over 2 seconds is printed on the serial terminal.
Below is presented a logic analyzer capture that contains the rotary encoder waveforms and the detected pulses.
Note: Follow the steps in the How to use MPLAB® Data Visualizer section to set up the Data Visualizer so that it can correctly show the text messages through UART1.
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.
-
Open the software terminal in MPLAB® X IDE. Left click on the Data Visualizer button.
-
Select the Input Source corresponding to the PIC16F13145 in the right side.
-
Identify the coresponding COMx that is represented by the PIC16F13145 Curiosity Nano Board and select it in the Connections tab in the left side.
-
Set the specific Baud Rate, in this case 115200.
-
Press 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.
-
Connect the board to the PC.
-
Open the Example_Project.X project in MPLAB X IDE.
-
Set the Example_Project.X project as main project.
Right click the project in the Projects tab and click Set as Main Project. -
Clean and build the Example_Project.X project.
Right click the Example_Project.X project 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.