Chapter 2: Development Tools

advertisement
Chapter 2: Development Tools
The process of developing a new embedded microcontroller application generally
involves:






Design and build the prototype circuit
Using a standard PC, write the code that will be run by the microcontroller in your
circuit.
Compile or assemble the code into a file (with extension .HEX) that can be loaded
into the microcontroller
Load the code into the microcontroller
Apply power and allow the microcontroller to run your code, and test to make
sure whether it works properly.
Debug the code or the prototype circuit or both.
As in the case of more traditional computer programming, there is a cycle of writing
code, compiling or assembling it, testing it, and debugging the results that iterates until
the application finally runs as expected. There are a couple of differences with
microcontrollers that make the process somewhat more difficult.
First, before the code can be tested, it is necessary to load it into the microcontroller and
test it in the prototype circuit. This used to be a very significant issue because
microcontrollers used to be based on EPROM technology that required that each time a
microcontroller was going to be reprogrammed it had to be placed in a ultra-violet light
eraser for 3-4 minutes. Most microcontrollers are now based on flash technology that
allows for very fast rewriting of the chip without erasing it first.
Second, if the prototype circuit doesn’t behave as expected, you have to determine
whether the problem lies with the code that you have written or the prototype circuit that
you have designed. Very often, even after the undesirable behavior has been isolated, it
isn’t clear whether the problem lies in hardware or software. Working with
microcontrollers can do wonders to improve a programmer’s debugging skills, because
even small projects can involve thorny debugging issues.
A development environment for microcontrollers involves a software assembler or
compiler (just as would be required to write PC software). In addition, it requires a
hardware device that uploads the code to the microcontroller. This is often referred to as
a “programmer.” Finally you will need a hardware platform to allow you to construct
your prototype circuit. Because it is likely that you will need to debug the prototype
hardware as well as the software, this platform should have the capability to make circuit
changes fairly easily. Ideally, the programmer should connect directly to your prototype
circuit so you don’t have to take the microcontroller out the circuit and put it in the
programmer in order to make changes to its code. This approach also has the advantage
that the programmer can be used to provide power to the project that you are building,
eliminating the need for a separate source of power, at least during the design and testing
phase.
Development Software
As with most software development projects, one of the key initial decisions concerns
which programming language to use. There are several very good reasons to begin
writing PIC programs in assembly language. PIC microcontrollers are Reduced
Instruction Set Computer (RISC) chips. As a result, writing in assembly language is very
different than writing in assembly language for, say, Pentium class processors. For the
16 series PIC microcontrollers, there are only 35 instructions to learn. The implications
of this are that there is a lot less to learn to get started in writing assembly applications,
but doing so can be tedious, because it often takes quite a few lines of code to accomplish
even simple tasks. This is compounded by the fact that 16 series PICs are 8 bit machines.
As a result, numbers greater than 255 (1111 1111 in binary) cannot be stored in a single
register. So if, for example, you want to repeat something 300 times, you can’t do it by
counting up (or down) a single register, because it can’t hold a value that large.
It is a good idea to begin working with assembly language because it forces you to focus
on the individual storage registers within the chip and how they control the various
functions of the chip. This is information that you need even if you are programming in a
higher-level language. Programming in assembly language gives you a very good
understanding of these various registers and functions. This is the main reason that the
first few chapters of this book are designed to teach assembly language programming.
In addition, the Microchip PIC assembler (called MPASM) is free. Some versions of
some C compilers are free as well (often labeled “student” or “lite” version) but generally
speaking, if you decide to move to working in C, eventually you will probably want to
buy a C compiler to use. The most widely used compilers for the 16 series are produced
by Custom Computer Services, Inc. (www.ccsinfo.com) and Hi-Tech Software
(www.htsoft.com). Hi-Tech also produces a “lite” version that is available for free. It
will be used as the basis for the C portion of this book. There are also a couple of
companies that produce BASIC compilers for PIC chips. If you end up moving on to the
more expensive PIC microcontrollers (the 18, 24 and 30 series) a C compiler is available
directly from Microsoft. While the full version is not free, student versions of these
compilers are available for free.
The decision concerning whether to write all of your code in assembly language, or at
some point to make the jump to a higher level language like C is vigorously debated
within the PIC community. While it is true that nothing is faster and smaller than hand
crafted assembly language, often programming space and speed are not the only (or even
the primary) design parameters. I’ve also found that a basic understanding of how PIC
chip registers are organized will allow you to write code that the compiler will translate
into more efficient machine language. Techniques for accomplishing this will be
discussed in later chapters.
The principal advantages of using C are that you have access to control structures (if, for,
while loops, etc.) and that most C compilers come with pre-built libraries that enable you
to easily add functionality to your applications. Of course, if you do a lot of assembly
language programming, over time you will generally develop these kinds of resources for
yourself. It may or may not be a consideration for you, but I’ve found that most
employers who are hiring PIC programmers, are very interested in developers who have
experience in C as well as assembly language.
My approach is to spend most of my time writing in C, but when necessary to craft
critical portions of code in assembly. All C compilers allow you to drop assembly
language code “in line”, so you can fall back to assembly language when you find it
necessary.
One interesting advantage of C is that PIC C compilers produce assembly language code
as well as machine language. This makes a C compiler an interesting learning tool
because you can write code in C, compile it, and examine the resulting assembly code to
gain insight on how to perform a particular function. Because this code is automatically
generated, by reviewing the code you can also sometimes see how to make
improvements.
It is worth noting that none of the development tools (assemblers or compilers) that you
are used to using when creating software for your PC will successfully create software for
PICs (or other microcontrollers). The purpose of an assembler or a complier is to
generate machine language code (1’s and 0’s) that represent instructions that the
processor will be able to understand. Since the underlying instruction sets for Pentiums
and PICs are different, programming that generates machine language instructions that
will run on a Pentium do not also generate machine language instructions that can run on
PICs.
Microchip has available an integrated development environment called MPLAB that
contains an editor, the MPASM assembler, an excellent debugger, and a simulator, as
well as hooks that allow it to connect to various compilers for higher level languages. It
works quite well and the price is free. It’s available from their web site:
(www.microchip.com).
Development Hardware
In addition to the software, you will need a hardware circuit that will upload your
application to the microcontroller, called a programmer. The cost of these programmers
range from almost nothing to literally thousands of dollars. In order to program PICs, a
programmer must be capable of generating two different voltages and manipulate signals
for 3 different PIC pins.1 Several years ago there was a proliferation of relatively
inexpensive PIC programming circuits. Most of these programmers used a computer’s
Technically, this is no longer correct. Many newer PIC microcontrollers have the capability to do “low
voltage programming” which allows the chip to be programmed with only a single five volt source. Most
hardware programmers do not use this method, in part because it limits their ability to program older PICs.
1
parallel port to provide the signals to program the PIC and contained on board power
supplies to generate the needed voltages. An example of this type of programmer is
contained in Microchip’s Application Note #AN589 [provide web link]. A few of these
simple programmers used a computer’s serial port to provide the signals. Both of the
needed voltages could be derived from the PC’s serial port, thereby avoiding the need for
an on board power supply.
One of the key problems of using this type of simple programmer is that they rely on PC
software that can directly manipulate the parallel or serial port data lines to program the
chip. This worked quite well under Microsoft’s MS-DOS software, or under earlier
versions of Windows. Starting with Windows NT, however, as well as Windows XP,
users were no longer allowed to address the computer BIOS and directly manipulate
these data lines. This made it more difficult to use these types of simple programmers
from within Windows. There are some work-arounds for this problem. One could
maintain an old computer running an early version of Windows, for the purpose of doing
PIC development. Alternatively, there is a driver that restores the ability to manipulate
these data lines under XP. If you decide you wish to take this approach, the easiest
approach is to download David Tait’s FPP software from:
http://people.man.ac.uk/~mbhstdj/fpp09.zip
and read the readme.htm file very carefully.
Aside from very high end PIC programmer hardware, probably the best programmer to
start with is the ICD-2, available from Microchip. This cost is not trivial (a bit under
$200), but it will work with the vast majority of Microchip’s PIC product line and is very
fast. It connects to a PC using USB and is designed to interoperate with MPLAB. Most
importantly, it contains a hardware debugger, which, when used with PICs that support
this feature, allows you to step through the code one instruction at a time and set up a
break point to stop execution at a particular point. This can greatly simplify the
debugging process.
ICD-2 clones are now widely offered on the Internet (they are regularly on Ebay for
under $50). There are also several build it yourself projects for an ICD-2 clone. If you
decide to take this approach, be careful. Most of these clones don’t really support USB
programming. Instead they may draw their power from the USB line, but use the
computer’s serial port for doing the actually programming. This results in substantially
slower programming times than are experienced with USB.
Because of the expense of the ICD-2, this book presumes that most beginners would
prefer a cheaper approach. A nice intermediate cost solution is the PicKit 2, also
produced by Microchip. This is also a USB programmer (though it lacks most of the
debugging capabilities of the ICD-2). It does not work with the entire line of PICs, but it
does work with a very large number of them. It comes in two varieties. One also
includes a small prototyping board with a connector on it that allows the PicKit 2 to plug
directly in to it. In addition to a 16F690 PIC, the prototyping board contains some LED’s
you can flash, a pushbutton switch you can read and a potentiometer that allows you
provide a variable voltage that can be routed to the PICs analog to digital converter. It is
possible to add parts to this board, but they have to be soldered in. If you use this board,
it will make it especially easy to undertake the simple projects in the early chapters of this
book. However, while it provides an easy way to get up and running by writing some
simple programs, it won’t take very long before you’ve done everything with the
components on this board that it is possible to do. At that point, you will have to obtain a
more flexible prototyping system. As described in the next session, I recommend that
you simply begin with a more capable prototyping system. The PicKit 2 with the
prototyping board (not recommended) is part number DV161420. The PicKit 2 without
the prototyping board (highly recommended) is part number PG161420. See below for
ordering information.
Prototyping System
When you get to the point of designing your own projects, you will probably begin by
wiring them on a “prototyping breadboard”. This is a device that has a series of rows and
columns of small holes. The holes are designed to allow the wires on electronic
components (or simply wires) to be plugged into them to form connections, and hence,
circuits. The holes in each row along the top and the bottom of the board are connected
together and generally used to transmit a positive voltage (the row marked “+”) or to
allow connections to ground (the row marked “-“). Even though there are small gaps
between the groups of five holes in each row, they are still all connected together. Some
larger boards have larger gaps in these rows, indicating that groups are not connected
internally in the board.
In the center of the board is a small trench. On either side of the trench there are groups
of 5 holes arranged in a column. Note that these columns are numbered on the above
example from 1 to 30. Each group of five holes is connected within the board itself. The
two groups of any given number on opposite sides of the trench are not connected. This
makes it relatively easy to plug in an integrated circuit chip (such as a PIC
microcontroller) so that one row of its pins are on one side of the trench and the other row
of pins are on the other side of the trench. This makes it very easy to hook up to four
additional components to each of the PIC’s pins.
Prototyping breadboards of this type are relatively inexpensive and widely available (see
resources section, below). If you are using either the ICD-2 or the PicKit 2, your project
can be powered directly from the programmer itself. Thus, all you need to add to the
board is the components that make up your circuit and probably some additional wires
that will connect the components together. Wire sets are available that contain wires of
varying lengths whose ends have already been stripped and bent so that they will easily
plug into the board.
When your project has been finalized, you will probably want to move it off the
prototyping breadboard to something a little more permanent. Strategies for doing this
are discussed in Chapter 17. At that point, you will have to consider the various options
that are available for providing power to your project.
Resources
To undertake the projects in this book, you will need the items in the following list. Most
of these can be acquired at your local Radio Shack, though the prices will generally be
quite a bit higher than if you order them on the web. In most cases I’ve listed alternatives
for a particular item. In those instances, any of the listed items will work, you only need
to order one of them.
ITEM
Source
(s)
Cost
Chapter Used
MPLAB
16F690 Datasheet
PICCLite
C18 compiler
www.microchip.com
www.microchip.com
www.htsoft.com
www.microchip.com
FREE
FREE
FREE
FREE
ALL
ALL
ALL
19
PicKit2
www.digikey.com
www.mouser.com
PG164120-ND
579-PG164120
$35
$35
ALL
16F690 Microcontroller
www.mouser.com
www.digikey.com
579-PIC16F690-I-P
PIC16F690-I-P-ND
$2.02
$4.13
ALL
Prototyping
Breadboard
Radio Shack
www.allelectronics.com
276-169
PB-840
or PB-1680
$23
$5.50
$13.50
ALL
Breadboard Wire Kit
Radio Shack
www.allelectronics.com
276-173
JW-140
$6.29
$6.25
ALL
1x5 pin .1 right angle header www.allelectronics.com
www.mouser.com
www.digikey.com
SHR-40 (snap 5 pins)
571-6404535
WM6014-ND
$1.25
$0.34
$1.18
ALL
10 .1 uf capacitors
272-135
$6.45
ALL
Radio Shack
Part Number
www.allelectronics.com
www.mouser.com
www.digikey.com
AM-10
239-M22U104M5
P4924-ND
$0.90
$2.50
$2.92
10 1K ohm resistors
Radio Shack
www.mouser.com
www.digikey.com
271-1321
30BJ250-1.0K
1.0KQBK-ND
$1.98
$2.20
$0.54
ALL
LED’s (2 each red,
green, yellow)
Radio Shack
276-209 (red)
276-306 (green)
276-021 (yellow)
$2.58
$2.78
$2.58
ALL
www.mouser.com
606-CMD5075C (red)
606-CMD54643 (green)
606-CMD5374C (yellow)
LED-1 (red)
LED-2 (green)
LED-3 (yellow)
$0.60
$0.60
$0.80
$1.00 (for 10)
$1.00 (for 10)
$1.50 (for 10)
www.allelectronics.com
10K potentiometer
Radio Shack
www.mouser.com
www.digikey.com
271-282
652-3318-1-102
3318-103-ND
$1.29
$0.26
$0.35
10
Piezo Speaker
www.mouser.com
254-35T2
$1.72
11
A MAX232A or
ST202E
level conversion chip
www.mouser.com
www.digikey.com
511-ST202EBN
497-2051-5-ND
$0.88
$1.61
13
A 1line or 4 line LCD
display
www.allelectronics.com
15
www.crystalfonz.com
LCD-87 (4 line)
$11.25
LCD-100 (1 line)
$5.00
some really beautiful displays for more money!
www.allelectronics.com
www.mouser.com
www.digikey.com
SHS-40 (snap 14 pins)
517-1640452-4
WM6414-ND
15
1x14 pin .1 inch header
$0.50
$0.84
$0.76
Download