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

Last modified by Microchip on 2024/06/24 06:29


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.

CLC Peripheral Structure

Exercise Files

Project File

Curiosity Board User Guide & Schematic



Connection Diagram

The Curiosity board has four LEDs prewired to the I/O pins shown below. This project controls three of the four LEDs.

Hardware FunctionPinSetting
IO_LED_D4RA5 (2)Output
IO_LED_D5RA1 (18)Output
IO_LED_D6RA2 (17)Output
IO_LED_D7RC5 (5)Output

Curiosity LEDs

1. Create a Project

Create a new project and select the PIC16F1619 along with the Curiosity Board and MPLAB XC8 compiler. ​

2. Launch MCC

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

MCC Launch

3. 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 the 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.

System Module

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

Timer 6 Setup

  1. Check the Enable Timer box
  2. Select Clock Source LFINTOSCPostscaler 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.
5. 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.

CLC Setup

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.

CLC Gate Settings

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.

I/O PIn Selection

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

7. main.c

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

Main build project icon

BUILD SUCCESSFUL (total time: 8s)
9. Program Device

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.

Make and Program Device icon

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.