You are on page 1of 24

Microprocessor & Microcontroller Laboratory Work Book

LABORATORY WORK BOOK

Microprocessor and Microcontroller

Department of Electronic Engineering


Lahore College for Women University, Lahore
Microprocessor & Microcontroller Laboratory Work Book

Experiment
Experiments Description
No

Microprocessor
(Assembly language programming for 8086/8088)
Write a program to:
a). Prompt the user.
1. b). Read first, Middle and last initials of a person’s name.
c). Display them down the left margin.

Write a program which prompt the user to enter an upper case letter and the next
2. line displays a message with the letter in lower case.

(a). Replace the number in AX by its absolute value.


(b). Suppose AL and BL contains extended ASCII characters. Display the one that
3. comes first in the character sequence.
(c). If AL contains 1 or 3, display “O”. if AL contains 2 or 4, display “E”.

(a). Read a character, and if its an uppercase letter, displays it.


(b). Write a controlled loop to display a row of 80 stars.
4. (c). Write some code to count the number of characters in an input line

Microcontroller
(Assembly language programming for 8051)
(a). Write a program to load the accumulator with value 55H and compliment the
accumulator 100 times.
5. (b). Write a program to clear the accumulator and then add 3 to the accumulator 10
times

6. Write a code that toggles the pin of Port0, 1 and 2 after sometimes delay.

a). Write a program to perform the following.


1. Keep monitoring P1.2 until it becomes high.
2. When P1.2 becomes high write value 45H on P0.
3. Sent a high to low pulse to P2.3
b).
7. A switch is connected to P1.7. Write a program to check the status of switch and
perform the following.
1. if switch = 0, send letter “N” to P2
2. if switch = 1, send letter “Y” to P2.

a). Write a program which creates a square wave of 50% duty cycle on P1.5. Use
Timer 0 to generate the time delay of Mode 1.
8. b). Write a program which generates a square wave on pin P1.0. Use Timer 1 in
Mode 2.
Microprocessor & Microcontroller Laboratory Work Book

a). Write a program for the 8051 to transfer letter “A” serially, continuously
b). Write a program to transfer the message “YES” serially. Do this continuously
9. c). Program the 8051 to receive bytes of data serially, and put them in P1.

Microcontroller (Hardware Projects using 8051)


Single Blinking LED
10.

LED Patterns
11.

Driving AC Load
12.

13. Driving DC Load

Sounding Buzzer
14.
Microprocessor & Microcontroller Laboratory Work Book

Experiment 1
Write a program to:

a). Prompt the user.


b). Read first, Middle and last initials of a person’s name.
c). Display them down the left margin.

CODE:

. MODEL SMALL
. STACK 100H
. DATA
MSG1 DB ‘Enter First, Middle and Last initials: $’
MSG2 DB ‘First, Middle and Last initials: $’
CRLF DB ‘0DH 0AH $’
. CODE
MAIN PROC
LEA 0X, MSG1
MOV AH, 9
INT 21H
LEA DX, CRLF
MOV AH, 9
INT 21H
MOV AH, 1
INT 21H
MOV BH, AL
LEA DX, CRLF
MOV AH, 9
INT 21H
MOV AX, 1
INT 21H
MOV BL, AL
LEA DX, CRLF
MOV AH, 9
INT 21H
MOV AH, 1
INT 21H
MOV CL, AL
LEA DX, CRLF
MOV AH, 9
INT 21H
LEA DX, MSG2
MOV AH, 9
INT 21H
Microprocessor & Microcontroller Laboratory Work Book

MOV AH, 2
MOV DL, BH
INT 21H
MOV DL, CL
INT 21H
LEA DX, CRLF
MOV AH, 9
INT 21H
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
Microprocessor & Microcontroller Laboratory Work Book

Experiment 2

Write a program which prompt the user to enter an upper case letter
and the next line displays a message with the letter in lower case.

.MODEL SMALL
.STACK 100H
.DATA
CR EQU ODH
LF EQU OAH
MSG 1 DB ‘Enter an uppercase letter: $’
MSG 2 DB ODH, OAH, ’In lower case letter it is:’
CHAR DB? , ‘$’
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS, AX
LEA DX, MSG 1
MOV AH, 9
INT 21 H
MOV AH, 1
INT 21H
ADD AL, 20H
MOV CHAR, AL
INT 21H
MOV AH, 4CH
INT 21H
END MAIN
Microprocessor & Microcontroller Laboratory Work Book

Experiment 3
(a). Replace the number in AX by its absolute value.
(b). Suppose AL and BL contains extended ASCII characters. Display the one that
comes first in the character sequence.
(c). If AL contains 1 or 3, display “O”. if AL contains 2 or 4, display “E”.

(a).
CMP AX, 0
JNL END_IF
NEG AX
END_IF

(b).
MOV AH, 2
CMP AL, BL
JNBE ELSE
MOV DL, AL
JMP DISPLAY

ELSE:
MOV DL, BL

DISPLAY:
INT 21 H

END_IF:

(c ).
CMP AL, 1
JE ODD
CMP AL, 3
JE ODD
CMP AL, 2
JE EVEN
CMP AL, 4
JE EVEN
JMP END_CASE

ODD:
MOV DL, O
JMP DISPLAY
Microprocessor & Microcontroller Laboratory Work Book

EVEN:
MOV DL, E
JMP DISPLAY

DISPLAY:
MOV AH, 2
INT 21H

END_CASE
Microprocessor & Microcontroller Laboratory Work Book

Experiment 4
(a). Read a character, and if its an uppercase letter, displays it.
(b). Write a controlled loop to display a row of 80 stars.
(c). Write some code to count the number of characters in an input line

(a).
MOV AH, 1
INT 21H

CMP AL,’A’
JNGE END_IF
CMP AL,’Z’
JNLE END_IF

MOV DL, AL
MOV AH, 2
INT 21H

END_IF

(b).
MOV CX, 80;
MOV AH, 2;
MOV DL, ‘*’

TOP:
INT 21H;
JCXZ SKIP;
LOOP TOP;
SKIP:

END

(c).

MOV DX, 0
MOV AH, 1
INT 21H

WHILE_:
CMP AL, ODH
JE END_WHILE
Microprocessor & Microcontroller Laboratory Work Book

INC DX
INT 21H
JMP WHILE_

END_WHILE:
Microprocessor & Microcontroller Laboratory Work Book

Experiment 5
(a). Write a program to load the accumulator with value 55H and
compliment the accumulator 100 times.
(b). Write a program to clear the accumulator and then add 3 to the
accumulator 10 times

(a).
ORG 0H
MOV A, #55H
MOV R3, #10H

NEXT:
MOV R2, #70

AGAIN:
CPL A
DJNZ R2, AGAIN
DJNZ R3, NEXT

END

(b).
ORG 0H

MOV A, #0H
MOV R2, #10

NEXT:
ADD A, #3H
DJNZ R2, NEXT

END
Microprocessor & Microcontroller Laboratory Work Book

Experiment 6
Write a code that toggles the pin of Port0, 1 and 2 after sometimes
delay.

ORG OH

BACK:
MOV A, #55H
MOV P0, A
MOV P1, A
MOV P2, A

ACALL DELAY

MOV A, #0AAH
MOV P0, A
MOV P1, A
MOV P2, A

ACALL DELAY

SJMP BACK

DELAY:
MOV R5, #10

H2:
MOV R4, #20

H1:
DJNZ R4, #H1
DJNZ R5, #H2
RET
END
Microprocessor & Microcontroller Laboratory Work Book

Experiment 7
a). Write a program to perform the following.
4. Keep monitoring P1.2 until it becomes high.
5. When P1.2 becomes high write value 45H on P0.
6. Sent a high to low pulse to P2.3

b).
A switch is connected to P1.7. Write a program to check the status of
switch and perform the following.
3. if switch = 0, send letter “N” to P2
4. if switch = 1, send letter “Y” to P2.

a).
ORG 0H
SETB P1.2
MOV A, #45H

AGAIN:
JNB P1.2, AGAIN
MOV P0, A
SETB P2.3
CLR P2.3
END

b).
ORG 0H
SETB P1.7
JNB P1.7, AGAIN
JB P1.7, BACK

AGAIN:
MOV P2, ‘N’

BACK:
MOV P2, ‘Y’

END
Microprocessor & Microcontroller Laboratory Work Book

Experiment 8
a). Write a program which creates a square wave of 50% duty cycle on
P1.5. Use Timer 0 to generate the time delay of Mode 1.
b). Write a program which generates a square wave on pin P1.0. Use
Timer 1 in Mode 2.

a).
ORG 0H
MOV TMOD, #0F2H

HERE:
MOV TL0, #0F2H
MOV TH0, #0FFH
CPL P1.5

ACALL DELAY
SJMP HERE

DELAY:
SETB TR0

AGAIN:
JNB TF0, AGAIN
CLR TR0
CLR TF0
RET
END

b).
ORG 0H
MOV TMOD, #20H
MOV TH1, #5
SETB TR1

BACK:
JNB TF1, BACK
CPL P1.0
CLR TF1
SJMP BACK
END
Microprocessor & Microcontroller Laboratory Work Book

Experiment 9
a). Write a program for the 8051 to transfer letter “A” serially, continuously
b). Write a program to transfer the message “YES” serially. Do this continuously
c). Program the 8051 to receive bytes of data serially, and put them in P1.

a).
MOV TMOD, #20H
MOV TH1, #-6
MOV SCON, #50H
SETB TR1
AGAIN: MOV SBUF, # “A”
HERE: JNB T1, HERE
CLR TI
SJMP AGAIN

b).
MOV TMOD, #20H
MOV TH1, #-3
MOV SCON, #50H
SETB TR1
AGAIN: MOV A, # “Y”
ACALL TRANS
MOV A, # “E”
ACALL TRANS
MOV A, # “S”
ACALL TRANS
SJMP AGAIN
TRANS: MOV SBUF, A
HERE: JNB TI, HERE
CLR TI
RET

c).
MOV TMOD, #20H
MOV TH1, #-6
MOV SCON, #50H
SETB TR1
HERE: JNB RI, HERE
MOV A, SBUF
MOV P1, A
CLR RI
SJMP HERE
Microprocessor & Microcontroller Laboratory Work Book

Experiment 10
Single Blinking LED
8 × Green LEDs are connected to P0 of the training board through a darlington IC
ULN2803.
For further hardware details, please refer to Page 11,12 of 8051ETK User Manual

Code:
/******************************************************************/
/* PROJECTS: Blinking LED */
/* */
/* OBJECTIVE: */
/* This project toggles the LED connected at Port P0.0 */
/* with approximate delay of 1 second. */
/* */
/* COPYRIGHTS: 8051 TRAINING KIT Team - All rights reserved - 2007
*/
/* */
/******************************************************************/

#include <AT89x51.h>
void delay (void);
sbit LED =P0^0;
void main (void)
{
P0=0x00; //Clearing Port0
for (;;)
{
LED=0;
delay ();
LED=1;
delay ();
}
}
void delay (void)
{
unsigned int y;
for(y=0; y<33000; y++);
}
Microprocessor & Microcontroller Laboratory Work Book

Experiment 11

LED Patterns

8 × Green LEDs are connected to P0 of the training board through a darlington IC


ULN2803.
For further hardware details and connections, please refer to Page 11, 12 of 8051ETK
User Manual

Code:
/******************************************************************/
/* PROJECTS: LED Pattern */
/* */
/* OBJECTIVE: */
/* The led's on Port 0 glow in an different fashions. furthrer Random LED patterns can
also be */
/* generated in the same way. */
/* */
/* COPYRIGHTS: 8051 TRAINING KIT Team - All rights reserved - 2007
*/
/* */
/******************************************************************/

#include <AT89x51.h>
void delay(void);
void main(void)
{
P0 = 0;
for(;;)
{
P0 = 0x01;//0000 0001
delay(); P0 = 0x02;
delay();P0 = 0x04;
delay();P0 = 0x08;
delay();P0 = 0x10;//0001 0000
delay();P0 = 0x20;// 0010 0000
delay();P0 = 0x40;
delay();P0 = 0x80;
delay();
P0=0xff;delay();P0=0x00;delay();
P0=0xff;delay();P0=0x00;delay();
/* reverse*/
P0 = 0x80;
delay(); P0 = 0x40;
Microprocessor & Microcontroller Laboratory Work Book

delay();P0 = 0x20;
delay();P0 = 0x10;
delay();P0 = 0x08;
delay();P0 = 0x04;
delay();P0 = 0x02;
delay();P0 = 0x01;
delay();
P0=0xff;delay();P0=0x00;delay();
P0=0xff;delay();P0=0x00;delay();

}
}
void delay(void)
{
unsigned int y;
for(y=0;y<30000;y++);
}
Microprocessor & Microcontroller Laboratory Work Book

Experiment 12
Driving AC Load
220V AC can also be controller (Switched ON and OFF) through
Microcontroller using a Traic interface circuit (Consisting of BT138 Triac and MOC3041
AC optocoupler)
Hardware connections on page 19 of 8051ETK User Manual
A red indicator LED is also connected. The connections with the blue colour terminal
connector should be as in fig below.

AC Out
MCTDK Board

1 ACBULB
AC BULB
TRIAC CONNECTIONS
2
3
Connector # 1

Code:

/******************************************************************/
/* PROJECTS: Switching ON/OFF AC Load */
/* */
/* OBJECTIVE: */
/* This project toggles the AC Load connected at Port P2.0 */
/* of the microcontroller through some interface cct.
*/
/* */
/* COPYRIGHTS: 8051 TRAINING KIT Team - All rights reserved - 2007
*/
Microprocessor & Microcontroller Laboratory Work Book

/* */
/******************************************************************/

#include <AT89x51.h>
void delay (void);
sbit Bulb =P2^0;
void main (void)
{
for (;;)
{
Bulb=0; // bulb gets OFF
delay ();
Bulb=1; //Bulb is OFF
delay ();
}
}
void delay (void)
{
unsigned int y;
for(y=0; y<33000; y++);
}
Microprocessor & Microcontroller Laboratory Work Book

Experiment 13

Driving DC Load
DC Load up to 30V, 1 Amp can also be controller (Switched ON and OFF) through
microcontroller using a darlington IC.
An optocoupler is also connected in between for safety purpose.
Hardware connections on page 19, 20 of 8051ETK User Manual
An Orange indicator LED is also connected. The connections with the blue colour
terminal connector should be as in fig below.

Code:

/******************************************************************/
/* PROJECTS: Switching ON/OFF DC Load */
/* */
/* OBJECTIVE: */
/* This project toggles the DC Load connected to Port P2.1 */
/* of the microcontroller through some interface cct.
*/
/* */
Microprocessor & Microcontroller Laboratory Work Book

/* COPYRIGHTS: 8051 TRAINING KIT Team - All rights reserved - 2007


*/
/* */
/******************************************************************/

#include <AT89x51.h>
void delay (void);
sbit FAN =P2^1;
void main (void)
{
for (;;)
{
FAN=0; // Fan gets OFF
delay ();
FAN=1; //Fan is OFF
delay ();
}
}
void delay (void)
{
unsigned int y;
for(y=0; y<33000; y++);
}
Microprocessor & Microcontroller Laboratory Work Book

Experiment 14

Sounding Buzzer

A 5-12V buzzer is connected to P3.5 (T1) of the microcontroller.


The buzzer can be sounded manually through a push button T1(near buzzer) or through
software by writing 0 on the said port.
Please refer to page 23, 24 for hardware connection.

Code:
/******************************************************************/
/* PROJECTS: Buzzer tone */
/* */
/* OBJECTIVE: */
/* This project sounds buzzer connected at Port P3.5 of uC */
/* */
/* COPYRIGHTS: 8051 TRAINING KIT Team - All rights reserved - 2007
*/
/* */
/******************************************************************/

#include <AT89x51.h>
void delay (void);
sbit Buzzer = P3^5;
void main (void)
{
for (;;)
{
Buzzer=0;
delay ();
Buzzer=1;
delay ();
}
}
void delay (void)
{
unsigned int y;
for(y=0; y<33000; y++);
}
Microprocessor & Microcontroller Laboratory Work Book