Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

UAV DevBoard:

Getting Started with PIC


Programming
5/11/2009

If you bought a UAV DevBoard, you will need to program it in order to do anything useful or
interesting with it. It is not hard to do, after a few times it will be easy. But the first time can be
confusing. This document will help you get started.
In addition to the UAV DevBoard itself, you will need the following items to program it:
• An ICD2. This is a programmer/debugger hardware item for connecting the
DevBoard up to your computer. I suggest that you buy the one recommended by
SparkFun, http://www.sparkfun.com/commerce/product_info.php?products_id=5. I
do not recommend the “PIC-ICD2-Pocket”. I have one of these, and I have been able
to get it to work with the dsPIC30F4011 CPU on the DevBoard, but there have been
several reports of problems with it.
• An IDE (interactive development environment). This software suite is available as a
free download from MicroChip. It provides an excellent support environment for
firmware development for the DevBoard.
• A C compiler for the dsPIC30 family of CPUs. This is also available as a free
download from MicroChip.

Download and install the IDE and C compiler


You should install the IDE before you connect the ICD2 to your computer, otherwise plug-
and-play will get confused and will try to install the ICD2 without having the drivers that it needs.
Go to the MicroChip website, www.microchip.com. Both the IDE and the compiler are
available under the Design->Development Tools menu, in the Software section. Close all other
applications first. Download and install the MPLAB IDE first. It will take a while. If it complains
about applications that you have open, close them down and proceed. It may ask you about
installing the HI-Tech C compiler, you should decline, you do not need the HI-Tech
compiler.You should also download the MPLAB IDE user’s guide, it has a very useful section on
getting started.
After you install MPLAB IDE, download and install MicroChip’s C compiler. It is available
under the same area as the IDE, under MPLAB C compilers. Click on MPLAB C compilers, and
scroll to the bottom of the downloads list. Download and install “MPLAB C Compiler for dsPIC,
Evaluation Version”. This is a free download. To get it, you will need to register with MicroChip.
The only difference between the free evaluation version of the compiler and the purchased
version is that after 60 days, the optimization options turn off. However, you do not need the
optimization options, so it does not matter. You should also download the reference manual for
the compiler. You can find it under the Design->Technical Documentation->User Guides,
“MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs Users Guide.” Depending on how
deeply you intend to get into the programming of the dsPIC, there are also reference manuals on
the dsPIC30F4011 hardware and programming model that you can download.
After you install both the IDE and the compiler, you should check that the IDE is working.
Click on the red and blue MPLAB IDE icon. The IDE should open. In the unlikely event that the
IDE does not open, check the IDE user’s manual for suggestions on what to do.

PIC Programming 2
5/11/2009

Installing the ICD2


After you install the IDE and the compiler, you need to install the ICD2. I am not real clear
on this, I did it only once, and that was several years ago. There are instructions for installing the
ICD2 in the help window of the IDE. My recollection is that you start up the IDE, and connect
the ICD2 to your computer with a USB cable. Installation is then automatic.
There are two ways that you can connect your ICD2 to your computer, either with the RS232
connector, or the USB, but not both. I recommend that you use the USB.
So, bring up MPLAB IDE and plug in your ICD2 and wait for it to install.

Programming the UAV DevBoard


You are now ready to program your board. It is shipped with self-test firmware already
installed. To do something more interesting, you will probably want to program it with something
else. There are several programs available on the main page of UAV DevBoard, including demos,
MatrixNav, and AileronAssist. Decide which one you want to start with, download it, and unzip
it. Then, do the following. Some of the steps can be done in a different order, as you gain
experience, it will become clear to you which ones can be shuffled around. Much of it is
“common sense”:
1. Start up MPLAB IDE.
If you have not already done so, start up the MPLAB IDE.
2. Connect your ICD2 to your computer through a USB connection. This will power up the
ICD2.
3. Power up your UAV DevBoard.
Apply approximately 5 volts on the power and ground pins of any servo or Rx
channel of the board. If you are connecting an Rx to the board, you can backfeed power
and ground through the Rx. Otherwise, the simplest thing to is to apply power and ground
to one of the unused Rx channels. The pins nearest the edge of the board are ground, the
center ones are power. Do not try to power the board from the spare Vcc pin. Your board
may or may not do anything when it is powered up, depending on whether it is also
connected to the ICD2 at the time. The ICD2 may hold the CPU in reset, if it is
connected.
4. Connect your ICD2 to your DevBoard.
Use the 6 wire ICSP cable that came with the ICD2. The connectors are keyed so that
it is not possible to connect them backwards. The ICD2 has two places where you could
make a connection, only one of them is the right one. Make a connection to the ICSP
connector that is between the USB connector and the RS232 connector.
5. Open the project in MPLAB IDE.
Under the project menu, select “open”. MPLAB will bring up a window for you to
browse for the project that you want to open. Projects have a .mcp extension, and will
have the MPLAB icon. Select the project that you want.
6. Make sure that the IDE is working in the RELEASE mode.

PIC Programming 3
5/11/2009

The IDE is capable of both simple programming as well as debugging. You will start
with programming. After you open the project, select RELEASE in the IDE toolbar.
There are only two options, RELEASE and DEBUG. Chances are the project will come
up in the DEBUG mode, change it to RELEASE.
7. Select your ICD2 as the programmer and check its settings.
Select MPLAB ICD2 from the programmer menu. Check a couple of settings under
programmer settings or debugger settings. Under the STATUS tab, select automatic
downloading of firmware, but do not select connect at startup. Under the POWER tab,
make sure that the programmer/debugger is not going to try to power the DevBoard from
the ICD2.
8. Do a project “make”.
This is an important step. Do not skip it. Otherwise you will simply erase the PIC
without reprogramming it. Select “make” from the project menu. The IDE will compile
and build the firmware. There will be several warnings issued, but there should not be
any errors. At the end, there will be a message that the build was successful. This step
usually goes smoothly, but there are a couple of things that could go wrong.
If something went wrong during the installation of the C compiler, the IDE might not
be able to find it. In that case, go to “Select Language Toolsuite” under the project menu.
You want the Microchip C30 Toolsuite. If it is not selected, use the Toolsuite browser to
find and select it. It should be located where ever you saved the IDE, probably under
ProgramFiles\Microchip\MPLABC30.
9. Execute a “Connect” operation.
Select connect from the programmer menu. You should receive the following
messages if everything goes right. If something goes wrong, skip to the last section for a
list of common mistakes, misunderstandings, and problems.
Connecting to MPLAB ICD 2
...Connected
Setting Vdd source to target
Target Device dsPIC30F4011 found, revision = Rev 0x1003
...Reading ICD Product ID
Running ICD Self Test
...Passed
MPLAB ICD 2 ready for next operation
Also, the first time that you connect, the IDE will install a new operating system into
the ICD2. It will ask you if you want to do that, you should accept. It will take a few
minutes. After the first time, the IDE will skip this step, unless you are using the ICD2 to
program another type of PIC.
10. Execute a “Program” operation.
The IDE will program the board through the ICD2. You should see something similar
to the following messages:
Programming Target...
...Validating configuration fields
...Erasing Part

PIC Programming 4
5/11/2009

...Programming Programming Executive


...Verifying Programming Executive
...Programming Program Memory (0x0 - 0x187F)
Verifying...
...Program Memory
...Verify Succeeded
...Programming Configuration Bits
.. Config Memory
Verifying configuration memory...
...Programming succeeded
10-May-2009, 18:04:07

MPLAB ICD 2 ready for next operation


If you get error messages instead, skip to the last section.
11. Run the firmware.
There are two ways of doing this. You can run the firmware with or without
disconnecting the ICD2. If you want to leave the ICD2 connected, select “Release from
Reset” from the programmer menu. If not, you can simply disconnect the ICD2
connection to the board, it will start running.
You can also reset the CPU from the programmer by selecting “Hold in Reset”, and
then “Release from Reset” from the programmer menu. This is a particularly convenient
way to restart the firmware without disturbing the gyros.
When you are done programming, I recommend that you do the following:
1. Deselect the programmer or debugger
2. Close the project.
3. Disconnect the ICD2.

Writing and debugging your own firmware


At some point you may want to write your own firmware, in which case you will want to do
some debugging. The process is similar to that of programming, except that you select DEBUG
instead of RELEASE in the IDE toolbar, and you work from the debugger menu instead of the
programmer menu.
There are two debugging functions available to you that you can select from the debugger
menu, either MPLAB SIM or MPLAB ICD2. MPLAB SIM allows you to debug your firmware
without using any hardware. It is very useful in the early stages of firmware development,
particularly when you just want to validate your math. MPLAB ICD2 debugs your firmware
while it is running in your hardware. You can set breakpoints, start and stop your program, and
read and write to memory locations.

PIC Programming 5
5/11/2009

Common mistakes, misunderstandings, and problems


I am really hoping that everything goes smoothly for you, but you may run into some
problems. This section describes how to recognize and correct the most common ones.
When things go wrong, the following is the most commonly encountered set of error
messages.
Connecting to MPLAB ICD 2
...Connected
Setting Vdd source to target
ICDWarn0020: Invalid target device id (expected=0x101, read=0x0)
...Reading ICD Product ID
Running ICD Self Test
... Failed Self Test. See ICD2 Settings (Programmer->Settings) (status tab) for details.
MPLAB ICD 2 ready for next operation
The most likely cause for this error is that the DevBoard does not have any power, or that the
ICD2 is not connected to the DevBoard. Go to the settings menu for the programmer and look
under the Power tab. There are three voltages listed. Vdd should be 5 volts or a little less, Vpp
should be around 13 volts.
Another possibility is a defective 6 wire programming cable. Check the continuity on all 6
wires. (One of the ICD2s that I have was shipped with a defective cable.) Make sure the wires are
tight in the connectors. I had a cable in which one of the wires was making an intermittent
connection, it caused me a great deal of grief. Make sure the cable is connected to the right place
on the ICD2, the connector between the USB and the RS232.
If you want to eliminate the 6 wire cable as a possible source of trouble, you can make the
connection between the ICD2 and the DevBoard with a pair of female-to-female 3 wire servo
cables, such as the ones that SparkFun sells,
http://www.sparkfun.com/commerce/product_info.php?products_id=9187. Be very careful to
connect the pins in the right order, refer to the 6 wire cable for the sequence.
Another cause for this error is that you have somehow selected a different CPU. Take a look
at the bottom of the IDE window, it should say “dsPIC30F4011”. If not, go to the Configure
menu and select the correct device.
Another possibility is that there is a compatibility issue involving the version of the IDE that
you are using. Some users have reported that after receiving the above error messages, they were
able to clear the problem by uninstalling the version of the IDE that they downloaded, and
loading version 7.50 instead. You can locate version 7.50 of the IDE by going to
www.microchip.com, and searching on IDE. One of the links will be to the IDE archives, from
which you can install version 7.50.
At this point, if you are still getting the above error messages, you probably have a defective
ICD2. Contact SparkFun, explain the situation, and ask for an exchange.
Another common set of error messages, that is easy to resolve, is the following:
Connecting to MPLAB ICD 2
ICD0019: Communications: Failed to open port (USB): (Windows::GetLastError() = 0x2, 'The
system cannot find the file specified.
')

PIC Programming 6
5/11/2009

ICD0021: Unable to connect with MPLAB ICD 2 (USB)


MPLAB ICD 2 ready for next operation
You are almost guaranteed to get this message if you disconnect and then reconnect the USB
cable to the ICD2 while it is powered up. The best thing to do is to deselect the programmer
(select none), power off your DevBoard, disconnect the USB, close your project, exit the IDE,
and start all over again. This error may not clear the first time you restart, but it will eventually
clear.
There are a few other common error messages that you may encounter, but they are self-
explanatory and easy to fix. There are some unusual error messages that you could encounter, but
I cannot reproduce them easily, so if you get stuck, contact me, I will give you a hand.

PIC Programming 7

You might also like