CLC Controlled Train Crossing Display

Last modified by Microchip on 2023/11/14 18:34

Objective

The Core Independent Peripherals within the latest PIC® 8-bit microcontrollers offer the opportunity to perform hardware functions without the 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 several Configurable Logic Cell (CLC) to produce a train crossing light. Two LEDs are controlled by a pair of CLC modules and a timer peripheral. To add function, the momentary switch S1 toggles between the train crossing flashing and a single LED light in the center (D6 LED), light through a separate set of CLC modules, and a timer used to control the switch debounce. The main.c file contains no special application code, just a continuous while(1) loop.

Train Crossing Schematic

Reference Materials

Project Files

Curiosity Development Board User’s Guide

MPLAB® X IDE

Using MPLAB® XC8 Compiler

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

The Curiosity board also has a momentary switch labeled S1 and is connected to the RC4 pin of the PIC16F1619 microcontroller. The switch will be used to toggle the flashing on and off, similar to the way that an approaching train triggers a real train crossing signal.

Curiosity Board Switch

Create a Project

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

Launch MCC

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

MCC Launch

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 the Internal Clock field to the 4MHz_HF setting.
  • 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

Timer 2 Setup

Add the TMR2 peripheral to the project from the Device Resources area of MCC. To do that, scroll down to the Timer entry and expand the list by clicking on the arrow. Now double click-on the TMR2 entry to add it to the Project Resources list. Then click on the TMR2 to open the configuration setup screen.

Timer 2 Setup

Check the Enable Timer box
Select Clock Source LFINTOSC.
Select Postscaler 1:10.
Select Prescaler 1:32.
Set Timer period value to 1 second.
Set External Reset Source to CLC4OUT.
Set Control mode setting to Roll over pulse.
Set Start/Reset Option to Resets at TMR2_ers = 1.
Leave the Enable Timer Interrupt box unchecked.

Timer 0 Setup

Add the TMR0 peripheral to the project from the Device Resources area of MCC. To do that, scroll down to the Timer entry and expand the list by clicking on the arrow. Now double click-on the TMR0 entry to add it to the Project Resources list. Then click on the TMR0 to open the configuration setup screen.

Check the Enable Prescaler box and set to 1:256.
Select Clock SourceFOSC/4.

Set Timer Period to: 32.768 ms.

Timer 0 Setup

CLC1-CLC4 Setup

Add the CLC1 - CLC4 peripherals to the project from the Device Resources area of MCC. To do that, scroll down to the CLC entry and expand the list by clicking on the arrow. Now double click-on the CLC1 through CLC4 one at a time to add them to the Project Resources list. Then click on the CLC to open the configuration setup screen.

CLC1

Make the changes shown to the CLC1 block diagram.

Change the first input to t2_postscaled_out.
Change second input to LC4_out.
Connect the upper input to OR gate 1.
Connect the second input to OR gate 3.
Invert the output for OR gate 2.
Invert the output for OR gate 4.

Change the Mode to JK Flip Flop with R.

clc1 setup

CLC2

Make the changes shown to the CLC2 block diagram.

Change the first input to LC1_out.
Change second input to LC4_out.
Connect the upper input to OR gate 1.
Connect the second input to OR gate 3.
Invert the output for OR gate 1.
Invert the output for OR gate 2.
Invert the output for OR gate 3.
Invert the output for OR gate 4.

Change the Mode to 4-input AND.

clc2 setup

CLC3

Make the changes shown to the CLC3 block diagram.

Change the first input to T0_overflow.
Connect the upper input to OR gate 1.
Invert the output for OR gate 2.
Invert the output for OR gate 4.

Change the Mode to 1-input D flip flop with S and R.

clc3 setup

CLC4

Make the changes shown to the CLC4 block diagram.

Change the first input to L3_out.
Connect the upper input to OR gate 1.
Connect the second Inverted input to OR gate 3.

Change the Mode to JK Flip Flop with R.

clc4 setup

Pin Manager Setup

Click on RA5 lock in the CLC1OUT row to turn the lock green.
Click on RC5 lock in the CLC2OUT row to turn the lock green.

Click on RA2 lock in the CLC4OUT row to turn the lock green.

I/O Setup

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.

main.c

Since the project runs completely independent of the core, the generated main.c doesn't need any modification. The default while(1) loop will be the only code running.

    while (1)
    {
        // Add your application code
    }

/**
 End of File
*/

Build Project

Click on Build Project build project button(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)

Program Device

Make sure your Curiosity Board is connected to the USB port. Then, click on Make and Program Device program target project icon. 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...
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 D4 and D7 LEDs will begin flashing back and forth like a train crossing. Press the S1 switch and that will stop and the D6 LED will light and stay lit until the S1 switch is pressed again. When it's pressed the second time the D6 LED will go out and the D4 and D7 will begin flashing back and forth again.

Curiosity Board

Conclusions

Setting up several CLCs along with the timers can take a little bit of time but the MCC makes generating the proper register setup for these Core Independent Peripherals as easy as clicking on the Generate button. Once the drivers are generated, programming the microcontroller is the only step left to have a train crossing application running on the Curiosity board. Additional code could be added later (typically to main.c) but it won't affect the operation of the train crossing as that will be running independently.