You are on page 1of 7

COMP551 – Interfacing

Fall 2021

Lab 2

Using MPLAB and C18 to Compile a Simple Program

Student:

Student ID:

Section:

NOTE: Labs are due at the start of the next lab period.

/conversion/tmp/activity_task_scratch/550304408.docx
2.2.2 Program Explanation

Explain what each line of code does in the Basic Calculations example;

#include <stdio.h>, #include <P18F458.h>

#include tells the processor to insert particular file,

stdio.h - is the header file for standard input and output. This is used to get the
input from the user and output result text to the monitor
#include<P18F458.h> stores input output functions related to microcontroller
P18F458.

#pragma config WDT = OFF

#pragma config control the initial settings of the PIC controller.

Here, WDT stands for watchdog Timer reset

void main (void)

This is the main function, which returns no value.

This indicates the loop start

int a, b, c;

this is a declaration of variable named a, b and c with datatype of integer which


allocates space in memory to store value.

a = 7;

This represent the value of variable a is equal to 7.


COMP551 – Interfacing Page 2 of 7
b = 5;

This represent the value of variable b is equal to 5.

c = a * b;

This represents the value of variable c. here so the value will be the multiplication
of variable a and b.

Printf ("%d x %d = %d\n", a, b, c);

as print f function is output function which will display (here) 7 * 5 = 35, %d is to


refer the int value, and after the first comma a indicates to display value of at
first %d. similarly, after second comma b tells processor to print value of b on
screen at second %d. and so on.

This indicates the end of loop.

Exercises:

1. Modify the Hello World program to use a for loop to print the phrase “Hello World,
my student number is; <insert student number>”, 20 times.

Code :
COMP551 – Interfacing Page 3 of 7
#include<P18F458.h>
#include<stdio.h>
#pragma config WDT = OFF
int main()
{
int i=0;
for (i = 1; i <= 20; i++)
{
printf( "Hello world!, my student number 10266303 is \n");
}

while(1);
}

Output :

2. Modify the Hello World program to use a while loop to print the phrase “Hello World,
my student number is; <insert student number>”, 30 times.

Code :
COMP551 – Interfacing Page 4 of 7
#include<P18F458.h>
#include<stdio.h>
#pragma config WDT = OFF
int main()
{
int i=1;
while(i<=30)
{
printf("Hello world!, my student number is 10266303 \n",i);
i++;

}
while(31);
}

Output :

3. Modify the Hello World program to use a do/while loop to print the phrase “Hello
World, my student number is; <insert student number>”, 40 times.

Code :

#include<P18F458.h>
#include <stdio.h>
int main(void)
{
int i = 0;

COMP551 – Interfacing Page 5 of 7


do
{
printf("Hellow world!, my student id is 10266303 is \n");
i++;
}
while( i <=40);
while(41);
}
Output:

4. Write a program to convert the temperature from degrees Fahrenheit to


degrees Celsius, from -300 degrees F to +300 degrees F in increments of 10
degrees F. The program should display the table of results in the output
window only once.

Code :
#include<P18F458.h>
#include <stdio.h>
#pragma config WDT = OFF

void main(void)
{
COMP551 – Interfacing Page 6 of 7
int c,f;
for(f=-300;f<=300;f++)
if(f%10==0)
{
c=(f-32)*5/9;
printf("Temperature in celcius is %d;\n",c);
}
while(1);
}
Output :

COMP551 – Interfacing Page 7 of 7

You might also like