31.01.2013 Views

mplab c18 c compiler getting started - Computer & Information Science

mplab c18 c compiler getting started - Computer & Information Science

mplab c18 c compiler getting started - Computer & Information Science

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

MPLAB ® C18 C Compiler Getting Started<br />

4.2.6 Summary for Program 1 “Hello, world!”<br />

This completes the first program example. These topics were covered:<br />

Writing MPLAB C18 code<br />

Building (compiling and linking) the project<br />

Testing the project with MPLAB SIM<br />

Troubleshooting beginner errors<br />

4.3 PROGRAM 2: LIGHT LED USING SIMULATOR<br />

The first example demonstrated the basics of creating, building and testing a project<br />

using MPLAB C18 with the MPLAB IDE. It did not go into the details of what the target<br />

processor would do with that code. In this next program, code will be generated to<br />

simulate turning on a Light Emitting Diode (LED) connected to a pin of the PIC18F452.<br />

4.3.1 Create a New Project<br />

Create a new project named “GS2” in a new folder named “Second Project.”<br />

Make sure the language tools are set up and the Build Options properly configured as<br />

shown in Section 3.7 “Verify Installation and Build Options”.<br />

4.3.2 Write the Source Code<br />

Create a new file and type in the code shown in Example 4-3. Save it with the name<br />

main.c in the “Second Project” folder:<br />

EXAMPLE 4-3: PROGRAM 2 CODE: main.c<br />

#include <br />

#pragma config WDT = OFF<br />

void main (void)<br />

{<br />

TRISB = 0;<br />

}<br />

/* Reset the LEDs */<br />

PORTB = 0;<br />

/* Light the LEDs */<br />

PORTB = 0x5A;<br />

while (1)<br />

;<br />

The first line in this code includes the generic processor header file for all PIC18XXXX<br />

devices, named p18cxxx.h. This file chooses the appropriate header file as selected<br />

in MPLAB IDE; in this case, the file named p18f542.h (which could have been<br />

included explicitly instead). This file contains the definitions for the Special Function<br />

Registers in these devices.<br />

“#pragma config WDT = OFF” is the same as in the first program.<br />

Note: In MPLAB C18, the main function is declared as returning void, since<br />

embedded applications do not return to another operating system or<br />

function.<br />

DS51295F-page 44 © 2005 Microchip Technology Inc.

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

Saved successfully!

Ooh no, something went wrong!