Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Getting Started with MPLAB for Mechatronics Projects using PIC18 Devices, Study Guides, Projects, Research of Mechanical Engineering

An introduction to using mplab for programming pic16 and pic18 devices, specifically the picdem 2 plus demo board with the mplab icd 2 debugger/programmer. It covers setting up a project in mplab, accessing special function registers (sfrs) and configuration bits using syntax and #pragma commands.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 08/19/2009

koofers-user-4h1
koofers-user-4h1 🇺🇸

5

(1)

10 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Mechatronics
Introduction to using MPLAB for programming pics
So I thought I’d borrow some graphics from the Microchip folks. For the introduction material
we’ll be using the PICDEM 2 Plus demo board with the MPLAB ICD 2 debugger/programmer.
MPLAB®-ICD 2 and PICDEM™ 2 Plus
All labs will use the ICD 2 and PICDEM 2 Plus
ICD2 In-circuit programmer and debugger
PICDEM 2 Plus demo board for most PIC16/18 devices
USB
ICSP
over
RJ11
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Getting Started with MPLAB for Mechatronics Projects using PIC18 Devices and more Study Guides, Projects, Research Mechanical Engineering in PDF only on Docsity!

Mechatronics

Introduction to using MPLAB for programming pics

So I thought I’d borrow some graphics from the Microchip folks. For the introduction material we’ll be using the PICDEM 2 Plus demo board with the MPLAB ICD 2 debugger/programmer.

MPLAB ®^ -ICD 2 and PICDEM™ 2 Plus

● All labs will use the ICD 2 and PICDEM 2 Plus

쟙 ICD2 In-circuit programmer and debugger

쟙 PICDEM 2 Plus demo board for most PIC16/18 devices

USB

ICSP over RJ

We’ll use both the PIC16 and PIC18 family of devices. We’ll start with the PIC18 family using the C18 compiler. It’s a good way to program in C and it’s free for students.

MPLAB C18 Compiler

● PIC18 only

● Targets ANSI:1989 standard

● Standard C functions

● Run time libraries

● $495US – One-time cost.

● Student Version ($free):

쟙 www.microchip.com/c

쟙 After 60 days:

● Optimizations disabled

● Extended mode disabled

Another type of common hardware settings are the configuration bits. We’ll be using this crazy #pragma thing a lot. Basically when Microchip needed a command that isn’t a real C command they say #pragma then do something important for microcontrollers that isn’t part of the default C language. There are a few special times whne we’ll need #pragma. There are just a few things you need to do that C can’t do without a few #pragma ‘s. We’ll talk more about these later, but I thought I’d inclue this little blurb here. :

Configuration Settings Support

● Language extension to specify hardware

configuration settings

#pragma config

● Used to specify “one-time” setting of

hardware resources

쟙 Ex. watchdog timer

Configuration Settings Support

● Example

#pragma config WDT = ON

쟙 Enables watchdog timer

● Configuration bits and available settings

documented in PIC18 Configuration

Settings Addendum (DS51537E)

To learn more about what configuration bits are available you can see a lot of useful things in the help section in MPLAB. Click on Help -> Topics -> Pic18 Config Settings:

To find the settings for your PIC go to the pic18F4520 by expanding the options and finding that number.

You can see the file I selected to the left and you can see the Oscillator Selection bits. We’ll use EC some on the PICDEM 2 board but mainly we’ll use the INTIO67. That setting uses the INTer- nal clock. The internal clock controls how fast the PIC goes through code. The internal clock can go as fast as 8 MHz and as slow as 32kHz. The default is 1MHz.

Let’s talk about the nuts and bolts of setting up a project in MPLAB:

for the program before I even start MPLAB. A workspace typically consist of a minimum of like 10 files.

Most of these files are used within the program and you won’t even open them but they are part of the workspace. I could explain some of them but I don’t really know the details for all of them. To be honest you don’t need to know what they are. They happen behing the scenes. You will notice the .mcw Workspace file, which you’ll open and the .mcp Project file that gets openned automatically within the Workspace.

Most of the work you do will be on the .c files.

Later after you have made a workspace and you want to open it back up, you’ll launch MPLAB and Open Workspace... When you open the workspace everything comes with it. No need to open individual files yadayadayada. Just one thing to open.

Project Wizard. So I usually create my folder in advance then come into MPLAB. The first step within MPLAB is to start with the Project Wizard...

Files Files are the basic components of any program. You’ll have a main source file (.c) that has you main function and primary code. For simple programs you’ll only have 1 source file. For more complex programs you’ll have a .c for you main function then various additional header files and source files for support functions. This is a method for modular programming that we’ll get into later. The files are what you get when you just say New... or Open..(kinda makes since that if you go to File->New you’ll get a new file).

So a file without a project doesn’t really do you any good. Just saying File->New then typing in some code is fun but a file without a project really isn’t all that useful. You’ll spend 99% of your time within a file (usually a .c file), but just know that you’ll need to create more than a file to do anything useful.

Name The New Project

Select MPLAB C18 Toolsuite

Note: mplink.exe , mcc18.exe & mplib.exe are located

in c:\mcc18\bin (default C18 install)

Add Header File(s) to Project

Right-Click on ‘Header Files’ÆAdd Files

Header file: c:\mcc18\h\p18f4520.h

Add Linker Script To Project

Right-Click on ‘Linker Scripts’ÆAdd Files

Linker File: c:\mcc18\lkr\18f4520i.lkr

Set Include & LIB Search Paths

Select The Programmer (ICD2)

Connect to the ProgrammerConnect to the Programmer

Successful Connection

So that’s the basic format for starting a new projects. You’ll do it a lot so get used to it. The first time is the worse time. You only need to add the vairous locations of the toolsuites once.

Program The Device