Professional Documents
Culture Documents
Fall 2021
Lab 2
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;
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.
int a, b, c;
a = 7;
c = a * b;
This represents the value of variable c. here so the value will be the multiplication
of variable a and b.
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;
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 :