Microchip | The Embedded Control Solutions Company Register Login
Browse All
Microchip Technology
 369 Code Snippets

LED Blink Using CLC using PIC16F18345

Microchip Technology

Check out this project's MPLAB Xpress Webpage to view the complete code example along with a detailed step by step write-up and description, or visit the PIC16F18345 Device Product Page for more information.

About this Code Example

Project Overview

The Configurable Logic Cell (CLC) is a hardware peripheral inside many of the latest PIC® Microcontrollers. Using the CLC is quite easy as there is a graphical user interface built into the MPLAB® Code Configurator (MCC) that will generate code required to setup the CLC. This project takes you step by step through setting up the CLC and Timer0 to toggle all four LEDs every 500 ms on the Xpress Evaluation Board featuring PIC16F18345 microcontroller.



To follow along with these steps, MPLAB® Xpress should be open and the user should be logged in so that the MPLAB Code Configurator plug-in can be used. The setup is described in the Hello World tutorial in the PIC16F18345 microchip series of examples which can be found Here You should see a screen similar to the one below to move on to step 1:

Step 1

The MPLAB Xpress Evaluation Board should be connected to an available USB port on the host computer through a USB cable to the micro B connector on the board. Drivers should install successfully the first time the board is connected and may take a minute or so. The connection is shown in the picture below. No other components are required.

Step 2

Create a new "Stand-Alone" project in MPLAB® Xpress for a PIC16F18345 using the MPLAB Xpress Development Board.

Step 3

Open the MPLAB® Code Configurator using the blue shield link on the toolbar. 

A file will automatically download in your browser similar to the picture shown. Click on that file to open it. This is a java application that will launch the MCC.

The process can take several seconds depending on your internet connection speed. Several windows may appear asking if you want to run the program. When the process is complete you will see a new screen appear, separate from the MPLAB Xpress IDE, that is the MCC control screen. In this screen you can select peripherals for your project, setup the oscillator system and other configuration settings, and input and output selections for your device. When all this is completed you can generate project code including a main.c file. All these generated files will be included in your MPLAB Xpress project.

There are three default Project Resources in MCC associated with any MPLAB Xpress project: System Module, Pin Module and Interrupt Module (as shown in the figure below). For this project we will leave the settings of these resources in their default state.

Step 4

Because the new Xpress Evaluation board uses the on-board LED for a different purpose, we will attach an LED to the board via two jumper wires and a breadboard, through a 1K Ohm resistor. Connect a wire between the RA0 pin on the evaluation board and the resistor. Then, connect the positive (longer) leg of the LED to the resistor. Next, connect a wire between the negative (shorter) leg of the LED and the GND pin on the evaluation board. Since only push in breadboard connectors are being used, make sure all the connections are established properly. You can see this circuit diagram below:

Step 5

The Timer 0 (TMR0) peripheral needs to be setup. From the Device Resources panel double click on TMR0 module under Timer to add it to the project.

Once TMR0 is added to the project and selected, the easy setup interface appears. Change the Clock Prescaler to 1:512Clock Source to FOSC/4 and Timer Period to 500 ms as highlighted in the following picture. All other options remain as default.

Step 6

Using a similar procedure to that used in task 5, CLC1 module is added to the project resource. CLC1 has different modes of operation and we are using the JK flip flop mode to toggle the output. According to the properties of the JK flip flop, if both J and K inputs are high, the output toggles on every rising edge of the clock signal. Select T0_overflow from the drop down menu as clock input and connect the wire to the first NOR gate by simply clicking on it. To set J and K input high, invert the second and fourth NOR gate's output by simply clicking the output side. Changes are highlighted in the image below.

Step 7

Now we must connect the otuput of our CLC to the port connected to the LED. Since we wired this to RA0, we need to "lock" the box that connects Port A0 to CLC1OUT in the pin manager:

Step 8

Click the Generate button in MCC to create the appropriate header and source files for this configuration. Upon successful code generation the “Generation Completed” notification should appear. Select “OK” to close the window.

Step 9

Compile and download the project .hex file by clicking on the Make and Program Device button at the top of the MPLAB Xpress IDE.

Step 10

Program the MPLAB Xpress Evaluation Board by dragging the project .hex file from the downloads section of the browser and dropping the file onto to the Xpress drive.

The Programmer LED on the Xpress board should quickly flash from green to red and then back to green indicating that the .hex file was successfully programmed to the PIC16F18345.


If the procedure was followed successfully, the LED connected to RA0 should begin to blink at the rate of 500 ms.


Release History

  • Dec 4, 2018

    Version: 1.0.0

    Initial Release

Download Contact the Developer

*Required Fields

  • Current Version:1.0.0
  • Created:Dec 4, 2018
  • Updated:Dec 4, 2018
  • Downloads:2236
  • C Compiler:XC8 (v1.41)
  • Development Tools:Xpress Board
  • Supported Devices:PIC16F18345

Corporate News


Stay Connected

Stay up to date with the latest news from Microchip Technology.