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

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

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: http://www.exploringpic32.com

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 (http://www.microchip.com). 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 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.

image

image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.187