Difference between revisions of "Getting Started With PIC(TM) MCU"

From AIRWiki
Jump to: navigation, search
Line 1: Line 1:
!!!PAGE UNDER CONSTRUCTION!!!
+
!!!__PAGE UNDER CONSTRUCTION__!!!
  
  
Line 10: Line 10:
 
=== Software tools ===
 
=== Software tools ===
  
Let’s verify we have all the necessary pieces of equipment ready and installed (latest version
+
Let’s verify you have all the necessary equipments, ready and installed (latest version available for download from Microchip web site at
available for download from Microchip web site at http://www.microchip.com):
+
http://www.microchip.com):
  
 
* PIC MCU Datasheet;
 
* PIC MCU Datasheet;
 
* MPLAB IDE(TM), free Integrated Development Environment;
 
* MPLAB IDE(TM), free Integrated Development Environment;
* MPLAB SIM(TM), software simulator;
 
 
* MPLAB C30(TM), C compiler (demo or free student version).
 
* MPLAB C30(TM), C compiler (demo or free student version).
  
Line 21: Line 20:
 
== '''Part 2: Basic hardware''' ==
 
== '''Part 2: Basic hardware''' ==
  
Besides a PC, because of we intend "make" something and not only talk about it, we need some
+
Besides a PC, because of we intend "make" something and not only talk about it, we need some hardware tools:
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 programmer-debugger (in Ai&R Lab Lambrate you can find some Microchip ICD2(TM) programmer-debugger, 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.
 
* PIC MCU demoboard or your board with power supply, PIC Microconroller, oscillator, MCLR circuitry and some input-output hardware.
 +
 +
If you are interested in buyng a programmer/debugger, Microchip PIC-KIT could be the right choise due to it's little dimensions, low price and good
 +
performances. For detailed information, including PIC microcontroller supported list, visit microchip web site.
  
  
 
=== 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).
+
The system clock (FOSC) source can be provided by a different number of options, depending by microcontroller family (low power, low cost microcontroller
Generally options can be divided in:
+
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).
+
* 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]]
 
[[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
+
In addition, oscillator system can includes on-chip Phase-Locked Loop (PLL) to boost internal operating frequency, on-the-fly clock switching between
detects clock failure and permits safe application recovery or shutdown.
+
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
+
This means, for example, that with a PIC24H series microcontoller, you can use a 10Mhz external crystal resonator for clock source, but configuring
internal clock frequency, achieving 40MIPS core operation speed (PIC24H need two clock cycles to execute a single instruction).
+
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 ===
 
=== MCLR circuit ===
  
PIC Microcontroller has a number of reset sources and controls (POR: Power-on Reset; BOR: Brown-out Reset; MCLR: Master Clear Pin Reset; WDTO: Watchdog Timer Reset).
+
PIC Microcontroller has a number of reset sources and controls (POR: Power-on Reset; BOR: Brown-out Reset; MCLR: Master Clear Pin Reset; WDTO: Watchdog
The external reset is generated by driving the MCLR pin low. The MCLR pin is a Schmitt trigger input with an additional glitch filter. Reset pulses that are longer than the minimum pulse width
+
Timer Reset).The external reset is generated by driving the MCLR pin low. The MCLR pin is a Schmitt trigger input with an additional glitch filter.
generates a Reset (see datasheet). The External Reset (MCLR) Pin (EXTR) bit in the Reset Control (RCON) register is set to indicate the MCLR Reset.
+
Reset pulses that are longer than the minimum pulse width generates a Reset (see datasheet). The External Reset (MCLR) Pin (EXTR) bit in the Reset
If your system has an external supervisory circuits that generate reset signals to Reset multiple devices in the system, this external Reset signal can be directly connected to the MCLR pin to
+
Control (RCON) register is set to indicate the MCLR Reset.If your system has an external supervisory circuits that generate reset signals to Reset
 +
multiple devices in the system, this external Reset signal can be directly connected to the MCLR pin to
 
reset the device when the rest of system is Reset.
 
reset the device when the rest of system is Reset.
When using the internal power supervisory circuit to Reset the device, the external reset pin (MCLR) should be tied directly or resistively to VDD. In this case, the MCLR pin is not used to
+
When using the internal power supervisory circuit to Reset the device, the external reset pin (MCLR) should be tied directly or resistively to VDD.
generate a Reset. The external reset pin (MCLR) does not have an internal pull-up and must not be left unconnected.
+
In this case, the MCLR pin is not used to generate a Reset. The external reset pin (MCLR) does not have an internal pull-up and must not be left
A typical power-on reset circuit is:
+
unconnected. A typical power-on reset circuit is:
  
 
[[Image:MCUMCLR.jpg]]
 
[[Image:MCUMCLR.jpg]]
  
  
=== Programming interface ===
+
=== Programming/Debugging interface ===
  
 +
ICSP(TM) (In-Circuit Serial Programming(TM)) mode is a special programming protocol that allows you to read and write to PIC Microcontroller and is the
 +
most direct method used to program the device.The ICSP capability is Microchip’s proprietary process for microcontroller programming in the target
 +
application. The ICSP interface uses two pins as its core.
 +
The programming data pin (PGD) functions as both an input and an output, allowing programming data to be read in and device information to be read out
 +
on command.The programming clock pin (PGC) clocks in data and controls the overall process.
  
=== Programming Cable ===
+
Serial programming allows customers to manufacture boards with unprogrammed devices and then to program the digital signal controller just before shipping
 +
the product. Serial programming also allows the most recent firmware or a custom firmware to be programmed.
 +
 
 +
Depending from used Microcontroller it's possible to have one or more programming ports. PIC24HJ256JP206, for example, has three pairs of programming
 +
clock/data pins:
 +
 
 +
* PGC1/EMUC1 and PGD1/EMUD1
 +
* PGC2/EMUC2 and PGD2/EMUD2
 +
* PGC3/EMUC3 and PGD3/EMUD3
 +
 
 +
Used programming port must be specified in MPLAB IDE selecting "Configuration Bits..." in "Configure" menu.
 +
The High-Level ICSP Programming Flow is:
 +
 
 +
[[Image:HL_ICSP_PROGRAMMING_FLOW.jpg]]
 +
 
 +
When the MPLAB(TM) ICD 2 or another in-circuit emulator is selected as a debugger,the in-circuit debugging functionality is enabled.
 +
This function allows simple debugging when used with MPLAB IDE. The debugging functionality is controlled through the EMUCx (emulation/debug clock) and
 +
EMUDx (emulation/debug data) pin functions. To use the in-circuit debugger function of the device, the design must implement ICSP connections to MCLR,
 +
VDD, VSS, PGCx/EMCx and PGDx/EMUDx pin pairs. In addition, when the feature is enabled, some of the resources are not available for general use.
 +
 
 +
 
 +
=== Programmer Connection ===
 +
 
 +
ICD 2 programmer/debugger has a RJ12 6 pin output connector while PIC-KIT has a more comfortable 6 pin female strip headers.
 +
The simplest way to interface programmer/debugger with microcontroller is to solder a 6 pin male strip headers to your board connetting PGCX, PGDX, MCLR,
 +
VDD and VSS pins, as in figure:
 +
 
 +
[[Image:ICSP_Connector.jpg]]
 +
 
 +
Connecting pins in the same order as in figure you can either connect directly PIC-KIT to your board, or use an RJ12->female_strip_headers adapter cable to
 +
connect ICD 2 to target device. You can find two of that selfmade cabe in AI&R Lab, or you can build your own one using an RJ12 connector, a piece of 6 core
 +
cable (you can use also a piece of 8 core ethernet cable), a 6 pin female strip headers and a bit of talent!
  
  
Line 73: Line 114:
 
Then, let’s follow the “New Project Set-up” checklist to create a new project with the MPLAB IDE:
 
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
+
Select “Project->Project Wizard” to activate the new project wizard, which will guide us automatically through the following steps…
automatically through the following steps…
+
 
* Select the correct PIC device, and click Next.
+
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.
+
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
 
Simply click Next to the following dialog box—there is no need to copy any source files from
Line 92: Line 135:
  
 
This is a pseudo-instruction for the preprocessor telling the compiler to read the content of a
 
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”
+
device-specifi c file 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.
 
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:
 
Let’s add a couple more lines that will introduce you to the main() function:

Revision as of 14:16, 7 November 2009

!!!__PAGE UNDER CONSTRUCTION__!!!


Part 1: Introduction

Chosing a PIC(TM) Microcontroller

Software tools

Let’s verify you have all the necessary equipments, 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 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, 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.

If you are interested in buyng a programmer/debugger, Microchip PIC-KIT could be the right choise due to it's little dimensions, low price and good performances. For detailed information, including PIC microcontroller supported list, visit microchip web site.


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

PIC Microcontroller has a number of reset sources and controls (POR: Power-on Reset; BOR: Brown-out Reset; MCLR: Master Clear Pin Reset; WDTO: Watchdog Timer Reset).The external reset is generated by driving the MCLR pin low. The MCLR pin is a Schmitt trigger input with an additional glitch filter. Reset pulses that are longer than the minimum pulse width generates a Reset (see datasheet). The External Reset (MCLR) Pin (EXTR) bit in the Reset Control (RCON) register is set to indicate the MCLR Reset.If your system has an external supervisory circuits that generate reset signals to Reset multiple devices in the system, this external Reset signal can be directly connected to the MCLR pin to reset the device when the rest of system is Reset. When using the internal power supervisory circuit to Reset the device, the external reset pin (MCLR) should be tied directly or resistively to VDD. In this case, the MCLR pin is not used to generate a Reset. The external reset pin (MCLR) does not have an internal pull-up and must not be left unconnected. A typical power-on reset circuit is:

MCUMCLR.jpg


Programming/Debugging interface

ICSP(TM) (In-Circuit Serial Programming(TM)) mode is a special programming protocol that allows you to read and write to PIC Microcontroller and is the most direct method used to program the device.The ICSP capability is Microchip’s proprietary process for microcontroller programming in the target application. The ICSP interface uses two pins as its core. The programming data pin (PGD) functions as both an input and an output, allowing programming data to be read in and device information to be read out on command.The programming clock pin (PGC) clocks in data and controls the overall process.

Serial programming allows customers to manufacture boards with unprogrammed devices and then to program the digital signal controller just before shipping the product. Serial programming also allows the most recent firmware or a custom firmware to be programmed.

Depending from used Microcontroller it's possible to have one or more programming ports. PIC24HJ256JP206, for example, has three pairs of programming clock/data pins:

  • PGC1/EMUC1 and PGD1/EMUD1
  • PGC2/EMUC2 and PGD2/EMUD2
  • PGC3/EMUC3 and PGD3/EMUD3

Used programming port must be specified in MPLAB IDE selecting "Configuration Bits..." in "Configure" menu. The High-Level ICSP Programming Flow is:

HL ICSP PROGRAMMING FLOW.jpg

When the MPLAB(TM) ICD 2 or another in-circuit emulator is selected as a debugger,the in-circuit debugging functionality is enabled. This function allows simple debugging when used with MPLAB IDE. The debugging functionality is controlled through the EMUCx (emulation/debug clock) and EMUDx (emulation/debug data) pin functions. To use the in-circuit debugger function of the device, the design must implement ICSP connections to MCLR, VDD, VSS, PGCx/EMCx and PGDx/EMUDx pin pairs. In addition, when the feature is enabled, some of the resources are not available for general use.


Programmer Connection

ICD 2 programmer/debugger has a RJ12 6 pin output connector while PIC-KIT has a more comfortable 6 pin female strip headers. The simplest way to interface programmer/debugger with microcontroller is to solder a 6 pin male strip headers to your board connetting PGCX, PGDX, MCLR, VDD and VSS pins, as in figure:

ICSP Connector.jpg

Connecting pins in the same order as in figure you can either connect directly PIC-KIT to your board, or use an RJ12->female_strip_headers adapter cable to connect ICD 2 to target device. You can find two of that selfmade cabe in AI&R Lab, or you can build your own one using an RJ12 connector, a piece of 6 core cable (you can use also a piece of 8 core ethernet cable), a 6 pin female strip headers and a bit of talent!


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 file 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.