Embedded C Basic Lab Manual 21EC481
Embedded C Basic Lab Manual 21EC481
Embedded C Basic Lab Manual 21EC481
Embedded C Basics
LABORATORY MANUAL
(21EC481)
IV Semester
Prepared by:
Dr. Raghunath B H
2022-23
Course Outcomes :
CO1: Write the C programs in 8051 for solving problems.
CO3: Tabulate, Validate the readings and infer the results logically.
CO4: Interpret the concepts and results both orally and written.
CO1 3 2 1 2 3 L2
CO2 3 2 2 2 1 2 3 L2
CO3 3 2 2 2 2 1 2 3 L3
CO4 3 2 2 2 2 1 2 3 L3
List of Experiments
3
Embedded C Basics 2022-23
8051 Highlights :
• Fast interrupt service routines with two or four priority levels and up to 32- vectored interrupts
• Four register banks for minimum interrupt prolog/epilog
• Bit-addressable space for efficient logical operations
• 128 Bytes of Special Function Register (SFR) space for tight integration of on-chip
peripherals. Some devices extend the SFR space using paging.
• Low-power, high-speed devices up to 100 MIPS are available 8051 Development Tool
Support The Keil C51 Compiler and the Keil Linker/Locator provide optimum 8051
architecture support with the following features and C language extensions.
• Interrupt functions with register bank support are written directly in C
• Bit and bit-addressable variables for optimal Boolean data type support
• Compile-time stack with data overlaying uses direct memory access and gives high-speed
code with little overhead compared to assembly programming
• Reentrant functions for usage by multiple interrupt or task threats
• Generic and memory-specific pointers provide flexible memory access
• Linker Code Packing gives utmost code density by reusing identical program sequences
• Code and Variable Banking expand the physical memory address space
• Absolute Variable Locating enables peripheral access and memory sharing
4
Embedded C Basics 2022-23
To launch µVision, click the µVision icon on desktop or select µVision from the start menu.
5
Embedded C Basics 2022-23
6
Embedded C Basics 2022-23
• Start the Keil software. Go to the Project > New Project then choose a location to store your
program, and give a name without extension, then click on Save.
• Now in the next window select the device from different manufacturers. Select Microchip,
and then by expanding select AT89C51 device and click ok.
OR
type AT89C51 in the search tab. Device will be shown. Select the device and click OK.
7
Embedded C Basics 2022-23
Click on yes.
8
Embedded C Basics 2022-23
Right click on source group and add new item to source group.
9
Embedded C Basics 2022-23
select C file and give file name as mul16.c and click on “Add” button.
10
Embedded C Basics 2022-23
• Go to the Project > click on Build Target. If any errors, it will be displayed on output window.
11
Embedded C Basics 2022-23
12
Embedded C Basics 2022-23
Click on OK button
13
Embedded C Basics 2022-23
14
Embedded C Basics 2022-23
15
Embedded C Basics 2022-23
#include <reg51.h>
void main()
{
while (1)
{
unsigned int num1, num2;
unsigned long int product;
num1 = 0x2222;
num2 = 0xBBBB;
product = (unsigned long int)num1 * num2;
P0= product &0xff;
P1= (product & 0xff00)>>8;
P2 =(product & 0xff0000)>>16;
P3 =(product & 0xff000000)>>24;
}
}
Output:
16
Embedded C Basics 2022-23
#include <reg51.h>
void main ( )
{
unsigned char sum=0;
unsigned char i;
for (i=1; i<=10; i++)
{
sum = sum + i;
}
ACC=sum;
P0=sum;
}
https://youtu.be/fdFlHLOgpBQ
17
Embedded C Basics 2022-23
#include <reg51.h>
#include <stdio.h>
void main()
{
unsigned int i;
unsigned char num = 12; // The number to find the factorial of
unsigned long factorial = 1;
https://youtu.be/yaARZTmMoMM
18
Embedded C Basics 2022-23
4. Write a 8051 C program to add an array of 16 bit numbers and send the sum to ports
#include <reg51.h>
void main()
{
unsigned int i, array[5] = { 0x1111, 0x2222, 0x8888, 0x4444, 0xABCD };
unsigned long sum = 0;
https://youtu.be/Lyws-Juk4LQ
19
Embedded C Basics 2022-23
5. Write a 8051 C program to find the square of a number ( 1 to 10) using look-up table.
#include <reg51.h>
void main ( )
{
unsigned char LUT[]={1,4,9,16,25,36,49,64,81,100};
unsigned char num, square;
for(num=1; num<11; num++)
{
square =LUT[num-1];
P0=square;
}
https://youtu.be/riqS4a7mfbg
20
Embedded C Basics 2022-23
#include <reg51.h>
void main ( )
{
unsigned long array[]={0x33334444,0x99998888,0xffffbbbbb,0x55559999,0x11110000};
unsigned long i,largest=0;
for(i=0;i<5;i++)
{
if(largest < array[i])
largest=array[i];
}
P0= largest & 0xff;
P1= (largest & 0xff00)>>8;
P2 =(largest & 0xff0000)>>16;
P3 =(largest & 0xff000000)>>24;
while(1);
}
Before Execution
21
Embedded C Basics 2022-23
After Execution
6b. Write a 8051 C program to find the largest number in an array of 32 numbers
#include <reg51.h>
void main ( )
{
unsigned long array[]={0x33334444,0x10008888,0xffffbbbb,0x00009999,0x11110000};
unsigned long i, smallest=0xFFFFFFFF;
for(i=0;i<5;i++)
{
if(smallest > array[i])
smallest=array[i];
}
P0= smallest & 0xff;
P1= (smallest & 0xff00)>>8;
P2 =(smallest & 0xff0000)>>16;
P3 =(smallest & 0xff000000)>>24;
22
Embedded C Basics 2022-23
while(1);
}
Before Execution
After Execution
https://youtu.be/M2xuLDhTE50
23
Embedded C Basics 2022-23
7. Write
a 8051 C
program to
arrange a
series of 32
bit numbers
in ascending
/descending
order
locations.
#include
<reg51.h>
void main ( )
{
unsigned long array[]={0x33556666, 0xCCAADD00, 0x55998888, 0x77664444, 0x11223344};
unsigned long temp,i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(array[j] > array[j+1])
{
temp=array[j+1];
array[j+1]=array[j];
array[j]=temp;
}
}
}
}
Before Execution
24
Embedded C Basics 2022-23
After Aexecution
https://youtu.be/OCNCUbuQHm4
for descending order if(array[j] > array[j+1] change to if(array[j] < array[j+1]
25
Embedded C Basics 2022-23
#include <reg51.h>
void main ( )
{
unsigned long array[]={0x33556666, 0xCCAADD00, 0x55998888, 0x77664444, 0x11223344};
unsigned long temp,i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(array[j] < array[j+1])
{
temp=array[j+1];
array[j+1]=array[j];
array[j]=temp;
}
}
}
while(1);
Before
26
Embedded C Basics 2022-23
After execution
27
Embedded C Basics 2022-23
9. Write a 8051 C program to count the number of ones and zeros in two consecutive
in memory locations.
#include <reg51.h>
void main ( )
{
unsigned char array[]={0x57,0xfc};
unsigned char i,ones, zeros;
CY=0;
for(i=0;i<8;i++)
{
array[0]>>=1;
if(CY==1) ones++;
else zeros++;
}
for(i=0;i<8;i++)
{
array[1]>>=1;
if(CY==1) ones++;
else zeros++;
}
P0=zeros;
P1=ones;
while(1);
Before Execution
28
Embedded C Basics 2022-23
After Execution
https://youtu.be/aDopleeAUzs
29
Embedded C Basics 2022-23
10. Write a 8051 C program to scan a series of numbers to find how many are negative.
#include <reg51.h>
void main ( )
{
unsigned long temp, array[]={0xff223344,0xaa336699,0x11223344,0x33445566,0x88aa3311};
unsigned char i, pos, neg;
CY=0;
for(i=0;i<5;i++)
{
temp = array[i]<< 1;
if(CY==1) neg++;
else pos++;
CY=0;
}
P0=neg;
P1=pos;
while(1);
}
30
Embedded C Basics 2022-23
https://youtu.be/IlA0z6SEp4k
31
Embedded C Basics 2022-23
11 .Write a 8051 C program to display “ Hello World” message in UART serial window
#include <reg51.h>
#include <stdio.h>
https://youtu.be/LQ4MM9cHKDE
32
Embedded C Basics 2022-23
12. Write a 8051 C program to convert the hexadecimal data 0xCFh to decimal and
display the digits on ports P0, P1 and P2 (port window in simulator).
# include <reg51.h>
void main (void)
{
unsigned char hexa=0xFF;
unsigned char hundreds, tens, units;
hexa=hexa/10;
P0=B;
units=B;
hexa = hexa/10;
hundreds=ACC;
tens=B;
P1=B;
P2=ACC;
while(1);
}
Ouput :
https://youtu.be/1YYZ_GOOiQg
33