Program Pic Microcontrollers with MikroC for Pic Compiler

In this tutorial, you will learn how to program a PIC Microcontroller compiler software called MikroC. This is a simple and user-friendly software designed to convert the C-language code into a hex file, which can then be fed to the microcontroller to perform specific tasks. After completing this tutorial, you will be able to generate the hex code of a program in the C language using any PIC Microcontroller and simulate it.

The hex code is loaded into the flash memory of the microcontroller in the form of zeros and ones, interpreted by the CPU as commands that are executed. To understand how the hex file is generated, let us take the example of a simple program to set all eight port pins of PORTC to logic 1.

If you have not used MikroC for Pic before, you can follow this tutorial:

Compiler Code with MikroC for Pic

Open the mikroC PRO software by double-clicking the icon on your desktop. From the menu on the top, select Project à New Project.

Mikro c tutorial
Mikro c tutorial

A New Project Wizard window will appear. Click “Next” to reach the “Project Settings” window. Specify a unique name for the project, select the device name as PIC16F877A, and browse the project folder where the hex file will be saved. Choose the device clock according to the one used in the design, and then press “Next.”

Mikro c tutorial
Mikro c tutorial

Step 2 allows you to add a file to the project, while Step 3 allows you to include all or select libraries. Press Next without making any changes on both windows and then press Finish to end the wizard. The Code Editor Window will be displayed with “void main()” written on the first line. This is where you have to write your program. Write the code and press BUILD on the horizontal toolbar at the top.

Mikro c tutorial
Mikro c tutorial

Here we have a two-liner code where all pins of Port C are set to high.

On successful completion of the Building process, a message would appear in the ‘Message’ window at the bottom of the screen, indicating that the code has been compiled without any errors and the hex file is ready.

Mikro c for pic tutorial
Mikro c for pic tutorial

Upload and Debug Code

The above-mentioned program is then simulated to ensure its proper functioning. To carry out the simulation, the required steps are as follows:

  • Select Run àStart Debugger from the menu bar.
Mikro c for pic tutorial
Mikro c for pic tutorial

The Debugger feature goes through all instructions one at a time and reflects the step-by-step changes caused to the port pins, registers, etc. The variables that you want to display can be selected from the ‘Watch Value’ window on the right side of the screen.

Select the variable PORTC by clicking on “Select Variable from list” and then click on the Add button to add it to the watchlist.

Mikro c for pic
MikroC for pic

Press F8 to move the blue line onto the next instruction, the blue line highlights the instruction that will be executed next. The point to note in the ‘Watch Value’ window is that the value of PORTC is still zero since the third instruction has not been executed yet.

Press F8 again, and now the value of PORTC has been changed to 255 i.e. all highs

Mikro c for pic tutorial
Mikro c for pic tutorial

If you want to upload code to a Pic microcontroller, you can follow this guide:

Conclusion

In summary, follow these step to flash code to a PIC microcontroller using the MikroC for PIC compiler, follow these steps:

  1. Launch the MikroC PRO software by double-clicking the icon on your desktop.
  2. From the menu on the top, select Project and then click on New Project.
  3. A “New Project Wizard” window will appear. Click “Next” to proceed to the “Project Settings” window.
  4. In the “Project Settings” window, provide a unique name for the project and select the device name as PIC16F877A (or any other specific PIC model you are using).
  5. Browse and select the project folder where the generated hex file will be saved.
  6. Choose the device clock according to the one used in your design and click “Next”.
  7. In Step 2 of the wizard, you have the option to add a file to the project. Select the C-language file containing your code.
  8. In Step 3, you can include any necessary libraries for your code. You can either include all libraries or select specific ones as needed.
  9. Press “Next” without making any changes on both Step 2 and Step 3 windows.
  10. Finally, click “Finish” to end the wizard.
  11. The Code Editor Window will be displayed with the “void main()” function written on the first line. This is where you can write your program.
  12. Write your code in the Code Editor Window.
  13. Once you have finished writing your code, click on the BUILD button on the horizontal toolbar at the top. This will compile your code into a hex file.
  14. After the successful completion of the building process, a message will appear in the “Message” window at the bottom of the screen, indicating that the code has been compiled without any errors and the hex file is ready.
  15. Now, you can proceed to upload the hex file to the PIC microcontroller using a programmer such as PICKit3, as described in the How to Use PICKit3 to Upload Code to PIC Microcontroller guide.

Remember to double-check the specific microcontroller model and connections to ensure compatibility and successful flashing of the code.

Related content:

4 thoughts on “Program Pic Microcontrollers with MikroC for Pic Compiler”

  1. Hi.
    Can you post all tutorials in one place for downloading. It is very learnfull. But we do not have access to internet all the time and it is very costly for us to travel to places where we can get to the internet.
    Regards.

    Reply
    • Hi Johnny,
      I have access to internet, if you wish I can compile what is on here and send it straight to you so that you can download it

      Reply

Leave a Comment