duminică, 24 aprilie 2011

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.
  • 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!
  • 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.
  • 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.

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