Difference between revisions of "Getting Started With PIC"

From AIRWiki
Jump to: navigation, search
(Getting Started With PIC: how to chose, assemble on a board and program a Microchip PIC microcontroller)
 
(2 intermediate revisions by the same user not shown)
Line 5: Line 5:
  
  
=== Chosing a PIC ===
+
=== Chosing a PIC(TM) Microcontroller ===
  
  
=== Hardware & software tools ===
+
=== Software tools ===
  
 
Let’s verify we have all the necessary pieces of equipment ready and installed (latest version
 
Let’s verify we have all the necessary pieces of equipment ready and installed (latest version
available for download from Microchip’s web site at http://www.microchip.com):
+
available for download from Microchip web site at http://www.microchip.com):
  
* PIC Datasheet
+
* PIC MCU Datasheet
* MPLAB IDE, free Integrated Development Environment
+
* MPLAB IDE(TM), free Integrated Development Environment
* MPLAB SIM, software simulator
+
* MPLAB SIM(TM), software simulator
* MPLAB C30, C compiler (demo or free Student Version).
+
* MPLAB C30(TM), C compiler (demo or free student version).
  
  
Line 24: Line 24:
 
hardware tools:
 
hardware tools:
  
* PIC programmer-debugger (in Ai&R Lab Lambrate you can find some Microchip ICD2 programmer-debugger and, some adapter boards with ZIF socket and some self-made programming cables)
+
* PIC MCU programmer-debugger (in Ai&R Lab Lambrate you can find some Microchip ICD2(TM) programmer-debugger and, some adapter boards with ZIF socket and some self-made programming cables);
* PIC demoboard or your board with power supply, PIC, oscillator MCLR circuitry and some input-output hardware
+
* PIC MCU demoboard or your board with power supply, PIC Microconroller, oscillator, MCLR circuitry and some input-output hardware.
  
  
 
=== Oscillator ===
 
=== Oscillator ===
 +
 +
The system clock (FOSC) source can be provided by a different number of options, depending by microcontroller family (low power, low cost microcontroller allow less options choise than high power, high cost MCU).
 +
Generally options can be divided in:
 +
• Internal Oscillator (in general an internal RC network);
 +
• External Oscillator (external clock source, external crystal or ceramic resonator operation and external RC network).
 +
 +
[[Image:MCUOscillator.jpg]]
 +
 +
In addition oscillator system can includes on-chip Phase-Locked Loop (PLL) to boost internal operating frequency, on-the-fly clock switching between various clock sources and Fail-Safe Clock Monitor (FSCM) that
 +
detects clock failure and permits safe application recovery or shutdown.
 +
This means, for example, that with a PIC24H series microcontoller, you can use a 10Mhz external crystal resonator for clock source, but configuring internal PLL circuitry through dedicated registers, you can obtain a 80Mhz
 +
internal clock frequency, achieving 40MIPS core operation speed (PIC24H need two clock cycles to execute a single instruction).
  
  
Line 40: Line 52:
  
  
== '''Part 3: PIC programming''' ==
+
== '''Part 3: PIC MCU programming''' ==
  
  
=== MPLAB settings ===
+
=== MPLAB IDE settings ===
  
  

Latest revision as of 08:49, 2 June 2009

!!!PAGE UNDER CONSTRUCTION!!!


Part 1: Introduction

Chosing a PIC(TM) Microcontroller

Software tools

Let’s verify we have all the necessary pieces of equipment ready and installed (latest version available for download from Microchip web site at http://www.microchip.com):

  • PIC MCU Datasheet
  • MPLAB IDE(TM), free Integrated Development Environment
  • MPLAB SIM(TM), software simulator
  • MPLAB C30(TM), C compiler (demo or free student version).


Part 2: Basic hardware

Besides a PC, because of we intend "make" something and not only talk about it, we need some hardware tools:

  • PIC MCU programmer-debugger (in Ai&R Lab Lambrate you can find some Microchip ICD2(TM) programmer-debugger and, some adapter boards with ZIF socket and some self-made programming cables);
  • PIC MCU demoboard or your board with power supply, PIC Microconroller, oscillator, MCLR circuitry and some input-output hardware.


Oscillator

The system clock (FOSC) source can be provided by a different number of options, depending by microcontroller family (low power, low cost microcontroller allow less options choise than high power, high cost MCU). Generally options can be divided in: • Internal Oscillator (in general an internal RC network); • External Oscillator (external clock source, external crystal or ceramic resonator operation and external RC network).

MCUOscillator.jpg

In addition oscillator system can includes on-chip Phase-Locked Loop (PLL) to boost internal operating frequency, on-the-fly clock switching between various clock sources and Fail-Safe Clock Monitor (FSCM) that detects clock failure and permits safe application recovery or shutdown. This means, for example, that with a PIC24H series microcontoller, you can use a 10Mhz external crystal resonator for clock source, but configuring internal PLL circuitry through dedicated registers, you can obtain a 80Mhz internal clock frequency, achieving 40MIPS core operation speed (PIC24H need two clock cycles to execute a single instruction).


MCLR circuit

Programming interface

Programming Cable

Part 3: PIC MCU programming

MPLAB IDE settings

Start a new project

Then, let’s follow the “New Project Set-up” checklist to create a new project with the MPLAB IDE:

  • Select “Project->Project Wizard” to activate the new project wizard, which will guide us

automatically through the following steps…

  • Select the correct PIC device, and click Next.
  • Select the MPLAB C30 Compiler Suite and click Next.
  • Create a new folder and name it “FolderName”; name the project “ProjectName” and click Next.

Simply click Next to the following dialog box—there is no need to copy any source files from any previous projects or directories. Click on Finish to complete the Wizard set-up.

Open a new editor window.

Select “File->Save As”, to save the file as: “FileName.c”. Select “Project->Save” to save the project. Our first line of code is going to be:

#include <p24fj128ga010.h>

This is a pseudo-instruction for the preprocessor telling the compiler to read the content of a device-specifi c fi le before proceeding any further. The content of the device-specifi c “.h” file chosen is a long list of the names (and sizes) of all the internal special-function registers (SFRs) of the chosen PIC model: those names reflect exactly those being used in the device datasheet. Let’s add a couple more lines that will introduce you to the main() function:

main()
{

}

The main() function is the place where the microcontroller (program counter) will go first at power-up or after each subsequent reset. Before entering the main() function, the microcontroller will execute a short initialization code segment automatically inserted by the linker. This is known as the c0 code. The c0 code will perform basic housekeeping chores, including the initialization of the microcontroller stack, among other things.