You are on page 1of 95

MICROCONTROLLER

LABORATORY
( 18EEL57 - For V Semester EEE )

Prepared by

Mrs. D Beula
Mrs. R Subha

Department of Electrical and Electronics Engineering


Sir M. Visvesvaraya Institute of Technology
BANGALORE 562 157

For Internal Circulation Only


VISION OF SIR MVIT
➢ To be a centre of excellence in technical and management education concurrently
focusing on disciplined and integrated development of personality through quality
education, sports, cultural and co-curricular activities.

➢ To promote transformation of students into better human beings, responsible citizens


and competent professionals to serve as a valuable resource for industry, work
environment and society.

MISSION OF SIR MVIT


➢ To impart quality technical education, provide state-of-art facilities, achieve high
quality in teaching-learning & research and encourage extra & co-curricular activities.

➢ To stimulate in students a spirit of inquiry and desire to gain knowledge and skills to
meet the changing needs that can enrich their lives.

➢ To provide opportunity and resources for developing skills for employability and
entrepreneurship, nurturing leadership qualities, imbibing professional ethics and
societal commitment.

➢ To create an ambience and nurture conducive environment for dedicated and quality
staff to up-grade their knowledge & skills and disseminate the same to students on a
sustainable long term basis.

➢ Facilitate effective interaction with the industries, alumni and research institutions.

VISION OF EEE DEPARTMENT


To be a pioneer in imparting quality technical education of high standards to produce
skilled manpower with trained intelligence and emotional balance.

MISSION OF EEE DEPARTMENT


To nurture an integrated growth of talented youngsters and enrich their knowledge in
modern branches of electrical sciences and develop them into competent technocrats
and disciplined humans beneficial to global society.

2
PROGRAM OUTCOMES
Engineering Graduates will be able to:

PO1: ENGINEERING KNOWLEDGE: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering problems.
PO2: PROBLEM ANALYSIS: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics, natural
sciences, and engineering sciences.
PO3: DESIGN/DEVELOPMENT OF SOLUTIONS: Design solutions for complex engineering
problems and design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental considerations.
PO4: CONDUCT INVESTIGATIONS OF COMPLEX PROBLEMS: Use research-based knowledge
and research methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions.
PO5: MODERN TOOL USAGE: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations.
PO6: THE ENGINEER AND SOCIETY: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
PO7: ENVIRONMENT AND SUSTAINABILITY: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.
PO8: ETHICS: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the engineering practice.
PO9: INDIVIDUAL AND TEAM WORK: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
PO10: COMMUNICATION: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write effective
reports and design documentation, make effective presentations, and give and receive clear instructions.
PO11: PROJECT MANAGEMENT AND FINANCE: Demonstrate knowledge and understanding of
the engineering and management principles and apply these to one’s own work, as a member and leader in
a team, to manage projects and in multidisciplinary environments.
PO12: LIFE -LONG LEARNING: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change.

3
PROGRAM SPECIFIC OUTCOMES
Engineering Graduates will be able to:

PSO1: Comprehend the breadth and depth of electrical and electronics engineering and apply
their knowledge in the fields of power system, power electronics and drives.
PSO2: Enhance their career by adapting contemporary tools and techniques to augment
electrical and electronic systems.

PROGRAM EDUCATIONAL OBJECTIVES

PEO1: Graduates of the program will have a successful career with sound base in domain
specific engineering skills.

PEO2: Graduates of the program will be capable of succeeding in diverse engineering fields
providing innovative solution with ethical and social responsibility.
PEO3: Graduates of the program will continue to pursue professional development and
engage in life-long learning.

4
B.E ELECTRICAL AND ELECTRONICS ENGINEERING(EEE)
CHOICE BASED CREDIT SYSTEM (CBCS)
SEMESTER -V
MICROCONTROLLER LABORATORY - 1
Subject Code 18EEL57 IA Marks 40
Number of Practical Hours/Week 03 Exam Hours 03
Total Number of Practical Hours 42 Exam Marks 60
Credits - 02
Course objectives:
To explain writing assembly language programs for data transfer, arithmetic, Boolean and
logical instructions.
To explain writing assembly language programs for code conversions.
To explain writing assembly language programs using subroutines for generation of delays,
counters, configuration of SFRs for serial communication and timers.
To perform interfacing of stepper motor and dc motor for controlling the speed.
To explain generation of different waveforms using DAC interface. ∎

Sl. no Experiments

Note: For the experiments 1 to 6, 8051 assembly programming is to be used.


1 Data transfer – Program for block data movement, sorting, exchanging, finding largest element in an array.
2 Arithmetic instructions: Addition, subtraction, multiplication and division. Square and cube operations for
16 bit numbers.
3 Counters
4 Boolean and logical instructions (bit manipulation).
5 Conditional call and return instructions.
6 Code conversion programs – BCD to ASCII, ASCII to BCD, ASCII to decimal, Decimal to ASCII,
Hexa decimal to and Decimal to hexa.
7 Programs to generate delay, Programs using serial port and on-chip timer/counters.
Note: Single chip solution for interfacing 8051 is to be with C Programs for the following experiments.
8 Stepper motor interface.
9 DC motor interface for direction and speed control using PWM.
10 Alphanumerical LCD panel interface.
11 Generate different waveforms: Sine, Square, Triangular, Ramp using DAC interface.
12 External ADC and Temperature control interface.
13 Elevator interface.
Revised
Bloom’s L1 – Remembering, L2 – Understanding, L3 – Applying, L4 – Analysing, L5 – Evaluating,
Taxonomy
Level L6 – Creating.

Course outcomes:
At the end of the course the student will be able to:
Write assembly language programs for data transfer, arithmetic, Boolean and logical instructions.
Write ALP for code conversions.
Write ALP using subroutines for generation of delays, counters, configuration of SFRs for
serial communication and timers.
Perform interfacing of stepper motor and dc motor for controlling the speed.
Generate different waveforms using DAC interface.
Work with a small team to carryout experiments using microcontroller concepts and prepare reports that
present lab work. ∎

Graduate Attributes (As per NBA)


Engineering Knowledge, Problem Analysis, Individual and Team work, Modern tool usage, Communication.

5
B.E ELECTRICAL AND ELECTRONICS
ENGINEERING(EEE) CHOICE BASED CREDIT
SYSTEM (CBCS) SEMESTER -V
15EEL57 MICROCONTROLLER LABORATORY – 1(continued)
Conduct of Practical Examination:
1. All laboratory experiments are to be included for practical examination.
2. Breakup of marks and the instructions printed on the cover page of answer script to be strictly
adhered by the examiners.
3. Students can pick one experiment from the questions lot prepared by the examiners.
4. Change of experiment is allowed only once and 15% Marks allotted to the procedure part to be made zero
Learning beyond the syllabus: To acquire a wide variety of skills and to develop society friendly
applications mini projects can be practiced by referring to “Microcontroller Based Projects” Second
Edition, An EFY (Electronics For You) Enterprise Pvt Ltd, 2013.

6
18EEL57 MICROCONTROLLERS LAB
LIST OF EXPERIMENTS
S. No Experiment Page No.
CYCLE 1 - Assembly language programs with 8051
1 Data Transfer Instructions
A. Block Data Transfer Without Overlap
B. Block exchange
C. Sorting – Ascending and Descending
D. Finding Largest and Smallest number in an array
2 Arithmetic Instructions
A. Arithmetic operations
B. Finding Square of a number using LUT
C. Finding Cube of an eight bit number
3 Counters
A. Hexadecimal up counter
B. Hexadecimal down counter
C. Decimal up counter
D. Decimal down counter
E. Generation of one second delay using on chip timer
4 Boolean and Logical Instructions
A. Program illustrating bit manipulations
B. Byte level logical operations
C. Bit level logical operations
5 Code Conversion Programs
A. Decimal to hexadecimal code conversion
B. Hexadecimal to decimal code conversion
C. Decimal to ASCII code conversion
D. ASCII to decimal code conversion
E. Hexadecimal to ASCII code conversion
F. ASCII to hexadecimal code conversion
CYCLE 2 - Interfacing with 8051 (C Programming)
1 Stepper Motor Interface

7
2 DC Motor Interface
3 Alphanumeric LCD Panel Interface
4 Generation of Waveforms using DAC Interface
A. Square wave of 1KHz having 50% duty cycle.
B. Square wave of 60% duty cycle
C. Triangular wave
D. Ramp wave
E. Sine wave
5 Elevator Interface
Beyond Syllabus Experiments
1 Interfacing LCD with PIC 16F877A Microcontroller
Temperature measurement using PIC 16F877A Microcontroller
2
and Sensor

8
CYCLE 1
Assembly language programs with 8051

9
Program

Mnemonics Comments

ORG 0000H

MOV DPH,#80H

MOV R0,#35H

MOV R1,#41H

MOV R3,#0AH

LOOP: MOV DPL,R0

MOVX A,@DPTR

MOV DPL, R1

MOVX @DPTR,A

INC R0

INC R1

DJNZ R3, LOOP

END

10
1. DATA TRANSFER INSTRUCTIONS

A. BLOCK DATA TRANSFER WITHOUT OVERLAP

Aim
Write an assembly language program to transfer 10 bytes of data from location 8035h
to location 8041h (without overlap).

Algorithm
1. Initialize registers to hold count data & also the source & destination
addresses.
2. Get data from source location into accumulator and transfer to the destination
location.
3. Decrement the count register and repeat step 2 till count is zero.

Sample Input and Output

Input Before Execution Output After Execution

Address Data Address Data


8035 8041
8036 8042
8037 8043
8038 8044
8039 8045
803A 8046
803B 8047
803C 8048
803D 8049
803E 804A

Result:

11
Program

Mnemonics Comments

ORG 0000H

MOV R2,#05H

MOV R0,#30H

MOV R1,#40H

REPEAT: MOV A,@R0

XCH A,@R1

MOV @R0,A

INC R0

INC R1

DJNZ R2,REPEAT

END

12
B. BLOCK EXCHANGE

Aim
Write an assembly language program to exchange 5 bytes of data at internal RAM
locations 30h and 40h

Algorithm
1. Get the count of the number of elements in the blocks
2. Initialize pointer1 to point to the starting address of the block1
3. Initialize pointer2 to point to the starting address of the block2
4. Exchange the data pointed to by pointer1 and the data pointed to by pointer2
through a temporary variable
5. Increment pointer1 and pointer2. Decrement count. Go to step 4 if count is not
equal to 0.

Sample Input and Output

Input before Execution Output after Execution

Address Data Address Data Address Data Address Data


30 40 30 40
31 41 31 41
32 42 32 42
33 43 33 43
34 44 34 44

Result:

13
Program

Mnemonics Comments

ORG 0000H

MOV R0,#05

L1: MOV DPTR, #9000H

MOV A,R0

MOV R1,A

L2: MOVX A, @DPTR

MOV B, A

INC DPTR

MOVX A,@DPTR

CLR C

MOV R2, A

SUBB A, B

JNC L3 JC for Descending order

MOV A,B

MOVX @DPTR,A

DEC DPL

14
C. SORTING – ASCENDING AND DESCENDING

Aim
Write an assembly language program to sort an array of n = 6 bytes of data in
ascending and descending order stored from location 9000h (Use bubble sort
Algorithm).

Algorithm
1. Store the elements of the array from the address 9000h
2. Initialize a pass counter with array size-1 count (for number of passes).
3. Load compare counter with pass counter contents & initialize DPTR to point
to the start address of the array (here 9000h).
4. Store the current and the next array elements pointed by DPTR in registers B
and r2 respectively.
5. Subtract the next element from the current element.
6. If the carry flag is set (for ascending order) then exchange the 2 numbers in
the array.
7. Decrement the compare counter and repeat through step 4 until the counter
becomes 0.
8. Decrement the pass counter and repeat through step 3 until the counter
becomes 0.

Sample Input and Output

Ascending Order
Input Before Execution

Address 9000 9001 9002 9003 9004 9005


Data

Output After Execution

Address 9000 9001 9002 9003 9004 9005


Data

15
Mnemonics Comments

MOV A,R2

MOVX @DPTR,A

INC DPTR

L3: DJNZ R1,L2

DJNZ R0,L1

END

16
Descending Order
Input Before Execution

Address 9000 9001 9002 9003 9004 9005


Data

Output After Execution

Address 9000 9001 9002 9003 9004 9005


Data

Result:

17
Program

Mnemonics Comments

ORG 0000H

MOV R3,#05

MOV DPTR,#9100H

MOVX A,@DPTR

MOV R1,A

L1: INC DPTR

MOVX A,@DPTR

CLR C

MOV R2,A

SUBB A,R1

JC SKIP JNC for Smallest Number

MOV A,R2

MOV R1,A

SKIP: DJNZ R3,L1

MOV DPL, #0AH

18
D. FINDING LARGEST AND SMALLEST NUMBER IN AN ARRAY

Aim
Write an assembly language program to find the largest element in a given string of 6
bytes at location 9100h. Store the largest element at location 910Ah.

Algorithm
1. Store the elements of the array from the address 9100h
2. Store the length of the array in r3 and set it as counter.
3. DPTR is loaded with starting address of the array.
4. Store the first number of the array in r1 (r1 is assigned to hold the largest
number).
5. Increment DPTR.
6. Subtract the number pointed by DPTR from the contents of r1 (to compare
whether the next array element is larger than the one in r1).
7. If the element pointed by DPTR is larger then load the larger number into r1.
8. Decrement the counter and repeat steps through 5 until the counter becomes 0.
9. Store the largest number in r1 in address 910Ah

Sample Input and Output

Largest Number
Input before execution

Address 9100 9101 9102 9103 9104 9105


Data

Output after Execution

Address 910A
Data

19
Mnemonics Comments

MOV A,R1

MOVX @DPTR,A

END

20
Smallest Number
Input before execution

Address 9100 9101 9102 9103 9104 9105


Data

Output after Execution

Address 910A
Data

Result:

21
Program

Mnemonics Comments

ORG 0000H

MOV R1,40H

MOV A,41H

MOV B,42H

CJNE R1,#00,CKSUB

ADD A,B

MOV B,#00

JNC SKIP

MOV B,#01H

SKIP: SJMP LAST

CKSUB: CJNE R1,#01, CKMUL

CLR C

SUBB A,B

MOV B,#00

JNC SKIP1

MOV B,#0FFH

22
2. ARITHMETIC INSTRUCTIONS

A. ARITHMETIC OPERATIONS

Aim
Write an ALP to perform the following: If x=0 perform addition(a+b); else if x=1
perform subtraction (a-b); else if x=2 perform multiplication (a*b); else if x=3
perform division (a/b), where a & b are eight bit numbers. Condition to perform the
specific operation and the two bit numbers are stored at RAM location starting from
40h.

Algorithm
1. Store the condition x in r1.
2. Load the first and second numbers to A and B registers respectively
3. Compare the contents of r1 and perform the operations add, sub, etc
accordingly.
4. Store the result present in A and B registers to the appropriate memory
locations.

Sample Input and Output


Addition (without carry) Addition (without carry)
Input before Output after Input before Output after
execution execution execution execution
40h 43h (sum) 40h 43h (sum)
41h 44h (carry) 41h 44h (carry)
42h 42h

Subtraction (without Borrow) Subtraction (with Borrow)


Input before Output after Input before
Output after execution
execution execution execution
40h 43h (Difference) 40h 43h (Difference)
41h 44h (Borrow) 41h 44h (Borrow)
42h 42h

23
Mnemonics Comments

SKIP1: SJMP LAST

CKMUL: CJNE R1,#02,CKDIV

MUL AB

SJMP LAST

CKDIV: CJNE R1,#03,OTHER

DIV AB

SJMP LAST

OTHER: MOV A,#00

MOV B,#00

LAST: MOV 43H,A

MOV 44H,B

END

24
Multiplication (without higher byte) Multiplication (with higher byte)
Input before Output after Input before Output after
execution execution execution execution
40h 43h (LB) 40h 43h (LB)
41h 44h (HB) 41h 44h (HB)
42h 42h

Division (without Remainder) Division (with Remainder)


Input before Output after Input before
Output after execution
execution execution execution
40h 43h (Quotient) 40h 43h (Quotient)
41h 44h (Remainder) 41h 44h (Remainder)
42h 42h

Result:

25
Program

Mnemonics Comments

ORG 0000H

MOV DPTR,#9000H

MOVX A,@DPTR

MOV DPTR,#8050H

MOVC A,@A+DPTR

MOV DPTR,#9001H

MOVX @DPTR,A

ORG 0050H

DB 00H

DB 01H

DB 04H

DB 09H

DB 10H

DB 19H

DB 24H

26
B. FINDING SQUARE OF A NUMBER USING LUT

Aim
Write an ALP to compute the square of an eight bit number(00h to 0Fh) stored in
external memory location 9000h. Store the result at location 9001h.

Algorithm
1. Store the number in A from the address 9000h
2. Initialize the DPTR to point to the starting of the LUT
3. Fetch the square from the LUT address DPTR+A
4. Store the result in location 9001

Sample Input and Output

Sample 1:
Input before execution Output after Execution

Address 9000 Address 9001


Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address 9001


Data Data

27
Mnemonics Comments

DB 31H

DB 40H

DB 51H

DB 64H

DB 79H

DB 90H

DB 0A9H

DB 0C4H

DB 0E1H

END

28
Result:

29
Program

Mnemonics Comments

ORG 0000H

MOV DPTR,#9000H

MOVX A,@DPTR

MOV R0,A

MOV B,A

MUL AB

PUSH B

MOV B,A

MOV A,R0

MUL AB

INC DPTR

MOVX @DPTR,A

MOV A,B

MOV R2,A

POP B

MOV A,R0

30
C. FINDING CUBE OF AN EIGHT BIT NUMBER

Aim
Write an ALP to compute the cube of an eight bit number stored in external
memory location 9000h. Store the result at locations 9001, 9002 and 9003h.

Algorithm
1. Store the eight bit number x in A, R0 & B registers.
2. Multiply A and B registers to obtain the 16 bit square(SQH and SQL)
3. The high part of the square result (SQH) is stored on the stack.
4. Multiply the low part of the square result (SQL) with x (partial cube result).
5. Store the low part of the above result at 9001h & the high part in R2.
6. Retrieve the high part of the square result (SQH) stored on the stack &
multiply with X.
7. Add the low part of the above result (SQH*X) with R2 and store in 9002h.
8. Add the high part (SQH*X) with the resulting carry and store in 9003.

Sample Input and Output

Sample 1:
Input before execution Output after Execution

Address 9000 Address 9001 9002 9003


Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address 9001 9002 9003


Data Data

31
Mnemonics Comments

MUL AB

ADD A,R2

LAST: INC DPTR

MOVX @DPTR,A

MOV A,B

INC DPTR

MOVX @DPTR,A

END

32
Result:

33
Program

Mnemonics Comments

ORG 0000H

MOV A,#00H

BACK: MOV P0,A

ACALL DELAY

INC A

JNZ BACK

DELAY: MOV R1,#0FFH

LOOP1: MOV R2,#0FFH

LOOP: DJNZ R2,LOOP

DJNZ R1,LOOP1

RET

END

34
3. COUNTERS

A. HEXADECIMAL UP COUNTER

Aim
Write an ALP for eight bit hexadecimal up counter and display the count value in
Port0.

Algorithm
1. Move 00 to A register
2. Call the display subroutine to display in the data field.
3. Call the delay subroutine, in delay program move FFH to registers r1 & r2
loop and decrement until 0.
4. Increment A register
5. Go to step 2 till A=00.

Result:

35
Program

Mnemonics Comments

ORG 0000H

MOV A,#0FFH

BACK: MOV P0,A

DEC A

ACALL DELAY

JNZ BACK

DELAY: MOV R1,#0FFH

LOOP1: MOV R2,#0FFH

LOOP: DJNZ R2,LOOP

DJNZ R1,LOOP1

RET

END

36
B. HEXADECIMAL DOWN COUNTER

Aim
Write an ALP for eight bit hexadecimal down counter and display the count value in
Port0.

Algorithm
1. Move 00 to A register
2. Decrement A register
3. Call the display subroutine to display in the data field.
4. Call the delay subroutine, in delay program move FFH to registers r1 & r2
loop and decrement until 0.
5. Go to step 2 till A=00.

Result:

37
Program

Mnemonics Comments

ORG 0000H

MOV A,#00H

BACK: MOV P0,A

ACALL DELAY

ADD A,#01H

DA A

JNZ BACK

DELAY: MOV R1,#0FFH

LOOP1: MOV R2,#0FFH

LOOP: DJNZ R2,LOOP

DJNZ R1,LOOP1

RET

END

38
C. DECIMAL UP COUNTER

Aim
Write an ALP for decimal up counter and display the count value in Port0.

Algorithm

1. Move 00 to A register
2. Call the display subroutine to display in the data field.
3. Call the delay subroutine, in delay program move FFH to registers r1 & r2
loop and decrement until 0.
4. Add 1 to accumulator and decimal adjust accumulator to convert the sum to
decimal.
5. Go to step 2 till A=00.

Result:

39
Program

Mnemonics Comments

ORG 0000H

MOV A,#99H

BACK: MOV P0,A

ACALL DELAY

ADD A,#99H

DA A

JC BACK

DELAY: MOV R1,#0FFH

LOOP1: MOV R2,#0FFH

LOOP: DJNZ R2,LOOP

DJNZ R1,LOOP1

RET

END

40
D. DECIMAL DOWN COUNTER

Aim
Write an ALP for decimal down counter and display the count value in Port0.

Algorithm

1. Move 99 to A register
2. Call the display subroutine to display in the data field.
3. Call the delay subroutine, in delay program move FFH to registers r1 & r2
loop and decrement until 0.
4. Add 99 to accumulator (subtraction by 1) and decimal adjust accumulator to
convert the sum to decimal.
5. Go to step 2 till A=00.

Result:

41
Program

Mnemonics Comments

ORG 0000H

MOV TMOD,#01H

MOV A,#00H

L2: MOV P0,A

MOV R1,#0EH

L1: SETB TR0

WAIT: JNB TF0,WAIT

CLR TF0

CLR TR0

DJNZ R1,L1

INC A

JNZ L2

END

42
E. GENERATION OF ONE SECOND DELAY USING ONCHIP TIMER

Aim: Write an ALP to generate a 1 second delay continuously using the on chip
timer.

Algorithm

1. Move 00 to A register
2. Set up timer0 in mode 1 operation
4. Load register R1 with 0Eh
5. Display A in the data field
6. start the timer and wait for overflow
6. decrement R1 and go to step 5 till R1 becomes zero.
7. Increment A register and go to step 4 till A becomes 00.

Timer Calculations:
Timer 1 is configured in mode 1
Maximum overflow time = maximum count x12/oscillator frequency
= 65536 x 12/(11.0596x106)
= 0.071 Sec
To get a delay of 1 sec, the timer must overflow 14 times

Result:

43
Program

Mnemonics Comments

ORG 0000H

MOV A, 20H

MOV R1,A

MOV A,21H

CJNE R1,#0,CKOR

ANL A, 22H

SJMP END1

CKOR: CJNE R1,#01,CKXOR

ORL A, 22H

SJMP END1

CKXOR: CJNE R1,#02,OTHER

XRL A, 22H

SJMP END1

OTHER: CLR A

END1: MOV 23H,A

END

44
4. BOOLEAN AND LOGICAL INSTRUCTIONS

A. BYTE LEVEL LOGICAL OPERATIONS

Aim: Write an ALP to compute the following.


IF X=0; THEN RES=NUM1 (AND) NUM2,
IF X=1; THEN RES=NUM1 (OR) NUM2,
IF X=2; THEN RES=NUM1 (XOR) NUM2,
ELSE RES =00,
RES is 23h Location. Three eight bit numbers X, NUM1 & NUM2 are stored in
internal data RAM locations 20h, 21h & 22h respectively.

Algorithm
1. Point to the data RAM register 20h and store the condition x.
2. Point to 21h and 22h and move the first number to A register.
3. Compare the contents of r1 and perform the operations accordingly.
4. The result will be stored in 23H register.

Sample Input and Output

AND Operation
Input before execution Output after Execution

Address 20H 21H 22H Address 23H


Data Data

OR Operation
Input before execution Output after Execution

Address 20H 21H 22H Address 23H


Data Data

45
46
XOR Operation
Input before execution Output after Execution

Address 20H 21H 22H Address 23H


Data Data

Result:

47
Program

Mnemonics Comments

ORG 0000H

MOV R0,20H

CJNE R0,#0,CK1

MOV C,08H

ANL C,10H

SJMP LAST

CK1: CJNE R0,#1,CK2

MOV C,0FH

ORL C,17H

SJMP LAST

CK2: CJNE R0,#2,CK3

CPL 0FH

MOV C,0FH

SJMP LAST

CK3: CLR C

LAST: MOV 1FH,C

END

48
B. BIT LEVEL LOGICAL OPERATIONS

Aim: Write an ALP to compute the following:


IF X=0; THEN RES=LSB OF NUM1 (AND) LSB OF NUM2,
IF X=1; THEN RES=MSB OF NUM1 (OR) MSB OF NUM2,
IF X=2; THEN RES=COMPLEMENT OF MSB OF NUM1
STORE THE BIT RESULT IN MSB OF 23H.
Three eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM
locations 20h, 21h & 22H respectively.

Algorithm
1. Move the condition X (from 20h location) into R0 register.
2. If X=0; then move LSB bit of 21h to carry flag and ‘AND’ Carry flag with
LSB bit of 22h. Go to step5
3. If X=1; then move MSB bit of 21h to carry flag and ‘OR’ Carry flag with
MSB bit of 22h. Go to step5
4. If X=0; then complement MSB bit of 21h and move it to carry flag. Go to
step5
5. Store Carry flag at MSB bit of 23h location.

Sample Input and Output

AND Operation
Input before execution Output after Execution

Address 20H 21H 22H Address 23H


Data Data

OR Operation
Input before execution Output after Execution

Address 20H 21H 22H Address 23H


Data Data

49
50
NOT Operation
Input before execution Output after Execution

Address 20H 21H 22H Address 23H


Data Data

Result:

51
Program

MNEMONICS COMMENTS

ORG 0000H

MOV DPTR,#9000H

MOVX A,@DPTR

MOV R0,A

INC DPTR

MOVX A,@DPTR

CLR C

SUBB A,R0

JZ EQUAL

JNC BIG

SETB 7FH

SJMP END1

BIG: SETB 78H

SJMP END1

52
C. PROGRAM ILLUSTRATING BIT MANIPULATIONS

Aim : Write an ALP to compare the 2 nos. Reflect your result as:
If NUMI<NUM2, SET LSB of data RAM 2F (bit address 78H)
IF NUM1>NUM2, SET MSB OF 2F(7FH).
If NUM1 = NUM2 Clear both LSB & MSB of bit addressable memory
location 2Fh. Two eight bit numbers NUM1 & NUM2 are stored in external memory
locations 9000h & 9001h respectively.

Algorithm
1. Store the elements of the array from the address 9000h
2. Move the first number in r0 and the second number in register A respectively
3. Clear carry flag and subtract the two numbers, if the carry flag is 0(if the nos
are equal), Clear both LSB & MSB of bit addressable memory location 2Fh.

Sample Input and Output

NUM1 > NUM2


Input before execution Output after Execution

Address 9000 9001 Address 2FH


Data Data

NUM1 < NUM2


Input before execution Output after Execution

Address 9000 9001 Address 2FH


Data Data

53
MNEMONICS COMMENTS

EQUAL: CLR 78H

CLR 7FH

END1: NOP

END

54
NUM1 = NUM2
Input before execution Output after Execution

Address 9000 9001 Address 2FH


Data Data

Result:

55
Program

Mnemonics Comments

ORG 0000H

MOV DPTR, #9000H

MOVX A,@DPTR

ANL A,#0FH

MOV R0,A

MOVX A,@DPTR

ANL A,#0F0H

SWAP A

MOV B,#0AH

MUL AB

ADD A,R0

MOV P1,A

END

56
5. CODE CONVERSION PROGRAMS

A. DECIMAL TO HEXADECIMAL CODE CONVERSION

Aim: Write an ALP to convert a decimal number to its equivalent hexa-decimal


number and display the result in the Port1.

Algorithm
1. Get the decimal number in the range 00 to 99 as input
2. Separate the higher and lower nibble of the two digit number
3. Bring the higher nibble to the ones position and multiply it by 0Ah
4. Add the result to the lower nibble and store the result

Sample Input and Output

Sample 1:
Input before execution Output after Execution

Address 9000 Address Port1


Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address Port1


Data Data

Result:

57
Program

Mnemonics Comments

ORG 0000H

MOV DPTR,#9000H

MOVX A,@DPTR

MOV B,#64H

DIV AB

MOV P1,A

MOV A,B

MOV B,#0AH

DIV AB

SWAP A

ADD A,B

MOV P2,A

END

58
B. HEXADECIMAL TO DECIMAL CODE CONVERSION

Aim: Write an ALP to convert a hexa-decimal number to its equivalent decimal


number and display the result in the Port1 and Port 2.

Algorithm
1. Get the hex number in the range 00 to FFh as input
2. Divide the number by 64h and store the quotient
3. Divide the remainder by 0Ah and store the quotient and remainder
4. Bring the quotient to the tens position and add the remainder to it.
5. The quotient from the first division and the result of step4 form the 16-bit
decimal number

Sample Input and Output

Sample 1:
Input before execution Output after Execution

Address 9000 Address P2 P1

Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address P2 P1

Data Data

Result:

59
Program

Mnemonics Comments

ORG 0000H

MOV DPTR,#9000H

MOVX A,@DPTR

ANL A,#0FH

ADD A,#30H

MOV P1,A

MOVX A,@DPTR

ANL A,#0F0H

SWAP A

ADD A,#30H

MOV P2,A

END

60
C. DECIMAL TO ASCII CODE CONVERSION

Aim: Write an ALP to convert a decimal number to its equivalent Ascii code and
display the result in the Port1 and Port2.

Algorithm
1. Get the decimal number in the range 00 to 99 as input
2. Separate the higher and lower nibble of the two digit number
3. Add 30h to the lower nibble and store the result
4. Bring the higher nibble to the ones position, add 30h to it and display the
result.

Sample Input and Output

Sample 1:
Input before execution Output after Execution

Address 9000 Address P2 P1

Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address P2 P1

Data Data

Result:

61
Program

Mnemonics Comments

ORG 0000H

MOV DPTR,#9000H

MOVX A,@DPTR

CLR C

SUBB A,#30H

MOV P1,A

END

62
D. ASCII TO DECIMAL CODE CONVERSION

Aim: Write an ALP to convert a Ascii to its equivalent decimal number and display
the result in the Port1.

Algorithm
1. Get the Ascii code in the range 30 to 39 as input
2. Clear carry bit
3. Subtract with borrow 30h from the input and display the result

Sample Input and Output

Sample 1:
Input before execution Output after Execution

Address 9000 Address P1


Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address P1


Data Data

Result:

63
Program

Mnemonics Comments

ORG 0000H

MOV DPTR,#9000H

MOVX A,@DPTR

ANL A,#0FH

ACALL SUB

MOV P1,A

MOVX A,@DPTR

ANL A,#0F0H

SWAP A

ACALL SUB

MOV P2,A

SJMP LAST

64
E. HEXADECIMAL TO ASCII CODE CONVERSION

Aim: Write an ALP to to convert a 8bit Hexa-decimal number into ASCII Code and
store the result in the Port1 and Port2.

Algorithm
1. Move the hexadecimal data to be converted to accumulator.
2. Get the lower nibble & call ASCII subroutine
3. Store the converted ASCII value
4. Get the higher nibble & call ASCII routine
5. Store the converted ASCII value
ASCII subroutine
1. If digit greater than 09,(for A-F) add 07h & 30h
2. Else (i.e., for 0-9) add only 30h

Sample Input and Output

Sample 1:
Input before execution Output after Execution

Address 9000 Address P2 P1

Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address P2 P1

Data Data

65
Mnemonics Comments

SUB: CLR C

CJNE A,#0AH,LOOP

SJMP LOOP2

LOOP: JC LOOP1

LOOP2: ADD A,#07H

LOOP1: ADD A,#30H

RET

LAST: NOP

END

66
Result:

67
Program

Mnemonics Comments

ORG 0000H

MOV DPTR,#9000H

MOVX A,@DPTR

CLR C

CJNE A,#41H,LOOP

SJMP LOOP1

LOOP: JC SKIP

CLR C

LOOP1: SUBB A,#07H

SKIP: CLR C

SUBB A,#30H

MOV P1,A

END

68
F. ASCII TO HEXADECIMAL CODE CONVERSION

Aim: Write an ALP to convert a 8bit ASCII Code into Hexa-decimal number and
store the result in the Port1.

Algorithm
1. Move the ASCII character to be converted to accumulator.
2. If character is greater than 41h,(for A-F), then subtract 07h & 30h
3. Else (i.e., for 0-9) subtract only 30h
4. Display the converted hexadecimal number into data field .

Sample Input and Output

Sample 1:
Input before execution Output after Execution

Address 9000 Address P1


Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address P1


Data Data

Result:

69
70
CYCLE 2
Interfacing with 8051 (C Programming)

71
Program

#include <Reg51.h>
#define phasea 0x0D
#define phaseb 0x0E
#define phasec 0x07
#define phased 0x0B
void delay(unsigned int);
void main( )
{
P2=0x00;
while(1)
{
P2=phasea;
delay(5000);
P2=phaseb;
delay(5000);
P2=phasec;
delay(5000);
P2=phased;
delay(5000);
}
}

void delay(unsigned int x)


{
for(;x>0;x--);
}

Note :
1. For changing the speed change the delay time
2. For changing the direction, change the excitation sequence order

72
1. STEPPER MOTOR INTERFACE

Aim: To interface a stepper motor and to write a C program to control its speed and
direction of rotation.

Hardware interfacing
• Stepper motor unlike DC motor rotates in steps.
• Stepper motor has 4 coils which forms the stator and a central rotor.
• Rotation depends on excitation of stator coils.

PS
PS

8051µC

P2 Stepper
Motor Stepper
FRC 26pin
Interface Motor
Cable
Card

Algorithm
• Configure P2 as output.
• For anticlockwise motion excitation sequence to motor coils are 0B,07,0E,0D.
Output through P2 with delay.
• For clockwise motion excitation sequence to motor coils are 0D,0E,07.0B.
Output through P2 with delay.

Result:

73
Program

#include<reg51.h>
sbit ibit=P2^0;
void msdelay(unsigned int value);
void main( )
{
ibit=0;
while(1)
{
ibit=0;
msdelay(25)
ibit=1;
msdelay(75)
}
}

void msdelay(unsigned int value)


{
unsigned char x,y;
for(x=0;x<400;x++)
for(y=0;y<value;y++);
}

Note: To change the speed of the DC motor, change the ON time and OFF time
of the PWM pulse.

74
2. DC MOTOR INTERFACE

Aim: To interface a DC motor and to write a C program to control its speed using
PWM.

Algorithm
1. Configure P2 bit 0 as output pin
2. Output value 00 through P2.1.Call delay to remain ON for 25ms.
3. Output value 01 through P2.1. Call delay to remain 75ms
4. Repeat step 2&3 continuously.

Result:

75
Program

#include<reg51.h>
sbit rs = P2^4; //register select rs=0
sbit rw = P2^5; //read/write
sbit en = P2^6; //enable pin
void lcdcmd(unsigned char);
void lcddata();
void tdelay(unsigned int);
unsigned char msg[16]={"ELECTRICAL DEPT."};
unsigned char x,temp,value;
void main()
{
lcdcmd(0x38); //initialize lcd lines 5x7 matrix
lcdcmd(0x0e); //display on and cursor on
lcdcmd(0x06); //shift cursor right
lcdcmd(0x01); //clear display
lcdcmd(0x80); //starting address of line1
tdelay(5);

for(x=0;x<8;x++)
{
temp=msg[x];
lcddata ();
}
lcdcmd(0xC0); //starting address of line2
for(x=8;x<16;x++)
{
temp=msg[x];
lcddata();
}
}

76
3. ALPHANUMERIC LCD PANEL INTERFACE

Aim: To interface an alphanumeric LCD panel and to write a C Program to send a


message to the LCD.

LCD details:
The LCD used is an alphanumeric 16*1 LCD.
P0.0-P0.7 are 8 bit data lines.
P2.4, P2.5 & P2.6 are RS, R/W and Enable pins
Character construction is 5*7dots.

LCD Pin Diagram

77
void lcdcmd (unsigned char value)
{
P0=value;
rs=0; //select instruction register
rw=0; //write
en=1;
tdelay(1);
en=0;
}
void lcddata (void)
{
P0=temp;
rs=1; //select data register
rw=0; //write
en=1;
tdelay(1);
en=0;
}

void tdelay(unsigned int item)


{
unsigned int i,j;
for (i=0;i<item;i++)
for(j=0;j<900;j++);
}

78
LCD Interfacing with 8051

Result:

79
80
5. GENERATION OF WAVEFORMS USING DAC INTERFACE

Aim: To write a C program to generate the following waveforms. Interface DAC and
observe the waveform on CRO
A) Square wave of 1KHz having 50% duty cycle.
B) Square wave of 60% duty cycle
C) Triangular wave
D) Ramp wave
E) Sine wave

Interfacing Diagram

8 CRO
0 Dual
5 P0 Xout
DAC
1 P1
μC

81
Program

#include <Reg51.h>
void tdelay( );
void main( )
{
while(1)
{
P0=0xFF;
tdelay( );
P0=0x00;
tdelay( );
}
}
void tdelay()
{
unsigned int i;
for(i=0;i<450;i++);
}

82
A. Square wave of 1 kHz Frequency and 50% Duty Cycle

Algorithm
1. Configure P0 as output port (Output the initial value 00 through P0).
2. Output value FFh(on) through P0.Call delay to remain ON for .5ms. (ie.count
450 in the for loop)
3. Output value 00h(off) through P0. Call delay to remain ON for .5ms. (ie.count
450 in the for loop)
4. Repeat step 2&3 continuously.
5. Note the waveform on CRO.

83
Program

#include <Reg51.h>
void tdelay( );
void main( )
{
while(1)
{
P0=0xFF;
tdelay( );
tdelay( );
tdelay( );
P0=0x00;
tdelay( );
tdelay( );
}
}
void tdelay( )
{
unsigned int i;
for(i=0;i<450;i++);
}

84
B. Square wave of 60% Duty Cycle

Algorithm
1. Configure P0 as output port (Output the initial value 00 through P0).
2. Output value FFh(on) through P0.Call delay three times
3. Output value 00h(off) through P0. Call delay two times.
4. Repeat step 2&3 continuously.
5. Note the waveform on CRO.

85
Program

#include <Reg51.h>
void main( )
{
unsigned char i ;
while(1)
{
for(i=0;i<0xFF;i++)
P0 = i ;
for(i=0xFE;i>0x00;i--)
P0 = i ;
}
}

86
C. Triangular Waveform

Algorithm
1. Output the initial value 00 through P0.
2. Increment it in steps of 1 until a count value of FFh (5V) is reached. Every
time output the value through P0.
3. Decrement it in steps of 1 until a zero value is reached. Every time output the
value through P0.
4. Repeat step 2&3 continuously.

87
Program

#include <Reg51.h>
void main( )
{
unsigned char i ;
while(1)
{
for(i=0;i<0xFF;i++)
P0 = i ;
}
}

88
D. Ramp Waveform

Algorithm
1. Output the initial value 00 through P0.
2. Increment it in steps of 1 until a count value of FFh (5V) is reached. .
Every time output the value through P0.
3. Repeat step 2 continuously.

89
Program

#include <Reg51.h>
void main( )
{
static int a[25]={128,161,

95,128}
;
unsigned char i ;
while(1)
{
for(i=0;i<25;i++)
P0 = a[i] ;
}
}

90
E. Sine Waveform

Algorithm
1. Compute different step values (θ = 0o,15o,30…360) using the equation
128+128 sin . Output the values through P0.
2. More the steps smoother will be sine wave.

Result:

91
Program

#include<reg51.h>
void delay_ms(unsigned int);
unsigned char reqflr,curflr,i,flr;
unsigned int r1;
void main()
{
P1=0xff;
P0=0x0F;
curflr=0x00;
while(1)
{
reqflr=P1 & 0x0f;
switch(reqflr)
{
case 0x07:
flr=0x09;
break;
case 0x0b:
flr=0x06;
break;
case 0x0d:
flr=0x03;
break;
case 0x0e:
flr=0x00;
break;
}
if(curflr<flr)
{
for(i=curflr;i<flr;i++)

92
5. ELEVATOR INTERFACE

Aim: To interface an elevator and to write a C Program to control its movement.

Hardware Details
The control and operation of an elevator functioning within a building, having four
floors is simulated. A key and a corresponding LED indicator serve as a request
button and request status indicator. The green LED’s when lit indicate that the
elevator is at a floor while the amber LED’s goes on, indicating the elevator to be in
between 2 floors. The serving of a request can be indicated by clearing the request. A
four line to ten line decoder (7442) is used to specify the location of the elevator
within the shaft. Two dual D flip-flops are used to sense requests and output the
status.
The connection diagram for the elevator control is as shown.

93
{
P0=0xf0|i;
delay_ms(25000);
}
}
else
{
for(i=curflr;i>flr;i--)
{
P0=0xf0|i;
delay_ms(25000);
}
}
curflr=flr;
switch(flr)
{
case 0x00:
P0=0xe0;
break;
case 0x03:
P0=0xd3;
break;
case 0x06:
P0=0xb6;
break;
case 0x09:
P0=0x79;
break;
}
}
}
void delay_ms(unsigned int r)
{
for(r1=0;r1<=r;r1++); }

94
Result:

95

You might also like