Rev MPMC Lab Manual 2025
Rev MPMC Lab Manual 2025
ELECTRICAL DEPARTMENT
Approved by:
Prof. V.Y.Doshi
Head, Dept. of EE
MPMC SUBMISSION
Faculty Signature
1
INDEX
1. Introduction to 8051
5. Counter Programming
7. Timer Programming
DC Motor Interfacing
10.
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
P0 P2 P1 P3 TXD RXD
X1 X2
ADDRESS /
DATA
Fig.1. General Block Diagram of 8051 MicrocontrollerArchitecture
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
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:
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
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)
Result:
Before Execution:
9
Experiment no. 3: Arithmetic Instruction Programming
Result:
Input: Output:
10
b. Write an assembly language program to perform the subtraction of
two16-bit numbers.
Result:
Note: Try with different data. Ex: (Smaller number) – (larger number).
Questions:
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 =
After Execution:D:02FH =
After Execution:D:02FH =
12
b. Write an assembly language program to count number of ones and zeros in a
eight bitnumber.
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.
Result:
Before Execution: D:21H= 22H=
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
15
Experiment no.6: Code Conversion Programming
a. Write an assembly language program to convert a BCD number in to ASCII.
Result:
Input: Output:
16
c. i Write an assembly language program to convert a binary (hex)number
intodecimal.
Result:
Input: Output:
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)
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
}
}
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:
2. Explain the function of the TMOD register and its various fields?
5. Explain the function of the TCON register and its various fields?
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.
RESULT:
Each time the program is executed, “ENTER YOUR NAME” will be displayed
on the serial window.
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:
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.
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