Embedded C Basic Lab Manual 21EC481

You are on page 1of 33

Acharya Institute of Technology

Achit Nagar, Dr. Sarvepalli Radhakrishnan Road


Soladevanahalli, Bengaluru-560107
Department of Electronics and Communication

Ability Enhancement Course

Embedded C Basics
LABORATORY MANUAL

(21EC481)
IV Semester

Prepared by:
Dr. Raghunath B H

2022-23

DEPT. OF ECE, AIT 1


Course Details:
Course Objectives :
• Understand the architecture and instruction set of 8051 microcontroller.
• Understand the basic programming of microcontroller

Course Outcomes :
CO1: Write the C programs in 8051 for solving problems.

CO2: Simulate/Demonstrate the experiment with given specifications.

CO3: Tabulate, Validate the readings and infer the results logically.

CO4: Interpret the concepts and results both orally and written.

Assessment of course outcomes:


COs Program Outcomes PSO BL
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3 BL

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

DEPT. OF ECE, AIT 1


Embedded C Basics 2022-23

List of Experiments

1. Write a 8051 C program to multiply two 16 bit binary numbers.


2. Write a 8051 C program to find the sum of first 10 integer numbers.
3. Write a 8051 C program to find factorial of a given numbers.
4. Write a 8051 C program to add an array of 16 bit numbers and store the 32 bit result in internal
RAM.
5. Write a 8051 C program to find the square of a number ( 1 to 10) using look-up table.
6. Write a 8051 C program to find the largest/smallest number in an array of numbers.
7. Write a 8051 C program to arrange a series of numbers in ascending /descending
order locations.
8. Write a 8051 C program to count the number of ones and zeros in two consecutive
in memory locations.
9. Write a 8051 C program to scan a series of numbers to find how many are negative.
10. Write a 8051 C program to display “ Hello World” message (either in simulation mode
or interface an LCD display).
11. 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).

3
Embedded C Basics 2022-23

Introduction to Keil Software


Keil is a software development tool for embedded microcontroller applications.
In Keil’s µVision IDE, the toolsets provide a powerful, easy to use and easy to learn environment for
developing embedded applications.
The minimum hardware and software requirements for installation:
• A standard PC running Microsoft Windows XP, or Windows Vista
• 1GB RAM and 500 MB of available hard-disk space is recommended
• 1024x768 or higher screen resolution; a mouse or other pointing device
• download from https://www.keil.com/download/product/

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

Three windows in the screen :


1. Project work space window – it shows all the related files connected with your project.
2. Editing window – Used to edit the code
3. Output window – It shows the output after compiling/building/running the project

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.

Type the Program.

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

Check the answer in the callstack window.

TO check output in Ports

To check putput in serial winow

15
Embedded C Basics 2022-23

1. Write a 8051 C program to multiply two 16 bit binary numbers.

#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:

Video tutorial : https://youtu.be/zXIlcylxfWE


Scan to play in youtube. Click on Subscribe the channel for all experiments video tutorial

16
Embedded C Basics 2022-23

2. Write a 8051 C program to find the sum of first 10 integer numbers.

#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;
}

Output: 1+2+3+4+5+6+7+8+9+10 = 55 (0x37) in Hexadecimal

https://youtu.be/fdFlHLOgpBQ

17
Embedded C Basics 2022-23

3. Write a 8051 C program to find factorial of a given number.

#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;

for ( i = 1; i <= num; i++)


{
factorial = factorial*i;
}
P0= factorial;
P1= (factorial & 0xff00) >>8;
P2= (factorial & 0xff0000) >>16;
P3= (factorial & 0xff000000) >>24;
}

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;

for (i = 0; i < 5; i++)


{
sum =sum + array[i];
}
P0 = sum & 0xFF;
P1= (sum & 0xff00)>>8;
P2=(sum & 0xFF0000)>>16;
}

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

6. 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,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

8. Write a 8051 C program to arrange a series of 32 bit numbers in 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;
}
}
}
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>

void main (void)


{
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD = 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xFD; /* TH1: reload value for 9600 baudrate */
TR1 = 1; /* TR1: timer 1 run */
TI = 1; /* TI: set TI to send first char of UART */
while (1)
{
printf ("Hello World ! \n ");
}
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy