Politeknik Kuala Terengganu: Jabatan Kejuruteraan Elektrik
Politeknik Kuala Terengganu: Jabatan Kejuruteraan Elektrik
Objectives:
At the end of the session, the students should be able to:
i. To introduce how to build a C Programming for PIC18.
ii. Explain the structure of C program for PIC18.
iii. Write simple I/O program for byte and bit I/O arithmetic, logic operation and bit
manipulation in C.
Preparation:
Microchip’s MPLAB IDE and MPLAB C18 compiler are required for this and all the lessons. It is
assumed that these have already been installed as instructed in an earlier lesson.
Required Resources:
Computer installed with MPLAB, Microchip C18 Compiler, Proteus 7 or 8 and PIC18F4580 datasheet.
In this lab a project will be created that is compiled under C18. The program is essentially an empty
program shell, but it illustrates the procedure for creating a new C18 project and compiling it under
MPLAB.
**Note: It is highly recommended that each project be saved under a separate directory. This will ensure that all project related files are
accessible from the same folder.
The basic structure of C for PIC18 is the minimum requirement for programmer to fulfill before the
program can be compiling using PIC18 compiler. All C programs contain pre-processor directives,
declarations, definitions, expressions, statements and functions. Figure 1 show the example of
basic structure of C program for PIC18.
asalam.saad,pkt.jun2012
6. Function - A function is a collection of declarations, definitions, expressions, and statements
that performs a specific task. Braces enclose the body of a function. Functions may not be
nested in C.
PIC18F4580 has 5 ports A, B, C, D and E. Each of the Ports A-E in the PICI8F4580 can be used for
input or output. Each port has three registers for its operation. These registers are:
Using C programming enable programmer to manipulate byte size and bit manipulation regarding to
I/O port in PIC.
Procedures:
It is highly recommended that each project be saved under a separate directory. This will ensure that
all project related files are accessible from the same folder.
1. Create a new folder on computer drive (eg. In directory D :) then, create a sub-folder and
name it Practical Work 1.
2. Open a MPLAB session in shortcut menu or start menu.
3. Click on the Menu Project -> select Project Wizard…
4. From the Welcome dialog, click on Next to proceed.
5. Having selected the correct device, PIC18F4580, clicks the Next button.
6. In Windows Step Two, at the Active Toolsuite, select Microchip C18 Toolsuite,
7. Then select MPLAB C18 C Compiler, then clicks the Next button.
8. In Step Three, requires the user/students to create new file for the project. Click browse
button and select your Practical Work 1 folder. Save file name as PW1. Click Next button.
asalam.saad,pkt.jun2012
9. Click Next Button.
10. Click Finish Button to complete the process.
11. To create C Programming by using MPLAB, click ‘New’ from the File tab and type the
following in Program 1 for basic C Programming.
#include <P18F4580.h>
void msdelay(void);
void main(void){
ADCON1 = 0x0F;
TRISA = 0x00;
TRISB = 0x00;
TRISC = 0x00;
TRISD = 0x00;
TRISE = 0x00;
while(1){
LATD = 0xFF;
msdelay();
LATD = 0x00;
msdelay();
void msdelay(void){
int a,b;
Program 1
asalam.saad,pkt.jun2012
12. Then, click Save File as a file name LAB_1.c.
13. Please tick the checkbox ‘Add File to Project’ at the bottom.
14. Click ‘Make’ button from the tab menu and select ‘Absolute’ button when it appear. This will
compile the assembly language code.
16. Download File PIC18F4580.dsn from CIDOS, or COPY from your LECTURER. Open the ISIS
Proteus PIC18F4580 program. Click PIC18F4580 i.c and select file with .HEX from Practical
asalam.saad,pkt.jun2012
Work 1 folder. Then click at the white box beside the ‘Microcontroller’ box and choose
‘PIC18F4580’. Other option remains the same.
17. From the schematic, double click on the microcontroller PIC, and from ‘Edit Component’,
open ‘Program File’, and find HEX file as shown above.
18. From the schematic, double click on the microcontroller PIC, and from ‘Edit Component’,
open ‘Program File’, and find HEX file as shown above.
a. From your working space, double click your PIC18F4580 and follow the instructions
number inside figure below. Setting Edit Component window by following the step
given. 1st setting frequency to 10 MHz, 2nd click at browse button and select the hex file
from the project folder created by compiler (MPLAB) and click “OK”.
b. Click DEBUG > EXECUTE to simulate your program and click DEBUG >STOP ANIMATION
to stop the program. From your window project, you can animate your program by
clicking suitable button as shown below. Explain your observation.
asalam.saad,pkt.jun2012
19. Observe the simulation output.
Discussion/Question:
Write a C program to solve the problem below, all the program need to be send in the report with
print screen for result.
1. Observes the blinking speed of LED, suggest how to increase and decrease the speed of the
blinking. Show the instruction in C language. How to increase the delay time (in for loop).
2. The Aviation System Application need 5 LED to blink at the same time. As an Embedded
System Engineer, show how the modified code to make 5 LED from any pin at PORTD to
blink.
3. Make a program in order to 2 LED in PORTC and 2 LED in PORTD to blink.
4. Create a program to light up LED as diagram below in LATC.
Conclusion:
Write your conclusions regarding to this Experiment 1. What you have learned from this experiment.
asalam.saad,pkt.jun2012