Manchester Decoder With Configurable Bitrate Based on CLB Using the PIC16F13276 Microcontroller With MCC Melody
The repository contains a Manchester Decoder hardware implementation which uses the Configurable Logic Block (CLB). Other peripherals are also used to support the CLB, including the Serial Peripheral Interface (SPI) and Timer0 (TMR0).
The CLB peripheral is a collection of logic elements that can be programmed to perform a 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.
The Manchester code combines data and clock into a single signal, where one clock cycle is a Manchester-bit period with a transition occurring in the middle of it. Logic ‘0’ is represented by a falling edge (HIGH to LOW transition) in the middle of the bit period, and logic ‘1’ is represented by a rising edge (LOW to HIGH transition) in the middle of the bit period.
More details and code examples on the PIC16F13276 can be found at the following links:
- PIC16F13276 Product Page
- PIC16F13276 Code Examples on Discover
- PIC16F13276 Code Examples on GitHub
- Manchester Encoder with Configurable Bitrate Based on CLB Using the PIC16F13276 Microcontroller with MCC Melody
- Manchester Encoder and Decoder - Use Cases for CIPs Using the AVR128DA48 Microcontroller with MCC Melody
- 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
- The PIC16F13276 Curiosity Nano Development board is used as a test platform:

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 project is a Core Independent Peripheral (CIP) implementation of a Manchester Decoder with configurable bitrate. The CPU usage is limited as the decoding process is mostly performed by the CLB. The input data is decoded by the circuit composed into the CLB peripheral mentioned above and presented to the user via the serial terminal. The CLB circuit is captured in the figure below:
The current implementation uses the transitions that occur in the middle of each Manchester-encoded data bit (clock edges) to decode the incoming data stream. For a correct decoder synchronization, the first edge after an idle period must be a clock edge. This is ensured by the usage of start and stop bytes in the data frames (software protocol).
The encoded data is received through a wire on the RA2 pin. The rising and falling edges of the incoming signal are detected using CLB edge detectors. A validation time slot is created using a D flip-flop with enable and reset. The detected edges, combined through an OR gate, reset the flip-flop, while the TMR0 overflow, configured to 3/4 of the Manchester bitrate, sets the same flip-flop. By using this validation mechanism, only the clock edges are validated and used to generate the decoded data stream (SDO_SPI signal on PPS_OUT1) through two AND gates, as shown below:
- A rising edge that appears after 3/4 of the bit period will set the output flip-flop (logic ‘
1’) - A falling edge that appears after 3/4 of the bit period will reset the output flip-flop (logic ‘
0’) - Any edge that appears before 3/4 of the bit period is ignored
The decoded data stream is sent via the SPI interface to an SPI Client, MSSP1 in this case. The SPI data is the output of the flip-flop, while the validation window is used as the SPI clock. The decoded data is further transmitted via the serial communication (UART).
Note: The TMR0 overflow is proportional with the Manchester-encoded bitrate. To change the bitrate, only the value of the MANCHESTER_BAUD_RATE macro must be updated, as for example:
#define MANCHESTER_BAUD_RATE 625000
The following peripheral and clock configurations are set up using the MPLAB® Code Configurator (MCC) Melody for the PIC16F13276:
-
Configuration Bits:
-
Clock Control:
-
Interrupt Manager:
-
MSSP1:
-
SPI:
-
CLB Synthesizer Library:
-
CLB:
-
CRC:
- Auto-configured by CLB
-
UART2:
-
EUSART2:
-
TMR0:
-
Pin Grid View and Pins:
In the demo, the Manchester-encoded messages received on the RA2 pin are decoded by the CLB logic and sent via the SPI interface (CLB is the SPI host). The CPU reads the data from its SPI client interface and forwards it to the serial terminal using the on-board UART interface.
If this example is used in combination with the PIC16F13276 Manchester Encoder, the PIC16F13276 CLB Manchester Encoder-Decoder\r\n message will appear periodically in the serial terminal. Note that both projects (Encoder and Decoder) must use the same bitrate, as for example:
#define MANCHESTER_BAUD_RATE 625000
In the demo, the Microchip! message was inserted by the user in the serial terminal. The signal received on the RD5 pin, the Manchester encoded signal, and the decoded message transmitted via the RD6 pin are visualized using a logic analyzer.
To use the embedded decoder from the Logic software, the following analyzers settings must be done:
This example demonstrates the capabilities of the CLB, a CIP, that can decode a Manchester-encoded message with minimal CPU intervention. The received data is then transmitted via EUSART and can be visualized in the serial terminal.
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.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 by clicking the SN.
Click Apply and then OK.
-
Program the project to the board.
Right click the project and click Make and Program Device.
