Breaking News
Home » Basic Electronics » How to Program PIC18 Microcontroller in C. Step by Step Tutorial (Pictorial Views)

How to Program PIC18 Microcontroller in C. Step by Step Tutorial (Pictorial Views)

How to Program PIC18 Microcontroller in C. Step by Step Tutorial (Pictorial Views)

How to Program a microcontroller
Microcontroller programming means coding of a microcontroller for different purposes in a special software. There are lots of software in which we can write different codes for microcontroller and IC Chips. Below, we will discuss that how to program a microcontroller. In this tutorial, we will program or write a simple code for PIC18 microcontroller in C where “C” is a widely-used computer language and it happens to be the only supported language (aside from assembly) at this time for 8-bit and 16-bit PIC microcontrollers.
8 bit PIC:
PIC10, PIC12, PIC16, PIC18 are series of 8bit MCU.
16 bit PIC:
PIC24, dsPIC30, dsPIC33 are the series of 16 bit MCU
32 bit PIC:
PIC32 is the series of 32 bit MCU.
(Below is the step by step process to program a PIC18 microcontroller)How to Program PIC18 Microcontroller in C. Step by Step Tutorial (Pictorial Views)
 How to burn a coded program to the microcontroller
Burning a microcontroller means to transfer the coded program from the compiler (where compiler is a software where we can write, analyze, test and debug the coded program for a microcontroller.) to the microcontroller memory. The coding or program written for microcontroller is generally in assembly/C language and the compiler generates a hex file which is understandable by the microcontroller. The hex file contains special instructions which are to be transferred to the microcontroller memory and then it works according to the given instruction and program.
Below are the simple steps tutorial to understand the programing / simple coding and burning a microcontroller.
When we programmed a microcontroller (we will discuss the step by step tutorial that how to program a write the special purpose coding for a microcontroller), then we need it to burn this program to the memory of the microcontroller. For this purpose, we need a hardware which understand and read the contents and programming codes of the hex file stored on our laptop or PC via software. So we connect this hardware via USB cable or serial to the PC/Laptop and transfer the written programming code to the memory of microcontroller and then microcontroller perform the exact function for which the designer and manufacturer designed the microcontroller IC Chip. 
Programming PIC18 Microcontroller in C

Microchip Technology is the 2nd largest electronics and IC fabrication industry. Microchip Technology sells microcontrollers in 6-pin packages (PIC10F2xx series) , 100-pin packages (dsPIC33EP512MU810) and even 144-pin packages (some PIC32 devices). Also, there are many previous series like PIC12, PIC16, PIC18.

Introduction of the Target Controller:
We are going to discuss here PIC18 series. A Common Microcontroller of this series is PIC18f452. This controller is a 8 bit microcontroller has 40 pins, 32kb of program memory and it can be operate up to 40 MHZ of crystal frequency so this controller is suitable for many applications. This controller can sink and supply up to 25mA current so no need to use a transistor to driver an LED and connecting them to other hardware. There are 3 external interrupt pins and two 16 bit timers, one 8 bit timer. This controller is equipped with Capture module and comparator module. This controller has on bard analogue to digital convertor so no need to connect external ADC with this device. This Controller can also have modules for communication with other hardware like RS232 Module, I2C Module, 1wire Module and Parallel Slave port. This Microcontroller is the aesthetically engineered device and suitable for beginner, hobbyist as well as professional for mini and complicated electronics projects.
Introduction of the Programming Environment:
The programming environment we are going to use is MikroC for PIC. This IDE is fully equipped with a range of built in libraries and easy to use interface. This integrated development environment is packed with many calculators like hex to binary and decimal conversion which helps a lot in programming. This Software size is also very small as compared to other software so it is easy to use and install. The Syntax of this software is a little Bit different then MPLAB Software but being a designer, I will suggest all the reader to use MikroC instead of MPLB because of it really easy to read syntax and lots of built in libraries to use. It will definitely reduce the programming time and you don’t have to build the common functions from scratch like delay, LCD read and writes, RS232 Protocol and lots of others.
Note that MikroC  is not a free software while Microchip’s MPLAB X IDE and XC8 compiler are free and offers many free libraries.
Let’s start with an example.
Note: it is considered that you downloaded and install MikroC Programming Software and we are going to Program it in C and you have initial knowledge of C programming.
Making new project in MikroC:
Step 1:
Double click the MikroC icon on your Desktop or where you have installed that software (as shown below).How to program a PIC18 microcontroller in C with MikroC?After the software is loaded you will see this window.How to program a PIC18 microcontroller?

Step 2:
Now go to the Project button on the upper right corner and click the “Project”.How to burn and Program a microcontroller

Step 3:
Now click on “Now Project” and the new below window will to program a microcontroller in MikroC
Step 4.
Now click next and select your target MCU Clock speed and project name and directory where you want to save it.
After that, don’t pay attention to the rest of the windows and just click next on all the remaining windows which appear.
Now the below window will appear. This is your programming Environment on this window you have to write code and compile it.
Now you have setup the software. Next step is writing code.
Writing Code for PIC18 Microcontroller:
PIC18f452 comes with 5 ports. Four ports are 8 bits and one port is 4 bits in this tutorial we are going to use 8 bits port. There are few things you have to keep in mind while writing code for PIC18. (Code is given below)
Making a port as input or output:
Each port has TRISX Register which defines that you are using this port for the input or for output. Where X can be, A, B, C, D, E, F e.g. TRISB.
For output you have to put 0x00 in TRISB and for input you have to put 0xFF in it. but this is not always the case because Each pin can be independently selected as an input or output. you could write 0x09 to TRISB which would select RB0 and RB3 for inputs and the rest of the pins for outputs.
Delay function:
In mikroC I have selected the delay library so I don’t have to create the delay functions here I can simply use Delay_ms() function. In this function you have to give how many mili seconds delay you want like if you want 1000 mili second delay you can write it like Delay_ms(1000).Writing Code and program for PIC18 microcontroller
Here is the First simple toggle PORT bits Code.
void main() {
TRISB=0x00; // Defineind as Output
While(1) //Infinite loop
            Delay_ms(500) ; //500 miliseconds delay
            Delay_ms(500) ;
When you write this code after that you have to compile it.
Click on this “build all “button code will be compiled and hex will be created in the folder in which you have saved your project file.toggle PORT bits Code for pic18 microcontroller

Once the hex is created you can burn this hex in your PIC18f452 by using PICKIT hardware or you can test it by using proteus.
That’s all about this first tutorial; stay tuned for upcoming posts about the topic.

You might also read:

EasyEDA: Ideas for Circuit Design, Innovation for Electronics Access
Free Circuit Design: Schematic - Simulation - PCB Layout - Gerber Viewer
Cheapest PCB Prototype: Only $2 for 10pcs, 24 hours Quick Turn, DHL Delivery in 3 days

About Electrical Technology

All about Electrical & Electronics Engineering & Technology. Follow , Facebook , Twitter , Instagram , Pinterest & Linkedin to get the latest updates or subscribe Here to get latest Engineering Articles in your mailbox. Also, Follow

Check Also

What is Stepper Motor: Construction, Types and Modes of Operation

What is Stepper Motor: Construction, Types and Modes of Operation

All You Have to Know about Stepper Motor: Types and Modes of Operation The invention …


  1. Pls i have a problem with my microcontroller(18F14K22) reading inputs. When i program my hardware (using PICKIT3) with a code like yours, it works but when i add an if condition (code below), nothing happens. What could be wrong. I also have the same results on proteus but it runs as expected on the mikroc compiler debugger.

    void main() {
    TRISA = 0xFF; //PORT A as inputs
    TRISB = 0x00; //set PORT B pins as output

    if (PORTA.f0==1){
    LATB= 0x00;
    PORTB = 0b11000000; //PORT B HIGH
    PORTB = 0b11100000;
    PORTB = 0b11110000;
    PORTB = 0b00111111; //PORT B LOW after 1sec
    PORTB = 0b00011111;
    PORTB = 0b00001111;


  2. Praveen raturi

    can you please mail me the programme to turn on and off the led when the ultrasonic sensor get triggered in avr my mail id is

Leave a Reply

Your email address will not be published. Required fields are marked *