Using the CLC JK FlipFlop to Control an I/O Port


The Core Independent Peripherals within the latest PIC® 8-bit microcontrollers offer the opportunity to perform hardware functions without the Central Processing Unit (CPU) core running any code. This can be extremely useful for applications that need to perform applications without interruption from the main application. This simple example shows how to set up and use the Configurable Logic Cell (CLC) peripheral and its JK Flip Flop option to control an LED through an I/O port. The clock of the JK Flip Flop comes from the internal 31 kHz internal oscillator, routed through a timer. The image shows the block diagram of the project. As you can see, the control runs completely independently from the CPU.



Hardware Tools (Optional)

Tool About Purchase
Development Board

Software Tools

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment
MPLAB® Code Configurator
Dynamic Code Generation
C Compiler

Exercise Files

File Download
Windows Linux Mac OSX
Project and Source Files
Curiosity Board User Guide/Schematic

 Connection Diagram

The curiosity board has four LEDs prewired to the I/O pins shown below. This project controls the D7 LED.

Hardware Function Pin Setting
IO_LED_D4 RA5 (2) Output
IO_LED_D5 RA1 (18) Output
IO_LED_D6 RA2 (17) Output
IO_LED_D7 RC5 (5) Output



Create a Project

Create a new project and select the PIC16F1619 along with the Curiosity Board and MPLAB® XC8 compiler. If this is your first time creating a project, click on the link below to expand the directions.

If this is your first project please visit the "MPLAB Xpress: Create a Project" page.


Launch MCC

Open the MPLAB Code Configurator (MCC) under the Tools > Embedded menu of MPLAB X IDE.



System Setup

From Project Resources choose System Module to open the System Setup window within MCC.

  • In the clock settings, make sure you select INTOSC
  • Select the system clock FOSC.
  • Set Internal Clock to 4MHz_HF setting.
  • Check the PLL Enabled box.
  • The Curiosity Board uses a programmer/debugger on board (PKOB) and uses a Low Voltage Program method to program the MCU, therefore we must enable low voltage programming by checking the Low-voltage programming Enable box.


Timer 6 Setup

Add the TMR6 peripheral to the project from the Device Resources area of MCC by scrolling down to the Timer entry and expand the list by clicking on the arrow. Now double click-on the TMR6 entry to add it to the Project Resources list. Then click on the TMR6 to open the Timer 6 configuration setup screen.

  1. Check the Enable Timer box
  2. Select Clock Source LFINTOSC, Postscaler 1:1, and Prescaler 1:32
  3. Set Timer period value to "100.129 ms"
  4. Set External Reset Source to T6IN, and Control mode setting to Roll over pulse,
  5. Set Start/Reset Option to Software Control (this will inhibit hardware reset of timer).
  6. Leave the Enable Timer Interrupt box unchecked.


CLC Setup

Add the CLC1 peripheral to the project from the Device Resources area of MCC by scrolling down to the CLC entry and expand the list by clicking on the arrow. Now double click-on the CLC1 entry to add it to the Project Resources. Then click on the CLC1 to open the Timer 6 configuration setup screen.


Select the J-K Flip-Flop with R tab from the logic functions (mode) list. Let’s connect J and K to a high logic level. The logic diagram can be modified by just clicking on the connections.

  1. Set first input signal to T6_postscaled_out
  2. Connect the first input signal to Gate 1 by clicking on the input line to Gate 1.
  3. Set Gate 2 and Gate 4 outputs to Inverted by clicking on the output line.

With the inputs of Gate 2 and Gate 4 grounded and then the output inverted this sets the J and K inputs of the JK flip-flop to always high. The R pin is set low by Gate 3 setup which didn't require any changes from the default.


Connecting I/O pin

The output of the CLC JK Flip Flop needs to be connected to the LED. We can do that using the Peripheral Pin Select (PPS) feature built into the device. The MCC does the setup code for us so all that is needed is to change the blue lock for the PortC 5 pin to green in the CLC1OUT row of the Pin Manager Grid. This will connect the output of the Flip-FlopFlop to the I/O pin using the PPS.



Generate Driver Code

Click on the Generate button in the Project Resources of the MCC screen to have the MCC create the drivers and a base main.c file for the project.



The generated main.c file doesn't need any additional code because this application runs within the core independent peripheral hardware separate from the CPU.

    while (1)
        // Add your application code

 End of File


Build Project

Click on Build Project (hammer icon) to compile the code and you will see a "BUILD SUCCESSFUL" message in the output window of MPLAB X within several seconds of processing time.

BUILD SUCCESSFUL (total time: 8s)


Make sure your Curiosity Board is connected to the USB port. Then, click on Make and Program Device. This will build the project again and launch the programmer built into the Curiosity Board. In the Output window, you should see a series of messages and when successful it will end with a "Programming and Verify Successful" message.


Output Window:

Connecting to MPLAB Starter Kit on Board...

Currently loaded firmware on Starter Kit on Board
Firmware Suite Version.....01.41.07
Firmware type..............Enhanced Midrange

Target detected
Device ID Revision = 2004

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory

Programming/Verify complete

If it's the first time the programmer is connected to the board, the programming tool may need to download the proper operating firmware for the exact device. You may see a series of messages if this occurs. This should only happen once.

Downloading Firmware…
Downloading bootloader
Bootloader download complete
Programming download…
Downloading RS…
RS download complete
Programming download…
Downloading AP…
AP download complete
Programming download…
Firmware Suite Version…..01.34.11
Firmware type…………..Enhanced Midrange


The D7 LED will begin to blink on the Curiosity Board. This shows the CIP hardware is controlling the LED I/O pin even though there is no code in the while (1) loop of main.c.


The CIP peripherals, and especially the CLC, can be built to perform various logical functions and take the burden off the main processor. It's almost like having two microcontrollers in one package.


The CLC module has many different configuration options. These should be explored beyond this simple project. Fortunately, the steps shown here to set up the CLC are common with any other similar type of project you may develop with these very useful peripherals.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.