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

MICROCONTROLLER

LABORATORY
( BEE403 - For IV Semester EEE )

Prepared by

Mrs. Rekha Radhakrishnan

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 a sound base in domain-
specific engineering skills.

PEO2: Graduates of the program will be capable of succeeding in diverse engineering fields
providing innovative solutions 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 BEE403 CIE Marks 25
Number of Practical Hours/Week 02 Observation + Record 15
Test 5
Credits - 01

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 Arithmetic instructions: Addition, subtraction, multiplication and division. Square using Matlab/Simulink
2 Data transfer – Program for block data movement, sorting, exchanging, finding largest element in an array.
3 Up/down BCD and binary counters
4 Boolean and logical instructions (bit manipulation).
5 Code conversion programs – BCD to ASCII, ASCII to BCD, ASCII to decimal, Decimal to ASCII,
Hexa decimal to and Decimal to hexa.
6 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 Simulate and test a PWM-controlled DC motor using Simscape.
9 Stepper motor interface for direction and speed control

10 Alphanumerical LCD panel interface.


11 Generate different waveforms: Sine, Square, Triangular, Ramp using DAC 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
CIE for the practical component of IPC
CIE for the practical component of the IPCC
 15 marks for the conduction of the experiment and preparation of laboratory record, and 10
marks for the test to be conducted after the completion of all the laboratory sessions.
 On completion of every experiment/program in the laboratory, the students shall be evaluated
including viva-voce and marks shall be awarded on the same day.
 The CIE marks awarded in the case of the Practical component shall be based on the
continuous evaluation of the laboratory report. Each experiment report can be evaluated for 10
marks. Marks of all experiments’ write-ups are added and scaled down to 15 marks.
 The laboratory test (duration 02/03 hours) after completion of all the experiments shall be
conducted for 50 marks and scaled down to 10 marks.
 Scaled-down marks of write-up evaluations and tests added will be CIE marks for the
laboratory component of IPCC for 25 marks.
 The student has to secure 40% of 25 marks to qualify in the CIE of the practical component of
the IPCC.

6
SIR M VISVESVARAYA INSTITUTE OF TECHNOLGY, BENGALURU-562157

DEPARTMENT OF ELECTRICAL & ELECTRONICS ENGINEERING


SCHEME: Name of the course: Subject Code: AY: 2023-2024
2022 Microcontroller BEE403 EVEN SEM

COURSE OUTCOME

At the end of the course, students will be able to

Apply the fundamentals of electronic circuits to differentiate between


CO1
the different architectures of microcontrollers and processors

Be able to analyze and develop programs in assembly language and C


CO2
to solve engineering problems

CO3 Design applications using on chip components.

Develop solutions to real world problems by interfacing off chip


CO4
peripherals

7
8
BEE403 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
3 Counters
A. Hexadecimal up counter
B. Hexadecimal down counter
C. Decimal up counter
D. Decimal down counter
Programs to generate delay, Programs using serial port and on-
4
chip timer/counters.
5 Boolean and Logical Instructions
A. Program illustrating bit manipulations
B. Byte level logical operations
C. Bit level logical operations
6 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

9
CYCLE 2 - Interfacing with 8051 (C Programming)
1 Simulate and test a PWM controlled DC motor using Simscape
2 Stepper 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)

10
CYCLE 1
Assembly language programs with 8051

11
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

12
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:

13
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

14
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:

15
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

16
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

17
Mnemonics Comments

MOV A,R2

MOVX @DPTR,A

INC DPTR

L3: DJNZ R1,L2

DJNZ R0,L1

END

18
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:

19
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

20
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

21
Mnemonics Comments

MOV A,R1

MOVX @DPTR,A

END

22
Smallest Number
Input before execution

Address 9100 9101 9102 9103 9104 9105


Data

Output after Execution

Address 910A
Data

Result:

23
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

24
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

25
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

26
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:

27
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

28
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

29
Mnemonics Comments

DB 31H

DB 40H

DB 51H

DB 64H

DB 79H

DB 90H

DB 0A9H

DB 0C4H

DB 0E1H

END

30
Result:

31
3. COUNTERS

A. HEXADECIMAL UP COUNTER

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

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:

32
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

33
B. HEXADECIMAL DOWN COUNTER

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

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:

34
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

35
C. DECIMAL UP COUNTER

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

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:

36
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

37
D. DECIMAL DOWN COUNTER

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

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:

38
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

39
4. PROGRAM TO GENERATE 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:

40
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

41
5. 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

42
43
XOR Operation
Input before execution Output after Execution

Address 20H 21H 22H Address 23H


Data Data

Result:

44
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

45
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

46
47
NOT Operation
Input before execution Output after Execution

Address 20H 21H 22H Address 23H


Data Data

Result:

48
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

49
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

50
MNEMONICS COMMENTS

EQUAL: CLR 78H

CLR 7FH

END1: NOP

END

51
NUM1 = NUM2
Input before execution Output after Execution

Address 9000 9001 Address 2FH


Data Data

Result:

52
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

53
6. 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 data field.

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 Data Field


Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address Data Field


Data Data

Result:

54
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

55
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 address field.

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


Address
Field
Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address


Address
Field
Data Data

Result:

56
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

57
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 address field.

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


Address
Field
Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address


Address
Field
Data Data

Result:

58
Program

Mnemonics Comments

ORG 0000H

MOV DPTR,#9000H

MOVX A,@DPTR

CLR C

SUBB A,#30H

MOV P1,A

END

59
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 data field.

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 Data Field


Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address Data Field


Data Data

Result:

60
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

61
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 address field.

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


Address
Field
Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address


Address
Field
Data Data

62
Mnemonics Comments

SUB: CLR C

CJNE A,#0AH,LOOP

SJMP LOOP2

LOOP: JC LOOP1

LOOP2: ADD A,#07H

LOOP1: ADD A,#30H

RET

END

63
Result:

64
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

65
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 data field.

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 Data Field


Data Data

Sample 2:
Input before execution Output after Execution

Address 9000 Address Data Field


Data Data

Result:

66
67
CYCLE 2
Interfacing with 8051 (C Programming)

68
Program

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

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

69
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:

70
Program

#include<reg51.h>
sbit ibit=P0^6;
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<value;x++)
for(y=0;y<400;y++);
}

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

71
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:

72
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();
}
}

73
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

74
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++);
}

75
LCD Interfacing with 8051

Result:

76
77
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

78
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++);
}

79
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.

80
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++);
}

81
82
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.

83
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 ;
}
}

84
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.

85
Program

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

86
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.

87
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] ;
}
}

88
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:

89
Program

#include <reg51.h>
void delay_ms(unsigned int);
unsigned char reqflr, curflr, i, flr;
unsigned int r1;
void main()
{
P1=0x00;// to turn off the red LEDs and turn on the green and amber LEDs
P0=0x0f;//Lower 4 bits used to read the switch pressed
curflr=0x00;
while(1)
{
reqflr=P0&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++)
{

90
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.

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

92
Result:

93
LCD INTERFACING IN 4 BIT MODE

//RS=P2.7, EN=P2.6, RW=P2.5


#include<reg51.h>
//sbit rs = P2^7; //register select rs=0
//sbit en = P2^6; //enable pin
//sbit rw = P2^5; //read/write
void lcdcmd(unsigned char);
void lcddata();
void write_cmd(void);
void write_data(void);
void tdelay(unsigned int);
unsigned char msg[16]={"ELECTRICAL ENGG."};
unsigned char x,temp,temp1,value1;
void main()
{
value1=0x30;
write_cmd(); //initialize lcd lines 5x7 matrix
tdelay(500);
value1=0x20;
write_cmd(); //4 bit mode
tdelay(500);
lcdcmd(0x28); //initialize 16x2 LCD in 4 bit mode
tdelay(500);
lcdcmd(0x0f); //display on,cursor on, cursor blinking
tdelay(500);
lcdcmd(0x06); //shift cursor right with auto increment
tdelay(500);
lcdcmd(0x01); //clear display
tdelay(500);
while(1)
{
lcdcmd(0x80); //starting address of line1
tdelay(500);

94
//temp='A';
//lcddata();

for(x=0; x<8; x++)


{
temp=msg[x];
lcddata();

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

}
}

}
//SEPRATING THE HIGHER AND LOWER NIBBLE
//D4-D7 OF THE LCD IS CONNECTED TO PORT2
void lcdcmd(unsigned char value)
{
value1=value&0xf0; // HIGHER NIBBLE
value1=value1>>4;
write_cmd();
value1=value &0x0f;
write_cmd();
tdelay(60);
}
void lcddata(void)
{

95
temp1=temp&0xf0;
temp1=temp1>>4;
write_data();
temp1=temp&0x0f;
write_data();
tdelay(60);
}
//WRITING TO THE COMMAND REGISTER MAKE RS=0, EN=1, TDELAY,
EN=0, RW=0
void write_cmd(void)
{
value1=value1&0x0f; //RS=0, RW=0
value1=value1|0x40; //en=1

P2=value1;
tdelay(60);
value1=value1&0x0f; //en=0
P2=value1;

}
//WRITING TO THE DATA REGISTER MAKE RS=0, EN=1, TDELAY, EN=0,
RW=0
void write_data(void)
{
temp1=temp1|0xc0; //RS=1,EN=1,RW=0
P2=temp1;
tdelay(60);
temp1=temp1&0x8f; //en=0
P2=temp1;

96
void tdelay(unsigned int item)
{
unsigned int i;
for (i=0;i<item;i++);

97

You might also like