Bi-Phase Encoder With Configurable Bitrate Based on CLB Using the PIC16F13276 Microcontroller With MCC Melody
The repository contains the Bi-Phase Encoder, an MPLAB® X project which uses Core Independent Peripherals (CIPs) by following the interaction between the Custom Logic Block (CLB), Serial Peripheral Interface (SPI) and Universal Asynchronous Receiver-Transmitter (UART) peripherals.
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.
The Bi-Phase Mark Code (BMC) combines both data and clock in a single signal. One clock cycle is a BMC bit period. A transition always occurs at the beginning of each bit period. A logic ‘1’ is represented by a transition (rising or falling edge) in the middle of the bit period and a logic ‘0’ is represented by no transition in the middle of the period. A BMC encoder accepts a data signal and a clock signal as inputs and produces a single BMC-encoded output. A BMC decoder accepts a BMC-encoded signal as input and produces two outputs: data and clock. BMC is used in USB 3.1 Power Delivery Specification CC signaling, AES3 digital audio or S/PDIF audio standards; an example of this type can be seen in the figure below.
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
- Bi-Phase Decoder with Configurable Bitrate Based on CLB Using the PIC16F13276 Microcontroller with MCC Melody
- Bi-Phase 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 an implementation of a Bi-Phase encoder using CIPs by following the interaction between CLB, SPI and UART peripherals. The raw data is received via serial communication, encoded using the circuit composed in the CLB peripheral mentioned above and sent through a single data wire. The CLB circuit is captured in the figure below:
The raw data is received via serial communication by the UART peripheral. This data is then stored in a buffer and used as input for the SPI peripheral which generates a Non-Return-to-Zero (NRZ) data signal and a clock signal. Those signals are selected as inputs for the circuit composed inside the CLB peripheral which outputs the Bi-Phase encoded signal. A First-In-First-Out (FIFO) buffer mechanism is used from the generated libraries for the UART peripheral to facilitate the data storage and minimize data loss.
After initializing all peripherals, a Start byte is sent for calibration purposes. The data received from the PC terminal is received by the UART peripheral and stored into the internal eusart1RxBuffer via a Receive interrupt. In the main function, the data from the receive buffer is read byte by byte until the end and follows the flowchart below. A Start byte and a Stop byte describe every message stored in the internal buffer. A GAP_DURATION delay is needed between the Start byte and the first read byte from the PC terminal to avoid the scenario when the FIFO buffer is empty.
In this example, the SPI peripheral can be initialized in multiple configurations, so the frequency of the encoded message can be chosen between different predefined values: 31.25 kHz, 62.5 kHz, 125 kHz, 250 kHz or 500 kHz.
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:
-
CLB1:
-
CRC:
- Auto-configured by CLB
-
UART2:
-
EUSART2:
-
Pin Grid View and Pins:
If the UART does not receive any character for 0.5s, the PIC16F13276 CLB Bi-Phase Encoder Example\r\n message is transmitted via the RD2 pin (BMC out). The output of the encoder can be visualized using a logic analyzer.
In the demo, the Microchip! message was inserted by the user in the terminal. The RD2 pin - BMC out (the output pin for the Bi-Phase encoded signal) is visualized using a logic analyzer.
To use the embedded decoder from the Logic software, the next analyzers settings must be set:

The output pin of the encoder platform (positioned on the left side), BMC out (the output pin for the Bi-Phase encoded signal), is connected to the input pin of the decoder board (positioned on the right side) and visualized using the MPLAB Data Visualizer plug-in.
This example demonstrates the capabilities of the CLB, a CIP that can encode a message from the SPI and UART modules.
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.














