0% found this document useful (0 votes)
17 views48 pages

Stalin Eee MPMC Lab Siet 2024

Uploaded by

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

Stalin Eee MPMC Lab Siet 2024

Uploaded by

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

SIDDHARTHA INSTITUTE OF ENGINEERING & TECHNOLOGY.

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING

LAB MANUAL
ON
MICROPROCESSORS & MICROCONTROLLERS LAB

JNTUH III B.Tech IIIrd Yr SEMESTER-I

-----------------------------------------------------------------------------
-

SIDDHARTHA INSTITUTE OF ENGINEERING&TECHNOLOGY

(Accredited by NBA, Approved by AICTE & Affiliated to JNTUH)


Vinobha Nagar, Ibrahimpatnam, Ranga Reddy Dist–501506, Telangana, India
Ph: 08414-222299, 222599, Fax: 08414-222399
E-mail: info@siddhartha.ac.in www.siddhartha.ac.in
-----------------------------------------------------------------------------------------------

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

1
MICROPROCESSORS & MICROCONTROLLERS LAB

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

2
MICROPROCESSORS & MICROCONTROLLERS LAB

Cycle 1: Using 8086 Processor Kits and/or Assembler (5 Weeks)


 Assembly Language Programs to 8086 to Perform
1. Arithmetic, Logical, String Operations on 16 Bit and 32-Bit
Data.
2. Bit level Logical Operations, Rotate, Shift, Swap and Branch
Operations.

Cycle 2: Using 8051 Microcontroller Kit (6 weeks)


 Introduction to IDE
1. Assembly Language Programs to Perform Arithmetic (Both
Signed and Unsigned) 16 Bit Data Operations, Logical
Operations (Byte and Bit Level Operations), Rotate, Shift,
Swap and Branch Instructions
2. Time delay Generation Using Timers of 8051.
3. Serial Communication from / to 8051 to / from I/O devices.
4. Program Using Interrupts to Generate Square Wave 10 KHZ
Frequency on P2.1 Using Timer 0 8051 in 8 bit Auto reload
Mode and Connect a 1 HZ Pulse to INT1 pin and Display on
Port 0. Assume Crystal Frequency as 11.0592 MHZ

Cycle 3: Interfacing I/O Devices to 8051(5 Weeks)


1. 7 Segment Display to 8051.
2. Matrix Keypad to 8051.
3. Sequence Generator Using Serial Interface in 8051.
4. 8 bit ADC Interface to 8051.
5. Triangular Wave Generator through DAC interfaces to 8051.

TEXT BOOKS:
1. Advanced Microprocessors and Peripherals by A K Ray, Tata McGraw-
Hill Education, 2006
2. The 8051 Microcontrollers: Architecture, Programming &
Applications by Dr. K. Uma Rao, Andhe Pallavi, Pearson, 2009.

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

3
INTRODUCTION TO MASM
Programming of a microprocessor usually takes several iterations before the right
sequence of machine code instruction is written. The process, how machine code is
facilitated

c asinga special program called an “assembler”. The assembler allows the user to
write alphanumeric instructions (or) mnemonics called assembly language
instructions. An assembler takes the written assembly code and converts it into
machine code. Often it will come with a linker that links the assembled file and
produces an executable from it. Window executables have the extension. Here are
some of the popular ones

MASM:-

Microsoft’s macro assembler (MASM) is an integrated Software package


written by Microsoft co-operation assembler, a linker and a debugger the
programmer’s work bench combines these four path for professional software
developers. It consists of an editor, an into a user friendly programming
environment with built in online help.

FAMILIARITY WITH MASM:-

o Available since the beginning of the IBM compatible PCS


o Works in MS-DOS and windows environments.
o It’s free: Microsoft no longer sells MASM as a standalone product.
o Bundled with the Microsoft visual studio product
o Numerous tutorials, books and samples floating around, many are free or low
cost

TASM:- Another popular assembler made by Borland but it is still a commercial


product.

ASSEMBLING THE PROGRAM:-

The assembler is used to convert the assembly language instructions to machine


code. It is used immediately after writing the assembly language program. The
assembler starts by checking the syntax or validity of the structure of each
instruction in the source file. If any errors are found the assembler displays a
report on these errors along with brief explanation of their nature. However, if
the program does not contain any errors. The assembler produces an object file
that the same name as original file but with the “obj” extension

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT
4
LINKING THE PROGRAM:-

The linker is used to convert the object file to an executable file the
executable file is the final set of machine code instructions that can
directly be executed by microprocessor. It is difficult and different than the
object file on the sense that it is self contained and locatable. An object
file may represent on segment of long program. This program cannot operate by
itself and must be integrated with other object file representing the rest of
the program in order to produce the final self contained exactable file.

DEBUGGING THE PROGRAM:-

The debugger can also be used to find logical errors in the program. Even if a
program does not contain syntax errors. It may not produce the desired result
after execution. Logical errors may be found by tracing the action of the
program. Once found the source file called “”Debugger” is designed for that
purpose.

The debugger allows the user to trace the action of the program by single
stepping through the program (or) executing the program up to a desired point
called breakpoint. It allows the user to inspect (or) change the contents of MP
internal register (or) the contents of any memory location.

EXECUTING THE PROGRAM:-

The executable file contains the machine language code. It can be based on the
RAM and be executed by the micro processor. Simply by typing from the DOS
prompt. It the program produces an output on the screen or a sequence of
control a piece of on hardware, if the programming manipulates data in memory
nothing would seem to have happened as a result of executing the program.

THE DOS-DEBUGGER:-

THE DOS-Debug program is an example of a simple debugger that comes with MS


DOS. Hence it will available on any pc. It was initially designed to give the
user capability to trace logical errors in executable files. It allows the user
to take an existing executable file and unassembled it i.e., convert into
assembly language also it allows the user to write assembly language
instructions directly and then convert them to machine language.

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT
5
MS-PWB

The PWB allows the user to define a project that means it contains one or more
files then the user may select and save all the necessary assembly linking and
debugging option for that project the PWB allows the leaving the PWB
environment it also allows the user to get help on any keyword by pointing to
the keyboard and pressing the F1 key.

COMMAND SYNTAX
Assemble A[address]
compare C[range address]
dump D[large]
enter E address[list]
fill F range list
go G[address][address]
hex H value 1 value 2
input I port
load L[address][drive][first
sector][number]
move M range address
name N[path name][argument list]
output O port byte
process P[address][number]
quit Q
register R[register]
search S range list
trace T[address][value]
Un assemble U[range]
write W[address][drive][first
sector][number]

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT
6
INTRODUCTION OF MICROPROCESSOR

INTRODUCTION:

Microprocessor is regarded as one of the most important devices in our everyday


machines called computers. Microprocessor is an electronic circuit that
functions as the CPU of a computer, providing computational control.
Microprocessor are also used in other advanced electronic systems, such as
computer printers, automobiles and jet airlines

The first Microprocessor was the Intel 4004, produced in 1971. Originally
developed for a calculator and revolutionary for its time, it contained 2300
transistors on a 4-bit microprocessor that could perform only 60,000 operations
per second. The first 8-bit microprocessor was the Intel 8008, developed in
1972 to run computer terminals.

INTEL MICROPROCESSOR:

8086(1979):

Created as a cheaper version of Intel’s 8086. The 8088 was a 16 bit an 8bit
processor with external bus-bit processor with an 8-bit external bus

80286(1982):

With 16MB of addressable memory and 1GB of a virtual memory, this 16-bit is
referred to as the first “modern” microprocessor. It contained 130,000
transistors and chip packed serious compute power 12MHz

80386(1985):

The price/performance curve continued its steep climb with the 386 and later
the 486- 32 bit processors that brought real computing to the masses. The 386,
featured 275,000 transistors, the 486 had more than a million

Pentium (1993):

Adding systems-level characteristics to enormous raw compute power, the


Pentium supports demanding I/O, graphics and communications intensive
applications with more than 3 million transistors

Pentium Pro (1995):

He nearest Pentium has dynamic instruction execution and the chip package is
more than 5.5 million transistors

Pentium II (1997):

The 7.5 million transistors Pentium II processor incorporates Intel MMX-TM


technology, which is designed specially to process video, audio and graphics
data efficiently
Prepared by: M.STALIN BABU
ASSISTANT PROFESSOR
ECE DEPARTMENT

7
Pentium III (1999):

The Pentium III processor features 70 new instructions. It designed to


significantly enhance Internet experience, allowing users to do such things as
browse through realistic online museums and stores and download high-quality
video. The processor incorporates9.5 million Transistors, and was introduced
using 0.25 micron technology.

Pentium-IV (2000):

The Pentium- 4 is fabricated in Intel’s 0.18 micron CMOS process. Its die
size is 217 , power consumption is 50W. The Pentium-4 is available in 1.4GHz &
1.5Hz bins

Buses and operation:

8086 has all internal registers are as well as internal and external data
buses, were 16 bits wide, finally estimating the “16 bit micro processor”
identity of the 8086.external address bus gave a 1MB physical address space
(220=1,048,576). This address space was addressed by means of internal
“segmentation”. The data was multiplexed with A 20 bit

the address bus in order to fit a standard 40-pin dual in line package. 16 bit
I/O address meant 64KB of separate I/O space (216=65,536). The maximum linear
address space was limited to 64KB.Simply because internal registers were only
16 bits wide, programming over 64KB, boundaries involved adjusting segment
register.

Some of the control pins which carry essential signals for all external
operations had more than one function depending upon whether device operated in
min or ‘max’mode. The former was intended for small single processor systems
while latter was for medium and large systems, using more than one processor.

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

8
CYCLE:1. EXP:1 8086 ASSEMBLY PROGRAM TO ADD TWO 16 BIT NUMBERS

1. data segment
2. a dw 0202h
3. b dw 0408h
4. c dw ?
5. data ends
6. code segment
7. assume cs:code,ds:data
8. start:
9. mov ax,data
10. mov ds,ax
11. mov ax,a
12. mov bx,b
13. add ax,bx
14. mov c,ax
15. int 3
16. code ends
17. end start

Output:
C:\TASM>masm an16add.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.

Object filename [an16add.OBJ]:


Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:

50402 + 450254 Bytes symbol space free

0 Warning Errors
0 Severe Errors

C:\TASM>link an16add.obj

Microsoft (R) Overlay Linker Version 3.60


Copyright (C) Microsoft Corp 1983-1987. All rights reserved.

Run File [AN16ADD.EXE]:


List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

9
C:\TASM>debug an16add.exe
-g
AX=060A BX=0408 CX=0022 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=0B97 ES=0B87 SS=0B97 CS=0B98 IP=0011 NV UP EI PL NZ NA PE NC
0B98:0011 CC INT 3
-d 0B97:0000
0B97:0000 02 02 08 04 0A 06 00 00-00 00 00 00 00 00 00 00 ................
0B97:0010 B8 97 0B 8E D8 A1 00 00-8B 1E 02 00 03 C3 A3 04 ................
0B97:0020 00 CC 86 72 FF 77 15 8A-86 70 FF 2A E4 50 B8 FD ...r.w...p.*.P..
0B97:0030 05 50 FF 36 24 21 E8 77-63 83 C4 06 FF 36 24 21 .P.6$!.wc....6$!
0B97:0040 B8 0A 00 50 E8 47 5E 83-C4 04 5E 8B E5 5D C3 90 ...P.G^...^..]..
0B97:0050 55 8B EC 81 EC 84 00 C4-5E 04 26 80 7F 0A 00 74 U.......^.&....t
0B97:0060 3E 8B 46 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC >.F..V..F..V..^.
0B97:0070 26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8 &.G.*.@P.....RP.
-q

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

10
EXP: 2. 8086 ASSEMBLY PROGRAM TO MULTIPLY TWO 16 BIT NUMBERS

1. data segment
2. a dw 1234h
3. b dw 5678h
4. c dd ?
5. data ends

6. code segment
7. assume ds:data, cs:code
8. start:
9. mov ax,data
10. mov ds,ax
11. mov ax,a
12. mov bx,b
13. mul bx
14. mov word ptr c,ax
15. mov word ptr c+2,dx
16. int 3
17. code ends
18. end start

Output:

C:\TASM>masm an16mul.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.

Object filename [an16mul.OBJ]:


Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:

50326 + 450330 Bytes symbol space free

0 Warning Errors
0 Severe Errors

C:\TASM>link an16mul.obj

Microsoft (R) Overlay Linker Version 3.60


Copyright (C) Microsoft Corp 1983-1987. All rights reserved.

Run File [AN16MUL.EXE]:


List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug an16mul.exe
-g

AX=0060 BX=5678 CX=0026 DX=0626 SP=0000 BP=0000 SI=0000 DI=0000


DS=0B97 ES=0B87 SS=0B97 CS=0B98 IP=0015 OV UP EI PL NZ NA PO CY
0B98:0015 CC INT 3
-d 0B97:0000
0B97:0000 34 12 78 56 60 00 26 06-00 00 00 00 00 00 00 00 4.xV`.&.........
Prepared by: M.STALIN BABU
ASSISTANT PROFESSOR
ECE DEPARTMENT

11
0B97:0010 B8 97 0B 8E D8 A1 00 00-8B 1E 02 00 F7 E3 A3 04 ................
0B97:0020 00 89 16 06 00 CC 15 8A-86 70 FF 2A E4 50 B8 FD .........p.*.P..
0B97:0030 05 50 FF 36 24 21 E8 77-63 83 C4 06 FF 36 24 21 .P.6$!.wc....6$!
0B97:0040 B8 0A 00 50 E8 47 5E 83-C4 04 5E 8B E5 5D C3 90 ...P.G^...^..]..
0B97:0050 55 8B EC 81 EC 84 00 C4-5E 04 26 80 7F 0A 00 74 U.......^.&....t
0B97:0060 3E 8B 46 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC >.F..V..F..V..^.
0B97:0070 26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8 &.G.*.@P.....RP.
-q

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

12
EXP: 3. 8086 ASSEMBLY PROGRAM TO SUBTRACT TWO 16 BIT NUMBERS

1. data segment
2. a dw 9A88h
3. b dw 8765h
4. c dw ?
5. data ends

6. code segment
7. assume cs:code,ds:data
8. start:
9. mov ax,data
10. mov ds,ax
11. mov ax,a
12. mov bx,b
13. sub ax,bx
14. mov c,ax
15. int 3
16. code ends
17. end start

output:

C:\TASM>masm an16sub.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.

Object filename [an16sub.OBJ]:


Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:

50402 + 450254 Bytes symbol space free

0 Warning Errors
0 Severe Errors

C:\TASM>link an16sub.obj

Microsoft (R) Overlay Linker Version 3.60


Copyright (C) Microsoft Corp 1983-1987. All rights reserved.

Run File [AN16SUB.EXE]:


List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug an16sub.exe
-g

AX=1323 BX=8765 CX=0022 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000


DS=0B97 ES=0B87 SS=0B97 CS=0B98 IP=0011 NV UP EI PL NZ NA PO NC
0B98:0011 CC INT 3
-d 0B97:0000
Prepared by: M.STALIN BABU
ASSISTANT PROFESSOR
ECE DEPARTMENT

13
0B97:0000 88 9A 65 87 23 13 00 00-00 00 00 00 00 00 00 00 ..e.#...........
0B97:0010 B8 97 0B 8E D8 A1 00 00-8B 1E 02 00 2B C3 A3 04 ............+...
0B97:0020 00 CC 86 72 FF 77 15 8A-86 70 FF 2A E4 50 B8 FD ...r.w...p.*.P..
0B97:0030 05 50 FF 36 24 21 E8 77-63 83 C4 06 FF 36 24 21 .P.6$!.wc....6$!
0B97:0040 B8 0A 00 50 E8 47 5E 83-C4 04 5E 8B E5 5D C3 90 ...P.G^...^..]..
0B97:0050 55 8B EC 81 EC 84 00 C4-5E 04 26 80 7F 0A 00 74 U.......^.&....t
0B97:0060 3E 8B 46 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC >.F..V..F..V..^.
0B97:0070 26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8 &.G.*.@P.....RP.
-q

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

14
EXP:4. 8086 ASSEMBLY PROGRAM TO DIVIDE TWO 16 BIT NUMBERS

1. data segment
2. a dw 4444h
3. b dw 0002h
4. c dw ?
5. data ends

6. code segment
7. assume ds:data, cs:code
8. start:
9. mov ax,data
10. mov ds,ax
11. mov ax,a
12. mov bx,b
13. div bx
14. mov c,ax
15. int 3
16. code ends
17. end start

Output:

C:\TASM>masm an16div.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.

Object filename [an16div.OBJ]:


Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:

50402 + 450254 Bytes symbol space free

0 Warning Errors
0 Severe Errors

C:\TASM>link an16div.obj

Microsoft (R) Overlay Linker Version 3.60


Copyright (C) Microsoft Corp 1983-1987. All rights reserved.

Run File [AN16DIV.EXE]:


List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug an16div.exe
-g

AX=2222 BX=0002 CX=0022 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000


DS=0B97 ES=0B87 SS=0B97 CS=0B98 IP=0011 NV UP EI PL NZ NA PO NC
0B98:0011 CC INT 3
-d 0B97:0000
0B97:0000 44 44 02 00 22 22 00 00-00 00 00 00 00 00 00 00 DD..""..........
0B97:0010 B8 97 0B 8E D8 A1 00 00-8B 1E 02 00 F7 F3 A3 04 ................
0B97:0020 00 CC 86 72 FF 77 15 8A-86 70 FF 2A E4 50 B8 FD ...r.w...p.*.P..
Prepared by: M.STALIN BABU
ASSISTANT PROFESSOR
ECE DEPARTMENT

15
0B97:0030 05 50 FF 36 24 21 E8 77-63 83 C4 06 FF 36 24 21 .P.6$!.wc....6$!
0B97:0040 B8 0A 00 50 E8 47 5E 83-C4 04 5E 8B E5 5D C3 90 ...P.G^...^..]..
0B97:0050 55 8B EC 81 EC 84 00 C4-5E 04 26 80 7F 0A 00 74 U.......^.&....t
0B97:0060 3E 8B 46 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC >.F..V..F..V..^.
0B97:0070 26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8 &.G.*.@P.....RP.
-q

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

16
EXP:1.8086 ASSEMBLY PROGRAM TO ADD TWO 32 BIT NUMBERS

1. data segment
2. abc dd 12345678h
3. def dd 9ABCDEF0h
4. ghi dw ?
5. data ends

6. code segment
7. assume cs:code, ds:data
8. start:
9. mov ax,data
10. mov ds,ax
11. mov dl,00h
12. mov ax, word ptr abc
13. mov bx, word ptr def
14. add ax,bx
15. mov word ptr ghi,ax
16. mov ax, word ptr abc+2
17. mov bx, word ptr def+2
18. adc ax,bx
19. mov word ptr ghi+2,ax
20. jnc move
21. inc dl
22. move: mov byte ptr ghi+4,dl
23. int 3
24. code ends
25. end start

Output:

C:\TASM>masm an32add.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.

Object filename [an32add.OBJ]:


Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:

50288 + 450368 Bytes symbol space free

0 Warning Errors
0 Severe Errors

C:\TASM>link an32add.obj

Microsoft (R) Overlay Linker Version 3.60


Copyright (C) Microsoft Corp 1983-1987. All rights reserved.

Run File [AN32ADD.EXE]:


List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug an32add.exe

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

17
-g

AX=ACF1 BX=9ABC CX=0038 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000


DS=0B97 ES=0B87 SS=0B97 CS=0B98 IP=0027 NV UP EI NG NZ AC PO NC
0B98:0027 CC INT 3
-d 0B97:0000
0B97:0000 78 56 34 12 F0 DE BC 9A-68 35 F1 AC 00 00 00 00 xV4.....h5......
0B97:0010 B8 97 0B 8E D8 B2 00 A1-00 00 8B 1E 04 00 03 C3 ................
0B97:0020 A3 08 00 A1 02 00 8B 1E-06 00 13 C3 A3 0A 00 73 ...............s
0B97:0030 02 FE C2 88 16 0C 00 CC-63 83 C4 06 FF 36 24 21 ........c....6$!
0B97:0040 B8 0A 00 50 E8 47 5E 83-C4 04 5E 8B E5 5D C3 90 ...P.G^...^..]..
0B97:0050 55 8B EC 81 EC 84 00 C4-5E 04 26 80 7F 0A 00 74 U.......^.&....t
0B97:0060 3E 8B 46 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC >.F..V..F..V..^.
0B97:0070 26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8 &.G.*.@P.....RP.
-q

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

18
EXP:2.8086 ASSEMBLY PROGRAM FOR SUBTRACTION OF TWO 32 BIT NUMBERS

1. data segment
2. abc dd 9ABCDEF0h
3. def dd 12345678h
4. ghi dw ?
5. data ends

6. code segment
7. assume cs:code, ds:data
8. start:
9. mov ax,data
10. mov ds,ax
11. mov dl,00h
12. mov ax, word ptr abc
13. mov bx, word ptr def
14. sub ax,bx
15. mov word ptr ghi,ax
16. mov ax, word ptr abc+2
17. mov bx, word ptr def+2
18. sbb ax,bx
19. mov word ptr ghi+2,ax
20. jnc move
21. inc dl
22. move: mov byte ptr ghi+4,dl
23. int 3
24. code ends
25. end start

Output:

C:\TASM>masm an32sub.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.

Object filename [an32sub.OBJ]:


Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:

50288 + 450368 Bytes symbol space free

0 Warning Errors
0 Severe Errors

C:\TASM>link an32sub.obj

Microsoft (R) Overlay Linker Version 3.60


Copyright (C) Microsoft Corp 1983-1987. All rights reserved.

Run File [AN32SUB.EXE]:


List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug an32sub.exe

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

19
-g

AX=8888 BX=1234 CX=0038 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000


DS=0B97 ES=0B87 SS=0B97 CS=0B98 IP=0027 NV UP EI NG NZ NA PE NC
0B98:0027 CC INT 3
-d 0B97:0000
0B97:0000 F0 DE BC 9A 78 56 34 12-78 88 88 88 00 00 00 00 ....xV4.x.......
0B97:0010 B8 97 0B 8E D8 B2 00 A1-00 00 8B 1E 04 00 2B C3 ..............+.
0B97:0020 A3 08 00 A1 02 00 8B 1E-06 00 1B C3 A3 0A 00 73 ...............s
0B97:0030 02 FE C2 88 16 0C 00 CC-63 83 C4 06 FF 36 24 21 ........c....6$!
0B97:0040 B8 0A 00 50 E8 47 5E 83-C4 04 5E 8B E5 5D C3 90 ...P.G^...^..]..
0B97:0050 55 8B EC 81 EC 84 00 C4-5E 04 26 80 7F 0A 00 74 U.......^.&....t
0B97:0060 3E 8B 46 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC >.F..V..F..V..^.
0B97:0070 26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8 &.G.*.@P.....RP.
-q

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

20
EXP:3. 8086 ASSEMBLY PROGRAM TO MULTIPLY TWO 32 BIT NUMBERS

1. data segment
2. abc dd 12345678H
3. def dd 12345678H
4. ghi dq ?
5. data ends

6. code segment
7. assume cs:code, ds:data
8. start:
9. mov ax, data
10. mov ds, ax
11. mov ax, word ptr abc
12. mul word ptr def
13. mov word ptr ghi, ax
14. mov cx, dx
15. mov ax, word ptr abc+2
16. mul word ptr def
17. add cx, ax
18. mov bx, dx
19. jnc move
20. add bx,0001H

21. move: mov ax,word ptr abc


22. mul word ptr def+2
23. add cx, ax
24. mov word ptr ghi+2, cx
25. mov cx,dx

26. jnc ma
27. add bx, 0001H
28. ma: mov ax, word ptr abc+2
29. mul word ptr def+2
30. add cx, ax

31. jnc mb
32. add dx, 0001H
33. mb: add cx, bx
34. mov word ptr ghi+4, cx

35. jnc mc
36. add dx, 0001H
37. mc: mov word ptr ghi+6, dx
38. int 3
39. code ends
40. end start

Output:
C:\TASM>masm an_32mul.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.

Object filename [an_32mul.OBJ]:


Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:
Prepared by: M.STALIN BABU
ASSISTANT PROFESSOR
ECE DEPARTMENT

21
50278 + 450378 Bytes symbol space free

0 Warning Errors
0 Severe Errors

C:\TASM>link an_32mul.obj

Microsoft (R) Overlay Linker Version 3.60


Copyright (C) Microsoft Corp 1983-1987. All rights reserved.

Run File [AN_32MUL.EXE]:


List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug an_32mul.exe
-g

AX=5A90 BX=0626 CX=66DC DX=014B SP=0000 BP=0000 SI=0000 DI=0000


DS=0B97 ES=0B87 SS=0B97 CS=0B98 IP=0052 NV UP EI PL NZ NA PO NC
0B98:0052 CC INT 3
-d 0B97:0000
0B97:0000 78 56 34 12 78 56 34 12-40 D8 F4 1D DC 66 4B 01 xV4.xV4.@....fK.
0B97:0010 B8 97 0B 8E D8 A1 00 00-F7 26 04 00 A3 08 00 8B .........&......
0B97:0020 CA A1 02 00 F7 26 04 00-03 C8 8B DA 73 03 83 C3 .....&......s...
0B97:0030 01 A1 00 00 F7 26 06 00-03 C8 89 0E 0A 00 8B CA .....&..........
0B97:0040 73 03 83 C3 01 A1 02 00-F7 26 06 00 03 C8 73 03 s........&....s.
0B97:0050 83 C2 01 03 CB 89 0E 0C-00 73 03 83 C2 01 89 16 .........s......
0B97:0060 0E 00 CC 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC .....V..F..V..^.
0B97:0070 26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8 &.G.*.@P.....RP.
-q

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

22
EXP:4. 8086 ASSEMBLY PROGRAM TO DISPLAY STRING ‘HELLO’
1. data segment
2. msg1 db 'hello'
3. data ends

4. code segment
5. assume cs:code,ds:data
6. start:
7. mov ax,data
8. mov ds,ax
9. mov sp,0d000h
10. mov bx,offset msg1
11. mov cx,0005h
12. move:
13. mov ah,02h
14. mov dl,[bx]
15. int 21h
16. inc bx
17. dec cx
18. jnz move
19. mov ax,4c00h
20. int 21h
21. int 3
22. code ends
23. end start

Output:

C:\TASM>masm an_hello.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.

Object filename [an_hello.OBJ]:


Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:

50318 + 450338 Bytes symbol space free

0 Warning Errors
0 Severe Errors

C:\TASM>link an_hello.obj

Microsoft (R) Overlay Linker Version 3.60


Copyright (C) Microsoft Corp 1983-1987. All rights reserved.

Run File [AN_HELLO.EXE]:


List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug an_hello.exe
-g
hello
Program terminated normally
-q

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

23
EXP: 5. PERFORMING BLOCK TRANSFER USING ASSEMBLY LANGUAGE

1. DATA SEGMENT
2. STRING1 DB 01H,02H,03H,04H,05H
3. STRING2 DB 4 DUP(0)
4. DATA ENDS

5. CODE SEGMENT
6. ASSUME CS:CODE,DS:DATA
7. START: MOV AX,DATA
8. MOV DS,AX
9. MOV ES,AX
10. LEA SI,STRING1
11. LEA DI,STRING2
12. MOV CX,05H
13. CLD
14. REP MOVSB
15. INT 3
16. CODE ENDS
17. END START

Output:

C:\TASM>masm bt.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.

Object filename [bt.OBJ]:


Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:
bt.asm(1): warning A4001: Extra characters on line

50422 + 450234 Bytes symbol space free

1 Warning Errors
0 Severe Errors

C:\TASM>link bt.obj
Microsoft (R) Overlay Linker Version 3.60
Copyright (C) Microsoft Corp 1983-1987. All rights reserved.

Run File [BT.EXE]:


List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug bt.exe
-g

AX=0B97 BX=0000 CX=0000 DX=0000 SP=0000 BP=0000 SI=0005 DI=000A


DS=0B97 ES=0B97 SS=0B97 CS=0B98 IP=0015 NV UP EI PL NZ NA PO NC
0B98:0015 CC INT 3
-d 0B97:0000
0B97:0000 01 02 03 04 05 01 02 03-04 05 00 00 00 00 00 00 ................
0B97:0010 B8 97 0B 8E D8 8E C0 8D-36 00 00 8D 3E 05 00 B9 ........6...>...
0B97:0020 05 00 FC F3 A4 CC 15 8A-86 70 FF 2A E4 50 B8 FD .........p.*.P..

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

24
0B97:0030 05 50 FF 36 24 21 E8 77-63 83 C4 06 FF 36 24 21 .P.6$!.wc....6$!
0B97:0040 B8 0A 00 50 E8 47 5E 83-C4 04 5E 8B E5 5D C3 90 ...P.G^...^..]..
0B97:0050 55 8B EC 81 EC 84 00 C4-5E 04 26 80 7F 0A 00 74 U.......^.&....t
0B97:0060 3E 8B 46 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC >.F..V..F..V..^.
0B97:0070 26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8 &.G.*.@P.....RP.
-q

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

25
EXP: 6.8086 ASSEMBLY PROGRAM TO CHECK IF STRING IS PALINDROME OR NOT

1. DATA SEGMENT

2. BLOCK1 DB 'MALAYALAM'
3. MSG1 DB "IT IS PALINDROME $"
4. MSG2 DB "IT IS NOT PALINDROME $"
5. PAL DB 00H

6. DATA ENDS

7. PRINT MACRO MSG

8. MOV AH,09H
9. LEA DX,MSG
10. INT 21H
11. INT 3H

12. ENDM

13. EXTRA SEGMENT

14. BLOCK2 DB 9 DUP(?)


15. EXTRA ENDS

16. CODE SEGMENT


17. ASSUME CS:CODE,DS:DATA,ES:EXTRA
18. START: MOV AX,DATA
19. MOV DS,AX
20. MOV AX,EXTRA
21. MOV ES,AX
22. LEA SI,BLOCK1
23. LEA DI,BLOCK2+8
24. MOV CX,00009H
25. BACK: CLD
26. LODSB
27. STD
28. STOSB
29. LOOP BACK
30. LEA SI,BLOCK1
31. LEA DI,BLOCK2
32. MOV CX,0009H
33. CLD
34. REPZ CMPSB
35. JNZ SKIP
36. PRINT MSG1
37. SKIP: PRINT MSG2
38. CODE ENDS
39. END START

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

26
Output:

C:\TASM>masm AMPE7.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.

Object filename [AMPE7.OBJ]:


Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:

49534 + 414818 Bytes symbol space free

0 Warning Errors
0 Severe Errors

C:\TASM>link AMPE7.obj

Microsoft (R) Overlay Linker Version 3.60


Copyright (C) Microsoft Corp 1983-1987. All rights reserved.

Run File [AMPE7.EXE]:


List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug AMPE7.exe
-g
IT IS PALINDROME
AX=0924 BX=0000 CX=0000 DX=0009 SP=0000 BP=0000 SI=0009 DI=0009
DS=14A4 ES=14A8 SS=14A4 CS=14A9 IP=0033 NV UP EI PL ZR NA PE NC
14A9:0033 CC INT 3
-d 14A4:0000
14A4:0000 4D 41 4C 41 59 41 4C 41-4D 49 54 20 49 53 20 50 MALAYALAMIT IS P
14A4:0010 41 4C 49 4E 44 52 4F 4D-45 20 24 49 54 20 49 53 ALINDROME $IT IS
14A4:0020 20 4E 4F 54 20 50 41 4C-49 4E 44 52 4F 4D 45 20 NOT PALINDROME
14A4:0030 24 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 $...............
14A4:0040 4D 41 4C 41 59 41 4C 41-4D 00 00 00 00 00 00 00 MALAYALAM.......
14A4:0050 B8 A4 14 8E D8 B8 A8 14-8E C0 8D 36 00 00 8D 3E ...........6...>
14A4:0060 08 00 B9 09 00 FC AC FD-AA E2 FA 8D 36 00 00 8D ............6...
14A4:0070 3E 00 00 B9 09 00 FC F3-A6 75 09 B4 09 8D 16 09 >........u......
*/

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

27
Cycle :2 EXP:1..Serial Communication from / to 8051 to / from I/O devices.

Serial communication is a method used to transfer data between the


8051 microcontroller and I/O devices or other microcontrollers/computers. The
8051 microcontroller has a built-in UART (Universal Asynchronous
Receiver/Transmitter) for serial communication. This allows it to send and
receive data serially over its serial port (typically using the SBUF
register).

Serial Communication Basics

1. Baud Rate:
o The baud rate is the rate at which data is transmitted. It is
defined as the number of bits transmitted per second.
o The 8051 microcontroller's serial communication baud rate is
determined by Timer 1.
2. Serial Port Configuration:

o The serial port on the 8051 is configured using the SCON (Serial
Control) register.
o The SBUF (Serial Buffer) register is used to hold the data to be
transmitted or received.
3. SCON Register:

o The SCON register is used to control the mode of operation and


status of the serial port.
o The important bits in the SCON register are SM0, SM1, REN, TI, and
RI.

Steps for Serial Communication

1. Initialize the Serial Port:


o Configure Timer 1 for the desired baud rate.
o Configure the SCON register for the desired mode (usually Mode 1
for 8-bit UART).
o Enable serial interrupts if necessary.
2. Transmit Data:

o Load the data to be transmitted into the SBUF register.


o Wait for the TI (Transmit Interrupt) flag to be set, indicating
the transmission is complete.
o Clear the TI flag.
3. Receive Data:

o Wait for the RI (Receive Interrupt) flag to be set, indicating


data has been received.
o Read the received data from the SBUF register.
o Clear the RI flag.

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

28
EXP:2.Time delay Generation Using Timers of 8051.

Generating time delays using the timers of the 8051 microcontroller involves
configuring the timers to create specific time intervals. The 8051 has two
timers, Timer 0 and Timer 1, which can be configured in various modes. For
creating time delays, the most commonly used modes are Mode 1 (16-bit timer)
and Mode 2 (8-bit auto-reload timer).

Timer Mode Overview


1. Mode 0: 13-bit Timer/Counter.
2. Mode 1: 16-bit Timer/Counter.
3. Mode 2: 8-bit Timer/Counter with Auto-Reload.
4. Mode 3: Split Timer mode (Timer 0 is split into two 8-bit timers).

Steps for Generating a Delay

1. Calculate the Timer Count:


o The delay time is derived from the crystal frequency of the
microcontroller.
o For a crystal frequency of 11.0592 MHz, the machine cycle
frequency is 11.0592 MHz / 12 = 921.6 kHz.
o Timer tick period = 1 / 921.6 kHz ≈ 1.085 µs.
o For a desired delay, calculate the timer count value needed.
2. Configure the Timer:

o Load the timer registers (THx and TLx) with the appropriate count
values.
o Set the timer mode (using the TMOD register).
o Start the timer (using the TCON register).
o Poll the TFx (Timer Flag) to know when the timer overflows, or use
interrupts.

Example: Generating a 1ms Delay Using Timer 0 in Mode 1

Given a crystal frequency of 11.0592 MHz, the following example demonstrates


how to generate a 1ms delay.

Calculations:
 Timer tick period = 1.085 µs.
 Number of ticks for 1ms delay = 1 ms / 1.085 µs ≈ 921 ticks.
 Timer 0 is a 16-bit timer, so it can count from 0 to 65535.
 Initial value to load into the timer = 65536 - 921 = 64615 (0xFC67).

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

29
Code:
#include <reg51.h>

void delay_1ms(void);

void main(void) {
while (1) {
// Your main code here
delay_1ms(); // Call the delay function
}
}

void delay_1ms(void) {
TMOD |= 0x01; // Set Timer 0 in Mode 1 (16-bit timer)
TH0 = 0xFC; // Load high byte of the count value
TL0 = 0x67; // Load low byte of the count value
TR0 = 1; // Start Timer 0

while (TF0 == 0); // Wait until Timer 0 overflows (TF0 is set)

TR0 = 0; // Stop Timer 0


TF0 = 0; // Clear the overflow flag (TF0)
}

Explanation:
1. TMOD |= 0x01;: Configures Timer 0 in Mode 1 (16-bit timer).
2. TH0 = 0xFC; TL0 = 0x67;: Loads the high and low byte of the count value
into Timer 0.
3. TR0 = 1;: Starts Timer 0.
4. while (TF0 == 0);: Waits until the timer overflow flag (TF0) is set,
indicating the timer has overflowed.
5. TR0 = 0; TF0 = 0;: Stops the timer and clears the overflow flag.

Generating Longer Delays

For longer delays, you can call the delay function multiple times or adjust
the initial count value to achieve the desired delay. For example, to generate
a 10ms delay, you can call the delay_1ms function 10 times.

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

30
Example: Generating a 10ms Delay

void delay_10ms(void);

void main(void) {
while (1) {
// Your main code here
delay_10ms(); // Call the 10ms delay function
}
}

void delay_10ms(void) {
unsigned char i;
for (i = 0; i < 10; i++) {
delay_1ms(); // Call the 1ms delay function 10 times
}
}

By following these steps, you can generate precise time delays using the
timers of the 8051 microcontroller. Adjust the timer count values and function
calls to achieve the required delay for your specific application.

Example Code:

The following example demonstrates how to set up serial communication on the


8051 microcontroller to transmit and receive data at 9600 baud using an
11.0592 MHz crystal.

#include <reg51.h>

void serial_init(void);
void serial_transmit(unsigned char data);
unsigned char serial_receive(void);

void main(void) {
unsigned char received_data;

serial_init(); // Initialize serial communication

while (1) {
received_data = serial_receive(); // Receive data from the serial
port
serial_transmit(received_data); // Echo received data back
}
}

void serial_init(void) {
TMOD |= 0x20; // Timer 1 in mode 2 (8-bit auto-reload)
TH1 = 0xFD; // Load value for 9600 baud rate with 11.0592 MHz crystal
Prepared by: M.STALIN BABU
ASSISTANT PROFESSOR
ECE DEPARTMENT

31
TR1 = 1; // Start Timer 1
SCON = 0x50; // Serial mode 1, 8-bit UART, enable receiver
}

void serial_transmit(unsigned char data) {


SBUF = data; // Load data into the serial buffer
while (!TI); // Wait for the transmission to complete
TI = 0; // Clear the transmit interrupt flag
}

unsigned char serial_receive(void) {


while (!RI); // Wait for the data to be received
RI = 0; // Clear the receive interrupt flag
return SBUF; // Return the received data
}

Output Explanation:

1. serial_init Function:
o Configures Timer 1 in mode 2 (8-bit auto-reload).
o Sets the reload value for Timer 1 to achieve 9600 baud rate with
an 11.0592 MHz crystal (TH1 = 0xFD).
o Starts Timer 1 (TR1 = 1).
o Configures the SCON register for 8-bit UART mode (SCON = 0x50).
2. serial_transmit Function:

o Loads the data to be transmitted into the SBUF register.


o Waits for the TI flag to be set, indicating the data has been
transmitted.
o Clears the TI flag.
3. serial_receive Function:

o Waits for the RI flag to be set, indicating data has been


received.
o Clears the RI flag.
o Returns the received data from the SBUF register.

This code sets up the 8051 microcontroller for serial communication, allowing
it to transmit and receive data at 9600 baud. The main loop continuously
receives data and echoes it back to the sender.

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

32
EXP: 8. Program Using Interrupts to Generate Square Wave 10 KHZ Frequency on
P2.1 Using Timer 0 8051 in 8 bit Auto reload Mode and Connect a 1 HZ Pulse to
INT1 pin and Display on Port 0. Assume Crystal Frequency as 11.0592 MHZ

Configuration and Calculations:

1. Timer 0 Configuration for 10 kHz Square Wave:


o Crystal frequency = 11.0592 MHz
o Machine cycle frequency = 11.0592 MHz / 12 = 921.6 kHz
o Timer clock period = 1 / 921.6 kHz ≈ 1.085 µs
o Desired frequency = 10 kHz
o Period of the square wave = 1 / 10 kHz = 100 µs
o Timer counts for half period (50 µs) = 50 µs / 1.085 µs ≈ 46
(approx.)

Since we are using Timer 0 in 8-bit auto-reload mode, we will load the
TH0 and TL0 registers with the value required to generate an interrupt
every 50 µs:

o Reload value = 256 - 46 = 210 (0xD2)


2. External Interrupt INT1 for 1 Hz Pulse:

o INT1 interrupt will be triggered by an external 1 Hz pulse.


o The interrupt service routine (ISR) will toggle Port 0.

INPUT Code:

#include <reg51.h>

sbit square_wave_pin = P2^1; // Define P2.1 as the square wave output pin
sbit pulse_pin = P0; // Define P0 as the output for the 1 Hz pulse

void timer0_ISR(void) interrupt 1 {


// Timer 0 interrupt service routine
TH0 = 0xD2; // Reload value for 50 µs
TL0 = 0xD2; // Reload value for 50 µs

square_wave_pin = ~square_wave_pin; // Toggle the square wave output pin


}

void external_interrupt1_ISR(void) interrupt 2 {


// External interrupt INT1 service routine
pulse_pin = ~pulse_pin; // Toggle the pulse output pin
}

void main(void) {
Prepared by: M.STALIN BABU
ASSISTANT PROFESSOR
ECE DEPARTMENT

33
// Timer 0 setup for 8-bit auto-reload mode
TMOD = 0x02; // Timer 0 mode 2 (8-bit auto-reload)
TH0 = 0xD2; // Initial reload value
TL0 = 0xD2; // Initial reload value
TR0 = 1; // Start Timer 0
ET0 = 1; // Enable Timer 0 interrupt

// External interrupt setup


IT1 = 1; // Configure INT1 to trigger on falling edge
EX1 = 1; // Enable external interrupt INT1

EA = 1; // Enable global interrupts

while (1) {
// Main loop does nothing, work is done in ISRs
}
}

Explanation:

1. Timer 0 Configuration:
o TMOD = 0x02; sets Timer 0 in 8-bit auto-reload mode.
o TH0 = 0xD2; and TL0 = 0xD2; load the timer with the reload value
for a 50 µs interval.
o TR0 = 1; starts Timer 0.
o ET0 = 1; enables Timer 0 interrupt.
2. External Interrupt Configuration:

o IT1 = 1; configures INT1 to trigger on the falling edge of the 1 Hz


pulse.
o EX1 = 1; enables the external interrupt INT1.
o EA = 1; enables global interrupts.
3. Interrupt Service Routines (ISRs):

o timer0_ISR toggles the P2.1 pin every 50 µs to generate a 10 kHz


square wave.
o external_interrupt1_ISR toggles the Port 0 pin every second when
the external 1 Hz pulse triggers INT1.

By following this code and configuration, the 8051 microcontroller will


generate a 10 kHz square wave on P2.1 and toggle the Port 0 pin at a 1 Hz
frequency using the external interrupt INT1.

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

34
CYCLE-3. EXP: 1.Interfacing I/O Devices to a 7 Segment Display to 8051

Interfacing an I/O device to a 7-segment display using the 8051


microcontroller involves several steps. Here's a general overview of the
process:

Components Needed
1. 8051 Microcontroller
2. 7-Segment Display
3. Current-limiting resistors (typically 220Ω)
4. I/O Devices (e.g., switches, sensors)
5. Connecting wires
6. Breadboard or PCB

Steps to Interface:

1. Understanding the 7-Segment Display

A 7-segment display consists of 7 LEDs arranged in a specific pattern that can


display numbers 0-9 and some alphabets. Each segment is labeled from 'a' to
'g'. There are two types of 7-segment displays:

 Common Anode: All anodes are connected together.


 Common Cathode: All cathodes are connected together.

2. Connecting the 7-Segment Display to 8051

Assuming we are using a common cathode display, the segments are connected to
the microcontroller's port pins through current-limiting resistors.

3. Segment Control

Each segment of the display is connected to a port pin of the 8051. To light
up a segment, you need to set the corresponding pin to high (1).

4. Code to Control the 7-Segment Display

Here is an example code snippet to display digits on a 7-segment display


connected to Port 1 of the 8051:

Input code:

#include <reg51.h>

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

35
void delay() {
int i, j;
for (i = 0; i < 1000; i++) {
for (j = 0; j < 100; j++) {}
}
}

void main() {
// Digit patterns for 0-9 on a common cathode 7-segment display
unsigned char segment_code[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,
0xF8, 0x80, 0x90};

while (1) {
int i;
for (i = 0; i < 10; i++) {
P1 = segment_code[i]; // Send segment code to Port 1
delay(); // Wait for a while
}
}
}

5. Interfacing I/O Devices

To interface an I/O device, such as a switch, to control which digit to


display, you can connect the switch to another port pin and read its status.

Here’s an example of reading a switch input and displaying a corresponding


digit:

#include <reg51.h>

sbit switch1 = P2^0; // Switch connected to P2.0

void delay() {

int i, j;

for (i = 0; i < 1000; i++) {

for (j = 0; j < 100; j++) {}

void main() {

// Digit patterns for 0-9 on a common cathode 7-segment display

unsigned char segment_code[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,
0xF8, 0x80, 0x90};

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

36
while (1) {

if (switch1 == 0) { // Check if switch is pressed

P1 = segment_code[1]; // Display digit 1

} else {

P1 = segment_code[0]; // Display digit 0

delay(); // Wait for a while

Explanation
1. delay(): This function provides a simple delay to make the digit
visible.
2. segment_code[]: This array holds the bit patterns for displaying digits
0-9 on a 7-segment display.
3. while(1): This loop continuously checks the switch status and updates
the 7-segment display.

By using similar techniques, you can interface other I/O devices (like
sensors) to control the display based on their inputs.

EXP:2. Interfacing I/O Devices to 8051 Matrix Keypad to 8051.

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

37
Interfacing a matrix keypad with an 8051 microcontroller involves connecting
the keypad to the microcontroller's I/O ports and writing a program to detect
key presses. A matrix keypad is typically arranged in rows and columns, and
pressing a key connects a specific row to a specific column.

Components Needed
1. 8051 Microcontroller
2. 4x4 Matrix Keypad
3. Pull-up resistors (if necessary)
4. Connecting wires
5. Breadboard or PCB

Matrix Keypad Layout

A 4x4 matrix keypad has 16 keys, arranged in a grid. Each key press connects a
unique row and column pair.

Connecting the Keypad to the 8051

The rows and columns of the keypad are connected to the I/O ports of the 8051.
For example, rows can be connected to Port 2 (P2.0 to P2.3) and columns to
Port 1 (P1.0 to P1.3).

Keypad Scanning Algorithm


1. Initialize all rows to logic 1 (high) and columns to logic 0 (low).
2. Sequentially set each row to 0 (low) while keeping the others high.
3. Check each column to see if a key press has connected the row to the
column (the column will read low if a key is pressed).
4. Identify the specific key based on the row and column detected.

Example Code:

Here’s an example code snippet to interface a 4x4 matrix keypad with an 8051
microcontroller and display the pressed key on an LED connected to another
port:

#include <reg51.h>

#define KEYPAD_PORT P1 // Keypad is connected to Port 1


#define ROW_PORT P2 // Rows are connected to Port 2

unsigned char key;


unsigned char row, col;

void delay() {
int i, j;
for (i = 0; i < 100; i++) {
for (j = 0; j < 100; j++) {}
}
}

void init_keypad() {
KEYPAD_PORT = 0xF0; // Set upper 4 bits (rows) as high and lower 4 bits
(columns) as low
ROW_PORT = 0xFF; // Set all rows to high initially
Prepared by: M.STALIN BABU
ASSISTANT PROFESSOR
ECE DEPARTMENT

38
}

unsigned char read_keypad() {


while (1) {
ROW_PORT = 0xFE; // Enable row 0
col = KEYPAD_PORT & 0x0F; // Read column values
if (col != 0x0F) {
row = 0;
break;
}

ROW_PORT = 0xFD; // Enable row 1


col = KEYPAD_PORT & 0x0F;
if (col != 0x0F) {
row = 1;
break;
}

ROW_PORT = 0xFB; // Enable row 2


col = KEYPAD_PORT & 0x0F;
if (col != 0x0F) {
row = 2;
break;
}

ROW_PORT = 0xF7; // Enable row 3


col = KEYPAD_PORT & 0x0F;
if (col != 0x0F) {
row = 3;
break;
}
}

if (col == 0x0E) return (row * 4 + 0); // Column 0


if (col == 0x0D) return (row * 4 + 1); // Column 1
if (col == 0x0B) return (row * 4 + 2); // Column 2
if (col == 0x07) return (row * 4 + 3); // Column 3

return 0xFF; // Return invalid key if none found


}

void main() {
init_keypad();

while (1) {
key = read_keypad(); // Read the pressed key

// Display the pressed key on Port 0 (connected to LEDs for


simplicity)
P0 = key;
delay(); // Simple delay for debouncing
}
}

Explanation

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

39
1. init_keypad (): Initializes the keypad by setting the appropriate port
directions.
2. read keypad (): Scans the keypad by setting each row to low and reading
the columns to detect a key press. It returns the key index.
3. main (): Continuously reads the keypad and displays the pressed key on
Port 0 (for example, connected to LEDs).

This basic example shows how to interface a 4x4 matrix keypad with an 8051
microcontroller and detect key presses. You can expand this by adding more
functionality or interfacing other devices based on the key pressed.

EXP:3. Interfacing I/O Devices to 8051 Sequence Generator Using Serial


Interface in 8051.

Interfacing an I/O device to an 8051 microcontroller to create a sequence


generator using a serial interface involves configuring the 8051 to
Prepared by: M.STALIN BABU
ASSISTANT PROFESSOR
ECE DEPARTMENT

40
communicate over a serial connection (typically UART) and generating sequences
based on received commands. Here’s a step-by-step guide to achieve this:

Components Needed
1. 8051 Microcontroller
2. Serial Interface (e.g., RS232, USB-to-Serial Converter)
3. MAX232 (for RS232 voltage level conversion, if necessary)
4. LEDs or other output devices to display the sequence
5. Connecting wires
6. Breadboard or PCB

Configuring the 8051 for Serial Communication

The 8051 microcontroller has an in-built UART that can be used for serial
communication. The typical baud rate for UART communication is 9600.

Circuit Diagram
 Connect the TXD (P3.1) and RXD (P3.0) pins of the 8051 to the serial
interface.
 Use a MAX232 IC for voltage level conversion if interfacing with RS232.

Example Code

Below is an example code to configure the 8051 for serial communication,


receive a command to start the sequence, and display the sequence using LEDs
connected to Port 2.

#include <reg51.h>

void delay() {

int i, j;

for (i = 0; i < 1000; i++) {

for (j = 0; j < 100; j++) {}

void serial_init() {

TMOD = 0x20; // Timer 1 in Mode 2

TH1 = 0xFD; // Baud rate 9600

SCON = 0x50; // Mode 1, 8-bit UART, enable receiver

TR1 = 1; // Start Timer 1

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

41
void serial_send(unsigned char data) {

SBUF = data; // Load data into serial buffer

while (TI == 0); // Wait until transmission is complete

TI = 0; // Clear the transmit interrupt flag

unsigned char serial_receive() {

while (RI == 0); // Wait until data is received

RI = 0; // Clear the receive interrupt flag

return SBUF; // Return the received data

void generate_sequence() {

unsigned char i;

for (i = 0; i < 8; i++) {

P2 = 1 << i; // Shift a '1' through the LEDs

delay();

void main() {

serial_init(); // Initialize serial communication

P2 = 0x00; // Initialize Port 2 to 0

while (1) {

unsigned char command = serial_receive(); // Receive command from


serial interface

if (command == 'S') { // If command is 'S', start sequence generation

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

42
generate_sequence();

Explanation
1. serial_init(): Initializes the UART for serial communication with a baud
rate of 9600.
2. serial_send(): Sends a byte of data over the serial interface.
3. serial_receive(): Receives a byte of data from the serial interface.
4. generate_sequence(): Generates a simple sequence by shifting a '1'
through the LEDs connected to Port 2.
5. main(): Continuously listens for a command from the serial interface. If
the command 'S' is received, it starts the sequence generation.

Additional Notes
 The example assumes a simple LED sequence. You can modify the
generate_sequence() function to create more complex sequences.
 Ensure that the baud rate of the terminal program or the device sending
commands matches the 8051’s configured baud rate (9600 in this case).
 The serial communication can be tested using a terminal program like
PuTTY, Tera Term, or a custom Python script on a PC.

This setup allows you to control the 8051 microcontroller via serial commands
and generate sequences or perform other actions based on the received
commands.

EXP:5. Interfacing I/O Devices to 8051 8 bit ADC Interface to 8051

Interfacing an 8-bit ADC (Analog-to-Digital Converter) with an 8051


microcontroller allows the microcontroller to read analog signals. The ADC0804
is a common 8-bit ADC that can be used for this purpose. Here's a step-by-step
guide on how to interface the ADC0804 with the 8051 microcontroller.

Components Needed
1. 8051 Microcontroller
2. ADC0804

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

43
3. Analog Sensor (e.g., a potentiometer or temperature sensor)
4. Capacitors and Resistors (for ADC0804 setup)
5. Connecting wires
6. Breadboard or PCB

Circuit Diagram

1. ADC0804 Pin Connections:


o Vcc: Connect to +5V.
o GND: Connect to Ground.
o Vref/2: Connect to a voltage divider to set the reference voltage
(usually 2.5V for a 5V system).
o IN+: Connect to the analog signal input (e.g., a sensor or
potentiometer).
o IN-: Connect to Ground (for single-ended input).
o CS: Connect to Ground (always selected).
o WR: Connect to a pin of the 8051 (e.g., P3.6).
o RD: Connect to a pin of the 8051 (e.g., P3.7).
o INTR: Connect to a pin of the 8051 (e.g., P3.2).
o D0-D7: Connect to a port of the 8051 (e.g., P1.0 to P1.7).
2. 8051 Pin Connections:

o Connect appropriate pins for power (Vcc) and ground (GND).


o Connect ADC0804 control pins to Port 3 (e.g., WR to P3.6, RD to
P3.7, INTR to P3.2).
o Connect ADC0804 data pins (D0-D7) to Port 1.

Example Code

Here's an example code to read data from the ADC0804 and display it on Port 0:

#include <reg51.h>

sbit WR = P3^6; // Write pin connected to P3.6


sbit RD = P3^7; // Read pin connected to P3.7
sbit INTR = P3^2; // Interrupt pin connected to P3.2

void delay() {
int i, j;
for (i = 0; i < 100; i++) {
for (j = 0; j < 100; j++) {}
}
}

void adc_init() {
WR = 1; // Initialize WR to high
RD = 1; // Initialize RD to high
}

unsigned char adc_read() {


unsigned char adc_value;

WR = 0; // Start conversion by bringing WR low


delay();
WR = 1; // End conversion by bringing WR high

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

44
while (INTR == 1); // Wait for INTR to go low

RD = 0; // Enable RD to read data


adc_value = P1; // Read the 8-bit data from ADC
RD = 1; // Disable RD

return adc_value;
}

void main() {
unsigned char adc_value;

adc_init(); // Initialize ADC0804


P1 = 0xFF; // Configure Port 1 as input for ADC data
P0 = 0x00; // Configure Port 0 as output for displaying ADC value

while (1) {
adc_value = adc_read(); // Read value from ADC0804
P0 = adc_value; // Display the ADC value on Port 0
delay(); // Simple delay
}
}

Explanation
1. delay(): Provides a small delay to allow the ADC to stabilize.
2. adc_init(): Initializes the control pins for the ADC0804.
3. adc_read(): Starts the conversion, waits for the INTR pin to go low
indicating conversion is complete, reads the 8-bit data from the ADC,
and returns it.
4. main(): Continuously reads the ADC value and displays it on Port 0.

Additional Notes
 The ADC0804 has a resolution of 8 bits, meaning it can represent the
analog input signal with 256 discrete levels.
 The delay() function can be adjusted based on the required timing for
your specific application.
 Ensure that the analog signal voltage does not exceed the reference
voltage set by Vref/2.

This setup allows the 8051 microcontroller to read analog signals, convert
them to digital values using the ADC0804, and process or display the digital
values as need

Prepared by: M.STALIN BABU


ASSISTANT PROFESSOR
ECE DEPARTMENT

45

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy