Lab Program
Lab Program
ORG 0000h
MOV R0, #40h ; initialize r0 for the source location
MOV R1, #50h ; initialize r1 for destination location
MOV R5, #0Ah ; initialize r5 for the count of 10 data transfer
Repeat: MOV A, @R0 ; transfer the data from source to A reg
MOV@R1,A ; move the source data from A reg to the destination using indirect addressing
INC R0 ; increment r0 to point the next source location
INC R1 ; increment r1 to point the next destination location
DJNZ R5, Repeat ; decrement and jump if not 0
Here: SJMP Here ; pre ending loop
END
2 Write an ALP to exchange 5bytes of data stored in i-RAM 35h and 45h
ORG 0000h
MOV R0, #35h ; Point to the source addr. Using R0 register
MOV R1, #45h ;Point to the destination addr. Using R1 register
MOV R5, #0Ah ; Keep a count for 5 bytes of data exchange
Repeat: MOV A,@R0 ;transfer data at address (@R0) to register A
MOV B,@R1 ; transfer data at address (@R1) to register B
MOV @R1, A ; transfer data from A to 45h
MOV @R0, B ; transfer data at B to 35h
INC R0 ; Point to next source location
INC R1 ; Point to next source location
DJNZ R5, Repeat ; Decrement the count and jump repeat data transfer if count is not zero
Here: SJMP Here ; pre ending loop
END
4 Write an ALP to transfer 8 bytes of data stored in ext. RAM memory location
1200h to ext. RAM memory location 1250h
ORG 0000h
MOV DPH, #12h ; store the higher byte addr. In dph
MOV R0, #00h ;point to the lower byte of source addr. Using R
MOV R1, #50h ;Point to the destination addr. Using R1 register
MOV R5, #08h ;Keep a count for 5 bytes of data transfer
UP: MOV DPL, R0 ;store the lower byte addr. In dpl
MOVX A,@DPTR ; transfer data from dptr location to A
MOV DPL,R1 ; point to lower byte of destination using dpl
MOVX @DPTR, A ;transfer data from A to location pointed by dptr{i.e-50h}
INC R0 ; point to next source
INC R1 ; point to next destination
DJNZ R5, UP ;Decrement the count and jump repeat data transfer if count is not zero
Here: SJMP Here
END
5) Write an ALP to exchange 5 bytes of data from ext. RAM location 1200h to
1250h
ORG 0000h
MOV DPH, #12h ; initialize the HB of the DPTR
MOV R0, #00h ; point to the LB of initial storage location using R0
MOV R1, #50h ; point to the LB of final storage location using R0
MOV R5, #05h ; initialize the count of 5 using reg r5
UP: MOV DPL, R0 ; moving the LB of initial location of the data to DPL
MOVX A,@DPTR ; get the data from the initial location using DPTR via MOVX comm.
MOV B, A ;copy the contents form A reg to B reg
MOV DPL, R1 ;move the LB of the final storage location to DPL
MOVX A, @DPTR ; move the contents from the final location to A reg
MOV DPL, R0 ;move the LB of initial location to DPL
MOVX @DPTR, A ;move the data currently stored in the A reg to the initial location
MOV A ,B ;move the contents of B reg to A reg
MOV DPL, R1 ; move the LB of the final storage loction to DPL
MOVX @DPTR, A ; move the data currently stored in the A reg to the final location
INC R0 ; increment R0
INC R1 ; increment R1
DJNZ R5, UP ; decrement R5 if not zero and jump to up
HERE: SJMP HERE ; short jump to here (pre ending loop)
END
7) Write an ALP to use lookup table technique to find the square of a number
btw 1 to 9 entered in location 30h and store the result in memory 31h
ORG 0000h
MOV A, 31h ;move the LB of the first number to A reg
ADD A, 33h ;Add the number in A reg to the LB of the second number
MOV 52h, A ; store the partial result (LB) in 52h location
MOV A, 30h ; move the HB of the first number to the A reg
ADDC A, 32h ;add with carry from the previous addition the contents of A reg and the 32h
location
MOV 51h, A ;move this result to 51h memory location
JNC Down ;jump if no carry to Down else continue
MOV 50h, #01h ;move o1h to 50h if the carry is present
Down: SJMP Down
END
11) Calculator
ORG 0000h
MOV A, 40h
MOV B, 41h
MOV R0, #30h
CJNE @R0, #00H, SUBT
ADD A,B
JNC L1
MOV B,#01
SJMP STORE
L1: MOV B,#00H
SJMP STORE
SUBT: CJNE @R0, #01H, MULT
SUBB A, B
JNC L2
MOV B, #01H
SJMP STORE
L2: MOV B, #00H
SJMP STORE
MULT: CJNE @R0, #02H, DIVSN
MUL AB
SJMP STORE
DIVSN: CJNE @R0, #03H, INVALID
DIV AB
SJMP STORE
INVALID: MOV 50H, #0FFH
MOV 51H, #0FFH
SJMP DOWN
STORE: MOV 51H, A
MOV 50H, B
DOWN: SJMP DOWN
END
12)Write an ALP to add 8 bit numbers stored at RAM location starting from
50h,store the result in 60h and 61h
ORG 0000H
MOV R0, #50H
MOV R5, #08H
CLR A
REPEAT: ADD A,@R0
JNC DOWN
INC 61H
DOWN: INC R0
DJNZ R5, REPEAT
MOV 60H, A
END
13) Write an ALP for bit wise logic executions, to evaluate 6th and 7th bit of RAM
location
ORG 0000h
MOV A, 50H
MOV B, 51h
JNB 05H, ANDOP
ORL A, B
SJMP STORE
ANDOP:JNB 06H, EXOROP
ANL A, B
SJMP STORE
EXOROP:XRL A, B
STORE: MOV 52H, A
END
16) WRITE AN ALP TO COUNT NUMBER OF ZEROS AND ONES IN A GIVEN 8-BIT
NO., STORED AT MEMORY LOCATIONS 45H. STORE THE NO. OF ONES AT 46 AND
NO. OF ZEROS AT 47 H
ORG 0000H
MOV R5, #08H
MOV A, 45H
UP: RRC A
JC ONES
INC 47H
SJMP DOWN
ONES: INC 46H
DOWN: DJNZ R5, UP
END
21) ALP to get the largest number from an array of 6 numbers starting from
4000h
ORG 0000H
MOV R3, #05H
MOV DPTR, #4000H
MOVX A,@DPTR
MOV R1, A
UP: INC DPTR
MOVX A,@DPTR
CLR C
MOV R2, A
SUBB A, R1
JC SKIP
MOV A, R2
MOV R1, A
SKIP: DJNZ R3, UP
MOV DPL, #62H
MOV A, R1
MOVX @DPTR, A
HERE: SJMP HERE
END
*** (Note: Change JC statement to JNC to get smallest no. in the array)
22) ALP to sort a given array of 6 numbers stored at 9000h in ascending order
(BUBBLE SORT)
ORG 0000H
MOV R0, #05H
PASS: MOV DPTR,#9000H
MOV R1, #05H
COMPR: MOVX A,@DPTR
MOV B, A
INC DPTR
MOVX A,@DPTR
CLR C
MOV R2, A
SUBB A, B
JNC NOXCH
MOV A, B
MOVX @DPTR, A
DEC DPL
MOV A, R2
MOVX @DPTR, A
INC DPTR
NOXCH: DJNZ R1, COMPR
DJNZ R0, PASS
HERE: SJMP HERE
END
**(Note: change JNC to JC for descending order)
Step 1. Create a New project by selecting AT89C51ED2 device in Keil Micro Vision
Step 2. Right click on the target in project workspace and type the frequency of Step
11.0592 MHz.
Step 3. Check the box next to create hex file in output options.
Step 4. Type C Program and save as .c file type
Step5. Add C file to Project workspace
Step 6. Build Target and check for error
Step 7. Ensure that the hex file created in your Project name and then minimize the
window.
#include "at89c51ed2.h"
static bit Dir=0;
//sbit buzzer = P0^5;
void ChangeDir(void) interrupt 0
{
Dir = ~Dir;
}
void delay(unsigned int x)
{
for(;x>0;x--);
}
main()
{
unsigned char Val,i;
EA=0x1;
EX0=0x1;
ES=1;
while(1)
{
if(Dir)
{
Val = 0x08;
for(i=0;i<4;i++)
{
P0 = Val;
Val = Val >> 1;
delay(1000);
}
}
else
{
Val = 0x01;
for(i=0;i<4;i++)
{
P0 = Val;
Val = Val<<1;
delay(1000);
}
}
}
}
2 DC Motor Interface for direction and Speed Control using PWM
#include <at89c51xd2.h>
sbit DC1 = P0^6;
sbit DC2 = P0^7;
idata unsigned char off_time,on_time;
idata unsigned int i=0;
void main ()
{
off_time = 50;
on_time = 10;
DC2= 1;
//DC2= 0;
while(1)
{
DC1 = 0;
for(i=0;i<on_time;i++)
{
TL0 = 0x00;
TH0 = 0xF8;
TR0 =1;
while(!TF0);
TF0 = 0;
TR0=0;
on_time=on_time+10;
}
if(on_time>30)
on_time=10;
DC1 = 1;
for(i=0;i<off_time;i--)
{
TL0 = 0x00;
TH0 = 0xF8;
TR0 =1;
while(!TF0);
TF0 = 0;
TR0=0;
off_time=off_time-10;
}
if(off_time<10)
off_time=30;
}
}
3 DAC Interface
#include <at89c51xd2.h>
idata unsigned char count;
void main ()
{
count = 0x0;
while(1)
{
P0 = count;
count++;
}
}
#include <at89c51xd2.h>
idata unsigned char count;
void main ()
{
while(1)
{
for(count=0;count!=0xff;count++)
{
P0=count;
}
for(count=0xff; count>0;count--)
{
P0=count;
}
}
}
#include<at89c51xd2.h>
void delay(void);
void main ()
{
while(1)
{
P0 = 0x0;
delay();
P0 = 0xff;
delay();
}
}
void delay(void)
{
int i;
for(i=0;i<=300;i++);
}
#include <at89c51xd2.h>
xdata unsigned char sine_tab[49]=
{ 0x80,0x90,0xA1,0xB1,0xC0,0xCD,0xDA,0xE5,0xEE,0xF6,0xFB,0xFE,
0xFF,0xFE,0xFB,0xF6,0xEE,0xE5,0xDA,0xCD,0xC0,0xB1,0xA1,0x90,
0x80,0x70,0x5F,0x4F,0x40,0x33,0x26,0x1B,0x12,0x0A,0x05,0x02,
0x00,0x02,0x05,0x0A,0x12,0x1B,0x26,0x33,0x40,0x4F,0x5F,0x70,0x80};
idata int count;
void main ()
{
while(1)
{
for(count=0;count<49;count++)
{
P0 = sine_tab[count];
}
}
}
4. Elevator Interface
#include <at89c51xd2.h>
void delay(unsigned int);
unsigned char Flr[9] = {0xff,0x00,0x03,0xff,0x06,0xff,0xff,0xff,0x09};
unsigned char Flr_clr[9] = {0xff,0x0E0,0x0D3,0xff,0x0B6,0xff,0xff,0xff,0x79};
unsigned char ReqFlr,CurFlr=0X01,i,j;
void main(void)
{
AUXR = 0x10;
P1 = 0x00;
P1 = 0x0f0;
while(1)
{
P0 = 0x0f; // Configure Port as i/p
ReqFlr = P0 | 0xf0; // Append '1' at the Higher nibble position
while(ReqFlr == 0xff) //
ReqFlr = P0 | 0xf0; /* Read Request Floor from P0 */
ReqFlr = ~ReqFlr; // Complement to get the proper input
if(CurFlr == ReqFlr) /* If Request floor is equal to Current Floor */
{
P1 = Flr_clr[CurFlr]; /* Clear Floor Indicator */
continue; /* Go up to read again */
}
else if(CurFlr > ReqFlr) /* If Current floor is > request floor */
{
i = Flr[CurFlr] - Flr[ReqFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];
for(;i>0;i--) /* Move the indicator down */
{
P1 = 0x0f0 | j;
j--;
delay(20000); // Delay to visualize the effect
}
}
else /* If Current floor is < request floor */
{
i = Flr[ReqFlr] - Flr[CurFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];
for(;i>0;i--) /* Move the indicator Up */
{
P1 = 0x0f0 | j;
j++;
delay(20000);
}
}
CurFlr = ReqFlr; /* Update Current floor */
P1 = Flr_clr[CurFlr]; /* Clear the indicator */
}
}
void delay(unsigned int x)
{
for(;x>0;x--);
}
5. ADC and Temperature Control Interface
#include<at89c51xd2.h>
#include<stdio.h>
// LCD FUNCTION PROTOTYPE
void lcd_init(void);
void lcd_comm(void);
void lcd_data(void);
void delay(int);
sbit EOC = P0^4;
sbit START_ALE = P0^7;
unsigned char xdata arr1[12]={"ADC Value = "};
unsigned char xdata arr2[12]={"TEMP 'C = "};
unsigned char temp_msg[]={" "};
unsigned char temp1=0x00;
unsigned char temp2;
unsigned char buf[8],a;
unsigned char i,temp_hi,temp_low,adc_val;
unsigned int vtemp1;
float analog_val,temp_float;
#define VREF 5000
#define FULLSCALE 0xff
void main ()
{
START_ALE = 0;
lcd_init();
temp1 = 0x80; // Display the data from first position of
first line
lcd_comm(); // Command Writing
for(i=0;i<12;i++)
{
temp2 = arr1[i];
lcd_data(); // Data Writing
}
temp1 = 0xC0; // Display the data from first position of first line
lcd_comm(); // Command Writing
for(i=0;i<12;i++)
{
temp2 = arr2[i];
lcd_data(); // Data Writing
}
P1 = 0xff; // Configure P1 as input to read the ADC o/p
delay (200);
while(1)
{
P0 &= 0xFA; // Select the temparrature sensor as input channel
START_ALE=1;
delay(5);
START_ALE = 0;
do
{
vtemp1=P0;
vtemp1=vtemp1 & 0x10;
}
while(vtemp1 == 0x10); // POLL EOC LINE HI TO LOW
do
{
vtemp1=P0;
vtemp1=vtemp1 & 0x10;
}
while(vtemp1 == 0x00); // LOW TO HIGH
adc_val = P1; // display adc result on the data field