0% found this document useful (0 votes)
1 views

Rev MPMC Lab Manual 2025

The document is a lab manual for the Microprocessor and Microcontrollers course for B.E – VI Semester at GEC, Bharuch, detailing various experiments related to the 8051 microcontroller. It includes instructions for programming, interfacing, and practical applications of microcontrollers, along with sample assembly language programs. The manual is prepared and reviewed by faculty members and includes an index of experiments and questions for students.

Uploaded by

yashkundan53
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Rev MPMC Lab Manual 2025

The document is a lab manual for the Microprocessor and Microcontrollers course for B.E – VI Semester at GEC, Bharuch, detailing various experiments related to the 8051 microcontroller. It includes instructions for programming, interfacing, and practical applications of microcontrollers, along with sample assembly language programs. The manual is prepared and reviewed by faculty members and includes an index of experiments and questions for students.

Uploaded by

yashkundan53
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

GEC,BHARUCH

ELECTRICAL DEPARTMENT

Department of Electrical Engineering

MPMC Lab Manual


B.E – VI Semester
YEAR-2025
GEC,BHARUCH

Department of Electrical Engineering

Microprocessor and Microcontrollers Lab


Manual

Prepared by: Reviewed by:


Prof. P.K.Shah / Prof. K.S.Shah Prof. V.Y.Doshi

Assistant Professor, Electrical H.O.D,Electrical

Approved by:
Prof. V.Y.Doshi

Head, Dept. of EE
MPMC SUBMISSION

Semester and Year:

Name of Student: Enrolment Number:

Date of Submission Grade

Faculty Signature

1
INDEX

Sr. No Aim Of Experiment Page No. Faculty Sign

1. Introduction to 8051

2. Data Transfer Programming

3. Arithmetic Instruction Programming

4. Boolean & Logical Instructions Programming

5. Counter Programming

6. Code Conversion Programming

7. Timer Programming

8. Serial Communication Programming

Stepper Motor Interfacing


9.

DC Motor Interfacing
10.

11. LCD and keypad interfacing

2
Exp1: Introduction to 8051
Earlier to Microcontrollers, Microprocessors were greatly used for each and
every purpose. Microprocessors were containing ALU, general purpose register, stack
pointer, program counter, clock counter and so many other features which the today’s
Microcontroller also possesses. But the difference between them exists with respect to
the number of instructions, access times, size, reliability, PCB size and so on.
Microprocessor contains large instruction set called as CISC processor whereas
Microcontroller contains less number of instructions and is called as RISC processor.
The access time is less in case of microcontrollers compared to microprocessors and
the PCB size reduces in case of microcontrollers.
There are many versions of microcontrollers 8051, 80528751, AT8951 from
Atmel Corporation and many more. In this manual we will study about the 8051
architecture, its features, programming and interfacing.
MCS 8051 is an 8-bit single chip microcontroller with many built-in functions
and is the core for all MCS-51devices.
The main features of the 8051 coreare:
 Operates with single Power Supply +5V.
 8-bit CPU optimized for control applications.
 16-bit program counter (PC) and 16-bit data pointer (DPTR).
 8-bit program status word (PSW).
 8-bit stack pointer (SP).
 4K Bytes of On-Chip Program Memory (Internal ROM or EPROM).
 128 bytes of On-Chip Data Memory (Internal RAM):
 Four Register Banks, each containing 8 registers (R0 to R7) [Total 32reg]
 16-bytes of bit addressablememory.
 80 bytes of general-purpose data memory (Scratch PadArea).
 Special Function Registers (SFR) to configure/operate microcontroller.
 32 bit bi-directional I/O Lines (4 ports P0 to P3).
 Two 16-bit timers/counters (T0 and T1).
 Full duplex UART (Universal Asynchronous Receiver/Transmitter).
 On-Chip oscillator and clock circuitry.

3
EXTERNAL
INTERRUPTS

4K
ROM TIMER 1
INTERRUPT 128 Byte RAM COUNTER
CONTROL INPUTS
TIMER 0

CPU

OSC BUS CONTROL 4 I/O PORTS SERIAL CONTROL

P0 P2 P1 P3 TXD RXD
X1 X2
ADDRESS /
DATA
Fig.1. General Block Diagram of 8051 MicrocontrollerArchitecture

STEPS TO CREATE AND COMPILE Keil µVision-3/4PROJECT:


1. Double Click onthe  icon on thedesktop.
2. Close any previous projects that were opened using – Project ->Close.
3. Start Project – New Project, and select the CPU from the device database
(Database-Atmel- AT89C51ED2 or AT89C51RD2 as per the board).On clicking
‘OK’, the following option is displayed. Choose ‘No’.

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->Manage->Components,
Environment 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.

4
5. Set the Target options using ->Project – Options for Target opens the
µ Options for Target – Target configuration dialog. Set theXtal
(Crystalfrequency)frequencyas11.0592MHz,andalsotheOptionsforTarget
– Debug – use either Simulator / Keil Monitor- 51 driver.

6. 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.
7. Build the project; using Project -> Build Project.
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, to build go to Debug mode.

8. NowusercanenterintoDebugmodewithDebug-Start/StopDebugsession

dialog. Or by clicking in the icon.


9. 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 (step, step over, and step into, run tillcursor).
10. IfitisaninterfaceprogramtheoutputscanbeseenontheLCD,CRO,motor,led
status, etc. If it is a part-A program, the appropriate memory window is opened
using View -> memory window (for data RAM & XRAM locations), Watch
window (for timer program), serial window, etc.

5
11. Note: To access data RAM area type address as D: 0020h. Similarly to access the
DPTR region (XRAM-present on chip in AT89C51ED2) say 9000h location type
in X: 09000H.

Questions:

1. What are the General Features of Microcontroller?

2. Explain briefly the classification of Microcontroller.

3. Explain briefly the Embedded Program development Tools.

6
Experiment no. 2 : Data Transfer Programming
a. Write an assembly language program to transfer N=05 bytes of data
fromlocation A: 30 h to location B:40h.
mov r0,#30h //source address
mov r1,#40h //destination address
mov r7,#05h //Number of bytes to be moved
back: mov a,@r0
mov @r1,a
inc r0
inc r1
djnz r7,back //repeat till all data transferred
end

Result:

Before execution

After execution

7
b. Write an assembly language program to find the largest element in a
given array of N =06 h bytes at location 4000h. Store the largest element
at location 4062h.

Let N = 06h
mov r3,#6 //length of thearray
mov dptr,#4000H //starting address of array
movx a,@dptr
movr1,a

nextbyte: inc dptr


movx a,@dptr
clrc //reset borrow flag
mov r2,a //next number in the array
sub a,r1 //other Num-Prev largest no.
jc skip
mov a,r2 //update larger number in r1
mov r1,a

skip: djnz r3,nextbyte


mov dptr, #4062H //location of the result-4062h
mov a,r1 //largest number
movx @dptr,a //store at #4062H
end

Result:

Before Execution:

After Execution:

8
c. Write an assembly language program to sort an array of N=06h bytes of
data in ascending/descending order stored from location 9000h.
(Using bubble sort algorithm)

mov r0,#05H //count(N-1) array size = N loop1:


mov dptr, #9000h //array stored from address 9000h
mov r1,#05h //initialize exchange counter
loop2: movx a, @dptr //get number from array and store in B register
mov b, a
inc dptr
movx a, @dptr //next number in thearray
clr c //reset borrowflag
mov r2, a //store inR2
subb a, b //2nd-1stNo, since no compare instruction in 8051
jnc noexchg // JC - FOR DESCENDINGORDER
mov a,b //exchange the 2 nos in the array
movx @dptr, a
dec dpl //DEC DPTR - instruction not present
mov a,r2
movx@dptr, a
inc dptr

noexchg:djnz r1,loop2 //decrement compare counter


djnz r0,loop1 //decrement pass counter
end

Result:

Before Execution:

After Execution :( Ascending order)

9
Experiment no. 3: Arithmetic Instruction Programming

a. Write an assembly language program to perform the addition of two


16-bitnumbers.

mov r0,#34h //lower nibble ofNo.1


mov r1,#12h //higher nibble ofNo.1
mov r2,#0dch //lower nibble ofNo.2
mov r3,#0feh //higher nibble of No.2
clr c
mov a,r0
add a,r2
mov 22h,a
mov a,r1
addc a,r3
mov 21h,a
mov 00h,c
end

Result:
Input: Output:

10
b. Write an assembly language program to perform the subtraction of
two16-bit numbers.

mov r0,#0dch //lower nibble of No.1


mov r1,#0feh //higher nibble ofNo.1
mov r2,#34h //lower nibble of No.2
mov r3,#12h //higher nibble of No.2
clrc //
mov a,r0
subb a,r2
mov 22h,a
mov a,r1
subb a,r3
mov 21h,a
mov 00h,c
end

Result:

Note: Try with different data. Ex: (Smaller number) – (larger number).

Questions:

1. Explain the Immediate Addressing Mode using arithmetical instruction set.

2. Explain the Register Addressing Mode using arithmetical instruction set.

4. Explain the Direct Addressing Mode using arithmetical instruction set.

5. Explain the Indirect Addressing Mode using arithmetical instruction set.

11
Experiment no.4: Boolean & Logical Instructions Programming

a. Write an ALP to compare two eight bit numbers NUM1 and NUM2 stored
in external memory locations 8000h and 8001h respectively. Reflect your
result as: If NUM1<NUM2, SET LSB of data RAM location 2FH (bit
address 78H). If NUM1>NUM2, SET MSB of location 2FH (bit address
7FH). If NUM1 = NUM2, then Clear both LSB & MSB of bit addressable
memory location2FH.

mov dptr,#8000h
movx a,@dptr
mov r0,a
inc dptr
movx a,@dptr
clrc
suba,r0
jz equal
jnc small
setb 7fh
sjmp end1
small: setb 78h
sjmp end1
equal: clr78h
clr7fh
end1:
end

Result:
1) Before Execution: X: 8000h = & X: 8001h =

After Execution:D:02FH =

2) Before Execution: X: 8000h = & X: 8001h =

After Execution:D:02FH =

3) Before Execution: X: 8000h = & X: 8001h =

After Execution:D:02FH =

12
b. Write an assembly language program to count number of ones and zeros in a
eight bitnumber.

movr1,#00h // to count number of 0s


movr2,#00h // to count number of 1s
movr7,#08h // counter for8-bits
mova,#97h // data to count number of 1s and 0s
again: rlca
jc next
incr1
sjmp here
next: inc r2
here: djnz r7,again
end
] Result:
Input: Output: Number of zero’s = r2 = Number of one’s = r1=

13
c. Write an assembly language program to find whether given eight bit number
is odd or even. If odd store 00h in accumulator. If even store FFh in
accumulator.

mov a,20h // 20h=given number, to find is it even or odd


jb acc.0,odd //jump if direct bit is set i.e., if lower bit is 1 then number is odd
mova,#0FFh
sjmp ext
odd: mova,#00h
ext: end

d. Write an assembly language program to perform logical operations AND,


OR, XOR on two eight bit numbers stored in internal RAM locations 21h,
22h.

mov a, 21h //do not use #, as data ram 21h is to be accessed


anl a,22h //logical and operation
mov 30h, a //and operation result stored in 30h
mov a,21h
orl a,22h //logical or operation
mov 31h, a //or operation result stored in 31h
mov a,21h
xrl a,22h //logical xor operation
mov 32h,a // xor operation result stored in 32h
end

Result:
Before Execution: D:21H= 22H=

After Execution: D: 030H = //AND operation


D: 031H = //OR operation
D: 032H = //XOR operation

14
Experiment no.5 : Counter Programming
a. Write an assembly language program to implement (display) an eight bit
UP/DOWN binary (hex) counter on watch window.
mov a,#00 //mov a, #0ffh for down counter
back: acall delay
inc a //dec a for binary down 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,decr1
ret
end

RESULT: Accumulator A is incremented in binary from


00, 01, 02…09,0A, 0B,…,0F,10,11,…FF
Note: To run this program, after selecting DEBUG session in the main menu use
View-> Watch & call Stack window, in the Watches select watch 1(or 2) and press
F2 and enter a (for accumulator A)

b. Write an assembly language program to implement (display) an eight bit


UP/DOWN decimal counter on watch window.
mov a,#99h //mov a, 00h for decimal up counter
back: acall delay
add a,#99h //add a,#01h for decimal up counter
da a //decimal adjust accumulator
jnz back
here: sjmp here
delay: mov r1,#0ffh
decr1: mov r2,#0ffh
decr: mov r3,#0ffh
djnz r3,$
djnz r2, decr
djnz r1, decr1
ret
end
RESULT: Accumulator A is incremented in BCD from99,98,97,.............. ,00.

15
Experiment no.6: Code Conversion Programming
a. Write an assembly language program to convert a BCD number in to ASCII.

mova, #09h //the BCD number to be converted to ASCII


mov r0,a
swap a
mov dptr,#9000h // output will be in 9000h and 90001h
acall ascii
mov a,r0
acall ascii
sjmp $
ascii: anl a,#0fh
add a,#30h
movx @dptr, a
inc dptr
ret
end
Result:
Input: Output:

b. i Write an assembly language program to convert a ASCII number into


Decimal.

mov dptr,#9000h //ASCII no. to be converted to decimal is stored in 9000h


movx a,@dptr
sub a,#30h
inc dptr
movx @dptr, a
end
Result:
Input: Output:

b. ii Write an assembly language program to convert a decimal number into


ASCII.

mov dptr,#9000h //Decimal number to be converted to ASCII is store in


movx a,@dptr // 9000h
add a,#30h
inc dptr // ASCII will be saved in 9001h
movx @dptr,a
end

Result:
Input: Output:

16
c. i Write an assembly language program to convert a binary (hex)number
intodecimal.

mov a,#0feh //binary number to be converted to decimal


mov b,#0ah
div ab
mov r0,b
mov b,#0ah
div ab
mov 30h,a
mov a,b
swap a
orl a,r0
mov 31h,a
end

Result:
Input: Output:

d. ii Write an assembly language program to convert a decimal number


into binary(hex).

mov a,#95h //a = Decimal number to be converted to the binary


mov b,#10h
div ab
mov r1,b
mov b,#0ah
mul ab
add a,r1
mov 30h,a
end

Result:
Input: Output:

17
Experiment no. 7: Timer Programming

Timer Calculation
8051 Oscillator frequency is divided by 12 and then fed to the controller, Time to increment the Timer
count by one(timer tick) can be determined as below.
tick = (1/(Fosc/12))
tick = 12/Fosc For Fosc == 11.0592Mhz, the tick time will be
tick = 12/11.0592= 1.085us

Now the Timer value for the required delay can be calculated as below.
Delay = Timer Count * tick
Count = (Delay/tick)
RegValue = Timer Max- Count
RegValue = Timer Max-(Delay/tick) = Timer Max - (Delay/1.085us)
RegValue = Timer Max-((Delay/1.085) * 10^6)

Timer Calculation for 50ms delay


Fosc = 11.0592Mhz
Delay = 50ms
RegValue = Timer Max-((Delay/1.085) * 10^6) RegValue = 65536 - (50ms/1.085)*10^6 = 65536 -
46082 = 19453 = 0x4BFD
(A) Polling Method
#include<reg51.h>
sbit LED = P0^0;
voidtimerDelay()
{
TH0 = 0X4B; //Load the timer value
TL0 = 0XFD;
TR0 = 1; //turn ON Timer zero
while(TF0 == 0); // Wait for Timer Overflow
TF0 = 0; //clear the timer Over flow flag
TR0 = 0;
}
voidmain()
{
TMOD = 0x01; //Timer0 mode 1
while(1)
{
LED = 1;
timerDelay();
LED = 0;
timerDelay();
}
}

18
Interrupt Method

#include<reg51.h>
sbit LED = P0^0;

voidtimer0_isr() interrupt 1
{
TH0 = 0X4B; //ReLoad the timer value
TL0 = 0XFD;
LED =! LED; // Toggle the LED pin
}

voidmain()
{
TMOD = 0x01; //Timer0 mode 1
TH0 = 0X4B; //Load the timer value
TL0 = 0XFD;
TR0 = 1; //turn ON Timer zero
ET0 = 1; //Enable TImer0 Interrupt
EA = 1; //Enable Global Interrupt bit
while(1)
{
// Do nothing
}
}

c. Conduct an experiment to generate 1second delay continuously using


on chip timer in assembly language.

clr a
setb tr0 again:
mov r7,#0ffh
loop: movr6,#14d
wait: jnb tf0,wait
clr tf0
djn r6,wait
djnzr7,loop
cpl P1.0
sjmp again
end

19
Questions:

What is the difference Modes 0,1,2 and3?

1. How do you differentiate between Timers and Counters?

2. Explain the function of the TMOD register and its various fields?

3. How do you control the timer/counter operation?

4. What is the function of TF0/TF1bit.?

5. Explain the function of the TCON register and its various fields?

6. Explain how the Timer/Counter Interrupts work.

7. What’s the benefit of the auto-reload mode?

20
Experiment no. 8: Serial Communication Programming
d. Conduct an experiment to configure 8051 microcontroller to transmit
characters “ENTER YOUR NAME” to a PC using the serial port and display
on the serial window.

Note: To use result of this program, after selecting DEBUG session in the
main menu use View-> serial window #1. On running & halting the program, the
data is seen in the serialwindow.

mov tmod,#20h //setting Timer-1 in mode-2


movscon,#70h
mov th1,#-3
setb tr1
again: mov r0,#03h
mov dptr,#8000h
nextchar: movx a,@dptr
acall transfer
incdptr
djnz r0,nextchar
sjmp again
transfer: mov sbuf,a
wait: jnb ti,wait
clrti
ret
end

RESULT:
Each time the program is executed, “ENTER YOUR NAME” will be displayed
on the serial window.

Baud rate Calculation:

Crystal freq/ (12*32) = (11.0592MHz)/(12*32) = 28800.


Serial communication circuitry divides the machine cycle frequency
(11.0592MHz)/(12) by 32 before it is being used by the timer to set the baudrate.

To get 9600, 28800/3 is obtained by loading timer1 with -3 (i.e., FF – 3 = FD) for
further clock division. For 2400 baud rate, 28800/12 => -12 = F4 in TH1.

21
Questions:

1. Explain Half Duplex data transmission with examples.

2. Explain Full Duplex data transmission with examples.

3. What is Baud rate?

4. What is a Modem?

5. Explain how the Baud rate is calculated for the Serial Data Mode1.

6. How is the Baud rate for the Multiprocessor communication Mode calculated?

22
Experiment no.9: Stepper Motor Interfacing
a. Write a C program to interface stepper motor.
Circuit Diagram:

8 P 0.0 Stepper
Motor Stepper
0 Motor
Driver circuit
5 P 0.7
1
Theory:
Stepper motor is an electromechanical device which converts electrical pulses
into discrete mechanical movements. The shaft or spindle of a stepper motor rotates in
discrete step increments when electrical command pulses are applied to it in the
proper sequence. The motors rotation has several direct relationships to these applied
input pulses. The sequence of the applied pulses is directly related to the direction of
motor shafts rotation. The speed of the motor shafts rotation is directly related to the
frequency of the input pulses and the length of rotation is directly related to the
number of input pulses applied.

Stepper Motor Advantages:


1. The rotation angle of the motor is proportional to the input pulse.
2. The motor has full torque at standstill (if the windings are energized)
3. Precise positioning and repeatability of movement since good stepper motors
have an accuracy of 3 – 5% of a step and this error is non cumulative from one
step to the next.
4. Excellent response to starting/stopping/reversing.
5. Very reliable since there are no contact brushes in the motor. Therefore the life
of the motor is simply dependent on the life of the bearing.
6. The motors response to digital input pulses provides open-loop control,
making the motor simpler and less costly to control.
7. It is possible to achieve very low speed synchronous rotation with a load that
is directly coupled to the shaft.
8. A wide range of rotational speeds can be realized as the speed is proportional
to the frequency of the input pulses.
Stepper Motor Disadvantages:
1. Resonances can occur if not properly controlled.
2. Not easy to operate at extremely high speeds.
Open Loop Operation:
One of the most significant advantages of a stepper motor is its ability to be
accurately controlled in an open loop system. Open loop control means no feedback
information about position is needed. This type of control eliminates the need for
expensive sensing and feedback devices such as optical encoders. Your position is
known simply by keeping track of the input step pulses.

23
#include <REG51xD2.H>
void delay (unsigned int x) /* Delay Routine*/
{
for(;x>0;x--);
return;
}

main ( )
{
unsigned char Val, i;
P0=0x00;
while(1)
{
Val = 0x11;
for(i=0;i<4;i++)
{
P0 =Val;
Val = Val<<1; /* Val= Val>>1; for clockwise direction*/
delay (500);
}
}
}

Motor Specifications:
Step Angle = 1.8 degrees
Step angle accuracy = 5%
Holding Torque = 40Ncm
Rotor Inertia = 115grcm2
Weight = 0.5Kg
Insulation = Class B

24
EXP 10.DC motor Interfacing .

Circuit Diagram:

8 P 3.2
0 (INCR) DC
5 P 3.3 Motor
1 (DECR)

Theory:
DC motors are used in many applications like process control and automation
in an industry, robotics, consumer electronics, office automation equipment like
printers and scanners etc. One can consider the use of a DC motor wherever there is
need to control the motion of an object. Speed control of the motor is important in the
applications involving them. For example, in an audio system, the DC motor that
drives the cassette should always run at a fixed speed. Like wise, there are
applications where the speed of the DC motor has to change according to some
defined conditions. The DC motor used in this interface module is a 12V, 4W motor
that can be seen in many electronic equipments. The circuit to control the speed of the
motor follows a general concept and can be applied to DC motors of higher capacity
also.
The pulse width modulation technique is used to vary the speed of the DC
motor. The frequency of the pulses is 120Hz. Keeping the frequency constant, the
width of the pulses is used to change the speed. When the pulse width is minimum,
the speed is minimum and when the width is maximum, the speed is maximum
(2400rpm). The ramp and pedestal technique is used to change the pulse width and
thereby the speed.

25
Program:
#include <REG51xD2.H>
sbit inr=P3^2; //speed increment switch
sbit dcr= P3^3; //speed decrement switch

main()
{
unsigned char i=0x80;
P0=0x7f; /*Run the motor at half speed.*/
while(1)
{
if(!inr)
{
while (!inr);
if(i>10)
i=i-10; //increase the DC motor speed
}

if(!dcr)
{
while(!dcr);
if(i<0xf0)
i=i+10; //decrease the DC motor speed
}
P0=i;
}
}

26
Experiment no. 11: LCD and keypad interfacing
Block Diagram:

27

You might also like