A Beginners Guide to Microchip PIC Microcontroller Programming

Tags: , ,

Microchip PIC Microcontroller 8 bit MCUThis tutorial will guide you through the process of creating your first Microchip PIC microcontroller based circuit.

It will cover everything that needs to be done in order to write a simple program that will blink a LED, in a step-by-step “hands-on” method. All the software used in this tutorial is absolutely free. All you need is a simple JDM programmer (read on for detials), and you are good to go!

Here’s a list of what you should get before we start:

  1. A PIC16F micro-controller. you can buy one in electronics stores or over the web.
    In this guide I’m going to use a PIC16F684 because it’s cheap, small and easy to use.
  2. development breadboard
  3. PC with Windows XP/2000 OS. your PC must have a Serial COM output.
  4. simple Female to Male 9 wire Serial Cable (DB9 PLUG)
  5. A few list of components.
  6. PIC Programmer

    you can buy all of the above here

Bedore we start, here’s a short explanation of the development process:

First, we’ll write our code in MPLAB – Development Environment, than we compile and build our code and get a HEX file.
in order to transfer the hex file to the microcontroller wi’ll use a PIC programmer, a simple electronic circuit that can get the code from our PC using
the serial port and program it to the PIC program Flash memory.

Let’s start, if you have any question please use our forum sysyem.

  1. Download and install MPLAB IDE from Microchip’s web page.
    In the installation process you’ll be asked for installing a PICC compiler developed by HI-TECH PIC company.
    Install it!, it’s a C complier that let us wite our code in C insted of Asembler.
  2. In order to Program our code to the microcontroller we should build a programmer. there’s a lot of designs for programmers over the web.
    I recommand on the following one.
  3. Download Winpic800. it’s a good program that controls the Programmer. It support most of Microchip’s microcontrollers.
  4. Build the following circuit. it’s a simple circuit that suply 5V voltage to the PIC and connects the programming
    lines to the apropiate legs of the microcontroller.

Short explanation about the circuit:

J1 – Power supply connector.
SW1 – main power switch
D1 -

U1- 5V voltage regulator.
C1,C2 -

D2 – power on led
SW2 – user reset




check your connections carefully!

Our circuit needs power supply. A good Power generator is recomanded, but if you don’t have one and don’t want to buy one, you can use a simple transformator
that can supply at least 500mA @ 9V. It better be fused because you dont want to burn your transformator if you mistakenly connects to + and – lines.


Connect the transformator to your circuit and turn the switch on. the green led should turn on. Connect a serial cable from you com port to the PIC programmer, and then connect the PIC programmer ICSP header to the breadboard using the right connector.

If everything is connected right it should look like this:


Writing the first PIC program

  • Start the MPLAB program
  • Create a new project (Projects->New Project)
  • Choose your microcontroller from the list (in our case, PIC16F684)


  • Then, choose the HI-TECH Univeral ToolSuite, and the Hi-TECH C Compiler (See screenshot below)
  • In the next screen, name your project, and click on “Next”.
  • Click on “Next” twice again, and that’s it, your new project is now created!
  • Now we’ll add a new file to the project we have just created. Click on File->New.

First PIC Program Code

Type the following code into the file you have just created:

include <pic.h>

void main(void)
	unsigned int i;

		for (i=0; i<0xFFFF; i++)

		for (i=0; i<0xFFFF; i++)


Explanation of the above code

include <pic.h>

This command instructs the compiler to include relevant code about the PIC we’re using.

main(void) {

Definition of the main function of the code.

unsigned int i;

creation of an integer variable, which will be used later in a delay loop.


Set the TRISC register. The TRISC register is an 8 bit register which sets the states of the PORTC pins of the PIC. A pin state can either be 0, which means an output, or 1, which means an input. We define all the port pins as outputs, and hence the 0×00 value.


Since we want the LED to blink endlessly, we create an endless loop (1 states ‘true’ in programming, so the following line reads as “while (true)” which is computerish for ‘always’ :).

for(i=0; i<0xFFFF; i++)

The purpose of this loop is to create a delay between the time the LED turns on, and when it turns off. This is not an optimal way to create a delay, but it will suffice for now.


The PORTC register contains the values of it’s input\output legs. Since we initialized the port to be an output port, this command will set the value of every leg to 1 (remember that 0xFF is 11111111 in binary). In other words the MCU will rise the voltage in those legs to VCC, or 5v in our case, and hence cause the led to turn on.

for(i=0; i<0xFFFF; i++)


The purpose of the above lines is the same as the last ones – to create a delay. But this time we change the state of the PORTC register to low (0×00) which will bring the voltage of the PORTC legs to ground, and hentch turn off the led.

Having these ‘turn off’ and ‘turn on’ commands repeated infinitely by the while command will cause the led to blink.

  • Press File->Save and save the file as src1.c
  • Press View-Project and you will the a window containing the directory tree of the project we have created.


  • Right click on the directory and choose Add Files, and then Source File
  • Now the project is ready for compilation. Compile it by clicking on the Project->Compile  menu
  • If the compilation process finished without errors, you should have a HEX file created in the project directory. This file contains the binary form of your code – and burning it to the PIC will make the PIC run your code.
  • To burn the PIC, load the winpic800 program.
  • Now, we will configure the program for the first use.


  • Press on Settings->Hardware and choose the comport to which you connected your JDM Programmer.


  • Set the rest of the settings as shown in the image about.
  • In the top right of the program, choose the PIC model you are using (in the image PIC 16F684 is used)
  • Now we will check that the program can successfully connect to the PIC Programmer we are using.


  • Click on the button marked in the image above.
  • If you get the same message as in the picture above, everything is connected properly and the hardware is recognized. Otherwise, check your connections and COM port settings.
  • Connect the PIC programmer to the breadboard again, and turn on the circuit. The led should turn on again.

winpic detect

  • Now we will check that the software successfully recognizes the PIC MCU we are using. Click on the icon marked in the image above, and the right PIC model should be detected.
  • Now choose the file we want to program the PIC with (it’s the same HEX file that was created by compiling our code in the previous level)

winpic set

  • Now press on the Settings button (marked in the image above).  This dialog allows us to configure several run-parameters of the PIC. For example – the internal clock speed, whether to use an internal or external clock, etc.
  • Choose INTOSCIO – Use the internal PIC clock.
  • Remove the marking of the WDTE.

winpic programming

  • Now everything is ready to programming your PIC with the code you have written and compiled!
  • Press on the red button, marked on the picture above. The program will be written to the PIC, and if everything is correct, upon turning on the circuit the LED will begin to blink!

Subscribe now via RSS and never miss a post!

Comments are closed.