MicroCont LabManual Updated

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

Prepared By Mamatha Mallesh, Dept.

of ECE, VKIT MICROCONTROLLERS [BEC456A]


Do’s:

1. Students must bring observation/Manual book along with pen, pencil and eraser etc.

2. Students must handle the hardware kit and other components carefully as they are expensive

3. Before entering to lab, must prepare for viva for which they are going to conduct experiment.

4. Before switching on the hardware kit, the connections must be shown to one of the lab in-charge faculty members or
instructors.

5. After the completion of the experiment should return the components to the lab instructors.

6. Strictly follow the procedures for conduction of experiments.

7. Any breakdown/failure of equipment must be reported to the technical staff immediately.

8. Uniform and ID card are must.

9. Maintain silence inside the laboratory

10.Keep your belongings in designated area.

11.Wear shock–proof footwear while entering into the circuit lab

12.Chairs and stools should be kept under the workbenches when not in use.

13.Sit upright on chairs or stools, keeping feet on the floor.

14.Every student should know the location and operating procedures of all Safety equipment including First Aid Kit and
Fire extinguisher.

DONT’S:

1. Don’t eat food, drink beverages or chew gum in the laboratory.

2. Don’t touch any live terminals.

3. Don’t spread unwanted connecting wires on the table.

4. Don’t play with the instruments laid on work bench.

5. Don’t transfer equipment to other workbench or other labs without permission.

6. Don’t change connection when supply is on.

7. Don’t leave the experiment table unattended when the experimental setup supply is on.

1
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]
Course outcomes (COs)

1. Understand the basic programming of Microcontrollers.


2. Develop the 8051 Microcontroller-based programs for various applications using Assembly Language
& C Programming.
3. Program 8051 Microcontroller to control an external hardware using suitable I/O ports.

2
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]
Program Outcomes (POs)
At the end of the B.E program, students are expected to have developed the following outcomes.

PO1 Engineering Apply the knowledge of mathematics, science, engineering


Knowledge fundamentals, and an engineering specialisation to the solution of
complex engineering problems.

PO2 Problem analysis Identify, formulate, research literature, and analyse complex
engineering problems reaching substantiated conclusions using first
principles of mathematics, natural sciences, and engineering sciences.
PO3 Design/development of Design solutions for complex engineering problems and design
solutions system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the
cultural, societal, and environmental considerations.
PO4 Conduct Use research-based knowledge and research methods including
investigations of design of experiments, analysis and interpretation of data, and
complex problems synthesis of the information to provide valid conclusions.
PO5 Modern Tool Usage Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modelling
to complex engineering activities with an understanding of the
limitations.
PO6 The Engineer and Apply reasoning informed by the contextual knowledge to assess
Society societal, health, safety, legal, and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
PO7 Environment and Understand the impact of the professional engineering solutions in
Sustainability societal and environmental contexts, and demonstrate the knowledge
of need for sustainable development.
PO8 Ethics Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.
PO9 Individual and Team Function effectively as an individual, and as a member or leader in
Work diverse teams, and in multidisciplinary settings.
PO10 Communication Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able
to comprehend and write effective reports and design documentation,
make effective presentations, and give and receive clear instructions.
PO11 Project Management Demonstrate knowledge and understanding of the engineering and
and Finance management principles and apply these to one‘s own work, as a
member and leader in a team, to manage projects and in
multidisciplinary environments
PO12 Life-long learning Recognise the need for, and have the preparation and ability to engage
in independent and life-long learning in the broadest context of
technological change

3
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]
Table of Contents
SL PAGE COs
EXPERIMENT
NO. NO.
I. Assembly Language Programming
Data Transfer Programs:
Write an ALP to move a block of n bytes of data from source (20h) to CO1
1
destination (40h) using Internal-RAM
Write an ALP to move a block of n bytes of data from source (2000h) to CO1
2
destination (2050h) using External RAM.
Write an ALP To exchange the source block starting with address 20h,
3 (Internal RAM) containing N (05) bytes of data with destination block CO1
starting with address 40h (Internal RAM).
Write an ALP to exchange the source block starting with address 10h
4 (Internal memory), containing n (06) bytes of data with destination block CO1
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 CO1
5
in the register R5 (LSB) and R6 (MSB), using Indirect Addressing Mode.
Write an ALP to subtract the bytes in Internal RAM 34h &35h store the CO1
6
result in register R5 (LSB) & R6 (MSB).
Write an ALP to multiply two 8-bit numbers stored at 30h and 31h and CO1
7
store16- bit result in 32h and 33h of Internal RAM.
Write an ALP to perform division operation on 8-bit number by 8-bit CO1
8
number.
9 Write an ALP to separate positive and negative in a given array CO1
10 Write an ALP to separate even or odd elements in a given array. CO1
11 Write an ALP to arrange the numbers in Ascending & Descending order. CO1
Write an ALP to find Largest & Smallest number from a given array CO1
12
starting from 20h & store it in Internal Memory location 40h.
Counter Operation Programs:
13 Write an ALP for Decimal UP-Counter. CO2
14 Write an ALP for Decimal DOWN-Counter. CO2
15 Write an ALP for Hexadecimal UP-Counter. CO2
16 Write an ALP for Hexadecimal DOWN-Counter. CO2
II. C Programming
1 Write an 8051 C program to find the sum of first 10 Integer Numbers. CO2
2 Write an 8051 C program to find Factorial of a given number. CO2
Write an 8051 C program to find the Square of a number (1 to 10) using CO2
3
Look-Up Table.
Write an 8051 C program to count the number of Ones and Zeros in two CO2
4
consecutive memory locations.
III. Hardware Interfacing Programs
Write an 8051 C Program to rotate stepper motor in Clock & Anti- CO3
1
Clockwise direction.
Write an 8051 C program to Generate Sine & Square waveforms using CO3
2
DAC interface.

4
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]
INTRODUCTION

8051 is an 8-bit single chip microcontroller with many built-in functions.

The main features of the 8051 microcontroller are:


 Operate switch 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):
o Four Register Banks, each containing 8 registers (R0 to
R7)(Total 32 registers).
o 16 bytes of bit addressable memory.
o 80 bytes of general-purpose data memory (Scratch Pad Area).
 Special Function Registers (SFR) to configure/operate microcontroller.
 32 bit bi-directional I/O Lines (4 ports P0 to P3).
 Two16-bit timers/counters (T0 and T1).
 Full duplex UART (Universal Asynchronous Receiver/Transmitter).
 6-source/5-vector interrupts (2 external and 3 internal) with two priority levels.
 On-Chip oscillator and clock circuitry.

General Block Diagram of 8051Microcontroller Architecture

5
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]
STEPS TO CREATE AND COMPILE KeilµVision-5 PROJECT:

PART-A EXECUTION

1. Double Click on the Vision5 icon on the desktop.


2. Close any previous projects that were opened using–Project->Close.
3. Start Project – New Project, create a new folder in any drive [ ex: mc], give file name
[ex:mc1 and save and select the CPU from the device database (Database-AT89C51 as
per the board). On clicking ‘OK’, the following option is displayed. Choose ‘YES’.

4. Create a source file (using File->New), type in the assembly or C program and save
this(file name.asm/file name.c) and add this source file to the project by selecting add
Existing files to group source group1

Then select.asm file [ex: MC1.asm], Click on Add then Click on Close. Select Translate, Build,
Rebuild with zero error

PART-B EXECUTION

5. Repeat the steps 1 to 4 from the Part A execution for software experiments Set the Target options using
->Project – Options for Target opens the µVision5 Options for Target – Target1 configuration
dialog. Set the Xtal (Crystal frequency) as 11.0592 MHz, and select use on chip Rom (0x0-0xffff) also
the Options for Target – 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

6
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]
Interrupt option if the user application is not using on-chip UART, to stop program execution press
ok then ok.
7. Build the project; using Project ->Build Project. Vision translates all the user application and links.
Any errors in the code are indicated by– “Target not created” in theBuild window, along with the error
line. Debug the errors. After an error free, to build go to Debug mode.

8. Now user can enter into Debug mode with Debug- Start / Stop Debug session 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 till cursor).
10. If it is an interface program the outputs can be seen on the LCD, 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.

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

7
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]
EXECUTION STEPS using KEIL µvision:

8
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

9
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

10
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

11
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

12
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

13
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

MEMORY ORGANIZATION

Internal RAM organization

Register Banks: 00h to 1Fh. The 8051 uses 8 general-purpose registers R0 through R7 (R0, R1, R2, R3,
R4, R5, R6, and R7). There are four such register banks. Selection of register bank can be done through
RS1,RS0 bits of PSW. On reset, the default Register Bank 0 will be selected.

Bit Addressable RAM: 20h to 2Fh . The 8051 supports a special feature which allows access to bit
variables. This is where individual memory bits in Internal RAM can be set or cleared. In all there are
128 bits numbered 00h to 7Fh. Being bit variables any one variable can have a value 0 or 1. A bit variable
can be set with a command such as SETB and cleared with a command such as CLR. Example instructions
are:
SETB 25h ; sets the bit 25h (becomes 1) CLR
25h ; clears bit 25h (becomes 0)
Note, bit 25h is actually bit 5 of Internal RAM location 24h.
The Bit Addressable area of the RAM is just 16 bytes of Internal RAM located between 20h and 2Fh.

14
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]
General Purpose RAM: 30h to 7Fh. Even if 80 bytes of Internal RAM memory are available for
general-purpose data storage, user should take care while using the memory location from 00 -2Fh
since these locations are also the default register space, stack space, and bit addressable space. It is a good
practice to use general purpose memory from 30 – 7Fh. The general-purpose RAM can be accessed using
direct or indirect addressing modes.

15
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]
I. ASSEMBLY LANGUAGE PROGRAMMING

DATA TRANSFER PROGRAMS:

PROGRAM NO. 1
AIM: To write an Assembly Language Program to move a block of n bytes of data from source (20h) to
destination (40h) using Internal-RAM.

; Program to move a block of 5 bytes of data from source (20h) to destination (40h) using Internal-RAM.

org 0000 ; defining code location to start from location 0000


mov r0, #0x20 ; initializing r0 as a memory pointer to location X
mov r1, #0x40 ; initializing r1 as a memory pointer to location Y
mov r2, #0x05 ; number of elements in the array is defined as a count
back: mov a, @r0 ; reads a data from location X
mov @r1, a ; transfers data to location Y
inc r0 ; points to next location in X
inc r1 ; points to next location Y
djnz r2, back ; repeats data transfer untill all the data are transferred
exit: sjmp exit ; specifies logical end of the program
end ; specifies physical end of the program

16
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

RESULT

Number of bytes need to be transferred: 05

Before Execution:

20 10
21 20
22 30
23 40
24 50

After Execution:

40 10
41 20
42 30
43 40
44 50
OUTPUT:

After Execution:

17
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 2
AIM: To write an Assembly Language Program to move a block of n bytes of data from source (2000h) to
destination (2050h) using External RAM

ORG 0000h ; defining code location to start from location 0000


mov dptr, #2000h ; load dptr with source location address
mov r0, #50h ; r0 is loaded with destionation location lower byte
mov r1, #20h ; r1 is loaded with destination location upper byte
mov r2, #05h ; r2 is loaded with block size
back: movx a, @dptr ; acc is loaded with content of address present in dptr
push dph ; save dptr content into stack
push dpl
mov dph, r1 ; load dph with r1 destionation location upper byte
mov dpl, r0 ; load dph with ro destionation location lower byte
movx @dptr, a ; move acc content to destination location
inc dptr ; increment dest location
mov r1, dph ; store dph content in r1
mov r0, dpl ; store dph content in r1
pop dpl ; restore dpl content content of source location
pop dph ; restore dpl content content of source location
inc dptr ; increment source by 1
djnz r2, back ; decrement r2, if not zero jump to label 'back'
end

18
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

RESULT

Number of bytes need to be transferred: 05

Before Execution:

2000 22
2001 13
2002 45
2003 38
2004 11

After Execution:

2050 22
2051 13
2052 45
2053 38
2054 11

OUTPUT

After Execution:

19
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 3

AIM: Write an ALP To exchange the source block starting with address 20h, (Internal RAM) containing N
(05) bytes of data with destination block starting with address 40h (Internal RAM).

org 0000 ; defining code location to start from location 0000

mov r0, #0x20 ; initializing r0 as a memory pointer to location X

mov r1, #0x40 ; initializing r1 as a memory pointer to location Y

mov r2, #0x05 ; number of elements in the array is defined as a count

back: mov a, @r0 ; reads a data into accumulator from location X

mov r3, a ; copies data to a temporary register

mov a, @r1 ; reads a data to Acc from location Y

mov @r0, a ; transfers data to location X

mov a, r3 ; form temp register move data to Acc

mov @r1, a ; transfers data to location Y

inc r0 ; points to next location in X

inc r1 ; points to next location Y

djnz r2, back ; repeats data transfer until all the data are transferred

exit: sjmp exit ; specifies logical end of the program

end ; specifies physical end of the program

20
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

RESULT

Number of bytes need to be exchanged: 05

Before Execution:

20 10 40 60
21 20 41 70
22 30 42 80
23 40 43 90
24 50 44 00

After Execution:

20 60 40 10
21 70 41 20
22 80 42 30
23 90 43 40
24 00 44 50

21
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

OUTPUT:
Before Execution:

After Execution:

22
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 4

AIM: Write an ALP to exchange the source block starting with address 10h (Internal memory), containing n
(06) bytes of data with destination block starting at location 00h (External memory).

org 0000h ; defining code location to start from location 0000

mov r0, #10h ; load r0 with source location address

mov dptr, #2000h ; load dptr register to hold destination address

mov r2, #06h ; load r2 with block size

back: movx a, @dptr ; load acc the content address present dptr

mov b, @r0 ; load reg b with internal mem content

xch a,b ; exchange Acc and reg b contents

movx @dptr, a ; from acc, move the content to dptr address [ external]

mov @r0, b ; from reg b move the content to r0 address [internal]

inc r0 ; int mem location increment r0 by 1

inc dptr ; ext mem location incremented by 1

djnz r2, back ; decrement reg r2, if not zero jump to label 'back'

exit: sjmp exit ; specifies logical end of the program

end ; specifies physical end of the program

23
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

RESULT

Number of bytes need to be exchanged: 06

Before Execution:

10 10 2000 20
20 20 2001 21
30 30 2002 22
40 40 2003 23
50 50 2004 24
60 60 2005 25

After Execution:

2000 10 10 20
2001 20 20 21
2002 30 30 22
2003 40 40 23
2004 50 50 24
2005 60 60 25

24
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

OUTPUT:
Before Execution:

After Execution:

25
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

ARITHMETIC & LOGICAL OPERATION PROGRAMS:

PROGRAM NO. 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 Mode.

AIM: To 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 Mode.

org 0000 ; defining program to start from location 0000

mov r0, #0x34 ; reg r0 loaded with first data location 34h, r0=34h

mov r6, #00 ; initialise carry =0

mov a, @ro ; reads first number into Accumulator A

inc r0 ; increment re content by 1, r0 34h+1 = 35h

mov b, @ro ; reads second number into register B

add a,b ; performs the addition of 2 numbers A= A+B

jnc skip ; Jump to label skip if NO carry generated

inc r6 ; if addition results in carry, increment carry r6=r6+1

skip: mov r5, a ; stores the sum value into Register r5

exit: sjmp exit ; specifies logical end of the program

end ; specifies physical end of the program

26
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

RESULT

Data at 34h = 89h


Data at 35h = 96h
Addition of the above data: r5= 1f, r6=01

OUTPUT:

27
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 6: Write an ALP to subtract the bytes in Internal RAM 34h &35h store the result
in register R5 (LSB) & R6 (MSB).

AIM: To write an ALP to subtract the bytes in Internal RAM 34h &35h store the result in register R5 (LSB)
& R6 (MSB).

org 0000 ; defining program to start from location 0000

mov r0, #34h ; reg r0 loaded with first data location 34h, r0=34h

mov a, @r0 ; reads first number into Accumulator A

inc r0 ; increment r0 content by 1, r0 = 34h+1 = 35h

mov b, @r0 ; reads second number into register B

subb a,b ; performs the addition of 2 numbers A= A-B

mov r5, a

jnc exit ; Jump to label skip if NO carry generated

dec r6

exit: sjmp exit ; specifies logical end of the program

end ; specifies physical end of the program

28
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

RESULT

Data at 34h = 67h


Data at 35h = 80h
Subtraction of the above data: r5=e7 , r6=ff

OUTPUT:

29
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 7: Write an ALP to multiply two 8-bit numbers

AIM: To write an ALP to multiply two 8-bit numbers stored at 30h and 31h and store16- bit result in 32h and
33h of Internal RAM

org 0000

mov r0, #30h ; load first number address in r0, r0=30h

mov a, @r0 ; move content of 30h into accumulator A

inc r0 ; increment r0 by 1 r0=30h+1= 31h

mov b, @r0 ; move content of 31h into register B

mul ab ; multiply content oa A with content of b register.

inc r0 ; increment r0 by 1 R0 = 31h+1 = 32h

mov @r0, a ; move lower byte [8bit] of product into 32h location

inc r0 ; increment rø by 1. R0=32h+1 = 33h

mov @r0, b ; move upper byte of product into 33h location

end

30
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

RESULT

Data at 30h = 24h


Data at 31h = 67h
Multiplication of two 8-bit nos.: 32h= 7C and 33h=0E

OUTPUT:
Before Execution:

After Execution:

31
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 8: Write an ALP to perform division operation on 8-bit number by 8-bit number.

AIM: To write an ALP to perform division operation on 8-bit number by 8-bit number.

org 0000

mov r0, #30h ; dividend is in 30h location

mov a, @r0 ; move dividend to accumulator 'a'

inc r0 ; increment r0 by 1. R0 is now 31h

mov b, @r0 ; load content of 31h into reg breg. divisor is loaded to b register.

div ab ; Reg 'a' content is divided by reg 'b' content.

inc r0 ; increment R0 by 1. R0=31h+1 = 32h

mov @r0, a ; move quotient into 32h location

inc r0 ; increment r0 by 1. R0= 32h+1= 33h

mov @r0, b ; move remainder into 33h location

end

32
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

RESULT

Data at 30h = 56h


Data at 31h = 04h
Division of two 8-bit nos.: 32h= 15 and 33h=02 (remainder)

OUTPUT:
Before Execution:

After Execution:

33
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 9 : Write an ALP to separate positive and negative in a given array.

A. To separate negative numbers in a given array.

AIM: To write an ALP to separate negative numbers in a given array.

ORG 0000 ; Start address of the program


mov r0, #30h ; Initialize R0 with the starting address of the source data
mov r1, #50h ; Initialize R1 with the starting address of the destination data
mov r2, #05 ; Initialize R2 with the number of bytes to copy

back: mov a, @r0 ; Move the byte from the source address pointed to by R0 into the accumulator
jnb acc.7, skip ; Jump to 'skip' if the MSB (most significant bit) of the accumulator is not set
mov @r1, a ; Copy the byte from the accumulator to the destination address pointed to by R1
inc r1 ; Increment the destination pointer
skip: inc r0 ; Increment the source pointer

djnz r2, back ; Decrement R2 (loop counter) and jump back to 'back' if it's not zero
sjmp $ ; Infinite loop (halt the program)
end ; End of program

OUTPUT:

Before Execution:

After Execution:

34
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

B. To separate positive numbers in a given array.

AIM: To write an assembly language program to separate positive numbers in a given array.

ORG 0000 ; Start address of the program


mov r0, #30h ; Initialize R0 with the starting address of the source data
mov r1, #50h ; Initialize R1 with the starting address of the destination data
mov r2, #05 ; Initialize R2 with the number of bytes to copy

back: mov a, @r0 ; Move the byte from the source address pointed to by R0 into the accumulator
jb acc.7, skip ; Jump to 'skip' if the MSB (most significant bit) of the accumulator is set
mov @r1, a ; Copy the byte from the accumulator to the destination address pointed to by R1
inc r1 ; Increment the destination pointer
skip: inc r0 ; Increment the source pointer

djnz r2, back ; Decrement R2 (loop counter) and jump back to 'back' if it's not zero
sjmp $ ; Infinite loop (halt the program)
end ; End of program

OUTPUT:

Before Execution:

After Execution:

35
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 10: Write an ALP to separate even or odd elements in a given array.
A: AIM: To write an assembly language program to separate even elements in a given array.
ORG 0000 ; Start address of the program
mov r0, #30h ; Initialize R0 with the starting address of the source data
mov r1, #50h ; Initialize R1 with the starting address of the destination data
mov r2, #05 ; Initialize R2 with the number of bytes to copy

back: mov a, @r0 ; Move the byte from the source address pointed to by R0 into the accumulator
jb acc.0, skip ; Jump to 'skip' if the least significant bit (LSB) of the accumulator is set
mov @r1, a ; Copy the byte from the accumulator to the destination address pointed to by R1
inc r1 ; Increment the destination pointer
skip: inc r0 ; Increment the source pointer

djnz r2, back ; Decrement R2 (loop counter) and jump back to 'back' if it's not zero
sjmp $ ; Infinite loop (halt the program)
end ; End of program

OUTPUT:

Before Execution:

After Execution:

36
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

B: AIM: To write an assembly language program to separate odd elements in a given array.
ORG 0000 ; Start address of the program
mov r0, #30h ; Initialize R0 with the starting address of the source data
mov r1, #50h ; Initialize R1 with the starting address of the destination data
mov r2, #05 ; Initialize R2 with the number of bytes to copy
back: mov a, @r0 ; Move the byte from the source address pointed to by R0 into the accumulator
jnb acc.0, skip ; Jump to 'skip' if the least significant bit (LSB) of the accumulator is set
mov @r1, a ; Copy the byte from the accumulator to the destination address pointed to by R1
inc r1 ; Increment the destination pointer
skip: inc r0 ; Increment the source pointer
djnz r2, back ; Decrement R2 (loop counter) and jump back to 'back' if it's not zero
sjmp $ ; Infinite loop (halt the program)
end ; End of program

37
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 11: Write an ALP to arrange the numbers in Ascending & Descending order.
AIM: To write an ALP to arrange the numbers in Ascending & Descending order.

ORG 0000H ; Start address


MOV R7,#04H ; Initialize R7 with the number of iterations
MAIN: MOV R0,#30H ; Load R0 with the starting memory location of the array
MOV R6,#04H ; Initialize R6 with the number of elements in the array
UP: MOV A,@R0 ; Load A with the current element of the array
INC R0 ; Move to the next element
MOV B,@R0 ; Load B with the next element of the array
CJNE A,B,NEXT ; Compare A and B, jump to NEXT if they are not equal
NEXT: JC NOEXCHANGE ; Jump to NOEXCHANGE if A > B
MOV @R0,A ; Exchange the elements: move A to current memory location
DEC R0 ; Move back to the previous memory location
MOV @R0,B ; Move B to the previous memory location
INC R0 ; Move back to the current memory location
NOEXCHANGE: DJNZ R6,UP ; Decrement R6 and repeat the inner loop if not zero
DJNZ R7,MAIN ; Decrement R7 and repeat the main loop if not zero
SJMP $ ; End of program
END ; End of program
OUTPUT

Before Execution:

After Execution:

NOTE: For descending use JNC instead of JC.

38
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 12: Write an ALP to find Largest & Smallest number from a given array
AIM: To Write an ALP to find Largest & Smallest number from a given array starting from 20h &
store it in Internal Memory location 40h.

org 0000h ; Set the origin of the program to address 0000h

mov r0, #20h ; Initialize register r0 with the value 20h


mov r1, #5h ; Initialize register r1 with the value 5h
mov b, #00h ; Initialize register b with the value 00h
back: mov a, @r0 ; Move the value at the memory address stored into register a

cjne a,b,next ; Compare the value in register a with the value in register b.
If not equal, jump to the next label, otherwise continue execution.
next: jc loop1 ; Jump to loop1 if the carry flag is set
mov b, a ; Move the value in register a into register b
mov 40h, a ; Move the value in register a to the memory address 40h
loop1: inc r0 ; Increment the value in register r0
djnz r1, back ; Decrement the value in register r1 and jump back to 'back' label
if r1 is not zero, otherwise continue execution.
sjmp $ ; Infinite loop - Jump to the current location
end ; End of the program

OUTPUT
Before Execution:

After Execution

Note: For the smallest number change mov b,#00h to mov b,#0ffh
and change jc loop1to jnc loop1.

39
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

COUNTER OPERATION PROGRAMS:

PROGRAM NO. 13: Write an assembly language program for Decimal UP-Counter

AIM: To write an assembly language program for Decimal UP-Counter

ORG 000H ; Start of program at memory address 000H


MOV A, #00H ; Move immediate value 00H to accumulator A
BACK: MOV P0, A ; Move the value in accumulator A to port 0
ACALL DELAY ; Call the delay subroutine
ADD A, #01H ; Add immediate value 01H to accumulator A
DA A ; Decimal adjust accumulator (convert result to BCD)
SJMP BACK ; Jump to the BACK label, creating an infinite loop
DELAY: MOV R1, #05FH ; Move immediate value 05FH to register R1
L1: MOV R2, #0FFH ; Move immediate value 0FFH to register R2
L2: MOV R3, #0FFH ; Move immediate value 0FFH to register R3
L3: DJNZ R3, L3 ; Decrement R3 and jump to L3 if not zero
DJNZ R2, L2 ; Decrement R2 and jump to L2 if not zero
DJNZ R1, L1 ; Decrement R1 and jump to L1 if not zero
RET ; Return from subroutine
END ; End of program

OUTPUT:

40
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 14: Write an assembly language program for Decimal DOWN-Counter.

AIM: To write an assembly language program for Decimal DOWN-Counter.


ORG 000H ; Start of program at memory address 000H
MOV A, #99H ; Initialize accumulator A with value 99H
BACK: MOV P0, A ; Move the value in accumulator A to port 0
ACALL DELAY ; Call the delay subroutine to introduce a delay
ADD A, #99H ; Add immediate value 99H to accumulator A
DA A ; Decimal adjust accumulator A (convert result to Binary Coded Decimal (BCD))
SJMP BACK ; Unconditional jump back to the BACK label, creating an infinite loop
DELAY: MOV R1, #05FH ; Initialize R1 with a delay count
L1: MOV R2, #0FFH ; Initialize R2 with a delay count
L2: MOV R3, #0FFH ; Initialize R3 with a delay count
L3: DJNZ R3, L3 ; Decrement R3 and jump to L3 if R3 is not zero
DJNZ R2, L2 ; Decrement R2 and jump to L2 if R2 is not zero
DJNZ R1, L1 ; Decrement R1 and jump to L1 if R1 is not zero
RET ; Return from the delay subroutine
END ; End of program

OUTPUT:

41
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 15: Write an assembly language program for Hexadecimal UP-Counter.

AIM: To write an assembly language program for Hexadecimal UP-Counter.

ORG 000H ; Start of program at memory address 000H


MOV A, #00H ; Move immediate value 00H to accumulator A
BACK: MOV P0, A ; Move the value in accumulator A to port 0
ACALL DELAY ; Call the delay subroutine
ADD A, #01H ; Add immediate value 01H to accumulator A
SJMP BACK ; Jump to the BACK label, creating an infinite loop
DELAY: MOV R1, #09FH ; Move immediate value 09FH to register R1
L1: MOV R2, #0FFH ; Move immediate value 0FFH to register R2
L2: MOV R3, #0FFH ; Move immediate value 0FFH to register R3
L3: DJNZ R3, L3 ; Decrement R3 and jump to L3 if not zero
DJNZ R2, L2 ; Decrement R2 and jump to L2 if not zero
DJNZ R1, L1 ; Decrement R1 and jump to L1 if not zero
RET ; Return from subroutine
END ; End of program

OUTPUT:

42
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 16: Write an assembly language program for Hexadecimal DOWN-Counter.
AIM: To write an assembly language program for Hexadecimal DOWN-Counter.

ORG 000H ; Start of program at memory address 000H


MOV A, #0xFF ; Initialize accumulator A with value FFH
BACK: MOV P0, A ; Move the value in accumulator A to port 0
ACALL DELAY ; Call the delay subroutine to introduce a delay
ADD A, #0FFH ; ADD immediate value 0FFH to accumulator A
SJMP BACK ; Unconditional jump back to the BACK label, creating an infinite loop
DELAY: MOV R1, #09FH ; Initialize R1 with a delay count
L1: MOV R2, #0FFH ; Initialize R2 with a delay count
L2: MOV R3, #0FFH ; Initialize R3 with a delay count
L3: DJNZ R3, L3 ; Decrement R3 and jump to L3 if R3 is not zero
DJNZ R2, L2 ; Decrement R2 and jump to L2 if R2 is not zero
DJNZ R1, L1 ; Decrement R1 and jump to L1 if R1 is not zero
RET ; Return from the delay subroutine
END ; End of program

OUTPUT:

43
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

II. C Programming

PROGRAM NO. 1: Write an 8051 C program to find the sum of first 10 integer numbers.
AIM: To write an 8051 C program to find the sum of first 10 integer numbers.

#include <reg51.h> // Include header file for 8051 microcontroller

void main ()
{
unsigned char sum = 0; // Initialize variable 'sum' to store the sum
unsigned char i; // Declare loop counter variable 'i'

// Loop to calculate sum of numbers from 1 to 10

for (i = 1; i <= 10; i++) // Loop runs from 1 to 10


{
sum = sum + i; // Increment 'sum' by 1 in each iteration
}

ACC = sum; // Assign the sum value to the Accumulator register (ACC)
P0 = sum; // Assign the sum value to Port 0 (P0)
}

OUTPUT:

44
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 2: Write an 8051 C program to find Factorial of given number.

AIM: To write an 8051 C program to find Factorial of given number.

#include<reg51.h> // Including header file for 8051 microcontroller


#include<stdio.h> // Including standard input/output header file
void main ()
{
unsigned int i ; // Declaring an unsigned integer variable 'i' for loop iteration
unsigned char num = 5 ; // Initializing unsigned char variable 'num' with value unsigned long
factorial = 1; // Initializing unsigned long variable 'factorial' with value 1 to store factorial
// Loop to calculate factorial of 'num'
for (i = 1; i <= num; i++) // Loop runs from 1 to 'num'
{
factorial = factorial * i; // Multiply 'factorial' by 'i' and store the result in 'factorial'
}
P1 = factorial;
}

OUTPUT:

45
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

46
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 3: Write an 8051 C program to find the Square of a number (1 to 10) using look
up table.

AIM: To write an 8051 C program to find Square of a number (1 to 10) using look up table.

#include <reg51.h> // Include the header file for 8051 microcontroller


void main()
{
unsigned char LUT[] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100};// Look-up table for squares of numbers
unsigned char num, square; // Variables to hold current number and its square
for(num = 1; num < 11; num++) // Loop through numbers 1 to 10
{
square = LUT[num - 1]; // Retrieve the square value from the look-up table
P0 = square; // Output the square value to Port 0 pins
}
while(1); // Infinite loop to keep the program running
}

OUTPUT:

47
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

48
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 4: Write an 8051 C program to count the number of ones and zeros on two
consecutive memory locations.

AIM: To write an 8051 C program to to count the number of ones and zeros on two consecutive memory
locations.

#include <reg51.H> // Include header file for 8051 microcontroller


void main()
{
unsigned char array[] = {0x57, 0xfc}; // Initialize array with hexadecimal values
unsigned char i, ones, zeros; // Declare variables for loop and counting
CY = 0; // Clear the carry flag
// Loop through the bits of the first byte in the array
for (i = 0; i < 8; i++)
{
array[0] >>= 1; // Right shift the bits in the first byte
if (CY == 1) // If carry flag is set
ones++; // Increment count of ones
else
zeros++; // Increment count of zeros
}
// Loop through the bits of the second byte in the array
for (i = 0; i < 8; i++)
{
array[1] >>= 1; // Right shift the bits in the second byte
if (CY == 1) // If carry flag is set
ones++; // Increment count of ones
else
zeros++; // Increment count of zeros
}
P0=zeros;
P1=ones;
while (1); // Infinite loop to prevent program from exiting
}

49
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

OUTPUT:

50
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

III. HARDWARE INTERFACING PROGRAMS


PROGRAM NO. 1: Write an 8051 C Program to rotate stepper motor in Clock & Anti-Clockwise
direction.

AIM: To write an 8051 C Program to rotate stepper motor in Clock & Anti-Clockwise direction.

BLOCK DIAGRAM

CIRCUIT DIAGRAM FOR STEPPER MOTOR DRIVER:

51
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM
ORG 00H
SJMP 30H
ORG 30H
MOV P0, #00H
MOV A, #11H
L1: MOV P0, A
ACALL DELAY
RR A
SJMP L1
DELAY: MOV R1, #0F0H
DECC : MOV R2,0FFH
B1: DJNZ R2, B1
DJNZ R1, DECC
RET
END

Steps to execute the programs

 Translate => Build =>Rebuild with zero errors


 Set the frequency by clicking on option for target
 Select target => Set Xtal (MHZ): 11.0952
 Select use on chip Rom (0x0-0xffff)
 In debug => select use kiel moniter 51 drive => go to setting =>
 select serial interrupt => Ok => on the power suppy
 Debug => start/stop debug => Ok
 Debub => Run => Stop
 For clock wise Direction => RRA
 For Anticlock wise Direction => RRA

52
Prepared By Mamatha Mallesh, Dept. of ECE, VKIT MICROCONTROLLERS [BEC456A]

PROGRAM NO. 2: Write an 8051 C program to Generate Sine & Square waveforms using DAC interface.
AIM: To write an 8051 C program to Generate Sine & Square waveforms using DAC interface.

53

You might also like