Embedded System Lab Report 1
Embedded System Lab Report 1
OBJECTIVES
To enable us to write assembly language code for the 8051/8052 microcontroller
capable of:
1. Data Manipulation
2. Arithmetic and logic operations
3. Looping and branching techniques
4. Subroutine calls
Equipment Required
Simulation Software: KEIL, Vision-Embedded development tool, Proteus Design
Suite – Professional PCB layout, circuit design and simulation tool.
In-System Programming (ISP) Software: ProgISP – An in-system-programmable
tool to load HEX files in to micro-controller.
Theory
8051 Microcontroller
Features of 8051
Instruction set
o Arithmetic Instructions
o Logical Instructions
PROBLEMS
1. Write code to add the numbers 897F9AH and 34BC48H and save the result in internal
RAM starting at 40H. The result should be displayed continuously on the LEDs of
the development board starting from least significant byte with an appropriate timing
interval between each byte. Use port zero (P0) of the micro-controller to interface
with LEDs.
2. Implement a subroutine that replaces the SWAP instruction using rotate right
instructions. Test your program on the contents of the accumulator when it contains
the number 6BH.
3. Multiply, by using looping and successive addition technique, the data in RAM location
22H by the data in RAM location 15H and put the result in RAM locations 19H (low
byte) and 1AH (high byte). Data in 22H should be FFH and data in 15H should be DEH.
4. Divide, by using looping and successive subtraction technique, the data in RAM
location 3EH by the number 12H; put the quotient in R4 and remainder in R5. Data in
3EH should be AF.
#include<regx51.h>
unsigned int dividend _at_ 0x3E;
void delay(int i)
{
unsigned int j,k;
for(j=0;j<i;j++)
for(k=0;k<1000;k++);
}
void main(void)
{
unsigned int divisor = 0x12;
unsigned int quotient = 0x00;
dividend = 0xAF;
while(dividend >= divisor)
{
dividend-=divisor;
quotient++;
}
while(1)
{
P0 = quotient;
delay(10000);
P0 = dividend;
delay(10000);
}
}
5. Store ten hexadecimal numbers in internal RAM starting from memory location 50H.
The list of numbers to be used is: D6H, F2H, E4H, A8H, CEH, B9H, FAH, AEH,
BAH, CCH. Implement a subroutine that extracts both the smallest and largest
numbers from the stored numbers.
Code in C:
#include<regx51.h>
unsigned int nums[10] _at_ 0x50;
void delay(int i)
{
unsigned int j,k;
for(j=0;j<i;j++)
for(k=0;k<1000;k++);
}
void main( )
{
int largest,smallest,i;
nums[0]=0xD6;
nums[1]=0xF2;
nums[2]=0xE4;
nums[3]=0xA8;
nums[4]=0xCE;
nums[5]=0xB9;
nums[6]=0xFA;
nums[7]=0xAE;
nums[8]=0xBA;
nums[9]=0xCC;
largest = nums[0];
smallest = nums[0];
for(i=1;i<10;i++)
{
if(nums[i]<smallest)
{
smallest = nums[i];
}
}
for(i=1;i<10;i++)
{
if(nums[i]>largest)
{
largest = nums[i];
}
}
while(1)
{
P0=smallest;
delay(10000);
P0=largest;
delay(10000);
}
}
6. Store ten hexadecimal numbers in internal RAM starting from memory location 60H.
The list of numbers to be used is: A5H, FDH, 67H, 42H, DFH, 9AH, 84H, 1BH, C7H,
31H. Implement a subroutine that orders the numbers in ascending order using bubble
or any other sort algorithm and implement s subroutine that order the numbers in
descending order using selection sort algorithm.
Code (i) in C:
#include<regx51.h>
unsigned nums[10] _at_ 0x60;
void delay(int i)
{
unsigned int j,k;
for(j=0;j<i;j++)
for(k=0;k<1000;k++);
}
void main( )
{
int i,j,temp;
nums[0]=0xA5; nums[1]=0xFD; nums[2]=0x67; nums[3]=0x42; nums[4]=0xDF;
nums[5]=0x9A; nums[6]=0x84; nums[7]=0x1B; nums[8]=0xC7; nums[9]=0x31;
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
if(nums[j]>nums[j+1])
{
nums[j+1]; temp;
}
}
}
temp = nums[j];
nums[j] =
nums[j+1] =
while(1)
{
for(i=0;i<10;i++)
{
P0 = nums[i];
delay(10000);
}
}
}
Code (ii) in C:
#include<regx51.h>
unsigned int nums[10] _at_ 0x60;
void delay(int i){
unsigned int j,k;
for(j=0;j<i;j++)
for(k=0;k<1000;k++);
}
void main( ){
int i,j,largest,laarge,datnum;
nums[0]=0xA5; nums[1]=0xFD; nums[2]=0x67; nums[3]=0x42; nums[4]=0xDF; nums[5]=0x9A; nums[6]=0x84;
nums[7]=0x1B; nums[8]=0xC7; nums[9]=0x31;
largest = nums[i];
laarge = nums[i];
for(j=i+1;j<=9;j++)
{
if(largest<nums[j])
{
largest = nums[j]; datnum = j;
}
}
nums[i]=nums[datnum];
nums[datnum]=laarge;
while(1)
{
for(i=0;i<10;i++)
{
P0 = nums[i];
delay(10000);
}
}
}
7. Store numbers from 00H to 20H in internal RAM starting from memory location 40H.
Implement a subroutine that extracts only the prime numbers.
Code in C:
#include <reg51.h>
unsigned char data d[21] _at_ 0x40;
if(isprime(d[i]))
a[count++] = d[i];
while(1)
P0 = a[i];
delay(1000);
}}
}
8. Find the factorial of a number stored in R3. The value in R3 could be any number in
the range from 00H to 05H. Implement a subroutine that calculates the factorial. The
factorial needs to be represented in both hexadecimal and decimal formats.
#include<reg51.h>
void main()
{
unsigned int val = 0x5; unsigned int fact = 0x1;
unsigned char i;
unsigned char x, d1, d2, d3;
x = fact / 0xa;
d1 = fact % 0xa;
d2 = x % 0xa;
d3 = x / 0xa;
while(1)
{
P0 = fact;
delay(1000); P0 = d1;
delay(1000); P0 = d2;
delay(1000); P0 = d3;
delay(1000);
}
}