You are on page 1of 11

A Tutorial on PIC18F Based Hardwar Design Using C Programming Language+ Shant Dilanian, Yumin L and Rajan Chandra The

objective of this tutorial is to use the PIC18F2221 Microcontroller and Microchip development tools to design and implement a simple project. Specifically we develop C-code in order to flash the LED is connected to the least significant bits of PORT B, RB0 (See Figure 1)
U1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 MCLR/Vpp/RE3 RB7/KBI3/PGD RA0/AN0 RB6/KBI2/PGC RA1/AN1 RB5/KBI1/PWM4/PGM RA2/AN2/Vref -/Cap1/Indx PB4/KBIO/PWM5 RA3/AN3/Vref +/Cap2/QEA RB3/PWM3 RA4/AN4/Cap3/QEB RB2/PWM2 RA5/AN5/LVDIN RB1/PWM1 RE0/AN6 RB0/PWM0 RE1/AN7 Vdd RE2/AN8 Vss AVdd RD7/PWM7 AVss RD6/PWM6 OSC1/CLKI/RA7 RD5/PWM5 OSC2/CLKO/RA6 RD4/FLTA RC0/T1OSO/T1CKI RC7/RX/DT/SDO RC1/T1OSI/CCP2/FLTA RC6/TX/CK/SS RC2/CCP1/FLTB RC5/INT2/SCK/SCL RC3/T0CKI/T5CKI/INT0 RC4/INT1/SDI/SDA RD0/T0CKI/T5CKI RD3/SCK/SCL RD1/SDO RD2/SDI/SDA 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21

D1 R1 1k C2 .16uF V1 5Vdc LED

5Vdc V2

C3 .16uF


Figure 1 The development tools used in this tutorial are MPLAB-C18-Academic-v3_30.exe and PICKIt2 Programmer

+ This hand out is a modified version of the original version that was first created by Dr. Sacco.

Start Microchips MPLAB IDE

From File Select New Enter your C source program and save it in the project directory

//LED Flashing Program to be saved as ADC.C #include <p18f4221.h> #pragma config OSC = INTIO2 #pragma config WDT = OFF #pragma config LVP = OFF #pragma config BOR = OFF void delay(); void main (void) {TRISB=0x00; while(1) { unsigned char i; unsigned char j; PORTB=0x00; delay(); PORTB=0x01; delay(); } } void delay(){ unsigned char i, j; for(i=0; i<100; i++) for(j=0; j<255; j++) ;

From Project select ProjectWizard Click Next

Select the device(Please Select 18F4221 instead) Click Next

Select Microchips C18 Toolsuite Click Next

Click Browse

Browse to the folder where you want your project to be saved and give it a name (Here it is called C1) Click Save

Click Next

Add the header, library, linker, and source files to the project . Click Next (Please select p18F4221.h, p18f4221.lib and p18f4221.lkr files instead)

Click Finish

From Project select Build Options / Project (In your work, you will see p18f4221 files)

Select Include Search Path Click New and browse to: Header directory Within MCC18 directory

Select Library Search Path Click New and browse to: Library directory Within MCC18 directory

Select Linker Script Search Path Click New and browse to: Linker directory Within MCC18 directory Click OK

From Programmer /Select Programmer select PICKit2

Click Build All icon

Click Program the target device icon

click Bring target MCLR to Vdd

The picture shown below shows the flashing LED:

PS: In the next page please see the connection details between the PICKIT2 and the breadboard.

PS: Shown below are the connection details between the PICKIT2 and the breadboard.