Introduction to MPLAB IDE

38
MPLAB IDE Starter Guide By: Karim El-Rayes

description

A starter guide how to use Microchip MPLAB IDE for PIC microcontrollers and related tools like MPLAB C18, C30 and C32 compilers, and how to MPLAB features to get connected and integrated with programmer/debugger devices and development kits from Microchip. for more discussion and articles about different microcontroller platforms and tutorials please visit: http://elrayescampaign.blogspot.ca/

Transcript of Introduction to MPLAB IDE

Page 1: Introduction to MPLAB IDE

MPLAB IDE Starter Guide

By: Karim El-Rayes

Page 2: Introduction to MPLAB IDE

Software to be installed

• MPLAB IDE from Microchip®, its free of charge and you can download from Microchip® website.

• C18 or C30 or C32 compiler according to the microcontroller family you are using, you can download Trail version or Liteversion for students from Microchip® website.

• Notes: For PIC18 family you will need C18 C – compiler.For PIC24 and PIC33 families you will need C30 C – compiler.For PIC32 family you will need C32 C – compiler.• PIC18F4550 was used as an example in many cases, but

most of the material is applicable for all PIC18F family of microcontrollers.

Page 3: Introduction to MPLAB IDE

Step 1

Page 4: Introduction to MPLAB IDE

Click next

Page 5: Introduction to MPLAB IDE

Step 2

Select microcontroller you will use from this list, then click “Next”

Page 6: Introduction to MPLAB IDE

Step 3

Select compiler C18 from the list, don’t forget to select the other settings, then click “Next”

Page 7: Introduction to MPLAB IDE

Step 4

Choose place to save your project, then click “Next”

Page 8: Introduction to MPLAB IDE

Add any files to the project if you want, then clic k “Next”

Page 9: Introduction to MPLAB IDE

Click “Finish”

Page 10: Introduction to MPLAB IDE

Step 5

Click “New file” button to start writing your first code

Page 11: Introduction to MPLAB IDE

Write your code here then click “Save file” button

Page 12: Introduction to MPLAB IDE

Choose place to save your code and don’t forget to save it in *.c

Page 13: Introduction to MPLAB IDE

Step 6

Right click on “Source Files” and select “Add Files”

Page 14: Introduction to MPLAB IDE

Choose your code *.c file you saved it

Page 15: Introduction to MPLAB IDE

Step 7 (optional)

Right click on “Linker script” and select “Add Files”

Page 16: Introduction to MPLAB IDE

Go to C:\MCC18\bin\lkrAnd select linker script file of the microcontroller you are using in your project(in this example we are using microcontroller PIC18F4550)

In C18 compiler Version 3.36 and higher, the linker scripts are in C:\MCC18\bin\LKR

Page 17: Introduction to MPLAB IDE

Step 8

Select “Release” if you want to download the generated .hex file on the microcontroller

Page 18: Introduction to MPLAB IDE

Step 9

Click “Project” then go to “Build options” then select “Project”

Page 19: Introduction to MPLAB IDE

Select “Library Search Path” from the directories drop list

Page 20: Introduction to MPLAB IDE

Click “New” then write here the libraries file path then click “Apply” then “Ok”

Page 21: Introduction to MPLAB IDE

Step 10

Click “Build All” button to build and compile your pr oject

Page 22: Introduction to MPLAB IDE

Check your program either “Build succeeded” or watch the errors here

Page 23: Introduction to MPLAB IDE

Hints: Setting microcontroller clock

Click “Configure” then select “Configuration Bits”

Page 24: Introduction to MPLAB IDE

If you want to make settings from this window and not in your code don’t check this mark.

If you are using crystal higher than 4MHz choose “HS-PLL Enabled”.If you are using 4MHz crystal choose XT.

Disable “Watchdog timer” if you are not using it.

Page 25: Introduction to MPLAB IDE

Hints: settings for PIC18F4550

Set the crystal you are using

If you are using a crystal higher than 4MHz select “HS” or “HS+PLL” Disable watchdog timer if

you are not using it.

Page 26: Introduction to MPLAB IDE

How to Program your PICChoose programmer device from here (here we chose PICKit 3)

Page 27: Introduction to MPLAB IDE

How to Program your PIC (cont.)

Step 1: Choose “Release” Option

Step 2: “Build” project

Step 3: “Program” the PIC

Note: After you program the PIC, disconnect the PIC programmer hardware from the microcontroller.

Page 28: Introduction to MPLAB IDE

How to Debug your PICChoose debugger device from here (here we chose PICKit 2)

Page 29: Introduction to MPLAB IDE

How to Debug your PIC (cont.)

Step 1: Choose “Debug” Option

Step 2: “Build” project

Step 4: “Run” to start debugging

Step 3: “Program” the PIC

Page 30: Introduction to MPLAB IDE

Hardware example: Connecting PICkit 2/PICkit 3 programmer/debugger to PIC18F4550 microcontroller

+5v

Ground

Notes:• Pin 2 in PICkit2/3 “VDD/Target” has to be connected to themicrocontroller VDD supply, if you are using +3.3v supply based microcontrollers then VDD = +3.3v, and the same for +5v supply. • Pin 6 in PICkit 2 is “Not connected”, while in PICkit 3 is Low Voltage Programming pin “LVP”, leave it not connected in most cases if you are not using it.

Page 31: Introduction to MPLAB IDE

Circuit schematic for PIC18F4550 microcontroller wi th USB module connection to PC

Notes:

• VCC = +5v, you can power the PICfrom the USB port itself.• You can replace the 8 MHz crystal with any value depending on your configuration.• The two 100nF capacitors on Pin 18 can be replaced with one 470nF capacitor.

Page 32: Introduction to MPLAB IDE

Example code 1: Simple digital output using PIC18F4550 // In this example we will configure port D as outp ut and send some data to be //output on it

#include<p18f4550.h>void main(){

TRISD = 0x00; //set port D to output , also can be wr itten 0b00000000LATD = 0x00; //set latch of port D to 0x00

while(1) //while(1) loop is used to run the applicat ion forever{

PORTD = 0x0f; // output 00001111 on port D pins}

}

Page 33: Introduction to MPLAB IDE

Example code 2: Simple digital input using PIC18F4550 // In this example we will configure port D as inpu t and read some data from and //store it to variable “i”

#include<p18f4550.h>int i = 0;void main(){

TRISD = 0xff; //set port D to input, also can be wri tten 0b11111111LATD = 0x00; //set latch of port D to 0x00

while(1) //while(1) loop is used to run the applicat ion forever{

i = PORTD; // read port D and save read value to variable i}

}

Page 34: Introduction to MPLAB IDE

Using PIC32 Starter kit or PIC32 microcontrollers

Page 35: Introduction to MPLAB IDE

PIC32 Starter kits

PIC32 Ethernet Starter kit PIC32 USB Starter kit II

Page 36: Introduction to MPLAB IDE

Connecting PIC32 ESK board

PIC32 Ethernet Starter kit

PIC32 USB Starter kit II

Page 37: Introduction to MPLAB IDE

Selecting PIC32 starter kit for debugging

Select “PIC32 Starter kit” from the debuggers list

Page 38: Introduction to MPLAB IDE

Debugging

Step 1: set the debugger to “Debug” mode

Step 2: “Build” project Step 3: “Program” thePIC microcontroller

Step 4: “Start” debugging