07.04.2013 Views

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using the <strong>Compiler</strong> on the Comm<strong>and</strong> Line<br />

• c:\Program Files\<strong>Microchip</strong>\<strong>MPLAB</strong> C30\lib - Library directory: this<br />

directory is where the libraries <strong>and</strong> precompiled object files reside.<br />

• c:\Program Files\<strong>Microchip</strong>\<strong>MPLAB</strong> C30\support\<strong>dsPIC</strong>30F\gld -<br />

Linker script directory: this directory is where device-specific linker script files may<br />

be found.<br />

• c:\Program Files\<strong>Microchip</strong>\<strong>MPLAB</strong> C30\bin - Executables directory:<br />

this directory is where the compiler programs are located. Your PATH environment<br />

variable should include this directory.<br />

The following is a simple C program that adds two numbers.<br />

Create the following program with any text editor <strong>and</strong> save it as ex1.c.<br />

#include <br />

int main(void);<br />

unsigned int Add(unsigned int a, unsigned int b);<br />

unsigned int x, y, z;<br />

int<br />

main(void)<br />

{<br />

x = 2;<br />

y = 5;<br />

z = Add(x,y);<br />

return 0;<br />

}<br />

unsigned int<br />

Add(unsigned int a, unsigned int b)<br />

{<br />

return(a+b);<br />

}<br />

The first line of the program includes the header file p30f2010.h, which provides<br />

definitions <strong>for</strong> all special function registers on that part. For more in<strong>for</strong>mation on header<br />

files, see Chapter 7. “Device Support Files”.<br />

Compile the program by typing the following at a DOS prompt:<br />

C:\> pic30-gcc -o ex1.o ex1.c<br />

The comm<strong>and</strong>-line option -o ex1.o names the output COFF executable file (if the -o<br />

option is not specified, then the output file is named a.exe). The COFF executable file<br />

may be loaded into the <strong>MPLAB</strong> IDE.<br />

If a hex file is required, <strong>for</strong> example to load into a device programmer, then use the<br />

following comm<strong>and</strong>:<br />

C:\> pic30-bin2hex ex1.o<br />

This creates an Intel hex file named ex1.hex.<br />

3.9 COMPILING MULTIPLE FILES ON THE COMMAND LINE<br />

Move the Add() function into a file called add.c to demonstrate the use of multiple<br />

files in an application. That is:<br />

File 1<br />

/* ex1.c */<br />

#include <br />

int main(void);<br />

unsigned int Add(unsigned int a, unsigned int b);<br />

unsigned int x, y, z;<br />

int main(void)<br />

{<br />

x = 2;<br />

y = 5;<br />

© 2008 <strong>Microchip</strong> Technology Inc. DS51284H-page 61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!