The above link takes you to the article about introduction to MPLAB IDE, in case if you have missed it. Having installed MPLAB IDE and one or more compiler suite, this article describes how to compile a program and extract the .hex file to program our chip. I will be using C language in this tutorial as it is requested by one of our CT readers and I will be using Microchip’s HI-TECH C compiler.
Creating and Saving a new project:
Open MPLAB X and in the window, select the new project as shown below
The new project wizard will appear as shown below which will guide you through the process of setting up a new project. Here you have to select the category and type of the project. Select the category as Microchip Embedded and Project as standalone project.
Then proceed (by pressing next). The next phase of the wizard asks you to select the family of your PIC and also the device name. In our case is is given as Family: Mid range 8 bit MCUs and the device: PIC16F84A as shown in the below picture.
Proceed by clicking next and in the next step of your wizard, it prompts for the selection of the tool you are going to use to connect the MCU to the computer. Select the appropriate tool if you have one. Since I don’t have any of the microchip’s tools, I selected the simulator. The color beads before the tools indicates the tool’s compatibility with the microcontroller you have selected. (Red – no compatibility, Green – Fully compatible and yellow – The device may work). You will find two such beads in the hardware category (One is for debugger support and other is for programmer support).
The next and the important step is to select the compiler to be used to compile this project from the list of installed compilers. In MPLAB, you can select different compilers for different projects. Here, I selected the HI-TECH C compiler to use with the project. Select the appropriate compiler and proceed.
The next step prompts you to save your project. I saved the project as “ctoday”. Also, note the directory where the project is saved, as this would be helpful in extracting the .hex file after compiling. In my case, the project is saved in “/home/anish/MPLABXProjects/”. If you are using windows, it may be stored in “C:/Users/username/MPLABXProjects/”. Of-course, you can also specify the location wherever you want to save the project by hitting the browse button. Leave the remaining settings intact for now.
On hitting finish, your new project will be created and will be displayed on your left project pane of your MPLAB window (I’ve shown only the project pane in the next figure). Now you have successfully created a new project. Now, its time to write some code. To write the C code, you have to create a file. Now click new file icon which is to the left of the new project icon as shown below.
In the following new file wizard, select the file category as “C” and file type as “C main file”, then click next and when prompted, save the file with the desired name (or leave it to default as “newmain.c”)
After the wizard, your c file will be created and in that type the program. The program I’ve included is for blinking LED on PORT A of the controller. I’ve included comments in the program which is self-explanatory. After finished typing the program, press the build project (The hammer icon) / clean and build (Hammer icon with a brush), alternatively you can also go to run>build project. Clean and build option cleans the intermediate files which are created while compiling the project. Now, after pressing Clean and build, and if your code is infallible, it must compile and in the output window, you must see “BUILD SUCCESSFUL” as shown below. If not errors will be shown.
And after successful building, we want the hex file which should be loaded into our micrcontroller. The hex file is created inside the project directory. You can also view the hex file in the files tab to the left of the window (next to the projects tab) as shown below.