Lab 3 Basic C Programming
Lab 3 Basic C Programming
EXPERIMENT
TITLE
OBJECTIVES
MATERIAL AND
APPARATUS
THEORY
3
C
At
1.
2.
3.
LANGUAGE PROGRAMMING
the end of this lab session, the student should be able to:
Create and manage Project C programming language.
Explain and Construct Basic I/O and Simple delay .
Examine the structure of C program for PIC 18.
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
*/
*/
*/
*/
*/
}
}
//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!
**********
LATBbits.LATB0
LATBbits.LATB1
PORTEbits.RE0
PORTEbits.RE1
//local variables
//PIC18FXXX : INTOSC 4MHz
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
//4MHz clock
//8MHz clock
}
6. Insert the instructions code to activate all the switches (SW1,SW2 and SW3)
QUESTIONS
1.
2.
List three Basic Forms of Structured Programming and sketch the flow chart for
each form.
3.
Draw the flow chart for the above programming code (LAB3B).