Hello World Project With PIC Microcontroller – Part I

Hello friends, welcome to this exciting tutorial were we will begin our journey with latest PIC18F micros from Microchip Technologies. This tutorial will give you information on what software/hardware you will require and basic steps on how to get, install, configure and use them. After going through this tutorial you will have a complete setup and knowledge to experiment with these powerful chips !

 

 

 

 

What you will learn ?

  • MPLab as a powerful IDE.
  • HI-TECH C for PIC18 MCUs as a powerful C Compiler.
  • Creating a new HI-TECH C project in MPLab.
  • Write a C Code to blink LED and compile it to get a HEX code.
  • Configure the MCU for proper oscillator selection.
  • Burn the HEX code to MCU using eXtreme Burner PIC from eXtreme Electronics.
  • Use the programmed MCU to actually blink the LED !

So lets get started !!!

First get these stuffs

  • Microchip’s MPLab IDE or Integrated development Environment. This is the central tool from where you can access most of other tools, like the C Compiler. This also lets you create and edit program files and projects. Download this from Microchips Web site and Install it in your computer.

Download MPLAB IDE Free.

  • HI-TECH C Pro for PIC18 MCUs – This is will compile the high level human readable programs (in C Language) to Machine Language Instructions (contained in a HEX file). You will not interact directly with this software instead the MPLAB will use it internally. Actually this is a commercial software but a fully functional demo is available. The demo has only restriction that codes are not highly optimized. Not a problem for startup hobbyist!

Download HI-TECH C Pro for PIC18 Free

  • eXtreme Burner PIC – This hardware + software tool is used for burning(uploading) the HEX file to the microcontroller chip. This is a USB Based programmer with GUI interface so it is very easy to install and use.

Buy eXtreme Burner PIC

  • If you have made a Simple Serial Port Based PIC Programmer as discussed here then you need this nice program from Mr Christian Stadler. This is similar to eXtreme Burner PIC but serial port based and the hardware can be made easily.

Download PICPgm Free.

Creating your first project with MPLAB + HI-TECH C.

After installing the above tools open MPLAB IDE from desktop Icon or Start Menu. You will get a main screen similar to this.

 

mplab tutorial

Fig.: Microchip MPLAB IDE Main Screen.

From the Project Menu Select Project Wizard.

The Project Wizard will come up that will help you easily create a C Project. Click Next.

On the next screen select the Microcontroller you want to use. in our case select "PIC18F4550" and Click Next.

 

mplab tutorial

Fig.: Microchip MPLAB IDE – Processor Selection.

After that select what tool suit you want to use this project. There are many C Compilers for PIC18 (from different vendors) and also many people use assembly language, so this step is required.

As we will be working will HI-TECH C, select that from the list. Do not touch any other fields in this dialog. Just select Active Tool Suit = HI-TECH Universal ToolSuit. as shown in image below.

 

microchip mplab tutorial pic18

Fig.: Microchip MPLAB IDE – ToolSuit Selection.

 

Now Select select a suitable folder for your new project. I recommend create a Folder Named MPLAB (To save all MPLAB project in one place) and then create subdirectories in this folder for each new project. Like use C:\Your Name\MPLAB\LED\ for this led blinky demo. Now show the project wizard the path to this folder so it can create a project file here. See the next image.

 

Fig.: Microchip MPLAB IDE – Project Location.

The next screen asks to add existing files to project. This is useful when you have some ready made and tested code that you can add to this project. Like if you have got a tested LCD interfacing library (which you have got from Net or made yourself) then you can add that now. But since this a simple "hello world" project you can skip this step. So click Next

 

microchip mplab tutorial pic18

Fig.: Microchip MPLAB IDE – Adding existing files.

The final screen shows the project summary, review that and click Finish.

 

microchip mplab tutorial pic18

Fig.: Microchip MPLAB IDE – Project Summary.

Now you will be presented with the WORKSPACE. Now our Workspace is ready we need to add some source files. As this is a very simple project we will have as single source file. To add a new source file click File>New from menu or Select New File from toolbar. We need to save this file and also "add" this to our project. Select File>Save from Menu or Save File from Toolbar or Ctrl+S from keyboard. Give a proper name to file like LED.c and save in your project folder.

Make Sure that Add File To Project is checked. As shown below.

 

microchip mplab tutorial pic18

Fig.: Microchip MPLAB IDE – Saving a C File.

Now your new file is ready, lets do some coding!!! Hey I mean just copy paste the following code.


#include <htc.h>

__CONFIG(1,0x0F24);
__CONFIG(2,0X0000);

void Wait()
{
   unsigned char i;
   for(i=0;i<100;i++)
      _delay(60000);
}


void main()
{
   //Initialize PORTD
   //PD0 as Output
   TRISD=0b11111110;

   //Now loop forever blinking the LED.
   while(1)
   {
      LATD=0B00000001;  //PORTD0 = HIGH

      Wait();

      LATD=0B00000000;  //PORTD0 = LOW
      Wait();
   }
}

And don’t scratch your head now if you don’t get the code, just copy paste, everything will be explained in latter tutorials. So don’t forget to register for updates (see Feedburner Sidebar).

Now time to convert that source code to machine code. So Select Rebuild form Project Menu.

 

pic18 c tutorial

Fig.: Microchip MPLAB IDE – Rebuild from Project Menu.

If everything is ok you will get a message "Build Successful". The the compiler has compiled and linked your project and a HEX file is generated. You have to transfer this HEX file to your MCU using a Programmer. The HEX file ready to burn is located in the project folder.

 

pic micro c tutorial

Fig.: HEX file Ready to burn to PIC MCU !

That’s it for Part I of this tutorial the rest will be covered in Next tutorial.

Goodbye ! And keep posting your response !

By Avinash Gupta

avinash@eXtremeElectronics.co.in

Facing problem with your embedded, electronics or robotics project? We are here to help!
Post a help request.

Avinash

Avinash Gupta is solely focused on free and high quality tutorial to make learning embedded system fun !

More Posts - Website

Follow Me:
FacebookLinkedInGoogle Plus

29 thoughts on “Hello World Project With PIC Microcontroller – Part I

  • By Anthony - Reply

    Great Job.

  • By Avinash - Reply

    Thanks Anthony !!! 🙂

  • By MGA - Reply

    Thank you…

  • Pingback: Hello World Project With PIC Microcontroller - Part II | eXtreme Electronics

  • Pingback: Interfacing LCD Modules with PIC Microcontrollers. | eXtreme Electronics

  • By Niketh - Reply

    Hey Avinash ..It would be very grateful if you put up more example codes for PIC programming ..

  • By anshul - Reply

    Great work sir,
    I am little bit confuse in PLL and oscillator of the PIC18F4550 . I mean if i want to run my PIC @16MHz then how i shoud configure the PLL and oscillator bits in their corresponding configure register.
    As in above program u have wrote the following lines
    /////////////////////
    __CONFIG(1,0x0F24);
    __CONFIG(2,0X0000);
    /////////////////////

    which PLL and oscillator configured by these lines ?

  • By Pooja - Reply

    nice tutorial

  • Pingback: RS232 Communication using PIC18F4520's USART - PIC Microcontroller Tutorial | eXtreme Electronics

  • Pingback: Interfacing LM35 Temperature Sensor with PIC Microcontroller. | eXtreme Electronics

  • By Romuald - Reply

    Hello , guys I’m romuald , pretty new here and also a beginner when it comes to pic programming using “c” language . i have tried to go through the tutorials posted above , but i can’t get the result , i mean , the Build failed and as error this is mentioned :
    Error : No file arguments

  • By PaulM - Reply

    If you get “Error [939]: no file arguments” it’s probably because the LCD.h file has not been added to the project. Click on the Project menu and select Add Files to Project, then select the LCD.h file. Should build fine after that.

  • By Megha10 - Reply

    Hi, ur example is very useful.I would like to know if this code works for pic18f6527 as well since I’m planning to purchase it for my project.

  • By kamalb008 - Reply

    hi..
    BUILD FAILED…:(
    error shown was…
    Cannot use literal values (0x0F24) with __CONFIG(), use __PROG_CONFIG() instead…
    what’s the prob…??could anyone say me solution…???

  • By Primus - Reply

    Hi

    I had the same problem kamalb008. Don’t know why is so but instead of: __CONFIG(1,0x0F24); write: __PROG_CONFIG(1,0x0F24);

    It should be working!

  • By sam - Reply

    Can you please show the process to find the desired config value?

  • By AhmedTouma - Reply

    Thanks man

  • By Oli - Reply

    I have the same error, just delete the lines “__CONFIG(1,0x0F24);
    __CONFIG(2,0X0000);”

    and it worked

    Very nice! thanks

    • By dvlee - Reply

      You’re right. Get rid of the 2 troublemaker lines and it does build!

  • By PIC freak - Reply

    great little project.

  • By Bhavik - Reply

    Hey thanks…
    I was searching this type of help for a long time. This is very helpful to me.

  • By zarazagoza - Reply

    I am new in this field. sorry. but what does these 2 lines mean actually?
    __CONFIG(1,0x0F24);
    __CONFIG(2,0X0000);

  • By Uttam Dutta - Reply

    Hitec C found free for 45 days full functional in microchip site, is there any link to download freely so that Hitec C-18 can work without limition of day
    regards,
    Uttam Dutta

    • By Avinash - Reply

      @Mr. Uttam Dutta,

      The HI-TECH C is free, after 45 days only some features are disabled but it can be used to compile all projects. Only certain optimizations are disabled.

  • By Vignesh - Reply

    Its a great thing u have explained it clearly, I am looking like this for a long time…..
    Thanks
    Thanks a lot…..

    • By Avinash - Reply

      @Vignesh,

      Thanks ! 🙂

  • By bashar - Reply

    very good project

  • By zubair - Reply

    Hi
    the programmer can program to pic18f2550?

  • By Ricardo - Reply

    Hey bro!
    You can help me to create a “HELLOW WORLD” in the MPLAB for “pic18f26k22.h”?

Leave a Reply

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


− one = 4

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>