MC Lab Manual Vtu8
MC Lab Manual Vtu8
Applications of 8051
Microcontroller
Course Code BUE358A Semester III
SYLLABUS
I. PROGRAMMING
1. Data Transfer - Block move, Exchange, Sorting, Finding largest
element in an array
2. Arithmetic Instructions - Addition/subtraction, multiplication
and division, square, Cube – (16 bits Arithmetic operations – bit
addressable)
3. Counters
4. Boolean & Logical Instructions (Bit manipulations)
5. Conditional CALL & RETURN
6. Code conversion: BCD – ASCII; ASCII – Decimal; Decimal -
ASCII; HEX - Decimal and Decimal -HEX
7. Programs to generate delay, Programs using serial port and on-
Chip timer / counter
8. Write C programs to interface 8051 chip to Interfacing modules
to develop single chip solutions
9. Elevator interface to 8051
10. Program to blink LED
Demonstration Experiment
11. Stepper and DC motor control interface to 8051
“MICROCONTROLLERS LAB”
4. Create a source file (using File->New), type in the assembly or C program and save this
(filename.asm/ filename.c) and add this source file to the project using either one of the
following two methods. (i) Project-Components,Environmentand Books->addfiles->
browse to the required file -> OK “OR”
(ii) right click on the Source Group in the Project Window and the Add Files to Group
option.
5. Set the Target options using -> Project – Options for Target opens the Vision2
Options for Target – Target configuration dialog. Set the Xtal frequency as 11.0592
Mhz, and also the Options for Target – Debug – use either Simulator / Keil Monitor-
51 driver.
If Keil Monitor- 51 driver is used click on Settings -> COM Port settings select the
COM Port to which the board is connected and select the baud rate as 19200 or 9600
(recommended). Enable Serial Interrupt option if the user application is not using on-
chip UART, to stop program execution.
6. Build the project; using Project -> Build Project. Vision translates all the user
application and links. Any errors in the code are indicated by – “Target not created” in the
Build window, along with the error line. Debug the errors. After an error free build, goto
Debug mode
“MICROCONTROLLERS LAB”
7. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog.
Or by clicking in the icon.
8.The program is run using the Debug-Run command & halted using Debug-Stop
Running. Also the (reset, run, halt) icons can be used. Additional icons are
1) a. Write an assembly language program to transfer n =10 bytes of data from location
8035h to location 8041h (without overlap).
ORG 0000H
SJMP 30H
ORG 30H
MOV DPH,#80H
MOV R0,#35H //source address
MOV R1,#41H //destination address
MOV R3,#0AH //count
BACK: MOV DPL, r0
MOVX A,@dptr
MOV DPL, R1
MOVX @dptr,A
INC R0
INC R1
DJNZ R3, BACK
HERE: SJMP HERE
END
RESULT:
After Execution: 10 locations X:8041h are filled up with data from 8035h.
Algorithm:
1. Initialize registers to hold count data & also the source & destination addresses.
2. Get data from source location into accumulator and transfer to the destination
location.
3. Decrement the count register and repeat step 2 till count is zero.
Note: For data transfer with overlap start transferring data from the last location of
source array to the last location of the destination array.
“MICROCONTROLLERS LAB”
ORG 00H
SJMP 30H
ORG 30H
MOV R0,#27H //source address
MOV R1,#41H //destination address
MOV R3,#05H //count
BACK: MOVX A,@r0
MOV r2,a
MOVX a,@r1
MOVX @r0,a
MOV a, r2
MOVX @r1,a
INC R0
INC R1
DJNZ R3, BACK
HERE: SJMP HERE
END
RESULT :
Before Execution: 5 locations at X:0027h & X:0041h are filled up with data
Algorithm :
1.Initialize registers to hold count data (array size) & also the source & destination
addresses.
2.Get data from source location into accumulator and save in a register.
3.Get data from the destination location into accumulator.
4.Exchange the data at the two memory locations.
5.Decrement the count register and repeat from step 2 to 4 till count is zero
“MICROCONTROLLERS LAB”
ORG 0000H
SJMP 30H
ORG 30H
MOV R0,#05 //count n-1 -ARRAY SIZE-n- Pass Counter
L1: MOV dptr, #9000h //array stored from address 9000h
MOV A,R0 //initialize exchange counter
MOV R1,A
L2: MOVX a, @dptr //GET NUMBER FROM ARRAY
MOV B, A //& STORE IN B
INC dptr
MOVX a, @dptr //next number in the array
CLR C //reset borrow flag
MOV R2, A //STORE IN R2
SUBB A, B //2nd - 1st no.—no compare instruction in 8051
JC NOEXCHG // JNC - FOR ASCENDING ORDER
MOV A,B //EXHANGE THE 2 NOES IN THE ARRAY
MOVX @dptr,a
DEC DPL //DEC dptr-INSTRUCTION NOT PTRESENT
MOV a,R2
MOVX @dptr,a
INC DPTR
NOEXCHG: DJNZ R1,L2 //decrement compare counter
DJNZ R0,L1 //decrement pass counter
here: SJMP here
END
Algorithm :
6.If the carry flag is set (for ascending order) then exchange the 2 numbers in the array.
7.Decrement the compare counter and repeat through step 4 until the counter becomes0.
8.Decrement the pass counter and repeat through step 3 until the counter becomes 0.
\
RESULT :
1) d. Write an assembly language program to find the largest element in a given string
of n = 6 bytes at location 4000h. Store the largest element at location 4062h.
ORG 0000H
SJMP 30H ORG
30H
MOV R3,#6 //length of the array
MOV DPTR,#4000H //starting address of the array
MOVX A,@DPTR
MOV r1,a
NEXTBYTE: INC DPTR
MOVX A,@DPTR
CLR C //reset borrow flag
MOV R2,A //next number in the array
SUBB A,R1 //OTHER Num - PREVIOUS LARGEST no.
JC skip // JNC for smallest element
MOV A,r2 //UPDATE larger number in r1
MOV R1,A
skip:DJNZ R3,NEXTBYTE
MOV DPL, #62H //LOCATION OF THE RESULT-4062H
MOV A,R1 //LARGEST NUMBER
MOVX @DPTR,A //STORE AT #4062H
OVER: SJMP OVER
END
“MICROCONTROLLERS LAB”
Algorithm :
RESULT :
Before Execution:
2. ARITHMETIC INSTRUCTIONS :
Algorithm:
1.Store the condition x in r1.
2.Load the first and second numbers to A and B registers respectively
3.Compare the contents of r1 and perform the operations add, sub, etc accordingly.
4.Store the result present in A and B registers to the appropriate memory locations.
“MICROCONTROLLERS LAB”
ADD SUB
After Execution:
Before Execution:
ORG 0000H
SJMP 30H ORG
30H
MOV DPTR,#9000H
MOVX A,@DPTR //GET NUMBER-X
MOV R0,A //STORE IN R0
MOV B,A
MUL AB //SQUARE IT-X^2
CLR C //FOR STORING RESULT
JB 01,LAST //IF BIT 01 IS SET THEN END, ELSE DO CUBE
PUSH B //STORE UPPER PART OF SQUARE
MOV B,A //B-LOWER PART OF X^2
MOV A,R0 //A-X
MUL AB //X*LOWER X^2INC
DPTR
MOVX @DPTR,A //STORE PARTIAL RESULT
MOV A,B
MOV R2,A //UPPER PART OF X*LOWER X^2 IN R2POP B
//GET BACK UPPER PART OF SQUARE
MOV A,R0 //A-X
MUL AB //X*UPPER X^2
ADD A,R2 //ADD TO PARTIAL RESULT
LAST:INC DPTR
MOVX @DPTR,A
MOV A,B
ADDC A,#00 //ADD CARRY TO B(FOR SQUARE RESULT, C=0)INC
DPTR
MOVX @DPTR,A
HERE:SJMP HERE
END
RESULT:
CUBE OF 56H IS 9B498 WHICH IS STORED AS 98, B4, 09 (LOWER BYTE FIRST)
“MICROCONTROLLERS LAB”
To get square make the D1 bit of data memory 20h high, say FF,02,06,etc. The bit addressis 01.
Similarly bit address 78h correspond to D0 bit 0f data ram location 2Fh.
Algorithm:
3) COUNTERS
ORG 0H SJMP
30H ORG 0H
MOV a,#00
BACK: ACALL DELAY
INC a //dec a for binary down counterJNZ
BACK
HERE:SJMP HERE
DELAY: MOV r1,#0FFH
DECR1:MOV r2,#0FFH DECR:
MOV r3,#OFFH DJNZ r3,$
DJNZ r2,DECR
DJNZ r1,DECR1RET
END
RESULT:
Accumulator A is incremented in binary from 00, 01,02…09,0A,
0B,…,0F,10,11,…FF
Algorithm:
1. Move 00 to A register
2. Call the delay subroutine for 1 second, in delay program move FFH to registers r1,r2
and r3, loop and decrement until 0.
3. Increment A register(decremant for down counter)
“MICROCONTROLLERS LAB”
ORG 0H SJMP
30H ORG 30H
MOV a,#00
BACK:ACALL DELAY
ADD a,#99H //ADD 01 for BCD up counter
DA A //for bcd counter
JNZ BACK
HERE:SJMP HERE
DELAY:MOV r1,#0FFH
DECR1:MOV r2,#0FFH
DECR:MOV r3, #0FFH
DJNZ r3,$ DJNZ r2,
DECR
DJNZ r1, DECR1RET
END
Algorithm:
1.Move 00 to A register
2.Call the delay subroutine for 1 second (in delay program move FFH to registersr1, r2 and r3, loop
and decrement until 0).
3.Increment A register(add 99h for down counter)
4.Decimal adjust accumulator for the BCD up/down counter.
RESULT: Accumulator A is incremented in BCD from 00, 01, 02…09, 10, 11,…99.
“MICROCONTROLLERS LAB”
4) a. Two eight bit numbers NUM1 & NUM2 are stored in external memory locations 8000h&
80001h respectively. Write an ALP to compare the 2 nos.
Reflect your result as: if NUMI<NUM2, SET LSB of data RAM 2F (bit address 78H) IF
NUM1>NUM2, SET MSB OF 2F(7FH). if NUM1 = NUM2-Clear both LSB & MSB
of bit addressable memory location 2Fh
ORG 0000H
SJMP 30H ORG
30H
MOV DPTR,#8000H
MOVX A,@DPTR MOV
R0,A
INC DPTR MOVX
A,@DPTRCLR C
SUBB A,R0JZ
EQUAL JNC
BIG SETB 78H
SJMP END1
BIG:SETB 7FH
SJMP END1
EQUAL:CLR 77H
CLR 7FH
END1:SJMP END1
END
Algorithm:
RESULT:
4) b. LOGICAL INSTRUCTIONS:
4)b. ASSEMBLY PROGRAM ILLUSTRATING LOGICAL INSTRUCTIONS
(BYTELEVEL)
3 eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM locations 20h,21h
& 22H respectively. Write an ALP to compute the following.
IF X=0; THEN NUM1 (AND) NUM2, IF X=1; THEN NUM1 (OR) NUM2,
IF X=2; THEN NUM1 (XOR) NUM2, ELSE RES =00, RES IS 23H LOCATION
ORG 0000H
SJMP 30HORG
30H
MOV A, 20h //do not use #, as data ram 20h is to be accessed
MOV R1,A //X IN R1
MOV A,21H //A -NUM1
CJNE R1,#0,CKOR
ANL A, 22H
SJMP END1
CKOR:CJNE R1,#01,CKXORORL
A, 22H
SJMP END1 CKXOR:CJNE
R1,#02,OTHER
XRL A, 22H
SJMP END1
OTHER: CLR A
END1: MOV 23H,A //STORE RESULT
HERE: SJMP HERE
END
Algorithm:
1. Point to the data RAM register 20h and store the condition x.
2. Point to 21h and 22h and move the first number to A register.
3. Compare the contents of r1 and perform the operations accordingly.
4. The result will be stored in 23H register.
RESULT:
20h = 01 =>complement of MSB of 21h location. Hence 21h is changed to A1 and 23h
location has 80h
Before Execution After Execution
Algorithm:
OBJECTIVES:
TO DEMONSTRATE CONDITIONAL BIT JUMP, CONDITIONAL BYTE JUMP,
UNCONDITIONAL JUMP, CALL and RETURN instructions
This program keeps checking P1.0 for low signal. Once a low signal arrives, program checks
for AAh at PORT 0. If the data is AAh a counter will start.
ORG 00H
/*INITIALIZING THE COUNTER AND PORTS*/
MAIN: MOV P0, #0FFH
MOV P1, #0FFH
MOV P2, #00H
MOV R0, #00H
/*WAITING FOR THE ARRIVAL OF LOW SIGNAL AT PORT 1.0*/
WAIT: JB P1.0, WAIT // CONDITIONAL BIT JUMP
ACALL VERIFY_DATA // CONDITIONAL CALL
SJMP WAIT // UNCONDITIONAL JUMP
//CHECKING THE DATA AT P0 FOR 0AAh
VERIFY_DATA:MOV A, P0
CJNE A, #0AAH, RETURN // CONDITIONAL BYTE JUMP
INC R0
MOV P2, R0
ACALL DELAY // UNCONDITIONAL CALL
RETURN:RET // RETURN
/*JUST A DELAY SUBROUTINE */
DELAY:NOP
MOV R2,#25
H0: MOV R3,#255
HI: MOV R4,#255
DJNZ R4,$ // CONITIONAL BYTE JUMPS
DJNZ R3,HI
DJNZ R2,H0
RET // RETURN
END
“MICROCONTROLLERS LAB”
“MICROCONTROLLERS LAB”
6. CONVERSION PROGRAMS
6 a)Write an ALP to implement BCD to ASCII conversion
ORG 0000H
SJMP 30h
ORG 30h
MOV R1,#50H
MOV A,@R1 //get BCD data byte from RAM location 50h
MOV R2,A //Store in R2
ANL A,#0FH //Get the lower nibble
ORL A,#30H //Add/or with 30h i.e., 0-9 converted to 30-39h
INC R1
MOV @R1,A //Store the lower digit's ASCII code
MOV A,R2 //Get back the number
SWAP A //Swap nibbles in A
ANL A,#0FH //Get the upper BCD digit
ORL A,#30H //Convert to ASCII
INC R1
MOV @R1,A //Store the upper digit's ASCII code
here: sjmp here
END
RESULT:
Algorithm :
ORG 0
MOV R5, #"4" // LOADING R5 WITH ASCII EQUIVALENT OF 4 (IE, 34)
MOV R6, #"7" // LOADING R6 WITH ASCII EQUIVALENT OF 7 (IE, 37)
MOV A, R5
ANL A, #0FH // MASK LOWER NIBBLE
MOV R5, A
MOV A, R6
ANL A, #0FH // MASK UPPER NIBBLE
MOV R6, A
MOV A, R5
RL A
RL A
RL A
RL A
ORL A, R6 // PACKING
MOV P1,A
SJMP $
END
“MICROCONTROLLERS LAB”
6 c) DECIMAL – ASCII
(Conversion of Decimal number 0-9 to corresponding ASCII equivalent)
ORG 00H
MAIN: MOV R0,#RAM_ADDR
MOV R1,#ASC_RSULT
MOV R2,#0AH
BACK: MOV A,@R0 //stored decimal values
ORL A,#30H //are fetched ,they are ‘OR’ed with
MOV @R1,A //30 and results are placed at memory
INC R0 //pointed by R1 pointer
INC R1
DJNZ R2,BACK
MOV R1,#ASC_RSULT
MOV R2,#0AH //results are verified on
NEXT: MOV A,@R1 //PORT 1
MOV P1,A
INC R1
DJNZ R2,NEXT
SJMP $
END
ORG 0000H
SJMP 30h
ORG 30h
MOV DPTR,#9000H
MOVX A,@DPTR //Get hex number
MOV B,#10
DIV AB //divide by 10 (0AH)
INC DPTR
XCH A,B
MOVX @DPTR,A //Store the remainder (in B) In units place
XCH A,B
MOV B,#10 //Divide the quotient in A by 10
DIV AB
INC DPTR
XCH A,B
MOVX @DPTR,A //Store the remainder (in B) In tens place
XCH A,B
INC DPTR
MOVX @DPTR,A //Store the quotient (in A) in hundreds place
HERE:SJMP HERE
End
“MICROCONTROLLERS LAB”
RESULT:
Algorithm
ORG 0000H
SJMP 30h
ORG 30h
MOV DPTR,#40H //2-digit decimal number to be converted is given in data
memory 40h
MOVX A, @DPTR
ANL A, #0F0H //obtain upper decimal digit
SWAP A //bring to the units place
MOV B,#0AH //MULTIPLY tens digit with #0A-toget tens in hex
MUL AB
MOV r1,a //temporarily store the converted tens value
MOVX A,@DPTR //get the decimal number again
ANL A,#0FH //obtain the units digit
ADD A,R1 //add to the converted tens value
INC DPTR //increment data address
MOVX @DPTR,A //converted hexadecimal number in next location
HERE:SJMP HERE
END
RESULT:
before execution- X:0040H = 45 (Decimal/BCD)
After Execution: X:0041h = 2D (hex value)
Algorithm
1. Move the decimal data to be converted from external memory 40h to accumulator.
2. AND A reg with 0f0h and obtain the upper MSB of the decimal digit and swap the
LSB and MSB of accumulator to bring the same to units place.
3. Move 0ah to B register and multiply with A reg to convert to hex value, store the
converted tens value in r1
4. Get the LSB of the decimal number and add to the converted tens value
5. point to the next memory location and store the result (hexadecimal).
“MICROCONTROLLERS LAB”
ORG 0H
SJMP 30H
ORG 30H
MOV TMOD,#20H //timer 1; mode 2
MOV TH1,#-3 //-3=FD loaded into TH1 for 9600 baud, 11.0592MHz.
MOV SCON,#50H //8-bit, 1 stop bit, REN enabled
SETB TR1 //Start timer 1
AGAIN:MOV A,#’y’ //transfer “y”
ACALL TRANS
MOV a,#’E’ //transfer “E”
ACALL TRANS
AGAIN1:SJMP AGAIN1
TRANS: MOV SBUF,a //load SBUF
HERE:JNB TI,HERE //Wait for last bit to transfer
CLR TI //get ready for next byte
RET
END
RESULT: yE is printed on the serial window each time the program is executed.
RESULT:
Algorithm:
Timerdelay = 12*(257-delay)/frequency
Timerdelay=0.05msec
Delay=256-((timerdelay * frequency)/12) =256-(0.05*10 -3 * 33*106)/12
=256-137.5 =118.5 //loaded in TH0
To get 1sec delay
1/0.05msec = 200*100 in the ISR
(assuming 33 MHZ crystal frequency. For 11 MHz, the calculations change).
“MICROCONTROLLERS LAB”
OBJECTIVE
THIS PROGRAM IS CAPABLE OF DEMONSTRATING THE BASIC WORKING OF
ELEVATOR . INPUT IS VIA A MATRIX KEYPAD AND OUTPUT IS DISPLAYED ON A
2X16
LCD.
Hardware:
Microcontroller 89s8252
Crystal Freq 11.0592
#include <REG52.H>
void main()
{
lcdcmd(0x38);
“MICROCONTROLLERS LAB”
lcdcmd(0x0e);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x80);
for (j=0;j<9;j++)
{
lcddata(msg[j]); //Get data from lookup
//table
}
lcdcmd(0x8A);
lcddata('0');
lcddata('0');
while(1)
{
do
{
getkey (); //get data from matrix key pad
MSDelay(10);
Data1 = key;
key = key & 0xF0;
}
while (key!=0x30);
carry =0;
if (Data1<Data2) down();
else up();
}
}
void down()
{
for(i=(Data2-Data1);i>0;i--)
{
for (j=3;j>0;j--)
{
lcdcmd(0x8E);
lcddata('v');
lcddata('v');
MSDelay(30);
lcdcmd(0x8E);
lcddata(' ');
lcddata(' ');
MSDelay(30);
}
Data2--;
lcdcmd(0x8b);
lcddata(Data2);
}
}
void up()
{
for(i=(Data1-Data2);i>0;i--)
{
for (j=3;j>0;j--)
“MICROCONTROLLERS LAB”
{
lcdcmd(0x8E);
lcddata('^');
lcddata('^');
MSDelay(30);
lcdcmd(0x8E);
lcddata(' ');
lcddata(' ');
MSDelay(30);
}
Data2++;
lcdcmd(0x8b);
lcddata(Data2);
}
}
void MSDelay (unsigned int value) // Delay routine
{
unsigned int x,y;
for (x=0;x<900;x++)
for (y=0;y<value;y++);
}
void lcdcmd (unsigned char value)
{
lcdready();
ldata = value;
rs = 0;
rw = 0;
en = 1;
MSDelay(1);
en = 0;
return;
}
void lcddata (unsigned char value)
{
lcdready();
ldata = value;
rs = 1;
rw = 0;
en = 1;
MSDelay(1);
en = 0;
return;
}
void lcdready ()
{
busy = 1;
rs = 0;
rw = 1;
while (busy == 1)
{
en = 0;
MSDelay(1);
“MICROCONTROLLERS LAB”
en = 1;
}
return;
}
unsigned char getkey () // Routine to read matrix
keyboard
{
unsigned char colloc, rowloc;
TMOD = 0x20;
TH1 = -3;
SCON = 0x50;
TR1 = 1;
P2 = 0xff;
do {
P2 = 0x0f; //Wait untill all keys are released
colloc = P2;
colloc &= 0x0f;
} while (colloc != 0x0f);
do {
do
{
MSDelay (1);
colloc = P2;
colloc &= 0x0f;
} while (colloc == 0x0f);
//Check whether any key is pressed
MSDelay (1);
colloc = P2;
colloc &= 0x0f;
//Confirm whether any ket is pressed after delay
} while (colloc == 0x0f);//to aviod spikes
while(1)
{
P2 = 0xfE; //get the row presses
colloc = P2;
colloc &= 0xf0;
if (colloc != 0xf0)
{
rowloc = 0;
break;
}
P2 = 0xfd;
colloc = P2;
colloc &= 0xf0;
if (colloc != 0xf0)
{
rowloc = 1;
break;
}
P2 = 0xfb;
colloc = P2;
“MICROCONTROLLERS LAB”
void Delay(void)
{
int j;
int i;
for(i=0;i<10;i++)
{
for(j=0;j<10000;j++)
{
}
}
}
“MICROCONTROLLERS LAB”
OUTPUT :
The output will be intensity variations in on-boards leds. Or if the variation is not
seen easily, one can program another led connected to any GPIO pin to visualize
the impact of potentiometer.
“MICROCONTROLLERS LAB”
OBJECTIVE
THIS PROGRAM DEMONSTRATES A STEPPER MOTOR CONTROL USING AN H-
BRIDGE CONTROLLER. DIRECTION AND SPEED ARE SELECTED WITH HYPER
TERMINAL.
Hardware:
Microcontroller 89s8252
Crystal Freq 11.0592
#include <REG52.H>
void MSDelay (unsigned int);
void Send (unsigned char);
unsigned int rdata=1; //for clockwise 0 and anticlockwise 1
void main()
{
if(rdata==0)
{
while (1) //keep doing the function until next command
is received
{
P0 =0x66; //Clockwise Rotation
MSDelay (100); //to change speed change delay
P0 =0xCC;
MSDelay (100);
P0 =0x99;
MSDelay (100);
P0 =0x33;
MSDelay (100);
}
}
else
{
“MICROCONTROLLERS LAB”
while (1)
{
P0 =0x33; //AntiClockwise Rotation
MSDelay (100);
P0 =0x99;
MSDelay (100);
P0 =0xCC;
MSDelay (100);
P0 =0x66;
MSDelay (100);
}
}
}
void MSDelay (unsigned int value) // Delay rouitne
{
unsigned int x,y;
for (x=0;x<600;x++)
for (y=0;y<value;y++);
}
“MICROCONTROLLERS LAB”
A
Report
MICROCONTROLLER
LABORATORY ( BUE304 )
USN :
2023 - 2024
“MICROCONTROLLERS LAB”
CERTIFICATE
Examiners :