0% found this document useful (0 votes)
87 views

Microcontroller Ab Manual New

manual

Uploaded by

shilpa
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)
87 views

Microcontroller Ab Manual New

manual

Uploaded by

shilpa
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/ 57

GURU NANAK DEV ENGINEERING COLLEGE, BIDAR

(ISO Certified Institute ISO 9001/2015)

DEPARTMENT OF ELECTRONICS &


COMMUNICATION ENGINEERING

MicroController LAB
(18ECL47)
B.E –IV Semester

Name :

USN :

Batch : Section :

1
VISION OF THE INSTITUTE

To keep pace with the fast developing scenario of


technology and socio–economic environments to emerge as a
world class technical institution.

MISSION OF THE INSTITUTE

M1. To contribute for the development of society in general and


development of technical man power in particular by
providing, integrating and managing the resources.
M2. To nurture scientific temperament through the process of
intellectual stimulation.
M3. To enhance employability, entrepreneurial ability along with
social responsibility by imparting quality education and
Providing entrepreneurial training, forging mutually
beneficial relationships with governmental entities, industry,
and the alumni through MOUs and regular interactions.

2
Electronics & Communication Engineering
Department

VISION
To excel in Electronics and Communication Engineering with social
responsibility and to moulding the graduates to face the global
challenges.

MISSION
M1: To impart quality education through advanced teaching and
learning aids facilitating the students to develop technical skills
through innovative ideas and their implementation.
M2: To Build competence through hands - on experience using state of
the art technology with the help of Industry- institute partnership.
M3: To guide the students to become responsible citizens of the
nation by inculcating ethical and moral values through
participation in Social activities.

Program Educational Objectives (PEO)


PEO1: The Graduates will be able to function as professional in rapidly
changing World keeping pace with the advances in technology.
PEO2: The Graduates will be able to contribute in improving the life
standards of the society.
PEO3: The Graduates will be capable of doing innovation, new research
activities and imparting knowledge.

Program Specific Outcomes (PSOs)


The graduates of the department will be able:
PSO1: To analyze, design, and develop various types of integrated
electronics systems.
PSO2: To apply their knowledge and skills to develop an application in
communication systems.

3
INDEX

Laboratory Experiments:

S.No Name of the Experiment Page.No


1. Data Transfer 11-16
2. Arithmetic Instructions 17-22
3. Counters. 23-26
4. Boolean & Logical Instructions 27-33
5. Conditional CALL & RETURN 34
6. Code conversion 35-38
7. Generate Delay 39
II. Interfacing( procedure) 40-41

1 LED 42-44
2 Transmit and Receive Set of Characters 45-47
3 ADC Interface 48
4 LCD Display 49-51
5 Stepper Motor 52
6 ADC 0804 53-54
VIVA Questions 55-57

4
B. E. (EC / TC)
Choice Based Credit System (CBCS) and Outcome Based Education (OBE)
Subject Code 18ECL47 IA Marks 40
Number of Lecture Hours/Week : 01Hr Tutorial (Instructions)+ 02 Hours
Laboratory=03
Exam Marks 60

Laboratory Experiments
I. PROGRAMMING
1. Data Transfer: Block Move, Exchange, Sorting, Finding largest element in an
array.
2. Arithmetic Instructions - Addition/subtraction, multiplication and division, square,
Cube – (16 bits Arithmetic operations – bit addressable).
3. Counters.
4. Boolean & Logical Instructions (Bit manipulations).
5. Conditional CALL & RETURN.
6. Code conversion: BCD – ASCII; ASCII – Decimal; Decimal - ASCII; HEX -
Decimal and Decimal -HEX.
7. Programs to generate delay, Programs using serial port and on-Chip timer/counter.
II. INTERFACING
1. Interface a simple toggle switch to 8051 and write an ALP to generate an interrupt
which switches on an LED (i) continuously as long as switch is on and (ii) only once
for a small time when the switch is turned on.
2. Write a C program to (i) transmit and (ii) to receive a set of characters serially by
interfacing 8051 to a terminal.
3. Write ALPs to generate waveforms using ADC interface.
4. Write ALP to interface an LCD display and to display a message on it.
5. Write ALP to interface a Stepper Motor to 8051 to rotate the motor.
6. Write ALP to interface ADC-0804 and convert an analog input connected to it.

5
Department of Electronics & Communication
Engineering

MicroController Lab

Course Outcome

On the completion of this laboratory course, the students will be able to:

• Write Assembly language programs in 8051 for solving simple


problems that manipulate input data using different instructions of 8051.

• Interface different input and output devices to 8051 and control them using
Assembly language
programs.

• Interface the serial devices to 8051 and do the serial transfer using C programming.

6
‘Safety precautions / Do’s and Dont’s’

O Leave your footwear outside.

O Be present in the lab on time.

O Maintain silence and discipline in the lab.

O Maintain a separate observation note book for the lab


experiments.

O Come prepared for a minimum of a next one set of program


and execute them.

O After executing the program show the result to the concerned


staff.

O Before leaving the lab properly shut down the computer


system .

O Write the executed program in the file and bring it in the


next class.

7
INTRODUCTION

Earlier to Microcontrollers, Microprocessors were greatly used for


each and every purpose. Microprocessors were containing ALU, general
purpose register, stack pointer, program counter, clock counter and so
many other features which the today’s Microcontroller also possesses.
But the difference between them exists with respect to the number of
instructions, access times, size, reliability, PCB size and so on.
Microprocessor contains large instruction set called as CISC
processor whereas Microcontroller contains less number of instructions
and is called as RISC processor. The access time is less in case of
microcontrollers compared to microprocessors and the PCB size reduces
in case of microcontrollers. There are many versions of microcontrollers
8051, 80528751, AT8951 from Atmel Corporation and many more. In
this manual we will study about the 8051 architecture, its features,
programming and interfacing. MCS 8051 is an 8-bit single chip
microcontroller with many built-in functions and is the core for all MCS-
51 devices.
The main features of the 8051 core are:
 Operates with 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):
 Four Register Banks, each containing 8 registers (R0 to R7) [Total
32 reg]
 16-bytes of bit addressable memory.
 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).
 Two 16-bit timers/counters (T0 and T1).
 Full duplex UART (Universal Asynchronous
Receiver/Transmitter).
 On-Chip oscillator and clock circuitry.

8
TO WRITE A PROGRAM IN DOS

1. Click on START / RUN / CMD/ ENTER

2. cd \ enter

3. cd xpo31 enter

4. c: \xpo31> cd asm enter

5. c:\xpo31\asm\edit enter

TYPE THE PROGRAM IN EDITOR WINDOW ( caps mode)


6. SAVE AS FILENAME.ASM

7. SELECT FILE EXIT

HEX FILE CREATION PROCEDURE:


8. XHEX(one space)c:(one space)\xpo31\asm\only file name

9. Ex: c:\xpo31\asm> XHEX c: \xpo31\asm\example1

10. IT DISPLAYS NO ERROR.

11. (If errors are found then go to editor by typing EDIT filename.asm)

MINIMIZE THE SCREEN


PROCEDURE TO TRANSFER THE PROGRAM FROM PC TO
KIT
12. Double Click On HyperTerminal Icon On Desktop.

13. Give any filename other than already used name for the main program. Then
press ok.

14. Select Your Com Port Which Is Displayed On Your Table.

15. Select 2400/8/stop bits 2/hardware/apply/ok .

16. Switch on the kit.

17. keep your keyboard in caps mode .

18. Press the reset switch on mc kit.

19. Press ‘I’ and enter.

20. Press any key then press enter two times.

9
21. Give strat adderss (ex 6000) enter. Then give end adderss (ex 6800) enter.

it will display “wait” .


22. Transfer: select transfer option on menu bar the select send text file.

Select c drive/xpo31/asm/all files./select filename.hex (hex )/open/ok


PROCEDRE FOR EXECUTION OF THE PROGRAM
23. Press “G” enter

24. Goto/enter/burst/strat address/enter

25. It displays “wait done”----indicating

Program has been executed.


Procedure for checking the result:
26. Press “Esc” and “S” on key board then enter

27. Choose appropriate location ( such as internal, external, gen register etc as per
your program)

28. Note down the result.

29. Verify it manually.

10
1 : Data Transfer Programming

A.WALP to copy the content of RAM location to ROM location.


CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV DPTR, #5000H
MOV R1, #30H
MOV R2, #05H
UP: MOV A,@R1
MOVX @DPTR, A
INC DPTR
INC R1
DJNZ R2, UP
RET

Result:

Before Execution After Execution

RAM ROM (copied) RAM ROM (copied)


30H<=01H 5000H<=01H 30H<=01H 5000H<=01H
31H<=03H 5001H<=03H 31H<=03H 5001H<=03H
32H<=04H 5002H<=04H 32H<=04H 5002H<=04H
33H<=06H 5003H<=06H 33H<=06H 5003H<=06H
34H<=0BH 5004H<=0BH 34H<=0BH 5004H<=0BH

11
B. WALP to interchange the content of RAM location to ROM
location.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV DPTR, #5000H
MOV R1, #30H
MOV R2, #05H
UP: MOVX A,@DPTR
XCH @R1, A
MOVX @DPTR, A
INC DPTR
INC R1
DJNZ R2, UP
RET

Result:
Before Execution
30H<=0FH 5000H<=01H
31H<=0EH 5001H<=02H
32H<=0DH 5002H<=03H
33H<=0CH 5003H<=04H
34H<=0BH 5004H<=05H

After Execution
30H<=01H 5000H<=0FH
31H<=02H 5001H<=0EH
32H<=03H 5002H<=0DH
33H<=04H 5003H<=0CH
34H<=05H 5004H<=0BH

12
C. WALP to find largest in a given array.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV R1, #30H
MOV R2, 50H (Length of a array)
MOV R0, #00H
AGAIN: MOV A,@R1
MOV 30H, R0
CJNE A, 30H, DOWN
SJMP SKIP
DOWN: JC SKIP
MOV R0, A
SKIP: INC R1
DJNZ R2, AGAIN
RET

Result:
50H=05H

After Execution
30H<=08H
31H<=05H
32H<=0AH
33H<=10H
34H<=01H

R1<= 0AH (greatest)

13
D. WALP to find smallest in a given array.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV R1, #30H
MOV R2, 50H (Length of a array)
MOV R0, #0FFH
AGAIN: MOV A,@R1
MOV 30H, R0
CJNE A, 30H, DOWN
SJMP SKIP
DOWN: JNC SKIP
MOV R0, A
SKIP: INC R1
DJNZ R2, AGAIN
RET

Result:
50H=05H
After Execution
30H<=08H
31H<=05H
32H<=01H
33H<=10H
34H<=0AH

R1<= 01H (smallest)

14
E. WALP to set the given array in ascending order.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV R3, #05H
UP: MOV DPTR, #5000H
MOV R2, #04H
AGAIN: MOV R1, 82H
MOVX A,@DPTR
MOV 30H, A
INC DPTR
MOVX A,@DPTR
CJNE A, 30H, DOWN
SJMP SKIP
DOWN: JNC SKIP
MOV 82H, R1
MOVX @DPTR, A
INC DPTR
MOV A, 30H
MOVX @DPTR, A
SKIP: DJNZ R2, AGAIN
DJNZ R3, UP
RET

Result:
Before Execution After Execution
5000H<=08H 5000H<=01H
5001H<=01H 5001H<=02
5002H<=02H 5002H<=05H
5003H<=09H 5003H<=08H
5004H<=05H 5004H<=09H

15
F.WALP to set the given array in descending order.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV R3, #05H
UP: MOV DPTR, #5000H
MOV R2, #04H
AGAIN: MOV R1, 82H
MOVX A,@DPTR
MOV 30H, A
INC DPTR
MOVX A,@DPTR
CJNE A, 30H, DOWN
SJMP SKIP
DOWN: JC SKIP
MOV 82H, R1
MOVX @DPTR, A
INC DPTR
MOV A, 30H
MOVX @DPTR, A
SKIP: DJNZ R2, AGAIN
DJNZ R3, UP
RET

Result:
Before Execution After Execution
5000H<=08H 5000H<=09H
5001H<=01H 5001H<=08H
5002H<=02H 5002H<=05H
5003H<=09H 5003H<=02H
5004H<=05H 5004H<=01H

16
2. Arithmetic Instructions

A. Write an ALP to ADD two 16 bit numbers.

CPU” 8051.TBL”
HOF”INT8”
ORG 6000H
CLR C
MOV A, #17H
ADD A, #12H
MOV R0,A
MOV A,#15H
ADD A,#13H
MOV R1,A
RET
Result:
After Execution
R0<= 17H+12H R1<= 15H+13H

00010111 00010101
+00010010 +00010011
------------------------------------------------

00101001 00101000

R0<=29H R1<=28H

1517H+ 1312H=2829H

17
B. Write an ALP to SUBB two 16-bit numbers.

CPU” 8051.TBL”
HOF”INT8”
ORG 6000H
CLR C
MOV A, #17H
SUBB A, #12H
MOV R0,A
MOV A,#15H
SUBB A,#13H
MOV R1,A
RET

RESULT : R1 = 02 R0 = 05

R0<= 17H-12H R1<= 15H-13H

00010111 00010101
-00010010 -00010011
------------------------------------------------
00000101 00000010

1517H- 1312H=0205H

18
C. WALP to multiply two 8-bit numbers.
CPU”8051.TBL”
HOF”INT8”
ORG 6000H
MOV A, 30H
MOV 0F0H, 31H
MUL AB
MOV 35H, A
MOV 36H, 0F0H
RET

Result:
Before Execution: After Execution:

30H<=09H 30H<= 09H 31H<= 04H


31H<=04H 09 × 04 = (36)10

09 × 04= 0024H (36)10 = (24)16

35H<= 24H (Lower byte) 35H<= 24H (Lower byte)


36H<= 00H (Upper byte) 36H<= 00H (Upper byte)

19
D. WALP to divide two 8-bit numbers.
CPU“8051.TBL”
HOF“INT8”
ORG 6000H
MOV A, 30H
MOV 0F0H, 31H
DIV AB
MOV 35H, A
MOV 36H, 0F0H
RET

Result:
Before Execution: After Execution:

30H<=09H 30H<= 09H 31H<= 02H


31H<=02H 09 ÷ 02 = 04H
Quotient= 04H
09 ÷ 02= 04H Remainder= 01H
Remain= 01H
35H<= 04H (Quotient) 35H<= 04H (Quotient)
36H<= 01H (Remainder) 36H<= 01H (Remainder)

20
E. WALP to find square of the given number.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV A, 30H
MOV 0F0H, A
MUL AB
MOV 35H, A
MOV 36H, 0F0H
RET

Result:
Before Execution After Execution

30H<= 10H 30H<= 10H

102= 100H 102= 100H

35H<= 00H
36H<=01H

21
F. WALP to find the cube of a given number.
CPU“8051.TBL”
HOF“INT8”
ORG 6000H
MOV A, 30H
MOV 0F0H, A
MUL AB
MOV 0F0H, 30H
MUL AB
MOV 35H, A
MOV 36H, 0F0H
RET

Result:
Before Execution After Execution

30H<= 03H 30H<= 03H

033= 1BH 033= (27)10H

(27)10= (1B)16

35H<= 1BH
36H<= 00H

22
3.Counters.
A. WALP to generate binary up-counter.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV A, #00H
UP: LCALL 061DH
MOV R3, A
MOV R4, #00H
MOV R5, #02H
LCALL 059EH
ADD A, #01H
CJNE A, #00H, UP
RET

Result:
After Execution
00
01

.
.
.
FC
FD
FE
FF

23
B. WALP to generate binary down-counter.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV A, #0FFH
UP: LCALL 061DH
MOV R3, A
MOV R4, #00H
MOV R5, #02H
LCALL 059EH
SUBB A, #01H
CJNE A, #0FFH, UP
RET

Result:
After Execution
FF
FE
FD
FC
.
.
.

04
03
02
01
00

24
C. WALP to generate BCD up counter.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV A, #00H
UP: LCALL 061DH
MOV R3, A
MOV R4, #00H
MOV R5, #02H
LCALL 059EH
ADD A, #01H
DA A
CJNE A, #00H, UP
RET

Result:
After Execution
00
01
02
03
.
.
.

96
97
98
99

25
D. WALP to generate BCD down counter.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV A, #99H
UP: LCALL 061DH
MOV R3, A
MOV R4, #00H
MOV R5, #02H
LCALL 059EH
ADD A, #99H
DA A
CJNE A, #99H, UP
RET

Result:
After Execution
99
98
97
96
.
.
.

4
3
2
1

26
4. Boolean & Logical Instructions
A. WALP to COUNT number of 1’s & 0’s in a number.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV R2, #08H
MOV A, 30H
MOV R0, #00H
MOV R1, #00H
CLR C
UP: RRC A
JNC GO
INC R1
SJMP LAST
GO: INC R0
LAST: DJNZ R2, UP
RET

Result:
Before Execution After Execution

30H<= 0FH 30H<= 0FH

R0<= 04 (no. of 0’s) 00001111


R1<= 04 (no. of 1’s) 0 0 0 0 1 1 1 1 0

Rotating right with carry


R0<= 04(no. of 0’s)
R1<= 04(no. of 1’s)

27
B. WALP to find even & odd from given array.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV 0D0H, #00H
MOV DPTR, #5000H
MOV R0, #30H
MOV R1, #20H
MOV R3, #05H
UP: MOVX A,@DPTR
RRC A
JNC NEXT
MOVX A,@DPTR
MOV @R1, A
INC R1
SJMP SKIP
NEXT: MOVX A,@DPTR
MOV @R0, A
INC R0
SKIP: INC DPTR
DJNZ R3, UP
RET

Result:
Before Execution After Execution

5000H<= 05H 5000H<= 05H


5001H<= 06H 5001H<= 06H
5002H<= 07H 5002H<= 07H
5003H<= 08H 5003H<= 08H
5004H<= 09H 5004H<= 09H

28
Even:

30H<= 06H 05 <= 0 0 0 0 0 1 0 1


06 <= 0 0 0 0 0 1 1 0
31H<= 08H
07 <= 0 0 0 0 0 1 1 1
08 <= 0 0 0 0 1 0 0 0
Odd: 09 <= 0 0 0 0 1 0 0 1
20H<= 05H
21H<= 07H Even: 06 & 08
22H<= 09H Odd: 05, 07 & 09

Even: Odd:
30H<= 06H 20H<=05H
31H<= 08H 21H<= 07H
22H<= 09H

29
C. WALP to find positive & negative from given array.
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV 0D0H, #00H(clear the PSW)
MOV DPTR, #5000H
MOV R0, #30H
MOV R1, #20H
MOV R3, #05H
UP: MOVX A,@DPTR
RLC A
JNC NEXT
MOVX A,@DPTR
MOV @R1, A
INC R1
SJMP SKIP
NEXT: MOVX A,@DPTR
MOV @R0, A
INC R0
SKIP: INC DPTR
DJNZ R3, UP
RET

Result:
Before Execution After Execution
5000H<= FFH 5000H<= FFH
5001H<= 08H 5001H<= 08H
5002H<= 80H 5002H<= 80H
5003H<= 05H 5003H<= 05H
5004H<= 16H 5004H<= 16H

Negative: FF <= 1 1 1 1 1 1 1 1
08 <= 0 0 0 0 1 0 0 0
80 <= 1 0 0 0 0 0 0 0
05 <= 0 0 0 0 0 1 0 1
16 <= 0 0 0 1 0 1 1 0
30
30H<= FFH
31H<= 80H

Positive:
20H<= 08H
21H<= 05H Negative: FF & 80
22H<= 16H Positive: 08, 05 & 16

Negative: Positive:
30H<= FFH 20H<=08H
31H<= 80H 21H<= 05H
22H<= 16H

D. WALP to unpack a pack number.

31
Method-1: Method-2 (using logical
instrucn):

CPU “8051.TBL” CPU “8051.TBL”


HOF “INT8” HOF “INT8”
ORG 6000H ORG 6000H
MOV R0,30H MOV R0,30H
MOV A, R0 MOV A, R0
MOV 0F0H, #10H ANL A, #0FH
DIV AB MOV R6, A
MOV R1, A MOV A, R0
MOV R2, 0F0H ANL A, #0F0H
RET SWAP A
MOV R1, A
MOV A, R6
MOV R2, A
RET

Result:
Before Execution After Execution
30H=85 R0<= 85H
R1<= 08H
R2<= 05H

E. WALP to pack the unpack numbers.

32
Method-1: Method-2:
CPU “8051.TBL” CPU “8051.TBL”
HOF “INT8” HOF “INT8”
ORG 6000H ORG 6000H
MOV R0,30H MOV R0,30H
MOV R1,31H MOV R1,31H
MOV A, R0 MOV A, R0
MOV 0F0H, #10H SWAP A
MUL AB ADD A, R1
ADD A, R1 MOV R2, A
MOV R2, A RET
RET

Result:
Before Execution After Execution

30H=08H R0<= 08H


31H=05H R1<= 05H
R2<= 80+5= 85H

5.Conditional CALL & RETURN

33
6.Code conversion
A. WALP to convert BCD to ASCII.

34
CPU “8051.TBL”
HOF “INT8”
ORG 6000H
MOV DPTR, #5000H
MOVX A,@DPTR
CLR C
ADDC A, #30H
INC DPTR
MOVX @DPTR, A
RET

Result:
Before Execution After Execution

5000H<= 08H 5000H<= 08H

5001H<= 08+30= 38H

B. WALP to convert ASCII to BCD number.


CPU “8051.TBL”

35
HOF “INT8”
ORG 6000H
MOV DPTR, #5000H
MOVX A,@DPTR
CLR C
SUBB A, #30H
INC DPTR
MOVX @DPTR, A
RET

Result:
Before Execution After Execution

5000H<= 38H 5000H<= 38H


5001H<= 38-30= 08H

C. WALP to convert ASCII to hexadecimal.


CPU “8051.TBL”

36
HOF “INT8”
ORG 6000H
MOV R1, #50H
MOV A,@R1
CLR C
SUBB A, #41H
MOV A,@R1
JC SKIP
CLR C
SUBB A, #07H
SKIP: CLR C
SUBB A, #30H
INC R1
MOV @R1, A
RET

Result:
Before Execution After Execution
50H<= 38H 50H<= 38H
51H<= 38-30= 08H

D. WALP to convert decimal no. to hexadecimal.


CPU “8051.TBL”

37
HOF “INT8”
ORG 6000H
MOV A, 20H
ANL A, #0F0H
SWAP A
MOV 0F0H, #0AH
MUL AB
MOV R2, A
MOV A, 20H
ANL A, #0FH
ADD A, R2
MOV R0, A
RET

Result:
Before Execution After Execution

20H<= 20H 20H<= 20H


R0<= 14H

38
7.Programs to generate delay, Programs using serial port and on-
Chip timer/counter.

mov tmod,#02h
mov th0,#00h
clr P1.0
clr a
setb tr0
again: mov r7,#0ffh
loop: mov r6,#14d
wait: jnb tf0, wait
clr tf0
djnz r6,wait
djnz r7,loop
cpl P1.0
sjmp again
end

RESULT:
Accumulator A is incremented in binary from 00, 01,02…09,0A, 0B, …, 0F,
10, 11, …FF every 1 second (for 33MHz clock setting & every 3 seconds for
11.0598MHz)

39
Part B Interfacing programs.

Steps to execute interfacing programs.

1. Open programmer notepad.


2. Select new file.
3. Write program.
4. Save the file
C: \BC folder \filename.c extension.
5. Select tools  options  tools
Select scheme c\c++
Click on add
New tool properties will be displayed.
a. Type your file name (OTHER THEN FILE NAME)
b. Command  C drive \Program file\sdcc\bin\sdcc
d. folder- %d
e. parameter- “%f”<space>-o“filename.hex”
f. Click on console IO. ChooseActivate capture output.
“Use an individual output window”
Activate “clear output before running”
Click ok on outer box.
6. Now in programmer notepad, go to tools then click on tool which you
have created now.
The program is now executed & if any error in the program is displayed
rectify those errors and run again.
Minimize programmer notepad.
7. Click on flash magic icon in the desktop.

Step 1- communication
Port-com 4[ ref your system]
Baud rate: 9600
Device : 89C51RD2XX
Interface : none(ISP)
Oscillator frequency(MHZ):16

40
Step 2: Error
Click on blocks
Step 3: HEX file
Hex file ------Browse ( C:BC:Filename.Hex)
Click on start.
Do you want to continue—yes
Then blocks are erased. Finished .
After the execution white bar in kit move left.
Press reset on kit.

41
1.Interface a simple toggle switch to 8051 and
write an ALP to generate an interrupt which
switches on an LED

i. continuously as long as switch is ON

START: CPL P1.0

ACALL WAIT

SJMP START

WAIT: MOV R4,#05H

WAIT1: MOV R3,#00H

WAIT2: MOV R2,#00H

WAIT3: DJNZ R2,WAIT3

DJNZ R3,WAIT2

DJNZ R4,WAIT1

RET

42
ii. only once for a small time when the switch is turned ON

START: CPL P1.0

ACALL WAIT

CPL P1.0

CPL P1.1

ACALL WAIT

CPL P1.1

SJMP START

WAIT: MOV R4,#05H

WAIT1: MOV R3,#00H

WAIT2: MOV R2,#00H

WAIT3: DJNZ R2,WAIT3

DJNZ R3,WAIT2

DJNZ R4,WAIT1

43
RET

2. Write a C program to (i) transmit and (ii) to receive a set of characters serially
by interfacing 8051 to a terminal.
/* This program is used to send ny charater from hyperterminal and show that char on
LED's

44
It is demonstration of serial communiction */
#include <89c51rd2.H>
#define LCD_RS P3_5 // LCD RS Pin
#define LCD_E P2_7 // LCD Enable Pin
#define LCDDATA P0 // LCD Data Lines

//#include <reg51.h> /* define 8051 registers */


void wait (void) { /* wait function */
;
}

void lcdins(unsigned char b) /* initialization routine */


{
unsigned int i;
LCDDATA = b; /* send data to port0*/
LCD_RS = 0; /* clear RS */
LCD_E = 1; /* set Enable */
for ( i = 0; i < 10000; i++)
{ /* Delay for 10000
Counts */
wait();
}//end of for

LCD_E=0; /* clear Enable */


for ( i = 0; i < 10000; i++)
{ /* Delay for 10000
Counts */
wait();
}//end of for

}//end of initialization routine

void lcdchar(unsigned char c) /* routine to send data on lcd */


{
unsigned int i;
LCDDATA = c; /* send data on Port 0 */
LCD_RS = 1; /* set RS */
LCD_E = 1; /* Set Enable */

for ( i = 0; i < 10000; i++)


{ /* Delay for 10000 Counts */
wait();
}//end of for

LCD_E=0; /* Clear Enable */

for ( i = 0; i < 10000; i++)


{ /* Delay for 10000 Counts */
wait();

45
}//end of for

}//end of lcdchar routine

timerint () interrupt 3 { /* use registerbank 2 for interrupt */


unsigned char d;
unsigned int i;

//P2=0X00;
//P0=0X00;
//P1=0X00;
//P3=0X00;
lcdins(0x30); /* Output $30 as the Command */
lcdins(0x30); /* Output $30 as the Command */
lcdins(0x30); /* Output $30 as the Command */
lcdins(0x18); /* Send 8 Bit, 1 Line Instruction */
lcdins(0x0c); /* Turn the Display On */
lcdins(0x01); /* Clear the Display RAM */
lcdins(0x06); /* Set the Entry Mode */

SCON = 0x50; /* mode 1: 8-bit UART, enable receiver */


TMOD = 0x20; /* timer 1 mode 2: 8-Bit reload */
TH1 = 0xEE; /* TH1 */
TL1 = 0xEE; /* reload value 2400 baud */
TCON |= 0X40; /* timer 1 run */
PCON &= 0X7F; /* enable serial port interrupt */

while(1)
{
P3_4=0;
// lcdins(0x80); /*Move the LCD pointer to Start of Line 1*/
if (RI)
{
RI=0;
d = SBUF;
TI = 0; /* clear interrupt request flag */
SBUF = d; /* read character */
// P0 = d;
lcdins(0x80); /*Move the LCD pointer to Start of Line 1*/
lcdchar(d);
for ( i = 0; i < 30000; i++) { /* Delay for 10000 Counts */
wait();
}
for ( i = 0; i < 30000; i++) { /* Delay for 10000 Counts */
wait();
}

46
}
}
}

3. Write ALP to generate waveform using ADC Interface


; Square Wave
; This program will generate square wave on LED's.

47
CPU"8051.TBL"
HOF"INT8"
P0: EQU 80H
P1: EQU 90H
P2: EQU 0A0H
P3: EQU 0B0H
R0: EQU 00H
R1: EQU 01H
R2: EQU 02H
R3: EQU 03H
R4: EQU 04H
R5: EQU 05H
R6: EQU 06H
R7: EQU 07H
DPL: EQU 82H
DPH: EQU 83H

ORG 0000H
STRT:
MOV P2,#00H ;PORT2
MOV P1,#00H ;PORT1
MOV P3,#00H ;PORT3
MOV P0,#00H ;PORT0
OUTPUT:
MOV P0, #00H ; Send low signal On P0
LCALL DELAY ; Delay
MOV P0, #0FFH ; Send high signal On P0
LCALL DELAY
HERE: NOP
SJMP OUTPUT ; loop again

DELAY:
MOV R1,#0FFH ; delay
DLY1: MOV R2,#0FFH
DLY2: MOV R3,#10H
DLY3: NOP
DJNZ R3,DLY3
DJNZ R2,DLY2
DJNZ R1,DLY1
RET

END

4. Write ALP to interface an LCD display and to display a message


on it.
LCD Display Routine

48
This program will display letter 'A' on LCD.

CPU"8051.TBL"
HOF"INT8"
P0: EQU 80H
P1: EQU 90H
P2: EQU 0A0H
P3: EQU 0B0H
R0: EQU 00H
A: EQU 0E0H
B: EQU 0F0H
LCDRS: EQU 0B5H
LCDE: EQU 0B3H
LCDRW: EQU 0B4H
BF: EQU 087H

Count: EQU 020h ; "Dlay" Count Values


Counthi: EQU 021h

ORG 0000H

START: MOV P2,#00H ;PORT2


MOV P3,#00H ;PORT3
MOV P1,#00H ;PORT1
MOV P0,#00H ;PORT0
Acall Dlay ; Wait 100 msec
before Initializing the LCD
Acall Dlay ; Wait 100 msec before
Initializing the LCD
mov A,#030h ; Output $30 as the
Command
ACALL LCDINS
mov A,#030h ; Output $30 as the Command
ACALL LCDINS
mov A,#030h ; Output $30 as the Command
ACALL LCDINS

49
mov A,#018h ; Send 8 Bit, 1 Line Instruction
ACALL LCDINS
mov A,#0Fh ; Turn the Display On
ACALL LCDINS
mov A,#01h ; Clear the Display RAM
ACALL LCDINS
mov A,#06h ; Set the Entry Mode
ACALL LCDINS
Acall Dlay ; Wait 100 msec before Initializing
the LCD
LOOP: mov A,#080h ; Move the LCD pointer to
Start of Line 1
Acall LCDINS
MOV A,#41H ; Ascii Value of char "A"
Acall LCDCHAR ; Display it
ACALL DLAY
LOOP1: NOP
Ajmp Loop
Dlay: ; Delay 100 msecs
mov Count,#0 ; Load the 100 msec Delay
mov Counthi,#0FFH
DlayLoop: ; Loop Here for 100
msec
djnz Count,DlayLoop
djnz Counthi,DlayLoop
ret
LCDINS: ; Output the LCD
Command

CLR LCDRS
SETB LCDRW
SETB LCDE

BACK:
JB BF,BACK
ACALL DLAY150
CLR LCDRS

50
CLR LCDRW
ACALL DLAY150
MOV P0,A
ACALL DLAY150
CLR LCDE
ACALL DLAY150
MOV A,#0FFh
MOV P0,A
ACALL DLAY150
RET
LCDCHAR: ; Output the LCD Command
CLR LCDRS
SETB LCDRW ;/* set RS */
SETB LCDE ;/* Set Enable */

BK1:
JB BF,BK1
SETB LCDRS
CLR LCDRW
MOV P0,A
ACALL DLAY150
CLR LCDE
ACALL DLAY150
MOV A,#0FFh
MOV P0,A
ACALL DLAY150
RET
Dlay150: ; 150 usec Dlay
mov Count,#0FFH ; Delay 116 usecs
Dlay150Loop:
djnz Count,Dlay150Loop
ret

5.Write ALP to interface stepper motor to 8051 to rotate the motor

51
ORG 0H
MAIN:SETB P2.7
MOV A,#66H
MOV P1,A
TURN:
JNB P2.7 , CW
RR A
ACALL DELAY
MOV P2, A
SJMP TURN
CW: RL A
ACALL DELAY
MOV P1, A
SJMP TURN
DELAY:
MOV R2,#100
H1: MOV R3,#255
H2: DJNZ R3,H2
DJNZ R2,H1
RET
END

6. Write ALP to interface ADC-0804 and convert an analog input


connected to it.

52
ORG 00H

MOV P1,#11111111B // initiates P1 as the input port

MAIN: CLR P3.7 // makes CS=0

SETB P3.6 // makes RD high

CLR P3.5 // makes WR low

SETB P3.5 // low to high pulse to WR for starting


conversion

WAIT: JB P3.4,WAIT // polls until INTR=0

CLR P3.7 // ensures CS=0

CLR P3.6 // high to low pulse to RD for reading the data


from ADC

MOV A,P1 // moves the digital data to accumulator

CPL A // complements the digital data (*see the notes)

MOV P0,A // outputs the data to P0 for the LEDs

SJMP MAIN // jumps back to the MAIN program

END

53
Viva Questions
1. What do you mean by Embedded System? Give examples.
2. Why are embedded Systems useful?
3. What are the segments of Embedded System?
4. What is Embedded Controller?
5. What is Microcontroller?
6. List out the differences between Microcontroller and Microprocessor.
7. How are Microcontrollers more suitable than Microprocessor for Real Time
Applications?
8. What are the General Features of Microcontroller?
9. Explain briefly the classification of Microcontroller.
10. Explain briefly the Embedded Tools.
11. Explain the general features of 8051 Microcontroller.
12. How many pin the 8051 has?

54
13. Differentiate between Program Memory and Data Memory.
14. What is the size of the Program and Data memory?
15. Write a note on internal RAM. What is the necessity of register banks? Explain.
16. How many address lines are required to address 4K of memory? Show the
necessary calculations.
17. What is the function of accumulator?
18. What are SFR’s? Explain briefly.
19. What is the program counter? What is its use?
20. What is the size of the PC?
21. What is a stack pointer (SP)?
22. What is the size of SP?
23. What is the PSW? And briefly describe the function of its fields.
24. What is the difference between PC and DPTR?
25. What is the difference between PC and SP?
26. What is ALE? Explain the functions of the ALE in 8051.
27. Describe the 8051 oscillator and clock.
28. What are the disadvantages of the ceramic resonator?
29. What is the function of the capacitors in the oscillator circuit?
30. Show with an example, how the time taken to execute an instruction can be
calculated.
31. What is the Data Pointer register? What is its use in the 8051?
32. Explain how the 8051 implement the Harvard Architecture?
33. Explain briefly the difference between the Von Neumann and the Harvard
Architecture.
34. Describe in detail how the register banks are organized.
35. What are the bit addressable registers and what is the need?
36. What is the need for the general purpose RAM area?
37. Write a note on the Stack and the Stack Pointer.
38. Why should the stack be placed high in internal RAM?
39. Explain briefly how internal and external ROM gets accessed.
40. What are the different addressing modes supported by 8051 Microcontroller ?
41. Explain the Immediate Addressing Mode.
42. Explain the Register Addressing Mode.
43. Explain the Direct Addressing Mode.
44. Explain the Indirect Addressing Mode.
45. Explain the Code Addressing Mode.
46. Explain in detail the Functional Classification of 8051 Instruction set
47. What are the instructions used to operate stack?
48. What are Accumulator specific transfer instructions?
49. What is the difference between INC and ADD instructions?
50. What is the difference between DEC and SUBB instructions?
51. What is the use of OV flag in MUL and DIV instructions?
52. What are single and two operand instructions?
53. Explain Unconditional and Conditional JMP and CALL instructions.
54. Explain the different types of RETURN instructions.
55. What is a software delay?
56. What are the factors to be considered while deciding a software delay?
57. What is a Machine cycle?
58. What is a State?
59. Explain the need for Hardware Timers and Counters?

55
60. Give a brief introduction on Timers/Counter.
61. What is the difference between Timer and Counter operation?
62. How many Timers are there in 8051?
63. What are the three functions of Timers?
64. What are the different modes of operation of timer/counter?
65. Give a brief introduction on the various Modes.
66. What is the count rate of timer operation?
67. What is the difference between mode 0 and mode 1?
68. What is the difference Modes 0,1,2 and 3?
69. How do you differentiate between Timers and Counters?
70. Explain the function of the TMOD register and its various fields?
71. How do you control the timer/counter operation?
72. What is the function of TF0/TF1 bit
73. Explain the function of the TCON register and its various fields?
74. Explain how the Timer/Counter Interrupts work.
75. Explain how the 8051 counts using Timers and Counters.
76. Explain Counting operation in detail in the 8051.
77. Explain why there is limit to the maximum external frequency that can be
counted.
78. What’s the benefit of the auto-reload mode?
79. Write a short note on Serial and Parallel communication and highlight their
advantages and disadvantages.
80. Explain Synchronous Serial Data Communication.
81. Explain Asynchronous Serial Data Communication.
82. Explain Simplex data transmission with examples.
83. Explain Half Duplex data transmission with examples.
84. Explain Full Duplex data transmission with examples.
85. What is Baud rate?
86. What is a Modem?
87. What are the various registers and pins in the 8051 required for Serial
communication? Explain briefly.
88. Explain SCON register and the various fields.
89. Explain serial communication in general (synchronous and asynchronous). Also
90. Explain the function of the PCON register during serial data communication.

91. How the Serial data interrupts are generated?


92. How is data transmitted serially in the 8051? Explain briefly.
93. How is data received serially in the 8051? Explain briefly.
94. What are the various modes of Serial Data Transmission? Explain each mode
briefly.
95. Explain with a timing diagram the shift register mode in the 8051.
96. What is the use of the serial communication mode 0 in the 8051?
97. Explain in detail the Serial Data Mode 1 in the 8051.
98. Explain how the Baud rate is calculated for the Serial Data Mode 1.
99. How is the Baud rate for the Multiprocessor communication Mode calculated?
100. Explain in detail the Multiprocessor communication Mode in the 8051.
101. Explain the significance of the 9th bit in the Multiprocessor communication
Mode.
102. Explain the Serial data mode 3 in the 8051.
103. What are interrupts and how are they useful in Real Time Programming?

56
104. Briefly describe the Interrupt structure in the 8051.
105. Explain about vectored and non-vectored interrupts in general.
106. What are the five interrupts provided in the 8051?
107. What are the three registers that control and operate the interrupts in 8051?
108. Describe the Interrupt Enable (IE) special function register and its various
bits.
109. Describe the Interrupt Priority (IP) special function register and its need.
110. Explain in detail how the Timer Flag interrupts are generated.
111. Explain in detail how the Serial Flag interrupt is generated.
112. Explain in detail how the External Flag interrupts are generated.
113. What happens when a high logic is applied on the Reset pin?
114. Why the Reset interrupt is called a non-maskable interrupt?
115. Why do we require a reset pin?
116. How can you enable/disable some or all the interrupts?
117. Explain how interrupt priorities are set? And how interrupts that occur
simultaneously are handled.
118. What Events can trigger interrupts, and where do they go after getting
triggered?
119. What are the actions taken when an Interrupt Occurs?
110. What are Software generated interrupts and how are they generated?
111. What is RS232 and MAX232?
112. What is the function of RS and E pins in an LCD?
113. What is the use of R/W pin in an LCD?
114. What is the significance of DA instruction?
115. What is packed and unpacked BCD?
116. What is the difference between CY and OV flag?
117. When will the OV flag be set?
118. What is an ASCII code?

57

You might also like