Change the world

The following section will provide a basic tutorial for using the Microchip C18 compiler with MPLAB.  The tutorial is based on the PIC18F4620 microcontroller, but will work for any other PIC18F device.

 

Creating a New Project

Before you can start writing code for the PIC Microcontroller, you have to create a project folder manually (e.g. c:\Tutorial).  Once you have created folder, follow these steps:

  1. Start MPLAB (v8.83)
  2. Select Project Project Wizard
  3. Click Next
  4. Select PIC18F4620 and click Next
  5. Select Microchip C18 Toolsuite under the Active Toolsuite dropdown box
  6. Select MPLAB C18 C Compiler (mcc18.exe) under the Toolsuite Contents dropdown box and click Next
  7. Click Browse under the Create New Project File
  8. Select the folder where you want to create the project (e.g. c:\Tutorial)
  9. Type the name of the project and try to keep the name of the project the same as the folder name to prevent confusion (e.g.
    Tutorial in this case).  The project file will now have a .mcp extension.
  10. Click Save.
  11. Click Next
  12. Click Next
  13. Click Finish

A project view of your project should appear in the top-left corner (Source Files, Header Files, etc.).  Now you are ready to create a new source file

  1. Click File → New
  2. Click File → Save As
  3. Select your project folder (e.g. c:\Tutorial) (make sure you change to the correct folder) and type the name of your source file PLUS the .c extension (e.g. flashleds.c).  Do not forget to add the .c after the filename, otherwise it will simply been see as a text file and will not compile.  The name of the source file does not have to be the same as the project.
  4. Click Save

The new source file is now created and needs to be added to the project

  1. Right-click on Source Files and select Add Files
  2. Select the source file you just created (flashleds.c) and click Open

The programming device must now be selected to program the source file onto the PIC

  1. Select Programmer ® Select Programmer ® MPLAB ICD2

An output view window should open now which gives feedback about the compiling and programming process.

At this stage all the windows can be rearranged to fit your needs. T he next time you open the project, the windows will stay in these positions.

 

Writing C Code, Compiling and Programming

In the flashleds.c window, type the following program to make 8 LEDs connected to PORTD flash at a rate of 250ms:

 

#include <p18f4620.h>
#include <delays.h>

void main(void)
{
    TRISD = 0x00;                   
    while(1)                       
    {
        PORTD = 0xFF;
        Delay10KTCYx(250);
        PORTD = 0x00;       
        Delay10KTCYx(250);
    }
}

 

To compile the source file, select Project Make (or just press F10).  The output window should now indicate if it compiled successful (“BUILD SUCCEEDED”) or not (“BUILD FAILED”).  If there are any errors, fix them first before trying to program the PIC Microcontroller.

Before we can use the PIC18F4620 for our project, we need to indicate what the configuration bits for the device should be (this only needs to be done once). 

  1. Select Configure → Configuration Bits.
  2. If the Configuration Bits set in code option is ticked, un-tick it.
  3. You should not be able to select the configuration for each of the configuration bits by clicking in the Settings field.  Select the following configurations:
    OSC      -       HS Oscillator, PLL enabled
    PWRT    -       PWRT enabled
    WDT      -       WDT disabled
     
  4. Close the Configuration Bits window
  5. Re-compile the code again (F10).

The above method could be simplified by making use of a user defined Config.h library file (in the Example programs – Libraries section) which must be copied into the project folder.  Change the source code to:


#include <p18f4620.h>
#include <delays.h>
#include "Config.h"             // *** ADD THIS LINE ***

void main(void)
{
    TRISD = 0x00;                   
    while(1)                       
    {
        PORTD = 0xFF;
        Delay10KTCYx(250);
        PORTD = 0x00;       
        Delay10KTCYx(250);
    }
}

 

and then tick the Configuration Bits set in code option in the Configuration Bits window.  Now all the configuration bits will be set to the settings as described in the Config.h file.

Once the code is compiled and the configuration bits are set, the source code can be programmed onto the PIC Microcontroller.

  1. Select Programmer Program.  This will download the source code to the PIC.
  2. Select Programmer → Release from Reset to run the code on the PIC Microcontroller.
  3. Select Programmer Hold in Reset to stop the code on the PIC Microcontroller and keep it in the reset state.
  4. There are also short-cut buttons at the top-right corner to program, run, and stop the PIC Microcontroller.
  5. The code will automatically run when the programmer is removed from the programming (ICSP or RJ-12) port.

 

Loading an Existing Project

To load an existing project (or a Workspace as it is called in MPLAB), you can:

  1. Start MPLAB (v8.83)
  2. Select the .mcp file in your project folder and click Open

OR

  1.  In windows explorer, go to the project folder
  2. Double-click on the project file (e.g. Tutorial.mcp)

The workspace should open exactly the same way it was when you closed it the previous time.

 

Adding a New Source File to the Project

If you want to create a new source file for the project, or compile an older source file that you have already created, do the following:

  1. Remove the previous source file by right-clicking on it in the project window and selecting Remove
  2. Right-click on Source Files and select Add Files
  3. Select the new source file and click Open

This is also the method that you will ise to load the example programs.