HOW TO PROGRAM A PIC MICROCONTROLLER

HOW TO PROGRAM A PIC MICROCONTROLLER,In this tutorial, you will learn how to program a PIC Microcontroller using a 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 the specific tasks.After the completion of this tutorial, you will be able to generate the hex code of a program in C-language using any PIC Microcontroller and then simulate it.




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

STEPS FOR GENERATING THE HEX CODE 

  • 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 to the ‘Project Settings’ window. Specify a unique name for the project, select the device name as PIC16F877A and browse the project folder; this is where the hex file will be saved.Choose the device clock as per the one used in design and then press Next.
Mikro c tutorial

Mikro c tutorial

  • Step2 allow you to add a file to the project while step3 lets you to include all or selective libraries. Press Next, without making any changes, on both the windows and then press Finish to end the wizard. TheCode Editor Window will be displayed with “void main ()” written in the first line. This is where you have to write your program. Write the code and press BUILD from the horizontal toolbar at the top.
Mikro c tutorial

Mikro c tutorial




Here we have a two liner code where all pins of portC 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

Steps for Simulation

The above mentioned program is then simulated to ensure its proper working. To carry out simulation, the required steps are given below:

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

Mikro c for pic tutorial

The Debugger featuregoes through all instructions with one instruction 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 of the screen.

  • Select the variable PORTC by clicking on ‘Select Variable from list’ and then click on the Addbutton to add this into the watch list.
Mikro c for pic

Mikro c 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

We hope that after reading this article, you have learnt that how to write your first program using pic micocontroller. For more tutorials on pic microcontroller keep visiting our website.

Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,248 other subscribers