Você está na página 1de 3

Douglas Guardino Understanding the Basics of Using C18 Compiler

The purpose of this project was to understand how to use the C18 compiler with MPLAB. This was achieved by reproducing a simple program from the MPLAB C18 Compiler Getting Started. This was done as a control. The code in the book was assumed to be working with no bugs. The Example 1 from the guide was followed including MPLAB project setup. After installing the C18 compiler a project must be created in MPLAB. Next, the processor to be used should be selected, in this case the 18F4320. It is a good idea to select the desired oscillator setting under configuration bits. The next thing to do is select Microchip C18 Toolsuite as the language toolsuite for the project. Under build options, it is a good idea to set the Library Path and Linker-Scrip Path to the lib and lkr directories located in the C18 complier directory. This will correct some errors with the complier and linker not being able to find files needed in their execution. Now is a good time to create the source code file and save it as a c code source file .c. After saving the source code file, it needs to be added to the project under Source Files in the project window. In this same window under Linker Scripts, the linker script file for the processor chosen needs to be added; in this case it is the 18f4320.lkr and can be found in the lkr directory in the C18 directory. The 18F4320 had Port B pins 0-5 hooked up to LED to show the output state of the pins. The purpose of the code shown here is to count in binary up to 15. The original code from the MPLAB C18 Compiler Getting Started is in Listing 1. Listing 1: Example1.c
#include <p18cxxx.h> /* for TRISB and PORTB declarations */ int counter; void main (void) { counter = 1; TRISB = 0; /* configure PORTB for output */ while (counter <= 15) { PORTB = counter; /* display value of 'counter' on the LEDs */ counter++; } }

Listing 2 contains the first modified code. The main difference between the codes is in Listing 2; there is the added delays.h library and the Delay10KTCYx() command. This command was added to the original code so that the output on PortB could be read. The code was run once without the delay included, but the LEDs flashed so fast that there was no way to determine if they followed the proper pattern. The Delay10KTCYx() delays 10,000 times the value entered, from 1 to 255, in clock cycles. If a value of 0 is used the delay is 2,560,000 clock cycles.

Listing 2: firstlighs02.c
#include <p18f4320.h> #include <delays.h> // adds the delays library int counter; void main (void) { counter = 1; TRISB = 0; while (counter <= 15) { PORTB = counter;

// sets the counter to 1 // sets PortB pins as outputs

// outputs the value of the // counter on PortB // adds a delay so the value // the output can bee seen by // human eye // increments the value of // counter

Delay10KTCYx(100);

counter++;

} }

Running the above code allows you to see whether the compiler works and whether the output is what was expected. The only unexpected thing was that when the program reached the end it started again from the beginning. The next logical step was hooking up an LCD screen for output from the microprocessor. The LDC can provide active feedback from the micro possessor. The LCD used in the development is a serial LCD that works at 9600 BPS. To learn how to interface and use the LCD screen in PIC C18 the code in Listing 2 was modified so that, besides outputting the value of the counter on the LED, it would also be sent to the LCD screen. This code can be found in Listing 3. Listing 3: lighs02.c
#include #include #include #include #include <p18f4320.h> <delays.h> <usart.h> <stdlib.h> <string.h> // // // // adds adds adds adds the delays library hardware serial support data conversion library string tools

int counter; char string[5]; void main (void) { counter = 1; TRISB = 0; // This initializes the serial port on the PIC OpenUSART( USART_TX_INT_OFF & // disables transmission // interrupt

// // USART_ASYNCH_MODE & // USART_EIGHT_BIT& // USART_CONT_RX & // // USART_BRGH_HIGH, // // 25 ); // memset ( string, 0, 5); // // while (counter <= 15) { PORTB = counter; Delay10KTCYx(100); counter++; itoa (counter, string); // // // while (BusyUSART());

USART_RX_INT_OFF &

disables reception interrupt sets it in asynchronous mode sets to use 8-bit data mode sets the port in continues receive mode uses the high sped Baud rate formula sets Baud to 9600 BPS sets the first five elements of the string to 0

converts the value of counter into a string so it can be displayed

// Makes sure the serial // port isnt busy // outputs the string over the // serial port // gives time to read the screen // clears the LCD screen // closes the serial port

putsUSART( string );

Delay10KTCYx(100); WriteUSART( 12 ); } CloseUSART(); }

The string.h library is where the command memset() comes from. The memset() command takes a pointer to a string , a value and number of times to repeat it in a string. The stdlib.h is where the command itoa() comes from. The itoa() command takes an integer value and changes it into an ASCII string that represents that value and places it into the specified string and null terminates the string. The usart.h library is where the commands OpenUSART(), BusyUSART(), putsUSART(), WriteUSART(), and CloseUSART() come from. OpenUSART() initializes the serial port with options linked by ANDs and the value for the Baud rate formula. BusyUSART() returns a 1 if the serial port is busy and a 0 if it is ready for use. The putsUSART() command takes a null terminated string and outputs it on the serial port. WriteUSART() command writes a single byte to the serial port. In the case of this project, 12 is the command for the LCD screen to clear. The CloseUSART command shuts down the serial port. When the code in Listing 3 was run, the processor counted from 1 to 15 in binary on the LED on PortB and in decimal on the LCD screen. This was the desired outcome. The values on the LED and on the LCD were equivalent, which was also part of the desired outcome. With the general understanding of how to operate the compiler and how to use the LCD screen with the PIC using the C18 language, more difficult problems can be addressed.

Você também pode gostar