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 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.
Exercise 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 three of the four LEDs.
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 |
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.
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.
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.
- Check the Enable Timer box
- Select Clock Source LFINTOSC, Postscaler 1:1, and Prescaler 1:32
- Set Timer period value to "100.129 ms"
- Set External Reset Source to T6IN, and Control mode setting to Roll over pulse,
- Set Start/Reset Option to Software Control (this will inhibit hardware reset of timer).
- 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.
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.
- Set first input signal to T6_postscaled_out
- Connect the first input signal to Gate 1 by clicking on the input line to Gate 1.
- 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.
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.
{
// 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.
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.
Output Window:
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...
Programming/Verify complete
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 the while (1) loop of main.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.