Vous êtes sur la page 1sur 17

1 Write an ALP to transfer 10 bytes of data stored in internal RAM.

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

3) Write an ALP to exchange 5 bytes of data in i-RAM(Using XCH)


ORG 0000h
MOV R0, #30h ; initialize r0 to point to the source location
MOV R1, #40h ; initialize r1 to point to the destination location
MOV R5, #05h ; initialize the count of 5 data exchange using r5 reg
Repeat: MOV A,@R0 ; move the source data into the A reg
XCH A,@R1 ; exchange the data from A to the destination location and the data from
destination back to A reg
MOV @R0, A ; to move the data which was taken from the destination location and
move it to
source location
INC R0 ; increment the address to point to the next source location
INC R1 ; increment the address to point to the next destination location
DJNZ R5, Repeat ; conditional jumping as long as r5 is not 0 to label "Repeat"
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

6) Write an ALP to transfer 5bytes data stored in Code Memory(ROM) starting


from 1500h to int. RAM memory starting from 30h.
ORG 1500h
DB 01h, 02h,03h,04h,05h ;define byte and enter the desired values into ROM
ORG 0000h ; start program
MOV DPTR, #1500h ;(point to source location using dptr in ROM memory)
MOV R1, #30h ;(point to destination using R1 in int. RAM memory)
MOV R5,#05h ;(Keep a count for 5 bytes of data transfer)
UP: CLR A
MOVC A,@A+DPTR ; Using index addressing get data from code memory into A
register)
MOV @R1, A ;move data from A register to address pointed by R1 i.e 30h)
INC DPTR ; increment DPTR)
INC R1 ; increment R1)
DJNZ R5, UP ;Decrement count and jump repeat data transfer if count is not zero)
Here: SJMP Here
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

8) Write an ALP to ADD two(16 bit numbers)stored at locations 30h,31h, 32h,33h


and store your result at 50h,51h,52h.
ORG 0000h
MOV A, 31h ;(store addr. Of LB in first number)
ADD A, 33h ;(add the LB of first and second number)
MOV 52h, A ;(store LB of sum to 52h)
MOV A, 30h ;(store higher byte of first number into A)
ADDC A, 32h ;add higher byte of 2nd no. and 1st no. with CY from previous addition
MOV 51h, A ;(store higher byte of sum in 51h)
JNC Down
MOV 50h, #01h
Down: SJMP Down
END

9)Write an ALP to SUB two(16 bit numbers)stored at locations 30h,31h, 32h,33h


and store your result at 50h,51h,52h.
ORG 8100h ;set the origin at 8100h
DB 00h,1h,4h,9h,10h,19h,24h,31h,40h,51h ;define the no.s in the code memory using DB
ORG 0000h ;set the origin at 0000h
MOV DPTR, #8100h ;initialize the base location of the code memory in DPTR
MOV A, 30h ;move the inputted number to a reg to act as the offset addresss
MOVC A,@A+DPTR ;bring the number from the code memory to A reg
MOV 31h,A ;move the number to 31h memory location
END

10)Write an ALP to multiply a 16 bit number stored in internal RAM location X


with an 8 bit no. stored in RAM location Y. Store the result in location Z
ORG 0000h
MOV A, 31h ;(move LB of 16 bit number to A)
MOV B, 40h ;(move 8bit into B)
MUL AB ;(multiply LB with 8bit number)
MOV 52h, A ;(store the LB of product in 52h)
MOV R1, B ;(store carry of LB from B to R1)
MOV A, 30h ;(move HB of 16 bit number to A)
MOV B, 40h ;(move 8bit number to B)
MUL AB ;(multiply HB and 8bit number)
ADD A,R1 ;add carry to lower byte of this product from previous multiplication
MOV 51h,A ;(move the result from a to destination i.e 51h)
JNC Label
INC B
Label: MOV 50h, B
Here: SJMP Here
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

14)WRITE AN ALP TO COVERT HEXADECIMAL NUMBER TO BCD NUMBER.


ORG 0000H
MOV A, 30H
MOV B, #64H
DIV AB
MOV 32H, A
MOV A, B
MOV B, #0AH
DIV AB
SWAP A
ADD A, B
MOV 31H, A
END

15) ALP TO CONVERT BCD NO. TO ITS CORRESPONDING ASCII CODE


ORG 0000H
MOV A, 50H
ANL A, #0F0H
SWAP A
ACALL ASCII
MOV 51H, A
MOV A, 50H
ANL A, #0FH
ACALL ASCII
MOV 52H, A
ASCII: MOV R2, A
SUBB A, #0AH
JC DOWN1
MOV A, R2
ADD A, #37H
SJMP DOWN2
DOWN1:MOV A, R2
ADD A, #30H
DOWN2:RET
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

17) ALP TO DISPLAY HEXADECIMAL UP COUNTER USING PORT 0


ORG 0000H
MOV A, #00H
UP: MOV P0, A
INC A
ACALL DELAY
SJMP UP
DELAY: MOV R2, #050H
LOOP3: MOV R3, #0FFH
LOOP2: MOV R4,#0FFH
LOOP1: DJNZ R4, LOOP1
DJNZ R3, LOOP2
DJNZ R2, LOOP3
RET
END

18) ALP TO DISPLAY HEXADECIMAL DOWN COUNTER USING PORT 0


ORG 0000H
MOV A, #0FFH
UP: MOV P0, A
DEC A
ACALL DELAY
SJMP UP
DELAY: MOV R2, #050H
LOOP3: MOV R3, #0FFH
LOOP2: MOV R4, #0FFH
LOOP1: DJNZ R4, LOOP1
DJNZ R3, LOOP2
DJNZ R2, LOOP3
RET
END

19) ALP to display BCD up counter using port 0


ORG 0000H
MOV A, #00H
UP: MOV P0, A
ADD A, #01H
DA A
ACALL DELAY
SJMP UP
DELAY: MOV R2, #050H
LOOP3: MOV R3, #0FFH
LOOP2: MOV R4, #0FFH
LOOP1: DJNZ R4, LOOP1
DJNZ R3, LOOP2
DJNZ R2, LOOP3
RET
END

20) ALP to display BCD down counter using port 0


ORG 0000H
MOV A, #00H
UP: MOV P0, A
ADD A, #99H
DA A
ACALL DELAY
SJMP UP
DELAY:MOV R2, #050H
LOOP3: MOV R3, #0FFH
LOOP2: MOV R4, #0FFH
LOOP1: DJNZ R4, LOOP1
DJNZ R3, LOOP2
DJNZ R2, LOOP3
RET
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)

23) ALP on Serial data transfer


ORG 0000H
MOV TMOD, #20h ; Timer1 in mode 2
MOV TH1, #0FDh ; Baud Rate of 9600
MOV SCON, #50h ; Serial mode 1
SETB TR1 ; Start Timer
MOV A, # ; Move the first character to A-reg
ACALL TRANS ;call the subroutine for serial data transfer
MOV A, # M
ACALL TRANS
MOV A, # S
ACALL TRANS
MOV A, # I
ACALL TRANS
MOV A, # T
ACALL TRANS
TRANS:MOV SBUF, A
WAIT: JNB TI, WAIT ;WAIT FOR Ti FLAG TO SET
CLR TI
RET
END
**************************************************
INTERFACING PROGRAMS

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.

Step 8. Go to ATMEL flip on Desktop


Step 9. Go to device and select AT89C51ED2
Step 10. Go to Settings => Communication and Select RS232 (COM 1) and press
Connect (Wait for the connection to happen, if time out error appears, then
refresh and continue)
Step 11. Go to file and Select Load hex file
Step 12. Browse and get the hex file (it will be in project name.hex)
Step 13. Select Run option to download the pgm to the chip (make sure the kit is in
Pgm mode while downloading)
Step 14. Once the download is over change the kit to RUN mode and Select the Start
application and observe your output

Any change made in the program, repeat from Step 6


1 STEPPER MOTOR INTERFACE

#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

A. RAMP Wave Generation

#include <at89c51xd2.h>
idata unsigned char count;
void main ()
{
count = 0x0;
while(1)
{
P0 = count;
count++;
}
}

B. Triangle Wave Generation

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

C. Square Wave Generation

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

D. Sine Wave Generation

#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

analog_val = ((float)adc_val * (float)VREF)/(float)FULLSCALE;


temp_float = (float)(analog_val - 2731.4)/10;
temp_float = (int)(temp_float*10);
i = 100; // used to extract digit of temp_float
for(a=0;a<4;a++)
{
buf[a] = temp_float / i;
temp_float -= buf[a] * i;
buf[a] += '0';
i /= 10;
}
buf[3] = buf[2];
buf[2] = buf[1];
buf[2] = '.';
buf[4] = '\0';
temp1 = 0xCC;
lcd_comm();
for(i=0;(buf[i]!='\0');i++)
{
temp2 = buf[i];
lcd_data();
}
temp_hi=adc_val>>4;
temp_hi=temp_hi & 0x0f;
temp_low=adc_val & 0x0f;
if(temp_hi>9) // Convert the received ADC o/p into ASCII code
temp_hi = temp_hi + 0x37;
else
temp_hi = temp_hi + '0';
if(temp_low>9)
temp_low = temp_low + 0x37;
else
temp_low = temp_low + '0';
delay(100);
temp_msg[1] = temp_hi ;
temp_msg[2] = temp_low ;
temp1 = 0x8C; // To display the ADC o/p
lcd_comm(); // Command Writing
temp2 = temp_hi;
lcd_data();
temp2 = temp_low;
lcd_data();
}
}

Vous aimerez peut-être aussi