CLC Controlled Train Crossing Display

 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 severalConfigurable Logic Cells (CLC) to produce a train crossing light. Two of theCuriosity boards LEDs (D4 and D7) are used to produce a flashing back and forth light display. The LEDs are controlled by a pair of CLC modules and atimer peripheral. To add function, the momentary switch S1 toggles between the train crossing flashing and a single LED light in the center (D6 LED), lit through a separate set of CLC modules and timer used to control the switch debounce. Themain.c file contains no special application code, just a continuouswhile(1) loop.

figure1train.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 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
figure1.png

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.

figure2train.jpg

 Procedure

1

Create a Project

Create a new project and select the PIC16F1619 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.

 Step-by-Step

1

Launch the new project wizard

Launch theNew Project Wizard using one of the following methods:

  • From the Toolbar: Click on theNew Project iconMain_New_Project.png, or
  • From the Menu: SelectFile ► New Project, or
  • From the Keyboard: Ctrl + Shift + N


The project wizard will walk you through the process and prompt you for all the required information. Your selections may be changed after the project has been created bymodifying the project's properties.

2

Choose Project

a

ChooseMicrochip Embedded from theCategories column (center).

b

ChooseStandalone Project from theProjects column (right).

c

Click theNext > button.

1a-ChooseProject.png
Click image to enlarge.

3

Select Device

a

Choose the device family from theFamily drop-down box.

If you have used MPLAB X IDE previously, choosingRecently Used from theFamily drop-down box will filter theDevice list to the 3 devices you have used most recently.

b

Choose your device's part number from theDevice drop down box. You may also type in the part number directly, and the list will adjust to the characters you enter.

2-SelectDevice.png
Click image to enlarge.

4

Select Header

The wizard will automatically skip this step if a header is not available for your selected device.

If you are using a debug header with your debugger, select it from theSupported Debug Header drop-down box. They are listed by the header's part number.
If you are not using a debug header (i.e. the debugger is directly connected to the target device), selectNone.

3-SelectHeader.png
Click image to enlarge.

5

Select Tool

If you are using a hardware debugger / programmer, you should see a serial number under its heading if it is connected to your system. Click on the serial number to associate that specific tool with your project. It is important to select the serial number itself because MPLAB X IDE permits multiple projects to be open simultaneously, and each one may use a different debug tool. For example, if you have three ICD3's connected to your PC, you could have three projects open and each one assigned to a different ICD3. This makes it possible to simultaneously debug multiple applications in the same instance of MPLAB X IDE.

For software based tools, such as the simulator, you only need to click on the tool's name to select it.

Hover your mouse pointer over the colored dots to see their meaning for a specific tool. Red = No Support, Yellow = Beta Support, Green = Production Tested.

4-SelectTool.png
Click image to enlarge.

6

Select Plug-in Board

The wizard will automatically skip this step if a plug-in board is not available for your selected device.

If you are using a plug-in board to enhance the features of your debugger (usually the Real ICE), select it from theSelected Plugin Board drop-down box. They are listed by function.
If you are not using a plug-in board (i.e. standard debugger configuration), selectNone.

5-SelectPlugin.png
Click image to enlarge.

7

Select Compiler

The IDE will display all compilers available on your system that support your selected device. Select the compiler (including version number) you wish to use for building the project.

6-SelectCompiler.png
Click image to enlarge.

8

Select Project Name and Folder

a

Choose aProject Name. This will be your project's name as well as the name of the project directory (with a ".X" appended) that the IDE will create.

b

Choose aProject Location. This is where the IDE will create the project directory.

If a directory with the specified name already exists and hasno MPLAB X IDE project files in it, the IDE will use the existing directory for the new project. If the directory does have MPLAB X IDE files in it from another project, the wizard will give an error that must be fixed by choosing a different project name or location. If the directory does not exist, the IDE will create it and use it for the new project.

7-SelectProjectName.png
Click image to enlarge.

2

Launch MCC

Open the MPLAB Code Configurator under theTools -> Embedded menu of MPLAB X IDE.

mcclaunch.png

3

System Setup

From Project resources chooseSystem Module to open the System Setup window within MCC.

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

4

Timer 2 Setup

Add theTMR2 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.

figure4train.png
  1. Check theEnable Timer box
  2. SelectClock Source LFINTOSC,
  3. SelectPostscaler 1:10
  4. SelectPrescaler 1:32
  5. SetTimer period value to 1 second
  6. SetExternal Reset Source toCLC4OUT
  7. SetControl mode setting toRoll over pulse
  8. SetStart/Reset Option toResets at TMR2_ers = 1
  9. Leave theEnable Timer Interrupt box unchecked.

5

Timer 0 Setup

Add theTMR0 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.

  1. Check theEnable Prescaler box and set to1:256
  2. SelectClock Source:FOSC/4
  3. SetTimer Period to:32.768 ms
figure5train.png

6

CLC1-CLC4 Setup

Add theCLC1 - 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.

  1. Change the first input tot2_postscaled_out
  2. Change second input toLC4_out
  3. Connect the upper input toOR gate 1
  4. Connect the second input toOR gate 3
  5. Invert the output forOR gate 2
  6. Invert the output forOR gate 4
  7. Change the Mode toJK Flip Flop with R
figure6train.png

CLC2

Make the changes shown to the CLC2 block diagram.

  1. Change the first input toLC1_out
  2. Change second input toLC4_out
  3. Connect the upper input toOR gate 1
  4. Connect the second input toOR gate 3
  5. Invert the output forOR gate 1
  6. Invert the output forOR gate 2
  7. Invert the output forOR gate 3
  8. Invert the output forOR gate 4
  9. Change the Mode to4-input AND
figure7train.png

CLC3

Make the changes shown to the CLC3 block diagram.

  1. Change the first input toT0_overflow
  2. Connect the upper input toOR gate 1
  3. Invert the output forOR gate 2
  4. Invert the output forOR gate 4
  5. Change the Mode to1-input D flip flop with S and R
figure8train.png

CLC4

Make the changes shown to the CLC4 block diagram.

  1. Change the first input toL3_out
  2. Connect the upper input toOR gate 1
  3. Connect the secondInverted input toOR gate 3
  4. Change the Mode toJK Flip Flop with R
figure9train.png

7

Pin Manager Setup

  1. Click on RA5 lock in the CLC1OUT row to turn the lock green
  2. Click on RC5 lock in the CLC2OUT row to turn the lock green
  3. Click on RA2 lock in the CLC4OUT row to turn the lock green
figure10train.png

Pin Module Setup

Check theRA2 andRA5Output Box and uncheck theWPU andAnalog Boxes

figure11train.png

Uncheck theRC4Output Box,WPU andAnalog Boxes
Check theRA5Output Box and uncheck theWPU andAnalog Boxes

figure12train.png

8

Generate Driver Code

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

9

main.c

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

    while (1)    {        // Add your application code    }/** End of File*/

10

Build Project

Click on the Build Project Icon (the Hammer) to compile the code and you will see aBUILD SUCCESSFUL message in the output window of MPLAB X within several seconds of processing time.

Main_Build_Project.png
BUILD SUCCESSFUL (total time: 7s)

11

Make sure your Curiosity Board is connected to the USB port. Then click on theMake and Program Device 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 aProgramming 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

TheD4 andD7 LEDs will begin flashing back and forth like a train crossing. Press theS1 switch and that will stop and theD6 LED will light and stay lit until theS1 switch is pressed again. When its pressed the second time theD6 LED will go out and theD4 andD7 will begin flashing back and forth again.

figure8adc.jpg

 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 theGenerate 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 tomain.c) but it won't affect the operation of the train crossing as that will be running independently.