Tutorial: Getting started with PIC18F / dsPIC30F

What we will cover in this tutorial:

This is an informal tutorial and we’ll see how we get on, but this is what I’m hoping to cover:

  • Basic breadboard circuit for PIC18F / dsPIC30F
  • What to download / install
  • Simple program to flash an LED
  • Further reading

Basic breadboard circuit for PIC18F / dsPIC30F

The hardware and components you’ll need for this tutorial are:

  • A computer running Windows XP
  • PICkit2 USB programmer
  • Breadboard
  • 6-pin header
  • PIC18F4620 or dsPIC30F4011 microcontroller
  • 1 resistor of about 330 Ohms
  • 1 LED
  • Some single-core wire: red, black and at least one other colour
  • It would be useful to have snips and pliers too.

Here’s the basic circuit for the PIC18F4620:

Here’s the basic circuit for the dsPIC30F4011:

What to download / install

There are basically four pieces of software I recommend installing for PIC/dsPIC development:

  1. MPLAB IDE v8.83 – This is the integrated development environment that Microchip (the company, that is) provide for PIC development. A newer cross platform version (MPLABX) is currently in beta, but I’m sticking with plain old MPLAB for the time being.
  2. For PIC18Fxxxx development in C, I recommend using Microchip’s C18 compiler (aka “MPLAB C for PIC18 v3.40 in LITE mode”), which can be downloaded for free (although you will need to register your email address with Microchip).
  3. For dsPIC30Fxxxx development in C, I recommend using Microchips C30 compiler (aka “MPLAB C Compiler for PIC24 and dsPIC v3.30c”), which can be downloaded for free (again, you will need to register your email address with Microchip).
  4. Finally, I recommend installing the PICkit2 utility application (PICkit 2 v2.61), which contains some really useful tools, including one which lets you use the PICkit2 as a simple logic analyzer and one which lets you display messages on the PC screen from a program running on the PIC.

MPLAB should be installed first, followed by one or both of the C compilers (depending which chips you intend to use). During the installation process of each C compiler, if you are asked whether you want the compiler to integrate itself with MPLAB, say yes. Finally, (optionally) install the PICkit2 application (it’s not required, but it comes in really handy).

You should also download some important documentation, which should be kept close at hand while developing for the PIC or dsPIC:

Simple program to flash an LED

Here’s a very simple program to flash an LED on pin 19 (RD0) of the PIC18F4620:

// Very simple PIC18F4620 example
// Written by Ted Burke (ted.burke@dit.ie)
// Last update 8-2-2012

#include <p18f4620.h>
#include <delays.h>

// Use internal oscillator. Disable reset pin,
// watchdog timer and low voltage programming.
#pragma config OSC = INTIO67
#pragma config MCLRE = OFF
#pragma config WDT = OFF
#pragma config LVP = OFF

void main(void)
	// Set RD0-3 as digital outputs, RD4-7 digital inputs
	TRISD = 0b11110000;

	// Keep flashing an LED on pin RD0 indefinitely
		LATDbits.LATD0 = 1; // Set RD0 high
		Delay10KTCYx(10);   // delay for 10x40ms
		LATDbits.LATD0 = 0; // Set RD0 low
		Delay10KTCYx(10);   // delay for 10x40ms

Here’s a simple program to flash an LED on pin 23 (RD0) of the dsPIC30F4011:

// This dsPIC30F4011 example program flashes an LED on RD0
// Written by Ted Burke - 9-2-2012

#include <p30f4011.h>

// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

int main()
	// Configure all four port D pins (RD0, RD1, RD2, RD3)
	// as digital outputs
	LATD = 0;
	TRISD = 0b1111111111110000;

	// Flash LEDs on RD0 and RD1 at 1Hz for 4 seconds
		_LATD0 = 0;
		_LATD0 = 1;

	return 0;

Further Reading

This entry was posted in Uncategorized and tagged , , , , , , , , , , . Bookmark the permalink.

8 Responses to Tutorial: Getting started with PIC18F / dsPIC30F

  1. Mo says:

    Hi dear friend
    I have gone through your tutorial for dspic30f4011 and I have found it very useful and well organised notes.

    At the moment I am working on my final year project and I have generated two PWM with the same duty ratio that I can control those pulses by a potentiometer. I just need to make a phase shift of 180 degrees between them.
    Phase shift is very important and crucial for my project and I dont know how to make 180degrees shift between them.

    Can you please help me to do that.
    I appreciate in advance for your help buddy


  2. Liam says:

    Great quick reference but the colours on the Vdd/Vss to AVdd/AVss links in the dsPIC diagram would benefit from being the other way around.

  3. Bell says:

    hey can you post the contents of the header file for dspic30f??? plz… it’ll be a life saver…

    • batchloaf says:

      Hi William,

      I can’t publish the header file here because the copyright is presumably reserved by Microchip. However, if you have installed the Microchip C compiler for the dsPIC family of microcontrollers, you should already have all the header files you need. I’m using Microchip’s XC16 compiler, so on my laptop the file is at the following location:

      D:\Program Files\Microchip\xc16\v1.00\support\dsPIC30F\h

      Actually, if you’re using XC16 (or MPLABX), then you only need to include the header file “xc.h” and the correct header file for your chip (i.e. “p30f4011.h” in this case) will be pulled in automatically.

      If you have the older C30 version of Microchips C compiler for dsPIC, then the header file is probably located in the following folder (adjust path for your installation obviously):

      C:\Program Files\Microchip\MPLAB C30\support\dsPIC30F\h

      Both XC16 and C30 can be downloaded free of charge from microchip.com.

      I hope that helps!


  4. agniteja says:

    Hey……thnks…for the turotrial.. I really managed to bring my own picmicro up and running. Pl. post.. more..and helps us to get better.

    Thanks a ton

  5. Pradeeshkumar says:

    Thanku all good program

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s