-
Microchip dsPIC Programming
Notes
Microchip has provided
very good documentation and tutorial on their
website, you can
get yourself started
with the resources. Here I'd like to present a few
notes from my experience
of programming
with MPLAB IDE and dsPIC30F family chips to save
your time.
1. Startup
Function
If you
use the Project Wizard in MPLAB to create a
project, you need to supply a main
function in order for the project to
build successfully. The wizard uses the startup
function from
library file, e.g.,
libp30F6010A-coff.a. After the initialization of
the stack pointer register,
the stack
size register, PSV and data, the startup code
calls function main() in which your
application code starts.
If you want to have your own startup
code, it is strongly recommended to use the
supplied
source crt0.s and crt1.s as a
baseline, and give a name other than the default
names (crt0.s,
crt1.s) to your startup
code, and make sure the following three global
variables of function type
exist in
your startup code:
__reset
__resetPRI
__resetALT
In
case any of the above symbols is missing from your
startup file, the linker will
supplement it from the default library.
2. Build
Options
Build
option settings shall be set up at the beginning
of project development. It is easier
for one to understand if the project
files are organized in logical hierachy. Suppose I
have the
following project folder
structure. All header files and includes files are
stored in
Header
subfolder,
all source files are stored in
Source
subfolder (you may create modular
subfolders
under this folder), all the
generated intermediary files are stored in
Obj
subfolder, and all
output files are stored in
Output
subfolder.
Figure 2-1 Project file structure
First, add all source
files to
Source Files
folder
and all include files to
Header Files
folder
in the project view.
Figure 2-2 Project view
Then, click menu Project->Build
Options->Project, set up
Directories
tab to set up
directories
and search path. The relative path is dependent on
the
selection at the lower part of the
tab. If the first option is checked, the path is
relative to
source-file directory
except link output; if the second option is
checked, the path is relative to
the
project directory. The following examples are
based on the first option being checked.
Because Output Directory
path is where builds (.hex, .map, etc) are stored,
and relative to
project root, so set it
as
.Output
.
Figure 2-3 Build Options -
Output Directory
Intermediary Directory is where obj
files are stored, and relative to source file
directory as
the first Build Directory
Policy
is checked, so set it
as
..Obj
.
Figure 2-4 Build Options -
Intermedary Directory
Assembler include search path is where
all include files are stored, and relative to
source
file directory as the first
Build Directory Policy
is
checked, so set it as
..Header
.
Figure 2-5
Build Options - Assembly Include Search Path
Similarly, Include search
path is where all C header files are stored, and
relative to C
source directory as the
first Build Directory Policy
is checked, so set it as
..Header
.
Figure 2-6 Build Options -
Include Search Path
Depening on build needs, set up other
build tabs. The following pictures show Link30 tab
General category and Diagnostics
category settings.