Skip to content

This code example demonstrates how to get results from the HC-SR04 with zero software written by the user. This is done using the pic16f13145, with the CLB and MCC.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/pic16f13145-cnano-ultrasonic-distance-sensor-mplab-mcc

Repository files navigation

Microchip Technologies Inc.

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.

Related Documentation

More details and code examples on the PIC16F13145 can be found at the following links:

Software Used

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.

Hardware Used

Operation

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.

Concept

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.

  1. Single LED drive configuration:

    • Add the following submodule named led_select within a New Schematic by clicking the Documents menu button in the CLB Synthesizer Library user interface.



    • In the main module, add the previously created submodule and a device_counter from the Modules section. The final design needs to look as below:



  2. All LEDs drive configuration:

    • Add the following submodule named led_all_counts within a New Verilog by 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 main module, add the previously created submodule and a device_counter from the Modules section. The final design needs to look as below:



Note: Any changes in the CLB Synthesizer Library user interface will have to be synthesized first before generating the files needed for programming.

Setup

The following peripheral and clock configurations are set up using the MPLAB® Code Configurator (MCC) Melody for the PIC16F13145:

  • Configuration Bits:

    • External Oscillator mode selection bits: Oscillator not enabled
    • Power-up default value for COSC bits: HFINTOSC (1 MHz)
    • Brown-out reset enable bits: Brown-out reset disabled
    • WDT operating mode: WDT Disabled, SEN is ignored
  • Clock Control:

    • Clock Source: LFINTOSC
  • Interrupt Manager:

    • Enable Single ISR Execution per entry: Enabled
  • TMR2:

    • Clock Source: FOSC/4
    • Clock Synchronization: Enabled
    • Prescalar: 1:128
    • Postscalar: 1:16
    • Time Period (s): 0.9
  • PWM1:

    • Timer Dependency Selector: Timer2
    • Duty Cycle (%): 10
  • TMR0:

    • Timer Mode: 8-bit
    • Clock Prescaler: 1:128
    • Clock Source: HFINTOSC
    • Requested Period (s): 0.000584
  • CLB Synthesizer Library:

    • Clock Divider: Divide clock source by 1
    • Clock Selection: TMR0_Overflow
  • CLB1:

    • Enable CLB: Enabled
  • CRC:

    • Auto-Configured by CLB
  • Pin Grid View

    • CLBPPSOUT0: RA5 (Closest Indicating LED)
    • CLBPPSOUT1: RA4
    • CLBPPSOUT2: RC3
    • CLBPPSOUT3: RC2
    • CLBPPSOUT4: RC1
    • CLBPPSOUT5: RC0
    • CLBPPSOUT6: RA1
    • CLBPPSOUT7: RA2 (Furthest Indicating LED)
    • CLBIN1PPS: RC4 (Echo Pin for HC-SR04+)
    • PWM1OUT: RC5 (Trig Pin for HC-SR04+)

Demo

All LEDs Drive



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.

Single LED Drive


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.

Summary

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.

How to Program the Curiosity Nano Board

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.

  1. Connect the board to the PC.

  2. Open the Example_Project.X project in MPLAB X IDE.

  3. Set the Example_Project.X project as main project.
    Right click the project in the Projects tab and click Set as Main Project.

  4. Clean and build the Example_Project.X project.
    Right click the Example_Project.X project and select Clean and Build.

  5. 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.

  6. Program the project to the board.
    Right click the project and click Make and Program Device.



Menu

About

This code example demonstrates how to get results from the HC-SR04 with zero software written by the user. This is done using the pic16f13145, with the CLB and MCC.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •