0% found this document useful (0 votes)
107 views

Basic Embedded C Programs Lab Manual

This document contains a list of 30 embedded C basic programs with descriptions. It covers topics such as arithmetic operators, conditional statements, bitwise operators, and conversions between number systems. The programs provide examples of simple calculations, conditional logic, and bit manipulation operations to demonstrate basic embedded C programming concepts.

Uploaded by

N Sornakumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

Basic Embedded C Programs Lab Manual

This document contains a list of 30 embedded C basic programs with descriptions. It covers topics such as arithmetic operators, conditional statements, bitwise operators, and conversions between number systems. The programs provide examples of simple calculations, conditional logic, and bit manipulation operations to demonstrate basic embedded C programming concepts.

Uploaded by

N Sornakumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1

EMBEDDED C BASIC PROGRAM

No Name of the program Page No


A. Simple Program
1 Display given value to all the port 2
B. Arithmetic operators
2 Simple Addition Program 2
3 Addition with carry 2
4 Simple Subtraction Program 3
5 Simple Multiplication Program 3
6 Simple Division Program 3
7 Simple Modulo Division Program 3
8 Simple program for pre increment 4
9 Simple program for post increment 4
10 Simple program for pre decrement 4
11 Simple program for post decrement 5
12 Swapping of two numbers using arithmetic operators 5
13 Hexa to decimal conversion 5
14 Hexa to ascii conversion 6
15 Hexa to bcd conversion 6
C. Conditional statement with relational operators & logical operators
16 Simple if program 6
17 Simple if else program 7
18 Simple else if program 7
19 Simple nested if program 8
20 Simple while program 8
21 Simple do while program 8
22 Simple for loop program 9
23 Simple switch statement program 9
24 Largest of three numbers 10
25 Smallest of three numbers 11
D. Bitwise Operators
26 Simple AND operation program 11
27 Simple OR operation program 11
28 Simple XOR operation program 12
29 Swapping of two numbers using Bitwise operators 12
2

A. SIMPLE PROGRAM

1. Display the given value to the all the port

#include<reg51.h> //header file


void main() //main function start
{
P1=0x01; //given data to port 1
P2=0x02; //given data to port 2
P3=0x03; //given data to port 3
}

B. ARITHMETIC OPERATORS
2. Simple Addition program
#include<reg51.h> //header file
void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=0x01; //assign value
b=0x02; //assign value
c=a+b; //add both a and b values
P1=c; //show the addition value to port 1
}

3. Addition With Carry


#include<reg51.h> //header file
void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=0xFF; //assign value
b=0x90; //assign value
c=a+b; //add both a and b values
P1=c; //show the addition value to port 1
P2=c>>8; //carry will be showed by port 2
}
3

4. Simple subtraction Program

#include<reg51.h> //header file


void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=0x02; //assign value
b=0x01; //assign value
c=a-b; //sub both a and b values
P1=c; //show the subtraction value to port 1
}

5. Simple Multiplication program

#include<reg51.h> //header file


void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=0x02; //assign value
b=0x03; //assign value
c=a*b; //multiply both a and b values
P1=c; //show the multiplication value to port 1
}

6. Simple Division program

#include<reg51.h> //header file


void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=0x06; //assign value
b=0x03; //assign value
c=a/b; //divide both a and b values
P1=c; //show the division value to port 1
}

7. Simple Modulo division program

#include<reg51.h> //header file


void main() //main function
{
4

int a,b,c; //declare some variable using_ integer data_ type


a=0x07; //assign value
b=0x02; //assign value
c=a%b; //modulo division of both a and b values
P1=c; //show the modulo division value to port 1
}

8. Simple program for pre increment


#include<reg51.h> //header file
void main() //main function
{
int a,c; //declare some variable using_ integer data_ type
a=0x07; //assign value
c=++a; //increment the value of a
P1=c; //show the increment value to port 1
}

9. Simple program for post increment


#include<reg51.h> //header file
void main() //main function
{
int a,c; //declare some variable using_ integer data_ type
a=0x07; //assign value
c=a++; //increment the value of a
P1=c; //show the increment value to port 1
}

10. Simple program for pre decrement

#include<reg51.h> //header file


void main() //main function
{
int a,c; //declare some variable using_ integer data_ type
a=0x07; //assign value
c=--a; //decrement the value of a
P1=c; //show the decrement value to port 1
}
5

11. Simple program for post decrement

#include<reg51.h> //header file


void main() //main function
{
int a,c; //declare some variable using_ integer data_ type
a=0x07; //assign value
c=a--; //decrement the value of a
P1=c; //show the decrement value to port 1
}

12. Swapping of two number using arithmetic operators


#include<reg51.h> //header file
void main() //main function
{
int a,b; //declare some variable using_ integer data_ type
a=10; //assign value
b=15; //assign value
P1=a; //show the a and b starting values both port1 and 2
P2=b;
a=a+b; //swapping operation
b=a-b; //swapping operation a value swap to b
a=a-b; //swapping operation b value swap to a
P1=a; //show the swapping values both port 1 and 2
P2=b;
}

13. Hexa decimal to decimal conversion

#include<reg51.h> //header file


void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=0x89; //assign value
P1=a; //show the hexa value to port 1
b=a/0x10; //decimal conversion
c=a%0x10; //decimal conversion
P2=b; //decimal value showed by port2
P3=c; //decimal value showed by port3
}
6

14. Hexa decimal to ascii conversion

#include<reg51.h> //header file


void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=0x89; //assign value
P1=a; //show the hexa value to port 1
b=a/0x10; //ascii conversion
c=a%0x10; //ascii conversion
P2=b+0x48; //ascii value showed by port2
P3=c+0x48; //ascii value showed by port3
}

15. Hexa decimal to bcd conversion

#include<reg51.h> //header file


void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=0x89; //assign value
P1=a; //show the hexa value to port 1
b=a/0x10; //bcd conversion
c=a%0x10; //bcd conversion
P2=b+0x30; //bcd value showed by port2
P3=c+0x40; //bcd value showed by port3
}

C. CONDITIONAL STATEMENT WITH


RELATIONAL OPERATORS, LOGICAL OPERATORS
16. Simple if program

#include<reg51.h> //header file


void main() //main function
{
int a; //declare some variable using_ integer data_ type
a=15; //assign value
if(a>10) //a value is greater than 10 so goes to inside the loop
{
P1=0x00; //show the inside loop value port 1
7

}
}

17. Simple if else program

#include<reg51.h> //header file


void main() //main function
{
int a; //declare some variable using_ integer data_ type
a=5; //assign value
if(a>10) //a value is greater than 10 so goes to inside the loop
{
P1=0x00; //show the inside loop value port 1
}
else //a value is lesser than 10 goes to else loop
{
P1=0x88; //show the inside loop value port 1
}
}

18. Simple else if program

#include<reg51.h> //header file


void main() //main function
{
int a; //declare some variable using_ integer data_ type
a=50; //assign value
if(0<a>10) //a value is lesser than 0 & greater than 10 so goes to
//inside the loop
{
P1=0x00; //show the inside loop value port 1
}
else if(10<a>20) //a value is lesser than 10 & greater than 20 so goes to
//inside the loop
{
P1=0x88; //show the inside loop value port 1
}
else //if a value is above 20 go inside below loop
{
P1=0x44; //show the inside loop value port 1
}
}
8

19. Simple nested if program

#include<reg51.h> //header file


void main() //main function
{
int a,b; //declare some variable using_ integer data_ type
a=60; //assign value
b=40; //assign value
if(a>20) //if a greater than 20 go inside the loop
{
if(b>10) //if b greater than 10 go inside the loop
{
P1=0x88; //show the inside loop value port 1
}
}
Else //if a lesser than 20 go inside the loop
{
P1=0x66; //show the inside loop value port 1
}
}

20. Simple while program

#include<reg51.h> //header file


void main() //main function
{
int a; //declare some variable using_ integer data_ type
a=1; //assign value
while(a) //while the condition satisfy loop is continuously running
{
P1=0x88; //show the inside loop value port 1
}
}

21. Simple do while program

#include<reg51.h> //header file


void main() //main function
{
int a; //declare some variable using_ integer data_ type
9

a=1; //assign value


do //do the first condition
{
a=a+1;
P1=a; //show the inside loop value port 1
}
while(a<20); //while the condition satisfy loop is continuously running
}

22. Simple for loop program

#include<reg51.h> //header file


void main() //main function
{
int i; //declare some variable using_ integer data_ type
for(i=0;i<10;i++) //declare the for loop
{
P1=i; //show the inside loop value port 1
}
}

23. Simple switch statement program

#include<reg51.h> //header file


void main() //main function
{
int a,b; //declare some variable using_ integer data_ type
a=50; //assign value
if(0<a>10) //a value is lesser than 0 & greater than 10 so goes to
//inside the loop
{
b=1;
}
else if(10<a>20) //a value is lesser than 10 & greater than 20 so goes to
//inside the loop
{
b=2;
}
else //if a value is above 20 go inside below loop
{
10

b=3;
}

switch(b) //check the value of b go to run the correct case


{
case 1:
P1=0x01;
break;

case 2:
P1=0x02;
break;

case 3: //if b equal to 3 so run this case


P1=0x03; //show the inside loop value port 1
break;
}
}

24. Largest of three numbers


#include<reg51.h> //header file
void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=8; //assign value
b=9; //assign value
c=10; //assign value
if(a>b&&a>c) //check a is greater both b and c it is no else go to next
//condition
{
P1=0x08; //show the inside loop value port 1
}
else if(b>c) //check b is greater than c it is no go to next condition
{
P1=0x09; //show the inside loop value port 1
}
else //if c is greatest go inside the loop
{
P1=0x0A; //show the inside loop value port 1
}
}
11

25. Smallest of three numbers


#include<reg51.h> //header file
void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=8; //assign value
b=9; //assign value
c=10; //assign value
if(a<b&&a<c) //check a is lesser both b and c it is no else go to next
//condition
{
P1=0x08; //show the inside loop value port 1
}
else if(b<c) //check b is lesser than c it is no go to next condition
{
P1=0x09; //show the inside loop value port 1
}
else //if c is lowest value go inside the loop
{
P1=0x0A; //show the inside loop value port 1
}
}

D. BITWISE OPERATORS
26. Simple AND operation program

#include<reg51.h> //header file


void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=60; //assign value
b=13; //assign value
c=a&b; //AND operation of both a and b values
P1=c; //show the addition value to port 1
}

27. Simple OR operation program

#include<reg51.h> //header file


12

void main() //main function


{
int a,b,c; //declare some variable using_ integer data_ type
a=60; //assign value
b=13; //assign value
c=a|b; //OR operation of both a and b values
P1=c; //show the addition value to port 1
}

28. Simple XOR operation program


#include<reg51.h> //header file
void main() //main function
{
int a,b,c; //declare some variable using_ integer data_ type
a=60; //assign value
b=13; //assign value
c=a^b; //XOR operation of both a and b values
P1=c; //show the addition value to port 1
}

29. Swapping of two number using bitwise operators


#include<reg51.h> //header file
void main() //main function
{
int a,b; //declare some variable using_ integer data_ type
a=10; //assign value
b=15; //assign value
P1=a; //show the a and b starting values both port1 and 2
P2=b;
a=a^b; //swapping operation
b=a^b; //swapping operation a value swap to b
a=a^b; //swapping operation b value swap to a
P1=a; //show the swapping values both port 1 and 2
P2=b;
}
13
14
16

You might also like