Zero-Software Ultrasonic Distance Sensing — Use Case for CLB Using the PIC16F13145 Microcontroller With MCC Melody
This repository creates a rangefinder using the HC-SR04+ ultrasonic sensor and displays the results on the eight LEDs bar-graph featured on the Curiosity Nano Explorer board.
The Configurable Logic Block (CLB) peripheral is a collection of logic elements that can be configured to implement discrete logic function(s). 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 HC-SR04+ is a standard ultrasonic sensor that can be used for object detection and distance sensing. When the transmitter sends a signal, the distance is calculated based on the time it took the signal to get back to the receiver. Unlike the older version, the HC-SR04+ is compatible with both 5V and 3.3V devices.
In this example, the CLB interprets the HC-SR04+ sensor data and displays it on eight LEDs. This removes the need for the user to write any executable code.
More details and code examples on the PIC16F13145 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
Important: The current version features an update to the CLB peripheral, which now includes the CLB Synthesizer Library. For migration details and required changes, refer to the Troubleshooting MCC Melody Configurable Logic Block (CLB) Projects Configured With CLB v1.x.x migration guide documentation.
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.
The HC-SR04+ sensor works by sending a series of pulses after the trigger pin has been toggled. These pulses travel until they reach an object, then they are reflected back to the source. If the object is too far away, the sensor will not receive the pulse properly. The distance traveled is based on the length of time the Echo pin is kept in a High state. By measuring the pulse width of the Echo pin, the CLB can determine the distance to the object.
To measure the pulse width, the Timer0 (TMR0) overflow and the internal CLB counter determine which bin the distance falls into.
In the PIC16F13145 family of microcontrollers (MCUs), the CLB consists of a series of 32 Look-up Tables (LUTs), similiar to an FPGA. To configure the logic, use the CLB Synthesizer tool inside the MPLAB Code Configurator (MCC).
This project provides two configurations of the CLB: single or all LEDs drive based on the detected distance of the object. To program the Curiosity Nano board with one design or the other, use the Load Design option from the CLB Synthesizer Library and choose between the ultrasonic_led_select.clb or ultrasonic_led_all_counts.clb files. Alternatively, the designs can be drawn from scratch following the images and instructions below.
-
Single LED drive configuration:
-
All LEDs drive configuration:
- Add the following submodule named
led_all_countswithin aNew Verilogby clicking the Documents menu button in the CLB Synthesizer Library user interface.module led_all_counts(in1, in2, in3, in4, in5, in6, in7, in8, led10cm, led20cm, led30cm, led40cm, led50cm, led60cm, led70cm, led80cm); input in1, in2, in3, in4, in5, in6, in7, in8; output led10cm, led20cm, led30cm, led40cm, led50cm, led60cm, led70cm, led80cm; assign led10cm = in1 | in2 | in3 | in4 | in5 | in6 | in7 | in8; assign led20cm = in2 | in3 | in4 | in5 | in6 | in7 | in8; assign led30cm = in3 | in4 | in5 | in6 | in7 | in8; assign led40cm = in4 | in5 | in6 | in7 | in8; assign led50cm = in5 | in6 | in7 | in8; assign led60cm = in6 | in7 | in8; assign led70cm = in7 | in8; assign led80cm = in8; endmodule
- In the
mainmodule, add the previously created submodule and adevice_counterfrom the Modules section. The final design needs to look as below:
- Add the following submodule named
Note: Any changes in the CLB Synthesizer Library user interface will have to be synthesized first before generating the files needed for programming.
The following peripheral and clock configurations are set up using the MPLAB® Code Configurator (MCC) Melody for the PIC16F13145:
-
Configuration Bits:
-
Clock Control:
-
Interrupt Manager:
-
TMR2:
-
PWM1:
-
TMR0:
-
CLB Synthesizer Library:
-
CLB1:
-
CRC:
- Auto-Configured by CLB
-
Pin Grid View
This demonstration shows the PIC16F13145 with a CLB configuration using Verilog and the internal counter to keep every LED below the measured value HIGH while running.
This demonstration shows the PIC16F13145 with a CLB configuration using logic gates and the internal counter to keep only the measured LED HIGH while running.
The CLB significantly reduces the software overhead associated with implementing this example. Usually, it is up to the software to handle a timer, counter, as well as the inputs and outputs. These tasks involve managing the pin placement (in some cases making sure there is an open row), checking if the counter and/or timer is reloaded, or bit-masking. With the CLB, all functions associated with this example are handled by the hardware, which leaves the CPU free to do other tasks without interruption.
The examples featured above showcase that the CLB is a highly configurable and open-ended tool.
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 on the SN.
Click Apply and then OK.
-
Program the project to the board.
Right click the project and click Make and Program Device.



















