MC Manual Ec
MC Manual Ec
DAVANAGERE
Dr.Geetha .V
Associate Professor
UBDTCE.
SYLLABUS
Sl.No
I. Assembly Language
Programming
Data Transfer Programs:
Pg.no
Introduction on 8051 Microcontroller 01-05
Write an ALP to move a block of n bytes of data from source (20h) to
06
1
destination (40h) using Internal-
RAM.
2 Write an ALP to move a block of n bytes of data from source (2000h) to 07-08
destination (2050h) usingExternal RAM.
3 Write an ALP To exchange the source block starting with address 20h, (Internal
09
RAM) containing N(05) bytes of data with destination block starting
with
address 40h (Internal
RAM).
4 Write an ALP to exchange the source block starting with address 10h (Internal 10-11
memory), containingn (06) bytes of data with destination block starting at
location 00h (External memory).
Arithmetic & Logical Operation
Programs:
Write an ALP to add the byte in the RAM at 34h and 35h, store the result in 12
5 the register R5 (LSB)and R6 (MSB), using Indirect Addressing Mode.
6 Write an ALP to subtract the bytes in Internal RAM 34h & 35h store the result 13
in register R5 (LSB)& R6 (MSB).
7
Write an ALP to multiply two 8-bit numbers stored at 30h and 31h and 13-14
store16- bit result in 32h and33h of Internal RAM.
8 Write an ALP to perform division operation on 8-bit number by 8-bit number. 14-
15
9 Write an ALP to separate positive and negative in a given array. 15-
16
10 Write an ALP to separate even or odd elements in a given array. 16-
17
11 Write an ALP to arrange the numbers in Ascending & Descending order.
17
12
Write an ALP to find Largest & Smallest number from a given array starting 18-19
from 20h & store it inInternal Memory location 40h.
Counter Operation
Programs:
13 Write an ALP for Decimal UP-Counter.
20
14 Write an ALP for Decimal DOWN-Counter. 21
15 Write an ALP for Hexadecimal UP-Counter. 22
16 Write an ALP for Hexadecimal DOWN-Counter.
23
II. C
Programming
1 Write an 8051 C program to find the sum of first 10 Integer Numbers.
24
2 Write an 8051 C program to find Factorial of a given number. 24
3 Write an 8051 C program to find the Square of a number (1 to 10) using Look-
25
Up Table.
4
Write an 8051 C program to count the number of Ones and Zeros in two 26
consecutive memory locations.
III.Hardware Interfacing
Programs
1 Write an 8051 C Program to rotate stepper motor in Clock & Anti-Clockwise 27-
29
direction.
2 Write an 8051 C program to Generate Sine & Square waveforms using DAC 30-
37
interface.
Microcontrollers persist to be an ideal choice for a huge group of hobbyists and experts.
The
Original 8051 microcontroller was initially invented by Intel. The two other members of this
8052-This microcontroller has 3 timers & 256 bytes of RAM. Additionally it has all
the
8031 – This microcontroller is ROM less, other than that it has all the features of
a
Traditional 8051 microcontroller. For execution an external ROM of size 64K bytes can Be
8051 microcontroller brings into 2 different sorts of memory such as – NV- RAM, UV –
FEATURES OF 8051
Has 4kb of ROM (on-chip programmable space) and 128 bytes of RAM space which is Inbuilt i
Two timers of 16 bits are also incorporated in it; these timers can be employed As timer
for internal functioning as well as counter for external functioning. The microcontroller
The programming mode of this micro-controller includes GPRs (general purpose registers),
SFRs (special function registers) and SPRs (special purpose
registers).
1|Page
INTERNAL ARCHITECHURE OF 8051 MICRO-CONTROLLER
1.
ALU
All arithmetic and logical functions are carried out by the ALU. Addition, subtraction with
carry, and multiplication come under arithmetic operations.
2. Program Counter
(PC)
A program counter is a 16-bit register and it has no internal address. The basic function
of
Program counter is to fetch from memory the address of the next instruction to be
executed.
2|Page
The PC holds the address of the next instruction residing in memory and when a command is
Encountered, it produces that instruction. This way the PC increments automatically, holding
The address of the next instruction.
3.Registers
Registers are usually known as data storage devices. 8051 microcontroller has 2
registers,
Namely Register A and Register B. Register A serves as an accumulator while Register
B
Functions as a general purpose register. These registers are used to store the output
of
Mathematical and logical instructions. The operations of addition, subtraction, multiplication
And division are carried out by Register A. Register B is usually unused and comes into
Picture
only when multiplication and division functions are carried out by Register A. Register A also
involved in data transfers between the microcontroller and external
memory.
Pin 9: RS A logic one on this pin disables the microcontroller and clears the contents of
most
Registers. In other words, the positive voltage on this pin resets the microcontroller.
By
Applying logic zero to this pin, the program starts execution from the
beginning.
Pins10-17: Port 3 Similar to port 1, each of these pins can serve as general input or
output.
Besides, all of them have alternative functions:
Pin 18, 19: XTAL2, XTAL1 are internal oscillator input and output pins. A quartz crystal
which
Pin 21-28: Port 2 If there is no intention to use external memory then these port pins are
Configured as general inputs/outputs. In case external memory is used, the higher
address
Byte, i.e. addresses A8-A15 will appear on this port. Even though memory with capacity of
64Kb
is not used, which means that not all eight port bits are used for its addressing, the rest Of
them
are not available as inputs/outputs.
Pin 29: PSEN If external ROM is used for storing program then a logic zero (0) appears on
it
Every time the microcontroller reads a byte from memory.
Pin 30: ALE Prior to reading from external memory, the microcontroller puts the
lower
Address byte (A0-A7) on P0 and activates the ALE output. After receiving signal from the
ALE pin, the external register (usually 74HCT373 or 74HCT375 add-on chip) memorizes
the
State of P0 and uses it as a memory chip address. Immediately after that, the ALU pin is
Returned its previous logic state and P0 is now used as a Data Bus. As seen, port
data
Multiplexing is performed by means of only one additional (and cheap) integrated circuit.
In
Other words, this port is used for both data and address
transmission.
Pin 31: EA By applying logic zero to this pin, P2 and P3 are used for data and
address
Transmission with no regard to whether there is internal memory or not. It means that
even
There is a program written to the microcontroller, it will not be executed. Instead, the
program Written to external ROM will be executed. By applying logic one to the EA pin, the
Microcontroller will use both memories, first internal then external (if
exists).
4|Page
Pin 32-39: Port 0 Similar to P2, if external memory is not used, these pins can be used
as
General inputs/outputs. Otherwise, P0 is configured as address output (A0-A7) when the ALE
Pin is driven high (1) or as data output (Data Bus) when the ALE pin is driven low
(0).
5|Page
I. Assembly Language Programming
Data Transfer Instructions:
PROGRAM 1:
MOV @R1,A ; TAKE FIRST ARRAY ELEMENT TO A AND MOVE THAT TO SECOND
ARRAY
INC R0
END ; HLT
Input :
Program.2
Write an ALP to exchange the source block starting with address 20h
(internal
RAM) containing N (05) bytes of data with
destinationblockstartingwithaddress40h (internal RAM).
7|Page
Input:
LOCATION DATA LOCATION
DATA
20 55 40
0A
21 12 41
3B
22 AC 42
4C
23 0F 43
13
24 3C 44
7A
Output:
LOCATION DATA LOCATION
DATA
20 0A 40
55
21 3B 41
12
22 4C 42
AC
23 13 43
0F
24 7A 44
3C
8|Page
Program
3:
MOVR1,83H
PUSH83h
MOV83h,R1
MOVR1,83H
POP82h
DJNZR2,BACK ;REPEATSTEPSUNTILR2=0
END;HLT
9|Page
INPUT:
Output:
Program
4:
Write an ALP to exchange the source block starting with address 10h(internalmemory),
containing n(06)to external memory block starting with
address#8100h
ORG0000H
MOVR0,#20H ;MOVE FIRST ARRAY ADDRESS TO
R0
MOVR2,#6H ;SETCOUNTER
END;
HLT
INPUT
:
Location Data Location
Data
20 55 8100
0A
21 12 8101
3B
22 AC 8102
4C
23 0F 8103
13
24 3C 8104
7C
25 08 8105
67
OUTPUT
:
11 | P a g e
Arithmetic & Logical Operation Programs
Program
5:
Write an ALP to add the byte in the RAM at 34h and 35h, store the result in the register
R5
(LSB) and R6 (MSB), using indirect
addressing
ORG
0000H
MOV R1,#34H ; DATA ADDRESS IS LOADED TO
R1
MOV R0,#35H ; DATA ADDRESS IS LOADED TO
R2
CLR
C
MOV
R6,#00H
MOV
A,@R0
ADD A,@R1 ; THE VALUE PRESENT IN 34H IS ADDED TO THE VALUE
PRESENT
;IN35H AND THE RESULT IS LOADED TO
A
JNC
NEXT
INC R6 ;IF CARRY SET DURING ADDITION, THEN R6 (MSB)
IS
NEXT: MOV
R5,A
EN
D
INPUT
:
LOCATION
DATA
34
FF
35
02
OUTPUT
:
LOCATION
DATA
R5
01
R6
01
12 | P a g e
Program
6:
Write an ALP to subtract the bytes in internal RAM 34H & 35H, store the result in
register
R5 (LSB) & R6
(MSB)
ORG
0000H
CLR
A
MOV A,34H; LOAD MINUEND TO
A
SUBB A,35H; SUBTRACT SUBTRAHEND FROM
MINUEND
JNC NEXT ; IF BORROW=0, GOTO NEXT
INC R6 ; IF BORROW SET, INCREMENT R6
NEXT: MOV R5,A; MOVE THE DIFFERENCE TO R5
EN
D
INPUT
:
LOCATION
DATA
34
01
35
02
OUTPUT
:
LOCATION
DATA
R5
FF
R6
01
PROGRAM
7
AIM:Write an ALP to multiply two 8bit numbers storedata 30H & b31H and store 16
bit result in 32H and 33H
ofinternalRAM.
MOVR0,#30H; R0 IS POINTING TO
MEMORY
MOVA,@R
0
INC
R0
MOV@R0,
A
INC
R0
MOVA,
B
EN
D
13 | P a g e
Input
:
LOCATION
DATA
30
0A
31
12
3
2
3
3
Output
:
LOCATION
DATA
30
0A
31
12
32
B4
33
00
PROGRAM
8
ORG0000
H
MOVA,30H; DIVIDEND IS MOVED TO A
MOVB,31H; DIVISOR MOVED B
DIVAB; DIVISION OPERATION PERFORMED
EN
D
Input
:
LOCATION
DATA
30
15
31
06
32
FF
14 | P a g e
3
3
C
Output
:
LOCATION
DATA
30
15
31
06
32
03
33
03
PROGRAM
9:
ORG
0000H
MOVR0,#30H //MOVE STARTING ARRAY LOCATION TO 30H
MOV R2,#5H //SET COUNTER
MOV R4,#50H //INITIALIZE R4 TO HOLD AN ARRAY TO ODD VALUES
MOVR1,#40H//INITIALIZE R1 TO HOLD AN ARRAYTO EVEN VALUES
BACK: MOV A,@R0 // TAKE ARRAY ELEMENT TO A
RRC
A //IF LSB='1' JUMP TO
"ODD"
JCODD RLC
A
MOV @R1,A //STORE EVEN NUMBER IN AN ARRAY POINTED BY
R1
INC R1 // POINT R1 TO STORE NEXT EVEN
ELEMENT
SJMPNEXT ODD:RLC
A
MOV R5,A //SAVE THE ODD NUMBER IN
R5
MOV A,R0 //SAVE POINT RO TO
R6
MOVR6,
A
MOV A,R4 //MOVE POINT R4 TO R0
MOV
R0,
A
MOV A,R5 //MOVE THE ODD NUMBER TO ARRAY OF ODD NUMBERS
MOV
@R0,A
MOVA,R
0
MOVR4,
A
MOVA,R
6
MOVR0,
A
INC R4 //POINT R1 TO STORE NEXT ODD ELEMENT
NEXT:INC R0// POINT RO TO NEXT ELEMENT
DJNZR2,BACK//IF COUNT!=0 REPEAT
PROCESS
15 | P a g e
EN
D
INPUT
OUTPUT
LOCATIO DATA EVEN DATA ODDARRAY
DATA
LOCATIO
N
N
ARRAY
LOCATIO
N
30 2 40 2 50
1
31 1 41 A 51
9
32 A 52
B
33
9
34
B
PROGRAM
10:
ORG0000
H
MOVR0,#30H//MOVE STARTING ARRAY LOCATION TO
30H
MOV R2,#5H //SET
COUNTER
MOVR4,#50H//INITIALIZE R4 TO HOLD AN ARRAY TO NEGATIVE
VALUES
MOVR1,#40H//INITIALIZE R1 TO HOLD AN ARRAY TO POSITIVE VALUES
BACK: MOV A,@R0 //TAKE ARRAY ELEMENT TO
A
JB
ACC.7,NEG
MOV @R1,A //STORE POSITIVE NUMBER IN AN ARRAY POINTED BY
R1
INC R1// POINT R1 TO STORELOCATION
NEXT POSITIVE A LOCATION
ELEMENT
SJMP NEXT
NEG:MOV R5,A //SAVE THE NEGATIVE NUMBER IN R5 MOV
A,R0
//SAVE POINT RO TO
R6
MOVR6,
A
MOV A,R4 //MOVE POINT R4 TO R0
MOV
R0,
A
MOV A,R5 //MOVE THE NEGATIVE NUMBER TO ARRAY OF NEG
NUMBERS
MOV
@R0,A
MOVA,R
0
MOVR4,
A
MOVA,R
6
MOVR0,
A
INC R4 //POINT R1 TO STORE NEXT NEGATIVE
ELEMENT
NEXT: INC R0 // POINT RO TO NEXT
ELEMENT
DJNZR2,BACK// IFCOUNT!=0 REPEAT
PROCESS
EN
D
INPUT
OUTPUT
LOCATION DATA POSITIVEARRAY DAT NEGATIVEARRAY
DATA
16 | P a g e
30 F4 40 1 50
F4
31 1 41 A 51
F7
32 F7 42
8
33
A
34
8
PROGRAM
11:
Write an ALP to find largest & smallest number from a given array starting from 20 h and
store largest number in internal memory location 40h and smallest number in
41h.
ORG
0000H
CJNEA,40H,NEX
T
NEXT1:INCR0//point r0 to next
element
DJNZ
R2,BACK
MOVR0,#20H//point r0 to first
element
CJNEA,41H,NEXT
2
NEXT3:INCR0//point r0 to next
element
DJNZ
R1,BACK1
EN
D
17 | P a g e
Program
12a
orde
r
ORG
0000H
LOOP1:MOV
R2,#05H
MOV R0,#40H //LOAD ARRAY ADDRESS TO R0
MOV
R3,#05H// LOAD COUNT TO R2 &
R3
LOOP:MOVA,
@R0
INCR
0
MOV 50H,@R0//LOAD 1ST ELEMENT AS SMALLES T NUMBER IN 50H AND COMPARE WITH NEXT
//ARRAY
ELEMENT
CJNEA,50H,NEX
T
SJMP
NOCHG
NEXT:JC
NOCHG
MOV@R0,A DEC
R0
MOV@R0,50H//SMALLEST NUMBER IS LOADED TO 50 TH LOCATION FOR
COMPARISION
INCR
0
NOCHG:DJNZR3,LOO
P
DJNZR2,LOOP1//DO COMPARISION TILL R2 AND R3 BECOMES ZERO
EN
D
INPUT
OUTPUT
LOCATION DATA LOCATION DATA
40 0F 40
1
41` 1 41`
5
42 9 42
9
43 0A 43
0A
44 5 44
0C
45 0C 45
0F
18 | P a g e
Program12
b
ORG
0000H
LOOP1:MOV
R2,#05H
MOV R0,#40H //LOAD ARRAY ADDRESS TO
R0
MOV R3,#05H// LOAD COUNT TO R2 &
R3
LOOP:MOVA,
@R0
INCR
0
MOV 50H,@R0//LOAD 1ST ELEMENT AS SMALLEST NUMBER IN 50H AND COMPARE WITH NEXT
//ARRAY
ELEMENT
CJNEA,50H,NEX
T
SJMP
NOCHG
NEXT:JNC
NOCHG
MOV@R0,A DEC
R0
MOV@R0,50H//SMALLEST NUMBER IS LOADED TO 50TH LOCATION FOR COMPARISION
INCR
0
NOCHG:DJNZR3,LOO
P
DJNZR2,LOOP1//DO COMPARISION TILL R2 AND R3 BECOMES ZERO
EN
D
INPUT
OUTPUT
LOCATION DATA LOCATION DATA
40 3F 40
3F
41` 1 41`
99
42 10 42
55
43 1C 43
1C
44 99 44
10
45 55 45
1
19 | P a g e
Counter Operation Program:
Program 13
ORG 00H
MOV A,#00H
DA A
JNZ BACK
DJNZ R2,DECR2
DJNZ R1,DECR1
RET
END
Output :
A=00
01
….
98
99.
20 | P a g e
Program 14 :
ORG 00H
MOV A,#00H
DA A
JNZ BACK
DJNZ R2,DECR2
DJNZ R1,DECR1
RET
END
OUTPUT;
A= 99
9
8
.
.
.
.
00
21 | P a g e
Program 15:
ORG 00H
MOV A,#00H
JNZ BACK
DJNZ R2,DECR2
DJNZ R1,DECR1
RET
END
O/P:
A=00
.
.
.
.
..
..
FE
FF
22 | P a g e
Program 16:
ORG 00H
MOV A,#00H
JNZ BACK
DJNZ R2,
DECR2
DJNZ R1,
DECR1
RET
END
•
Output:
A=FF
FE
..
.
.
..
01
23 | P a g e
II. C PROGRAMMING
#include <reg51.h>
void main()
{
unsigned char sum =
0;
unsigned char i;
for(i=1;i<=10;i++)
{
sum+=i;
}
ACC=sum;
P0=sum;
}
#include
<reg51.h>
void main()
{
unsigned inti;
unsigned char num=5;// number to find the factorial
of
unsigned long factorial=1;// calculate the factorial of the number
for(i=1;i<=num;i++)
{
factorial*=i;
} // output the
result
P1=factorial;
}
24 | P a g e
3.Write an 8051 C program to find the square of a number (1 to 10) using Look-Up Table.
#include <reg51.h>
//Look up table of
squares
const unsigned char
squares[]={ 1,4,9,16,25,36,49,64,81,100};
void main()
{
unsigned char number=9;//number for which we want to find the
square
unsigned char
square;
while(number<=10)
{
square=squares[number-1];
P0=square;//Dispay the result on
port1
}
while(1);
}
25 | P a g e
4.Write an 8051 C program to count the number of ones and zeros in two
consecutive
memory locations
#include <reg51.h>
void main()
{
unsigned char
array[]={0x31,0x26};
unsigned char
i,ones,zeros;
CY=0;
for(i=0;i<8;i++)
{
array[0]>>=1;
if (CY==1) ones++;
else zeros++;
}
for(i=0;i<8;i++)
{
array[1]>>=1;
if (CY==1) ones+
+;
else zeros++;
}
P0=zeros;
P1=ones
;
while(1);
}
26 | P a g e
STEPPER MOTOR
Theory
:
Stepper motors work on the principle of electromagnetism. There is a soft iron or magnetic rotor shaft
surrounded by the electromagnetic stators. The rotor and stator have poles which may be teethed or not
depending upon the type of stepper. When the stators are energized the rotor moves to align itself along
with the stator(in case of a permanent magnet type stepper) or moves to have a minimum gap with the
stator(in case of a variablereluctance stepper). This waythestators are energizedin a sequence to rotate the
stepper motor.
EXPERIMENT:01
sfrP0=0x80
;
for(x=0;x<value;x++)
27 | P a g e
for(y=0;y<1275;y+
+);
main(
)
while(1
)
P0=0xCC
;
MSDelay( 25)
;
P0=0x99
;
MSDelay(25)
;
P0=0x33
;
MSDelay(25)
;
P0=0x66
;
MSDelay(25)
;
}
28 | P a g e
Program2: Write an 8051 C Program to rotate Stepper motor in Anticlockwise
direction.
sfrP0=0x80
;
for(x=0;x<value;x++)
for(y=0;y<1275;y+
+);
main(
)
while(1
)
P0=0x66
;
MSDelay( 25)
;
P0=0x33
;
MSDelay(25)
;
P0=0x99
;
MSDelay(25)
;
P0=0xCC
;
MSDelay(25)
;
29 | P a g e
DAC INTERFACING
Introduction
:
A DAC (Digital to Analog converter) is a crucial component in digital systems , converting digital signals
into
analog signals. Here’s an outline for DAC interface theory suitable for a lab manual.
• A DAC converts digital binary values (0s and 1s) into corresponding analog and voltage
currents.
• Applications: used in audio equipment, video systems, signal processing, instrumentation, and control
system.
• R-2R Ladder DAC: Utilizes a repetitive structures of resistors with values R and 2R.
• Sigma-Delta DAC: Uses oversampling and noise shaping to achieve high-resolution conversion.
Circuit Diagram:
Features:
31 | P a g e
Program 1
{ while(1)
P0=
(table[i]);
}}
}
32 | P a g e
b. Square wave generation
#include <reg51v.h> void MSDelay (unsigned int);
void
main()
{ while(1)
P0 = 0x00;
MSDelay (50);
P0 = 0xFF;
MSDelay (50);
OUTPUT:
33 | P a g e
c. Triangular Wave generation
#include <reg51v.h> void main()
while(1)
unsigned char i;
for (i=0;i<255;i++)
P0= i;
for (i=255;i>0;i--)
P0= i;
OUTPUT:
34 | P a g e
d. Ramp generation
#include <reg51V.h>
void main()
while(1)
unsigned char i;
for (i=0;i<255;i++)
P0= i;
OUTPUT:
e.Rectangular waveform
35 | P a g e
#include <reg51v.h.H>
void
P0 = 0x00;
MSDelay (50);
P0 = 0xFF;
MSDelay (100);
}}
OUTPUT:
#include <reg51v.h>
36 | P a g e
void main()
while(1)
P0= i;
} for (i=255;i>1;i--)
P0= i;
OUTPUT:
*******
37 | P a g e