0% found this document useful (0 votes)
20 views67 pages

MC Manual Ec

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views67 pages

MC Manual Ec

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 67

UNIVERSITY BDT COLLEGE OF ENGINEERING

DAVANAGERE

(A Constituent College of VTU,


Belagavi)

Department of Electronics and Communication


Engineering

MICROCONTROLLER LAB MANUAL


SUBJECT:8051 MICROCONTROLLER

UNDER THE GUIDANCE OF

Dr.Geetha .V

Associate Professor

Electronics and Communications Department

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.

data using different instructions.


2. Develop Testing and experimental procedures on
8051
Microcontroller, Analyze their operationunder different cases.
3. Develop programs for 8051 Microcontroller to implement real
world
problems.
4.Develop Microcontroller applications using external hardware
interface.
Introduction to Microcontroller 8051
The most universally employed set of microcontrollers come from the 8051 family.
8051

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

8051 family are-

8052-This microcontroller has 3 timers & 256 bytes of RAM. Additionally it has all
the

Features of the traditional 8051


microcontroller.

8051 microcontroller is a subset of 8052 Microcontroller.

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

added to its chip.

8051 microcontroller brings into 2 different sorts of memory such as – NV- RAM, UV –

EPROM and Flash.

8051 is the basic microcontroller to learn embedded systems


projects.

FEATURES OF 8051

8051 microcontroller is an eight bit microcontroller. It is available in 40 pin DIP package.


It

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

comprise of 5 interrupt sources namely- Serial Port Interrupt, Timer Interrupt 1,

External Interrupt 0, Timer Interrupt 0, External Interrupt


1.

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

Fig. 1 Internal Architecture 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.

Logical AND, OR and exclusive OR (XOR) come under logical 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 DIAGRAM OF 8051 MICRO-CONTROLLER


PINOUT DESCRIPTION

Pins 1-8: Port 1 Each of these pins can be configured as an input or an


output.

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 10: RXD Serial asynchronous communication input or Serial synchronous


communication
Output.

Pin 11: TXD Serial asynchronous communication output or Serial synchronous


Communication
clock output.

Pin 12: INT0 Interrupt 0


input.

Pin 13: INT1 Interrupt 1


input.

Pin 14: T0 Counter 0 clock


input.
3|Page
Pin 15: T1 Counter 1 clock
input.

Pin 16: WR Write to external (additional) RAM.

Pin 17: RD Read from external


RAM.

Pin 18, 19: XTAL2, XTAL1 are internal oscillator input and output pins. A quartz crystal
which

Specifies operating frequency is usually connected to these pins. Instead of it,


miniature
Ceramics resonators can also be used for frequency stability. Later versions of
Microcontrollers
operate at a frequency of 0 Hz up to over 50
Hz.

Pin 20: GND Ground.

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).

Pin 40: VCC +5V power


supply.

5|Page
I. Assembly Language Programming
Data Transfer Instructions:
PROGRAM 1:

Write an ALP to move a block of n (05)bytes of data from source (20h) to


destination (40h) using internal RAM

MOV R0,#20H ; MOVE FIRST ARRAY ADDRESS TO


R0

MOV R1,#40H ; MOVE SECOND ARRAY ADDRESS TO


R1

MOV R2,#5H ; SET COUNTER

BACK: MOV A,@R0 ;

MOV @R1,A ; TAKE FIRST ARRAY ELEMENT TO A AND MOVE THAT TO SECOND
ARRAY

INC R0

INC R1 ; POINT TO NEXT ARRAY


LOCATION

DJNZ R2,BACK ; REPEAT STEPS UNTIL R2=0

END ; HLT

Input :

LOCATIO DATA LOCATION DATA


N
20 6 40 6
21 9 41 9
22 A 42 A
23 1 43 1
24 F 44 F
6|Page
Output:
LOCATIO DAT LOCATION DATA
N A
20 6 40 6
21 9 41 9
22 A 42 A
23 1 43 1
24 F 44 F

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).

MOVR0,#20H; MOVE FIRST ARRAY ADDRESS TO R0


MOVR1,#40H; MOVES ECONDARRAY ADDRESS TO R1
MOVR2,#5H; SETCOUNTER
BACK
:

MOVA,@R0; TAKE FIRST ARRAY ELEMENT TO A


XCHA,@R1; EXCHANGE WITH SECOND ARRAYT
MOV@R0,A; MOVE SECOND ARRAY DATA TO FIRST ARRAY THROUGH A
INCR0; POINT TO NEXT ARRAY LOCATION
INCR1; POINT TO NEXT ARRAY LOCATION
DJNZR2,BACK; REPEAT STEPS UNTIL R2=0
END; HLT

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:

Write an ALP to move a block of n bytes of data from


source(9000h)to
destination(9050h) using external
RAM
ORG0000H

MOVR2,#5h ;SET THE COUNT VALUE

MOV DPTR,#9050h ; MOVE DESTINATION ADDR. TO DPTR

MOV R0, 82H ;COPY DESTINATION ADD.INR0ANDR1

MOVR1,83H

MOV DPTR,#9000h; MOV SOURCE ADDR.TODPTR

BACK:MOVXA,@DPTR; TAKE SOURCE DATA TO A

INCDPTR ;POINT DPTR TO NEXT SOURCE LOCATION

PUSH82h; PUSH NEXT SOURCE ADDRESS TO STACK

PUSH83h

MOV82h,R0 ;RESTORE DESTINATION ADDR.TO DPTR FROM R0 AND R1

MOV83h,R1

MOVX@DPTR,A; MOVE SOURCE DATA TO DESTINATION THROUGH A

INCDPTR ; POINT DPTR TO NEXT DESTINATION LOCATION

MOVR0,82H ; SAVE NEXT DESTINATION ADDR. IN R0 AND R1

MOVR1,83H

POP83h ;POP SOURCE ADDRESSTO DPTR

POP82h

DJNZR2,BACK ;REPEATSTEPSUNTILR2=0

END;HLT

9|Page
INPUT:

Location Data Location


Data
9000 55
9050
9001 12
9051
9002 AC
9052
9003 0F
9053
9004 3C
9054
9005 08
9055

Output:

Location Data Location


Data
9000 55 9050
55
9001 12 9051
12
9002 AC 9052
AC
9003 0F 9053
0F
9004 3C 9054
3C
9005 08 9055
08

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

MOVDPTR,#8100H ;MOVE EXTERNAL MEMORY ADDRESS TO DPTR

MOVR2,#6H ;SETCOUNTER

BACK:MOVX A,@DPTR ;TAKE EXTERNAL MEMORY FIRST ARRAY ELEMENT TO


A

XCHA,@R0 ; EXCHANGE WITH INTERNAL ARRAY DATA

MOVX@DPTR,A ;MOVE INTERNA LARRAY DATA TO EXTERNAL MEMORY ARRAY


THROUGH
A
10 | P a g e
INCR0 ;POINT TO NEXT ARRAY
LOCATION

INCDPTR ;POINT TO NEXT ARRAY


LOCATION

DJNZR2,BACK ;REPEAT STEPS UNTIL R2=0

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
:

Location Data Location


Data
20 0A 8100
55
21 3B 8101
12
22 4C 8102
AC
23 13 8103
0F
24 7C 8104
3C
25 67 8105
08

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 B,@R0; DATA MOVED TO REGISTER A AND B INC R0

MUL AB; MULTIPLICATION IS DONE

MOV@R0,
A

INC
R0

MOVA,
B

MOV@R0,A; RESULT IS MOVED TO MEMORY USING POINTER


R0

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

AIM: Write an ALP to write to perform division operation on 8bit number


by
8bit
number.

ORG0000
H
MOVA,30H; DIVIDEND IS MOVED TO A
MOVB,31H; DIVISOR MOVED B
DIVAB; DIVISION OPERATION PERFORMED

MOV 32H,A; QUOTIENT IS MOVED TO 32H


MOV33H,B; REMAINDER IS STORED IN 33H

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:

Write an ALP to separate even and odd elements in a given


array

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:

Write an ALP to separate positive and negative elements in a given


array

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

MOVR0,#20H //point r0 to first


element

MOV40H,#00H//consider'00'aslargestelement MOV R2,#5//


set
counte
r

BACK:MOVA,@R0//take the array element to'a'and compare with datat


the

CJNEA,40H,NEX
T

NEXT:JCNEXT1//if array element>dataat40h,move array elementto40h MOV 40H,A

NEXT1:INCR0//point r0 to next
element

DJNZ
R2,BACK

MOVR0,#20H//point r0 to first
element

MOV41H,#0FFH//consider'ff'as smallestelement MOV R1,#5// set


counte
r
BACK1:MOVA,@R0//take the array element to'a'and compare with data at
the

CJNEA,41H,NEXT
2

NEXT2:JNCNEXT3//if array element<dataat41h,move array element to41h MOV


41H,A

NEXT3:INCR0//point r0 to next
element

DJNZ
R1,BACK1

EN
D

17 | P a g e
Program
12a

Write an ALP to arrange the numbers In ascending

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

Write an ALP to arrange the numbers in descending


order

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

Write an ALP for decimal up counter

ORG 00H

MOV A,#00H

BACK: ACALL DELAY

ADD A,#01H //ADD 01 FOR BCD UP


COUNTER

DA A

JNZ BACK

HERE: SJMP HERE

DELAY: MOV R1,#0FFH

DECR1: MOV R2,#0FFH

DECR2: MOV R3,#0FFH

DECR3: DJNZ R3,


DECR3

DJNZ R2,DECR2

DJNZ R1,DECR1

RET

END

Output :

A=00

01

….

98

99.

20 | P a g e
Program 14 :

Write an ALP for decimal down


counter

ORG 00H

MOV A,#00H

BACK: ACALL DELAY

ADD A,#99H //ADD 99 FOR BCD DOWN


COUNTER

DA A

JNZ BACK

HERE: SJMP HERE

DELAY: MOV R1,#0FFH

DECR1: MOV R2,#0FFH

DECR2: MOV R3,#0FFH

DECR3: DJNZ R3,


DECR3

DJNZ R2,DECR2

DJNZ R1,DECR1

RET

END

OUTPUT;

A= 99

9
8

.
.

.
.

00
21 | P a g e
Program 15:

Write an ALP for hexadecimal up


counter

ORG 00H

MOV A,#00H

BACK: ACALL DELAY

INC A //INCREMENT A FOR HEXADECIMAL UP


COUNTER

JNZ BACK

HERE: SJMP HERE

DELAY: MOV R1,#0FFH

DECR1: MOV R2,#0FFH

DECR2: MOV R3,#0FFH

DECR3: DJNZ R3,


DECR3

DJNZ R2,DECR2

DJNZ R1,DECR1

RET

END

O/P:

A=00

.
.

.
.

..

..

FE

FF
22 | P a g e
Program 16:

Write an ALP for hexadecimal down


counter

ORG 00H

MOV A,#00H

BACK: ACALL DELAY

DEC A //DECREMENT A FOR HEXADECIMAL DOWN


COUNTER

JNZ BACK

HERE: SJMP HERE

DELAY: MOV R1,#0FFH

DECR1: MOV R2,#0FFH

DECR2: MOV R3,#0FFH

DECR3: DJNZ R3,


DECR3

DJNZ R2,
DECR2

DJNZ R1,
DECR1

RET

END


Output:

A=FF

FE

..

.
.

..
01

23 | P a g e
II. C PROGRAMMING

1. Write an 8051 C program to find the sum of first 10 integer numbers

#include <reg51.h>
void main()
{
unsigned char sum =
0;
unsigned char i;
for(i=1;i<=10;i++)
{
sum+=i;
}
ACC=sum;
P0=sum;
}

2.Write an 8051 C program to find the factorial of a given number

#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

Program1:Write an 8051 C Program to rotate Stepper motor in Clockwise direction .

sfrP0=0x80
;

void MS Delay (unsigned int


value)

unsigned int x,y;

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
;

void MS Delay (unsigned int


value)

unsigned int x,y;

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.

Types of DACs•Binary-Weighted DAC: Uses resistors with weights corresponding to binary


bits.

• 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:

• Relative accuracy : +0.19% error maximum (DAC0808)


• Full scale current match: +1 LSB type
• 7 and 6-bit accuracy available (DAC0807,DAC0806)
• Fast settling time: 150 ns type
30 | P a g e
• Noninverting digital inputs are TTL and CMOS compatible
• High speed multiplying inputs slew rate: 8 mA/us
• Power supply voltage range: +4.5V to +18V
• Low Power consumption: 33 mW @ + 5V

31 | P a g e
Program 1

AIM: Generating different waveforms using DAC interface.

a. Sine wave generation


#include <reg51v.h> #include "SINE.H"
void
main(
)

{ while(1)

unsigned char i; for (i=0;i<180;i+


+)

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);

void MSDelay (unsigned int


value)

unsigned int y; for (y=0;y<value;y+


+);

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

MSDelay (unsigned int); void


main()

P0 = 0x00;

MSDelay (50);

P0 = 0xFF;

MSDelay (100);

}}

void MSDelay (unsigned int


value)

unsigned int y; for (y=0;y<value;y++); }

OUTPUT:

f. Saw tooth Waveform

#include <reg51v.h>

36 | P a g e
void main()

while(1)

unsigned char i; for


(i=0;i<255;i=i+5)

P0= i;

} for (i=255;i>1;i--)

P0= i;

OUTPUT:

*******

37 | P a g e

You might also like