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

 Objective

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 theConfigurable 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 atimer. The image shows the block diagram of the project. As you can see, the control runs completely independently from the CPU.

figure1jk.png

 Materials

Hardware Tools (Optional)

Tool About Purchase
Curiosity-50px.png
Curiosity
Development Board

Software Tools

Tool AboutInstallers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
swtool-28px.png
MPLAB® Code Configurator
Dynamic Code Generation
MPLAB® XC8
C Compiler

Exercise Files

FileDownload
Installation
Instructions
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 FunctionPinSetting
IO_LED_D4RA5 (2)Output
IO_LED_D5RA1 (18)Output
IO_LED_D6RA2 (17)Output
IO_LED_D7RC5 (5)Output
figure1.png

 Procedure

1

Create a Project

Create a new project and select thePIC16F1619 along with theCuriosity Board andMPLAB® 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.

2

Launch MCC

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

mcclaunch.png

3

System Setup

FromProject Resources chooseSystem Module to open theSystem Setup window within MCC.

  • In the clock settings, make sure you selectINTOSC
  • Select the system clockFOSC.
  • Set Internal Clock to4MHz_HF setting.
  • Check thePLL 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 theLow-voltage programming Enable box.
figure1cip.png

4

Timer 6 Setup

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

figure2jk.png
  1. Check theEnable Timer box
  2. SelectClock Source LFINTOSC,Postscaler 1:1, andPrescaler 1:32
  3. Set Timer period value to "100.129 ms"
  4. Set External Reset Source toT6IN, and Control mode setting toRoll over pulse,
  5. Set Start/Reset Option toSoftware Control (this will inhibit hardware reset of timer).
  6. Leave theEnable 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 theCLC entry and expand the list by clicking on the arrow. Now double click-on theCLC1 entry to add it to theProject Resources. Then click on theCLC1 to open the Timer 6 configuration setup screen.

figure4jk.png

Select theJ-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 toT6_postscaled_out
  2. Connect the first input signal toGate 1 by clicking on the input line toGate 1.
  3. SetGate 2 andGate 4 outputs toInverted by clicking on theoutput 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.

figure4ajk.png

Connecting I/O pin

The output of the CLC JK Flip Flop needs to be connected to the LED. We can do that using thePeripheral 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.

figure4bjk.png

6

Generate Driver Code

Click on theGenerate button in theProject Resources of the MCC screen to have the MCC create the drivers and a basemain.c file for the project.

7

main.c

The generatedmain.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 onBuild 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.png
BUILD SUCCESSFUL (total time: 8s)

9

Make sure your Curiosity Board is connected to the USB port. Then, click onMake 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.

Main_Program_Target_Project.png

Output Window:

Connecting to MPLAB Starter Kit on Board...Currently loaded firmware on Starter Kit on BoardFirmware Suite Version.....01.41.07Firmware type..............Enhanced MidrangeTarget detectedDevice ID Revision = 2004The following memory area(s) will be programmed:program memory: start address = 0x0, end address = 0x7ffconfiguration memoryProgramming...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

 Results

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 thewhile (1) loop ofmain.c.

 Analysis

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.

 Conclusions

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.