Chapter 4: Initial Hardware Set Up and Test

advertisement
Chapter 4: Initial Development Environment Set Up and Test
Hardware Setup
The purpose of this chapter is to get your development system up and running and make sure that your
hardware is fully debugged. At this point you won’t actually be writing any code yourself, but you will
load a program into the PIC to demonstrate that your hardware is working.
First, you will need a means of connecting your programmer to the prototyping breadboard. In you use
the PicKit 2, you can use a 5 pin right angle header (see Resources, Chapter 2) and plug it into five
adjacent pins on the breadboard (see diagram and photograph, below). Pin 1 on the PicKit 2 is marked
with an arrow. While there are 6 pins on the programming output of the PicKit2, you will only actually
use 5 of them. These are as follows:
1
2
3
4
5
Programming Voltage (pin one is indicated by an arrow)
Voltage to power your circuit (+5 volts)
Ground
Programming Data
Programming Clock
These same five signals will be provided by any PIC programmer that you decide to use. If you are
using a programmer other than the PicKit2, you will need to find a method for connecting the signal
wires to your prototype board as shown on the following page. The connections to the programmer may
be somewhat different, but the connections to the prototyping board will be the same.
As long as the proper signals get to the proper pins on the PIC, you should be fine. However, the
following page shows a reasonably simple and straightforward way to hook up these connections to a
PIC16F690.
Several features of this board layout are worth noting. First, the board upon which this diagram was
based has rows of pins along the edges that are labeled with blue and red stripes. The intention here is
for the red “buss” to indicate the positive voltage (+5 volts) and the blue buss to indicate ground. These
busses are connected at the bottom of the board so that both positive and ground are easily available to
any point on the board. Pin 1 of the cable that will connect to the programmer is labeled in this diagram.
Follow pin 2 (the +5 volts line) though the board to see how it gets to the red (+) buss. It runs first to an
orange wire in the fourth row that spans the trench. From there it connects to a red wire and then to
another orange wire which connects directly to the + buss. [revise when the diagram is redone to have
rows numbered or lettered]. Note that it also connects to the top left corner pin on the PIC16F690. The
reason for this is that this pin is where the PIC expects to receive power. The ground connection on this
chip is the top right corner pin. Take a moment to follow the circuit from pin number 3 of the
programming header to the ground pin on the PIC and to blue (ground) buss.
Three more connections are required in order to be able to program the PIC. Pin 1 on the programming
header connects to the fourth pin down on the left side of the PIC. This connection has a relatively high
voltage applied to it when the PIC is being programmed. It places the PIC in programming mode rather
than operating mode. The data that actually is transmitted to the PIC to program it is communicated via
programming header connections 4 and 5. These are connected through to the second and third pins
down on the right side of the PIC.
[
If you are using an ICD 2 instead of a PicKit 2, the easiest approach is to get obtain a six conductor
telephone style cable (make sure it is 6 conductor, not 4) and cut off an end with a plug and a foot or two
of cable (see, for example, www.allelectronics.com part number MT-363 for $1.25). Then you can
solder the bare wires onto the right angle header and plug it into the board as shown above. The end of
the cable with the 6 conductor modular telephone plug will fit directly into the ICD-2. If you are using a
programmer other than one of these, you will need to ensure that the appropriate signals from your
programmer are routed to these 5 pins. If you use this approach, however, you should exercise some
care, because not all do it yourself programmers support the 16F690 PIC (or other PICs for that matter).
Check this before you spend your money.
Double check to make sure the wires are set up exactly the way they are shown on the board drawing
above. Note the two labels that say “cap”. Here take two of your .1 uf capacitors, clip the leads so they
are only about ½” long, and plug them in so that one lead is in the positive buss and the other is in the
negative buss. These are the power supply bypass capacitors that were discussed in Chapter 3.
Software Setup
[This section will be added later. It consists of screen shots of the setup process for all software
(MPLAB and PICClite). The accompanying text is fairly pedestrian.]
Burning and Running Your First Program
To test your hardware and software setup, we’ll create a project that blinks a single LED. Follow the
steps below carefully, but don’t worry about the details of either the hardware setup or the programming
code that you are entering. These will all be explained in detail in the next chapter. The point here is
simply to make sure that everything works properly.
Start by adding a resistor and an LED (whatever color you like) to your prototyping board. When you
are finished, it should look like this:
Open MPLAB and select Project/New… from the menu. You’ll be asked for a project name, and to
specify the directory where the project should reside. It is probably a good idea (though it is not
required) to have each project that you create occupy it’s own directory. So, for example, you could use
the directory c:\Pic Projects\Blink for this project. Be careful with the length of the path you use.
MPLAB does not support paths that are more than about 60 characters in length.
After you click ok to close this screen, select Project/Select Language Toolsuite from the menu. A
dialog box will pop up that will allow you to pick the Microchip MPASM Toolsuite.
Check to make certain that the Location that you specified during the MPLAB setup in the first part of
this chapter shows in the box marked location. Click OK.
Now select File/New from the menu. A window should open that will allow you to enter your program
text. Before you do so, however, select File/Save from the menu.
Navigate to the subdirectory that you specified for this project and enter the file name:
Blink
If it is not already checked, check the box that says “Add File to Project”, and press Save. The new
source code file should now show up under the “Source Files” label in your project:
Select Configure/Select Device from the main menu. A box will pop up that will look like this:
Use the combo box to make sure that the Device is the 16F690. There are a lot of microcontrollers in
this list so you may have to do a little bit of hunting to find it. Press OK.
Now enter the following code in the Blink.asm edit window. If you don’t want to type it all in, you can
find this text as Blink.asm on www.coastalchip.com/code.
radix
hex
include "P16F690.INC"
__config _CP_OFF & _WDT_OFF & _BOR_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF
errorlevel -302
org
0
count
count2
equ
equ
20
21
call
INITIAL
call
call
goto
BLINK
DELAY
LOOP
bsf
movlw
movwf
bcf
STATUS, RP0
b'01111111'
TRISB
STATUS, RP0
LOOP
INITIAL
return
BLINK
movlw
xorwf
return
b'10000000'
PORTB,F
DELAY
movlw
movwf
movwf
DELAYLOOP
decfsz
goto
decfsz
goto
return
0xFF
count
count2
count,f
DELAYLOOP
count2,f
DELAYLOOP
end
A couple of observations are in order here. The fourth line begins with two underscores, not one. It
starts __config, not _config. The other underscores in that line are single underscores. The code on
each line is divided into three columns. Most lines of code have nothing in the first column. In that case
it does not matter how much space you leave, but you must make sure that you leave at least one blank
space if there is nothing in the first column. The easiest way to do this is to insert a tab between
columns. Be careful to distinguish zero’s from O’s. The 0 in 0xFF, for example, is a zero, not a capital
O. Finally, capitalization does matter.
After you have entered the text (or copied it from the web site), press the F10 key (or select
Project/Make from the menu to assemble it and produce a .HEX file that can be used to load into the
PicKit 2.
[Text to be added on how to program the chip … currently it requires the PicKit2 programming
software. However, in the latest version of MPLAB, there is a greyed out entry for PicKit2. I assume
that this means that they are going to make it possible to program the chip directly from MPLAB…
which would be simpler and involve one less piece of software. If this option doesn’t exist by the time
the text is finalized, I’ll add the information on using the PicKit2 programming software.]
Download