Stalin Eee MPMC Lab Siet 2024
Stalin Eee MPMC Lab Siet 2024
LAB MANUAL
ON
MICROPROCESSORS & MICROCONTROLLERS LAB
-----------------------------------------------------------------------------
-
1
MICROPROCESSORS & MICROCONTROLLERS LAB
2
MICROPROCESSORS & MICROCONTROLLERS LAB
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.
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:-
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.
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.
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 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]
INTRODUCTION:
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):
He nearest Pentium has dynamic instruction execution and the chip package is
more than 5.5 million transistors
Pentium II (1997):
7
Pentium III (1999):
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
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.
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.
0 Warning Errors
0 Severe Errors
C:\TASM>link an16add.obj
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
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.
0 Warning Errors
0 Severe Errors
C:\TASM>link an16mul.obj
C:\TASM>debug an16mul.exe
-g
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
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.
0 Warning Errors
0 Severe Errors
C:\TASM>link an16sub.obj
C:\TASM>debug an16sub.exe
-g
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
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.
0 Warning Errors
0 Severe Errors
C:\TASM>link an16div.obj
C:\TASM>debug an16div.exe
-g
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
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.
0 Warning Errors
0 Severe Errors
C:\TASM>link an32add.obj
C:\TASM>debug an32add.exe
17
-g
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.
0 Warning Errors
0 Severe Errors
C:\TASM>link an32sub.obj
C:\TASM>debug an32sub.exe
19
-g
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
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.
21
50278 + 450378 Bytes symbol space free
0 Warning Errors
0 Severe Errors
C:\TASM>link an_32mul.obj
C:\TASM>debug an_32mul.exe
-g
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.
0 Warning Errors
0 Severe Errors
C:\TASM>link an_hello.obj
C:\TASM>debug an_hello.exe
-g
hello
Program terminated normally
-q
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.
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.
C:\TASM>debug bt.exe
-g
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
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
8. MOV AH,09H
9. LEA DX,MSG
10. INT 21H
11. INT 3H
12. ENDM
26
Output:
C:\TASM>masm AMPE7.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.
0 Warning Errors
0 Severe Errors
C:\TASM>link AMPE7.obj
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......
*/
27
Cycle :2 EXP:1..Serial Communication from / to 8051 to / from I/O devices.
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:
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).
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.
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).
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
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.
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.
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:
#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;
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
}
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:
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.
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
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:
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 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
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:
34
CYCLE-3. EXP: 1.Interfacing I/O Devices to a 7 Segment Display to 8051
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:
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).
Input code:
#include <reg51.h>
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
}
}
}
#include <reg51.h>
void delay() {
int i, j;
void main() {
unsigned char segment_code[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,
0xF8, 0x80, 0x90};
36
while (1) {
} else {
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.
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
A 4x4 matrix keypad has 16 keys, arranged in a grid. Each key press connects a
unique row and column pair.
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).
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>
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
}
void main() {
init_keypad();
while (1) {
key = read_keypad(); // Read the pressed key
Explanation
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.
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
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
#include <reg51.h>
void delay() {
int i, j;
void serial_init() {
41
void serial_send(unsigned char data) {
void generate_sequence() {
unsigned char i;
delay();
void main() {
while (1) {
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.
Components Needed
1. 8051 Microcontroller
2. ADC0804
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
Example Code
Here's an example code to read data from the ADC0804 and display it on Port 0:
#include <reg51.h>
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
}
44
while (INTR == 1); // Wait for INTR to go low
return adc_value;
}
void main() {
unsigned char 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
45