Anda di halaman 1dari 11

LAB REPORT

EC501- EMBEDDED SYSTEM APPLICATION


LAB

TITTLE :

3
C LANGUAGE PROGRAMMING

GROUP MEMBER:
NO.

NAME

REG. NUMBER

MOHAMAD AMERUDIN BIN RAMLI

16DET12F1047

BADRUL AMIN BIN MOHD RADZI

16DET12F1061

16DET12F10

16DET12F10

Cover page
Objective
Equipment

/5

Background
Theory

/10

Procedures Data
collection

/20

Discussion

/30

Conclusion

/15

Total Marks

/100

EXPERIMENT
TITLE
OBJECTIVES

MATERIAL AND
APPARATUS
THEORY

3
C LANGUAGE PROGRAMMING
At the end of this lab session, the student should be able to:
1. Create and manage Project C programming language.
2. Explain and Construct Basic I/O and Simple delay .
3. Examine the structure of C program for PIC 18.
ESPIC40C Trainer board , Personal Computer with MPLAB IDE, Serial
Bootloader and, PIC18F45K22 Datasheet.
As the scale of microcomputer based systems has increased over the
years, productivity and maintainability using Assembly language has
become an issue. As a result, C language has become a popular
alternative.
The following explains the main features of the C language and
describes how to write a program in "C".
Features of the C Language
(1) An easily traceable program can be written.
The basics of structured programming, i.e., "sequential processing",
"branch processing", and "repeat processing", can all be written in a
control statement. For this reason, it is possible to write a program
whose flow of processing can easily be traced.
(2) A program can easily be divided into modules.
A program written in the C language consists of basic units called
"functions". Since
functions have their parameters highly independent of others, a
program can easily be
made into parts and can easily be reused. Furthermore, modules
written in the assembly language can be incorporated into a C
language program directly without modification.
(3) An easily maintainable program can be written.
For reasons (1) and (2) above, the program after being put into
operation can easily be
maintained. Furthermore, since the C language is based on standard
specifications
(ANSI standard(Note)), a program written in the C language can be
ported into other
types of microcomputers after only a minor modification of the source
program.
Rules on C language
The following lists the six items that need to be observed when writing
a C language
program:

(1) As a convention, use lowercase letters to write a program.


(2) Separate executable statements in a program with a semicolon ";".
(3) Enclose execution units of functions or control statements with
brackets "{" and "}"
(4) Functions and variables require type declaration.
(5) Reserved words cannot be used in identifiers (e.g., function names
and variable
names).
(6) Write comments between "/" and "/".

PROCEDURES:
EXERCISES 1 (CREATE NEW PROJECT)
After finished install the MPLAB IDE, we are going to show you how to create a
project using it. There is
just a few simple step to follow on how to create it. Please follow the step below:
1. Go to to MENU BAR and click Project- > Project Wizard...

2. Click Next> . On the Device menu, choose PIC18F45K22 because we are going
to use this
PIC MCU for coming project.

3. Choose HI-TECH Universal Toolsuite in Active Toolsuite. A HI-TECH ANSI C


Compiler
will show up in the Toolsuite Content.

4. After that, choose a destination where the project you create to be save as. And
also the project
name. After that, click NEXT>.
5. In step 4, there are no existing file to be added so we can skip this step by just
click NEXT> FINISH.
After that, a project are created with the project name you added earlier.
6. Create New File -> Save as LAB3.c
7. Source Files -> Add Files ->DIR -> LAB3.c (From your project folder)

PROGRAMMER SETUP
In here, we want our compiler to automatically create the .HEX file into our PIC MCU
after compiler. There
will be just only a few step to do it. Please follow the instruction below:
1. Go to MENU BAR. Programmer> Select Programmer> None (Bootloader)
or PICkit 3 For this exercises click None.
2. Again, go to Configure > Select Device > PIC18F45K22.OPEN PROJECT

There are only few simple step to open an project. 1st Open the project file.
Then, double click
the project file with MPLAB logo.

EXERCISES 2

(BASIC I/O AND SIMPLE DELAY )

1. Write the source code as shown below.


/***************************************************************/
/*
EC501 EMBEDDED SYSTEMS
/* FILE
: Lab3.c
/* DATE
:
/* CPU GROUP : PIC18LF45K22
/* Description : Basic I/O, simple delay
/**************************************************************/
//Reading header file
#include <p18Lf45k22.h>
//Function proto-type declaration;
void Initialize(void);
void msDelay(unsigned int);

*/
*/
*/
*/
*/

//Macro definition
#define LED
LATBbits.LATB0
//Program Utama
void main(void)
{
unsigned char d,i; //local variables
OSCCON=0x52;
//PIC18FXXX : INTOSC 4MHz
Initialize();
//Main Loop
while(1){
LATB=0;
LED=1;
msDelay(500);
LED=0;
msDelay(500);
}
}
//Init Micro-controller
void Initialize(void)
{
ANSELB=0;
//Analog selection
TRISB=0b11110000;
//PORTB I/O Direction (0:Output 1:Input)
}
//mili-second delay (max 65535ms)
void msDelay(unsigned int ms)
{
unsigned char i;
while(ms){
ms--;
i=122;
//4MHz clock
//i=244;
//8MHz clock
while(i) i--;
}
}

2. Press "CTRL + F10" for the first time or " F10" to compile. You also can click
'Build with Compiler' icon to compile. Do the same procedure till ********** Build
successful! **********

3. Write to the project board. Reset the project board.


4. Change the instructions code to change blinking/delay time and change output to
another LEDs port.
5. Write the code below. Save as LAB3B.
#include <p18Lf45k22.h>
//Function proto-type declaration;
void Initialize(void);
void msDelay(unsigned int);
//Macro definition
#define LED
#define LED1

LATBbits.LATB0
LATBbits.LATB1

#define SW1
PORTEbits.RE0
#define SW2
PORTEbits.RE1
//Program Utama
void main(void)
{
unsigned char d,i; //local variables
OSCCON=0x52;
Initialize();

//PIC18FXXX : INTOSC 4MHz

//Main Loop
while(1){
if (!SW1){
LATB=0;
LED=1;
msDelay(500);
LED=0;
msDelay(500);
}
if (!SW2){
LATB=0;
LED1=1;
msDelay(500);
LED1=0;
msDelay(500);
}
}
}
//Init Micro-controller
void Initialize(void)
{
ANSELB=0;
ANSELE=0;
TRISB=0b11110000;

//Analog selection

//PORTB I/O Direction (0:Output 1:Input)


}
//mili-second delay (max 65535ms)
void msDelay(unsigned int ms)
{
unsigned char i;
while(ms){
ms--;
i=122;
//i=244;
while(i) i

//4MHz clock
//8MHz clock

Source code

6. Insert the instructions code to activate all the switches (SW1,SW2 and SW3)
LAB3C

Anda mungkin juga menyukai