Tải bản đầy đủ (.pdf) (554 trang)

Programming 32 bit microcontrollers in c

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.34 MB, 554 trang )


Programming 32-bit
Microcontrollers in C
Exploring the PIC32


This page intentionally left blank


Programming 32-bit
Microcontrollers in C
Exploring the PIC32

Lucio Di Jasio

AMSTERDAM • BOSTON • HEIDELBERG • LONDON
NEW YORK • OXFORD • PARIS • SAN DIEGO
SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO
Newnes is an imprint of Elsevier


Newnes is an imprint of Elsevier
30 Corporate Drive, Suite 400, Burlington, MA 01803, USA
Linacre House, Jordan Hill, Oxford OX2 8DP, UK
Copyright © 2008, Elsevier Inc. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, electronic, mechanical, photocopying,
recording, or otherwise, without the prior written permission of the publisher.
Permissions may be sought directly from Elsevier’s Science & Technology Rights
Department in Oxford, UK: phone: (ϩ44) 1865 843830, fax: (ϩ44) 1865 853333,
E-mail: You may also complete your request online


via the Elsevier homepage (), by selecting “Support & Contact”
then “Copyright and Permission” and then “Obtaining Permissions.”
Recognizing the importance of preserving what has been written, Elsevier prints its
books on acid-free paper whenever possible.
Library of Congress Cataloging-in-Publication Data
Application submitted
British Library Cataloguing-in-Publication Data
A catalogue record for this book is available from the British Library.
ISBN: 978-0-7506-8709-6
For information on all Newnes publications
visit our Web site at www.books.elsevier.com
08 09 10 11 12 10 9 8 7 6 5 4 3 2 1
Typeset by Charon Tec Ltd (A Macmillan Company), Chennai, India
www.charontec.com
Printed in the United States of America


Dedicated to my son, Luca.


This page intentionally left blank


Acknowledgments

Once more this project would have never been possible if I did not have 110% support
from my wife Sara, who understands my passion(s) and constantly encourages me to
pursue them. Special thanks go to Steve Bowling and to Garry Champ. Their passion
and experience in embedded control application caused them to volunteer for reviewing
the technical content of this book. While Garry did not know what he was signing up

to, Steve should have known better having been my primary technical resource for the
previous book. I owe big thanks also to Patrick Johnson, who enthusiastically supported
this book idea from the very beginning and pulled all the stops to make sure that I would
be able to work in direct contact with his most advanced design and application teams
working on the PIC32 project. Thanks to Joe Triece, “the architect”, for being always
available to me and always curious about my experiences and impressions. Thanks to
Joe Drzewiecky for assembling such a complex tool suite, always working hard to make
MPLAB© IDE a better tool. Special thanks also go to the entire PIC32 application team
headed by Nilesh Rajbharti and a special mention to Adrian Aur, Dennis Lehman, Larry
Gass and Chris Smith for addressing quickly all my questions and offering so much help
and insight into the inner workings of the microcontroller, the peripherals and its libraries.
But I would like to extend my gratitude to all my friends, the colleagues at Microchip
Technology and the many embedded control engineers I have been honored to work with
over the years. You have so profoundly influenced my work and shaped my experience in
the fantastic world of embedded control.
Finally, since the publication of my previous book on Programming 16-bit
microcontrollers in C, I have received so much feedback and so many readers have
written to me to congratulate but also to point out errors and issues. This has been a very
humbling but also rewarding experience and I want to thank you all. I tried to incorporate
as many of your suggestions as possible in this new work but I am still looking for your
continued support and advice.


This page intentionally left blank


Contents

Introduction ................................................................................................ xix
Part 1: Exploring ...................................................................................................... 1


Day 1: The Adventure Begins .......................................................................... 3
The Plan .........................................................................................................................3
Preparation ....................................................................................................................3
The Adventure Begins ...................................................................................................6
Compiling and Linking .................................................................................................8
The Linker Script.........................................................................................................10
Building the First Project ............................................................................................11
Using the Simulator .....................................................................................................12
Finding a Direction ......................................................................................................14
The JTAG Port .............................................................................................................16
Testing PORTB............................................................................................................17
Mission Debriefing ......................................................................................................19
Notes for the Assembly Experts ..................................................................................20
Notes for the PIC MCU Experts..................................................................................22
Notes for the C Experts ...............................................................................................22
Tips & Tricks ...............................................................................................................22
Exercises ......................................................................................................................23
Books ...........................................................................................................................24
Links ............................................................................................................................24

Day 2: Walking in Circles ............................................................................. 25
The Plan .......................................................................................................................25
Preparation...................................................................................................................25
The Exploration ...........................................................................................................27
While Loops ................................................................................................................28


x


Contents
An Animated Simulation .............................................................................................31
Using the Logic Analyzer............................................................................................35
Debriefing ....................................................................................................................37
Notes for the Assembly Experts ..................................................................................38
Notes for the 8-Bit PIC Microcontroller Experts ........................................................38
Notes for the 16-Bit PIC Microcontroller Experts ......................................................38
Notes for the C Experts ...............................................................................................39
Notes for the MIPS Experts.........................................................................................39
Tips & Tricks ...............................................................................................................39
Notes on Using the Peripheral Libraries .....................................................................40
Exercises ......................................................................................................................42
Books ...........................................................................................................................42
Links ............................................................................................................................42

Day 3: Message in a Bottle ........................................................................... 43
The Plan .......................................................................................................................43
Preparation...................................................................................................................43
The Exploration ...........................................................................................................43
Do Loops .....................................................................................................................44
Variable Declarations ..................................................................................................45
for Loops ...................................................................................................................47
More Loop Examples ..................................................................................................48
Arrays ..........................................................................................................................49
Sending a Message ......................................................................................................50
Testing with the Logic Analyzer .................................................................................53
Testing with the Explorer 16 Demonstration Board ....................................................54
Testing with the PIC32 Starter Kit ..............................................................................55
Debriefing ....................................................................................................................57
Notes for the Assembly Experts ..................................................................................57

Notes for the PIC Microcontroller Experts .................................................................58
Notes for the C Experts ...............................................................................................58
Tips & Tricks ...............................................................................................................59
Exercises ......................................................................................................................60
Books ...........................................................................................................................60
Links ............................................................................................................................60

Day 4: NUMB3RS ...................................................................................... 61
The Plan .......................................................................................................................61
Preparation...................................................................................................................61
The Exploration ...........................................................................................................61
On Optimizations (or Lack Thereof) ...........................................................................64
Testing .........................................................................................................................64


Contents

xi

Going long long ....................................................................................................65
Integer Divisions .........................................................................................................67
Floating Point ..............................................................................................................69
Measuring Performance ..............................................................................................70
Debriefing ....................................................................................................................73
Notes for the Assembly Experts ..................................................................................73
Notes for the 8-Bit PIC® Microcontroller Experts ......................................................75
Notes for the 16-Bit PIC and dsPIC® Microcontroller Experts ..................................76
Tips & Tricks ...............................................................................................................77
Exercises ......................................................................................................................78
Books ...........................................................................................................................79

Links ............................................................................................................................79

Day 5: Interrupts......................................................................................... 81
The Plan .......................................................................................................................81
Preparation...................................................................................................................81
The Exploration ...........................................................................................................81
Interrupts and Exceptions ............................................................................................82
Sources of Interrupt .....................................................................................................84
Interrupt Priorities .......................................................................................................85
Interrupt Handlers Declaration ....................................................................................88
The Interrupt Management Library .............................................................................90
Single Vector Interrupt Management ..........................................................................90
Managing Multiple Interrupts .....................................................................................95
Multivectored Interrupt Management..........................................................................98
A Simple Application ................................................................................................103
The Secondary Oscillator ..........................................................................................108
The Real-Time Clock Calendar (RTCC) ...................................................................109
Debriefing ..................................................................................................................111
Notes for the PIC Microcontroller Experts ...............................................................111
Tips & Tricks .............................................................................................................112
Exercises ....................................................................................................................113
Books .........................................................................................................................113
Links ..........................................................................................................................113

Day 6: Memory ......................................................................................... 115
The Plan .....................................................................................................................115
Preparation.................................................................................................................115
The Exploration .........................................................................................................116
Memory Space Allocation .........................................................................................118
Looking at the MAP ..................................................................................................123

Pointers ......................................................................................................................127


xii

Contents
The Heap ...................................................................................................................128
The PIC32MX Bus ....................................................................................................129
PIC32MX Memory Mapping ....................................................................................130
The Embedded-Control Memory Map ......................................................................134
Debriefing ..................................................................................................................135
Notes for the C Experts .............................................................................................135
Notes for the Assembly Experts ................................................................................136
Notes for the PIC Microcontroller Experts ...............................................................136
Tips & Tricks .............................................................................................................137
Exercises ....................................................................................................................137
Books .........................................................................................................................138
Links ..........................................................................................................................138

Part 2: Experimenting ........................................................................................... 139
Day 7: Running .................................................................................................... 141
The Plan .....................................................................................................................141
Preparation.................................................................................................................141
The Exploration .........................................................................................................142
Performance vs. Power Consumption .......................................................................144
The Primary Oscillator Clock Chain .........................................................................146
The Peripheral Bus Clock..........................................................................................147
Initial Device Configuration ......................................................................................148
Setting Configuration Bits in Code ...........................................................................150
Heavy Stuff ................................................................................................................152

Ready, Set, Go! ..........................................................................................................158
Fine-Tuning the PIC32: Configuring Flash Wait States ............................................160
Fine-Tuning the PIC32: Enabling the Instruction and Data Cache ...........................163
Fine-Tuning the PIC32: Enabling the Instruction Pre-Fetch .....................................164
Fine-Tuning the PIC32: Final Notes .........................................................................165
Debriefing ..................................................................................................................167
Notes for the Assembly Experts ................................................................................167
Notes for the PIC® Microcontroller Experts .............................................................167
Tips & Tricks .............................................................................................................168
Exercises ....................................................................................................................171
Books .........................................................................................................................171
Links ..........................................................................................................................171

Day 8: Communication ............................................................................... 173
The Plan .....................................................................................................................173
Preparation.................................................................................................................173
The Exploration .........................................................................................................174


Contents

xiii

Synchronous Serial Interfaces ...................................................................................174
Asynchronous Serial Interfaces .................................................................................176
Parallel Interfaces ......................................................................................................177
Synchronous Communication Using the SPI Modules .............................................178
Testing the Read Status Register Command .............................................................182
Writing Data to the EEPROM ...................................................................................186
Reading the Memory Contents ..................................................................................187

A 32-bit Serial EEPROM Library .............................................................................187
Testing the New SEE Library ....................................................................................191
Debriefing ..................................................................................................................193
Notes for the C Experts .............................................................................................193
Notes for the Explorer 16 Experts .............................................................................193
Notes for the PIC24 Experts ......................................................................................194
Tips & Tricks .............................................................................................................194
Exercises ....................................................................................................................195
Books .........................................................................................................................196
Links ..........................................................................................................................196

Day 9: Asynchronous Communication .......................................................... 197
The Plan .....................................................................................................................197
Preparation.................................................................................................................197
The Exploration .........................................................................................................197
UART Configuration .................................................................................................200
Sending and Receiving Data .....................................................................................202
Testing the Serial Communication Routines .............................................................204
Building a Simple Console Library ...........................................................................206
Testing a VT100 Terminal .........................................................................................209
The Serial Port as a Debugging Tool .........................................................................211
The Matrix Project.....................................................................................................211
Debriefing ..................................................................................................................214
Notes for the C Experts .............................................................................................214
Notes for the PIC® Microcontroller Experts .............................................................215
Tips & Tricks .............................................................................................................215
Exercises ....................................................................................................................216
Books .........................................................................................................................216
Links ..........................................................................................................................217


Day 10: Glass ‫ ؍‬Bliss ................................................................................ 219
The Plan .....................................................................................................................219
Preparation.................................................................................................................219
The Exploration .........................................................................................................219
HD44780 Controller Compatibility...........................................................................221


xiv

Contents
The Parallel Master Port ............................................................................................223
Configuring the PMP for LCD Module Control .......................................................224
A Small Library of Functions to Access an LCD Display ........................................225
Building an LCD Library and Using the PMP Library .............................................231
Creating the include and lib Directories ....................................................................237
Advanced LCD Control.............................................................................................240
Progress Bar Project ..................................................................................................241
Debriefing ..................................................................................................................245
Notes for the PIC24 Experts ......................................................................................245
Tips & Tricks .............................................................................................................246
Exercises ....................................................................................................................246
Books .........................................................................................................................247
Links ..........................................................................................................................247

Day 11: It’s an Analog World ..................................................................... 249
The Plan .....................................................................................................................249
Preparation.................................................................................................................249
The Exploration .........................................................................................................249
The First Conversion .................................................................................................253
Automating Sampling Timing ...................................................................................254

Developing a Demo ...................................................................................................255
Creating Our Own Mini ADC Library ......................................................................257
Fun and Games ..........................................................................................................258
Sensing Temperature .................................................................................................261
Debriefing ..................................................................................................................266
Notes for the PIC24 Experts ......................................................................................266
Tips & Tricks .............................................................................................................267
Exercises ....................................................................................................................267
Books .........................................................................................................................268
Links ..........................................................................................................................268

Part 3: Expansion ............................................................................................... 269
Day 12: Capturing User Inputs ................................................................... 271
The Plan .....................................................................................................................271
Preparation.................................................................................................................271
Buttons and Mechanical Switches .............................................................................272
Button Input Packing .................................................................................................275
Button Inputs Debouncing.........................................................................................277
Rotary Encoders ........................................................................................................280
Interrupt-Driven Rotary Encoder Input .....................................................................283
Keyboards ..................................................................................................................288
PS/2 Physical Interface ..............................................................................................288


Contents

xv

The PS/2 Communication Protocol ...........................................................................289
Interfacing the PIC32 to the PS/2 ..............................................................................290

Input Capture .............................................................................................................290
Testing Using a Stimulus Scripts...............................................................................296
The Simulator Profiler ...............................................................................................301
Change Notification...................................................................................................302
Evaluating Cost .........................................................................................................308
I/O Polling .................................................................................................................309
Testing the I/O Polling Method .................................................................................314
Cost and Efficiency Considerations ..........................................................................317
Keyboard Buffering ...................................................................................................319
Key Code Decoding ..................................................................................................324
Debriefing ..................................................................................................................328
Notes for the PIC24 Experts ......................................................................................329
Tips & Tricks .............................................................................................................329
Exercises ....................................................................................................................330
Books .........................................................................................................................330
Links ..........................................................................................................................331

Day 13: UTube ......................................................................................... 333
The Plan .....................................................................................................................333
Preparation.................................................................................................................333
The Exploration .........................................................................................................334
Generating the Composite Video Signal ...................................................................337
The Output Compare Modules ..................................................................................342
Image Buffers ............................................................................................................345
Serialization, DMA, and Synchronization ................................................................346
Completing a Video Library ......................................................................................353
Testing the Composite Video.....................................................................................357
Measuring Performance ............................................................................................360
Seeing the Dark Screen .............................................................................................360
Test Pattern ................................................................................................................362

Plotting ......................................................................................................................364
A Starry Night ...........................................................................................................366
Line Drawing .............................................................................................................368
Bresenham Algorithm ...............................................................................................370
Plotting Math Functions ............................................................................................373
Two-Dimensional Function Visualization .................................................................376
Fractals ......................................................................................................................381
Text ............................................................................................................................389
Printing Text on Video...............................................................................................391
Text Test ....................................................................................................................394


xvi

Contents
The Matrix Reloaded .................................................................................................395
Debriefing ..................................................................................................................398
Notes for the PIC24 Experts ......................................................................................399
Tips & Tricks .............................................................................................................399
Exercises ....................................................................................................................401
Books .........................................................................................................................402
Links ..........................................................................................................................402

Day 14: Mass Storage................................................................................ 403
The Plan .....................................................................................................................403
Preparation.................................................................................................................403
The Exploration .........................................................................................................404
The Physical Interface ...............................................................................................405
Interfacing to the Explorer 16 Board .........................................................................406
Starting a New Project ...............................................................................................407

Selecting the SPI Mode of Operation ........................................................................408
Sending Commands in SPI Mode .............................................................................408
Completing the SD Card Initialization ......................................................................411
Reading Data from an SD/MMC Card ......................................................................413
Writing Data to an SD/MMC Card ...........................................................................416
Testing the SD/MMC Interface .................................................................................419
Debriefing ..................................................................................................................424
Tips & Tricks .............................................................................................................425
Exercises ....................................................................................................................426
Books .........................................................................................................................426
Links ..........................................................................................................................426
Day 15: File I/O................................................................................................... 427
The Plan .....................................................................................................................427
Preparation.................................................................................................................427
The Exploration .........................................................................................................428
Sectors and Clusters ..................................................................................................428
The File Allocation Table ..........................................................................................429
The Root Directory ....................................................................................................430
The Treasure Hunt .....................................................................................................433
Opening a File ...........................................................................................................444
Reading Data from a File ..........................................................................................454
Closing a File.............................................................................................................459
The Fileio Module .....................................................................................................460
Testing fopenM() and freadM() ........................................................................463
Writing Data to a File ................................................................................................465
Closing a File, Take Two ...........................................................................................471


Contents


xvii

Accessory Functions..................................................................................................473
Testing the Complete Fileio Module .........................................................................476
Code Size ...................................................................................................................480
Debriefing ..................................................................................................................481
Tips & Tricks .............................................................................................................481
Exercises ....................................................................................................................482
Books .........................................................................................................................482
Links ..........................................................................................................................483

Day 16: Musica, Maestro!.......................................................................... 485
The Plan .....................................................................................................................485
Preparation.................................................................................................................485
The Exploration .........................................................................................................486
OC PWM Mode.........................................................................................................488
Testing the PWM as a D/A Converter .......................................................................490
Producing Analog Waveforms...................................................................................492
Reproducing Voice Messages ....................................................................................497
A Media Player ..........................................................................................................498
The WAVE File Format .............................................................................................500
The Play() Function ..............................................................................................501
The Audio Routines...................................................................................................510
A Simple WAVE File Player .....................................................................................513
Debriefing ..................................................................................................................515
Tips & Tricks .............................................................................................................516
Exercises ....................................................................................................................516
Books .........................................................................................................................516
Links ..........................................................................................................................517
Disclaimer .................................................................................................................517

Final Note for the Experts .........................................................................................517

Index .................................................................................................................. 519


This page intentionally left blank


Introduction

The first step in almost every rehabilitation program is A- Acknowledge . . . your
limitations. So this is how I need to start this book, I will admit it: I am an
8-bitter!
I have been programming 8-bit microcontrollers since I was in high school and for most
of my professional career. And there is worse, while I am relatively fluent in several high
level programming languages, I truly love assembly programming!
There, I said it! I love that kick that I get when I know I used every single machine cycle
in every microsecond my embedded applications run. I am also obsessed with control:
I like to know of every configuration bit in every peripheral I use. As a consequence,
in general, I don’t trust compilers or other people’s libraries unless I really cannot live
without them or I have them completely disassembled.
So why would I write a book about 32-bit programming in C?
In fact I started what I should call my “rehabilitation program” a couple of years ago
by approaching the programming of 16-bit microcontrollers first. The introduction of
the PIC24 family of microcontrollers gave me the motivation to try and migrate to C
programming with a new and exciting architecture. As a result of my experience, I wrote
the first book: “Programming 16-bit microcontrollers in C. Learning to fly the PIC24”.
But by the time the book was published, rumors circulated in Microchip that a new 32-bit
chip had just come out of the “ovens” and I had to have one!
I’ll spare you the details of how I got my hands around one of the very first test chips,

but what you need to know is that in a matter of days I had most of the code, originally
developed for the PIC24 book, ported and running on the PIC32 plugged in my old
Explorer16 board.


xx

Introduction

Microchip marketing folks will tell you that the PIC32 architecture was specifically
designed so to make the “migration” from 8-bit and 16-bit PIC architectures smooth and
seamless, but I had to see it with my eyes to believe it.
So who better than an assembly-loving, control-obsessed, 8-bitter can tell you about the
exploration of the PIC32?

Who Should Read this Book?
The PIC32 turns out to be a remarkably easy to use device, but nonetheless, it is a truly
powerful machine based on a well established 32-bit core (MIPS) and supported by a
large number of tools, libraries and documentation. This book can only offer you a small
glimpse into such a vast world and in fact I call it a first “exploration”. It is my strong
belief that learning should be fun, and I hope you will have a good time with some
of the “playful” exercises and projects I present throughout each chapter in the book.
However you will need quite some preparation and hard work in order to be able to digest
the material I am presenting at a pace that will accelerate rapidly through the first few
chapters.
This book is meant for programmers of a basic to intermediate level of experience, but
not for “absolute” beginners; so don’t expect me to start with the basics of the binary
numbers, the hexadecimal notation or the fundamentals of programming. Although,
we will briefly review the basics of C programming as it relates to the applications for
the latest generation of general-purpose 32-bit microcontrollers, before moving on to

more challenging projects. My assumption is that you, the reader, belong to one of the
following four categories:




Embedded Control programmer: experienced in assembly-language microcontrollers programming, but with only a basic understanding of the C language.
PIC® microcontroller expert: with a basic understanding of the C language.



Student or professional: with some knowledge of C (or Cϩϩ) programming
for PCs.



Other SLF (superior life forms): I know programmers don’t like to be classified
that easily so I created this special category just for you!

Depending on your level and type of experience, you should be able to find something
of interest in every chapter. I worked hard to make sure that every one of them contained


Introduction

xxi

both C programming techniques and new hardware peripherals details. Should you already
be familiar with both, feel free to skip to the experts section at the end of the chapter, or
consider the additional exercises, book references and links for further research/reading.

A special note is reserved for those of you who have already read my previous book on
programming 16-bit microcontrollers in C. First of all let me thank you, then let me explain
why you will get a certain sensation of deja vu. No, I did not try to cheat my way through
the old 16-bit material to produce a new book, but I have re-produced most of the projects
to demonstrate practically the main claims of the PIC32 architecture and toolset: its
seamless migration from 8 and 16-bit PIC applications, the vastly increased performance
and nonetheless the great ease of use. For you, at the end of every chapter, I have included
a special section where I detail the differences encountered, the enhancements and other
information that will help you port your applications faster and with greater confidence.
These are some of the things you will learn:


The structure of an embedded-control C program: loops, loops and more loops



Basic timing and I/O operations



Basic embedded control multitasking in C, using the PIC32 interrupts



New PIC32 peripherals, in no specific order:
1. Input Capture
2. Output Compare
3. Change Notification
4. Parallel Master Port
5. Asynchronous Serial Communication

6. Synchronous Serial Communication
7. Analog-to-Digital conversion



How to control LCD displays



How to generate video signals



How to generate audio signals



How to access mass-storage media



How to share files on a mass-storage device with a PC


xxii

Introduction

Structure of the Book
Each chapter of the book is offered as a day of exploration in the 32-bit embedded

programming world. There are three parts. The first part contains six small chapters
of increasing levels of complexity. In each chapter, we will review one basic hardware
peripheral of the PIC32MX family of microcontrollers and one aspect of the C language,
using the MPLAB C32 compiler (Student Version included in the CD-ROM). In each
chapter, we will develop at least one demonstration project. Initially, such projects will
require exclusive use of the MPLAB SIM software simulator (a part of the MPLAB
toolsuite included in the CD-ROM), and no actual hardware will be necessary; although,
an Explorer 16 demonstration board or a PIC32 Starter kit might be used.
In the second part of the book, titled “Experimenting” and containing five more chapters,
an Explorer 16 demonstration board (or third-party equivalent) will become more critical,
as some of the peripherals used will require real hardware to be properly tested.
In the third part of the book, titled “Expansion”, there are five larger chapters. Each one
of them builds on the lessons learned in multiple previous chapters while adding new
peripherals to develop projects of greater complexity. The projects in the third part of the
book require the use of the Explorer 16 demonstration board and basic prototyping skills
too (yes, you might need to use a soldering iron). If you don’t want to or you don’t have
access to basic PCB prototyping tools, an ad hoc expansion board (AV32) containing all
the circuitry and components necessary to complete all the demonstration projects will be
made available on the companion web site:
All the source code developed in each chapter is also available for immediate use on the
companion CD-ROM.

What this Book is Not
This book is not a replacement for the PIC32 datasheet, reference manual and
programmer’s manual published by Microchip Technology. It is also not a replacement
for the MPLAB C32 compiler user’s guide, and all the libraries and related software tools
offered by Microchip. Copies are available on the companion CD-ROM, but I expect you
to download the most recent versions of all those documents and tools from Microchip’s
Web site (). Familiarize yourself with them and keep them
handy. I will often refer to them throughout the book, and I might present small block

diagrams and other excerpts here and there as necessary. But, my narration cannot replace


Introduction

xxiii

the information presented in the official manuals. Should you notice a conflict between
my narration and the official documentation, ALWAYS refer to the latter. However
please send me an email if a conflict arises, I will appreciate your help and I will publish
any correction and useful hint I will receive on the companion web site: http://www.
exploringpic32.com
This book is also not a primer on the C language. Although a review of the language is
performed throughout the first few chapters, the reader will find in the references several
suggestions on more complete introductory courses and books on the subject.

Checklists
Although this book is not directly making references to aviation and flight training
as my previous book was, I decided to maintain some important elements introduced
there.
The use of checklists to perform every single procedure before and during each project
is one of them. Pilots don’t use checklists because the procedures are too long to be
memorized or because they suffer from short memory problems. They use checklists
because it is proven that the human memory can fail, and tends to do so more often
when stress is involved. Pilots can perhaps afford less mistakes than other categories,
and they value safety above their pride. There is nothing really dangerous that you, as a
programmer can do or forget to do, while developing code for the PIC32. Nonetheless,
I have prepared a number of simple checklists to help you perform the most common
programming and debugging tasks. Hopefully, they will help you in the early stages,
when learning to use the new PIC32 toolset or later if you are, like most of us, alternating

between several projects and development environments from different vendors.


The Pilot Checklist – MPLAB® IDE Quick Start Guide
New Project Setup
ProjectϾProject Wizard
Step 1: Device
Step 2: ToolSuite
Step 3: NewProject dialog box
Folder
Project name
Step 4: Copy files
Step 5: Complete wizard

Start
PIC32MX360F512L
MPLAB C32 C Compiler
Select BROWSE
Select or create new
Type new name here
Only if necessary
Click on Finish

Manual Device Configuration (if not using pragmas)
ConfigureϾConfiguration Bits
Open window
Configuration bits set in ocde
Unchecked
ICE/ICD Comm channel select
ICE EMUC2/EMUD2 share with PGCD2

Boot Flash Write Protect
Boot Flash is writable
Code Protect
Protection Disabled
Oscillator Selection bits
Primary OSC with PLL (XT, HS, EC)
Secondary Oscillator Enable
Enabled
Internal External Switchover
Disabled
Primary Oscillator Configuration
XT osc mode
CLKO output signal active on OSCO Disabled
Peripheral Clock Divisor
PB clock is Sys clock/2
Clock Switching and Monitor
Disabled and clock monitor disabled
Watchdog Timer Postscaler
Any
Watchdog Timer Enable
Disabled
PLL Input Divider
2ϫ Divider
PLL Multiplier
18ϫ Multiplier
System PLL output clock divider
PLL Divide by 1
Create New File and Add to Project
ProjectϾAddNewProjectFile
FileϾOpen

if main source file & using pragmas
Header/comments
Add code
FileϾSave
ProjectϾSaveProject

Assign name (.c or .h)
Select “\c32\include\Template.c”
Select “\c32\include\Template wPragmas.c”
Copy
As needed
Select
Select

MPLAB SIM Debugger Setup
DebuggerϾSelect Tool
DebuggerϾSettings
1. Osc/Trace Tab
1.1 Processor Frequency
1.2 Trace Options
2. Animation/Real Time Updates
2.1 Animate Step
3. Apply/OK

Select MPLAB SIM
Select
Select
72 MHz
Trace All
Select Tab

Slow 500 ms/Fast 10 ms
Select

PIC32MX Family Characteristics
Vdd range
Digital input pins
Analog input pins

2.0 V to 3.6 V
5 V tolerant
0 V to 3.6 V max

MPLAB ICD2 In Circuit Debugger Setup
Target Board
Power Up
ICD2 to Target
Connect
ICD2 to PC
Connect (wait for triple ding-dong)
DebuggerϾSelectTool
Select MPLAB ICD2
DebuggerϾSettings
Select
1. Status Tab
Select
1.1 Automatically Connect
Verify NOT Checked
2. Power Tab
Select
2.1 Power target from ICD2

Verify NOT Checked
3. Program Tab
Select
3.1 Allow ICD2 to select ranges
Verify Checked
3.2 Program after successful build Select if desired (not recommended)
3.3 Run after successful program
Select if desired (not recommended)
OK button
Click
DebuggerϾConnect
Select
Emergency: USB Drivers Re-start (Debugger fails to connect)
DebuggerϾSelecTool
Select None
ProjectϾClose
Save Project and close
FileϾExit
Terminate MPLAB
USB cable
Disconnect
Target
Cycle Power
MPLAB
Launch
USB cable
Connect (wait for enumeration)
DebuggerϾSelecTool
Select Debugger model
DebuggerϾConnect

Select (not required for REAL ICE)
Emergency: Breakpoint Cannot Be Set (debugging)
1. Verify the C source code line is not commented
2. Verify you have not used more than six breakpoints (see breakpoints list F2)
3. Verify the C source line does not contain only a variable declaration
4. Verify the C source file is part of the Project Files list
5. Verify the project has been Built before placing a breakpoint
Explorer16 Demonstration Board
Power Supply
Main oscillator
Secondary oscillator

9 V to 15 V (reversed polarity protected)
8 MHz crystal (use 4ϫ PLL to obtain 32MHz )
32,768 Hz (connected to TMR1 oscillator)


×