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

Microcontroller Lab Manual PDF

This document outlines the syllabus for a Microcontroller Laboratory course. It includes the vision, mission and objectives of both the institute and electronics department. It describes the course outcomes which are to apply 8051 assembly programming, analyze programs, and design embedded applications. It also lists the experiments covering 8051 programming and interfacing, including data transfer, arithmetic, counters, logical operations, code conversion, delays, serial communication and interfacing switches, LEDs, terminals and other devices.

Uploaded by

Manoj Sk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
417 views

Microcontroller Lab Manual PDF

This document outlines the syllabus for a Microcontroller Laboratory course. It includes the vision, mission and objectives of both the institute and electronics department. It describes the course outcomes which are to apply 8051 assembly programming, analyze programs, and design embedded applications. It also lists the experiments covering 8051 programming and interfacing, including data transfer, arithmetic, counters, logical operations, code conversion, delays, serial communication and interfacing switches, LEDs, terminals and other devices.

Uploaded by

Manoj Sk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING

MICROCONTROLLERS LAB - 18ECL47

LABORATORY MANUAL

IV SEMESTER
ELECTRONICS AND COMMUNICATION ENGINEERING


Vision of the Institute

1
To emerge as one of the finest technical institutions of higher learning, to develop
engineering professionals who are technically competent, ethical and environment friendly
for betterment of the society.
Mission of the Institute
Accomplish stimulating learning environment through high quality academic instruction,
innovation and industry-institute interface.


Vision of the Department
Be a pioneer in providing quality education in electronics, communication and allied
engineering fields to serve as valuable resource for industry and society.

Mission of the Department
1. Impart sound theoretical concepts & practical skills through innovative pedagogy
2. Promote interdisciplinary research
3. Inculcate professional ethics

Programme Educational Objectives
1. Our graduates will be able to work as professionals in the area of electronics,
communication, and allied engineering fields.
2. Our graduates will be able to pursue higher studies and involve in interdisciplinary
research work.
3. Our graduates will be able to exhibit ethics, professional skills and leadership
qualities in their profession.

Course coordinators
1. Saneesh Cleatus T
2. Thyagaraj T
3. Vinutha B

Laboratory Staff
1. Venkatasubbu Babu

Course Outcomes
On the completion of this laboratory course, the students will be able to:
1. Apply the knowledge of instructions to develop 8051 assembly programs.
2. Analyze the 8051 assembly level programs and Embedded C programs for
applications of the timer, serial communication and interrupts.
3. Design a small embedded application by interfacing 8051 to I/O and peripheral
devices.

2
PROGRAMME OUTCOMES
Our graduates will be able to
1. Apply the knowledge of mathematics, science, engineering fundamentals, and an
engineering specialization to the solution of complex engineering problems.
2. Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
3. 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.
4. 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.
5. 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.
6. 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.
7. Understand the impact of the professional engineering solutions in societal and
environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Function effectively as an individual, and as a member or leader in diverse teams, and
in multidisciplinary settings.
10. 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.
11. 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.
12. 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.

PROGRAMME SPECIFIC OUTCOMES


Graduates will be able to:-
• Exhibit competency in embedded system domain.
• Exhibit competency in RF & Signal processing domain.

3
CONTENTS
Exp Page
No No
Syllabus 1
PART I – PROGRAMMING
1. Data transfer. - block move, exchange, sorting, finding largest element in an 3
array.
2. Arithmetic instructions - Addition/Subtraction, Multiplication, Square & 8
Cube (16 bits arithmetic operations – bit addressable)
3. Counters: 8 bit Binary up/down counter, 8 bit BCD up/down counter 12
4. Boolean & Logical instructions (bit manipulations) 14
5. Conditional call and return 19
6. Code conversion: BCD – ASCII, ASCII – DECIMAL, DECIMAL –ASCII. HEX – 21
DECIMAL. DECIMAL – HEX
7. Programs to generate delay. Programs using serial port and on-chip timer / 25
counter.
General Worksheet 27
PART II – INTERFACING
Port Details of 8051 30
1. Interface a simple toggle switch to 8051 and write an ALP to generate an 31
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 33
serially by interfacing 8051 to a terminal.
3. Write ALPs to generate waveforms using ADC interface. 35
4. Write ALP to interface an LCD display and to display a message on it. 37
5. Write ALP to interface a Stepper Motor to 8051 to rotate the motor. 39
6. Write ALP to interface ADC-0804 and convert an analog input connected to it. 41
Appendix - General Questions 42
Instruction set sumamry 47
References 52

4
SYLLABUS

B.E. (ECE/ETE)
Choice Based Credit System (CBCS) and Outcome Based Education (OBE)
SEMESTER – IV
Microcontroller Laboratory
Laboratory Code 18ECL47 CIE Marks 40
Number of Lecture 2 Hr Tutorial (Instructions) SEE Marks 60
Hours/Week + 02 Hours Laboratory

RBT Levels L1, L2, L3 Exam Hours 03
CREDITS: 02
Course Learning Objectives: This laboratory course enables students to
• Understand the basics of microcontroller and its applications.
• Have in-depth knowledge of 8051 assembly language programming.
• Understand controlling the devices using C programming.
• The concepts of I/O interfacing for developing real time embedded systems.
Laboratory Experiments
II. INTERFACING
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.
I. PROGRAMMING
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.
Course outcomes:
On the completion of this laboratory course, the students will be able to:
1. Apply the knowledge of instructions to develop 8051 assembly programs.
2. Analyze the 8051 assembly level programs and Embedded C programs for applications of
the timer, serial communication and interrupts.
3. Design a small embedded application by interfacing 8051 to I/O and peripheral devices.
Conduct of Practical Examination:
• All laboratory experiments are to be included for practical examination.
• Students are allowed to pick one experiment from the lot.
• Strictly follow the instructions as printed on the cover page of answer script for breakup of
marks.
• Change of experiment is allowed only once and 15% Marks allotted to the procedure part to be
made zero.

1
PART I

PROGRAMMING

EXPERIMENT NO. 1.

2
DATA TRANSFER. - BLOCK MOVE, EXCHANGE, SORTING,
FINDING LARGEST ELEMENT IN AN ARRAY

OBJECTIVES:
§ To write a program to transfer data from code ROM space into RAM locations.
§ To write a program to transfer data from RAM locations to other RAM locations.
§ To experiment with a look-up table.

1.1 DATA TRANSFER

OBJECTIVE:
TO TRANSFER A BLOCK OF DATA BYTES FROM SOURCE MEMORY TO
DESTINATION MEMORY USING 8051.

PROGRAM:
MOV R0,#50H // Initialize the source memory pointer
MOV R1,#60H // Initialize the destination memory pointer
MOV R2, #05H // Initialize Iteration counter
BACK: MOV A,@R0 // Get the data from source memory pointer
MOV @R1,A // Store the data into destination memory pointer
INC R0 // Increment the source memory pointer
INC R1 // Increment the destination memory pointer
DJNZ R2, BACK // Decrement iteration count and if it
// is not zero, go to relative Address and
// repeat the same process until count become
// zero.
END
MEMORY WINDOW:
Before execution:
D:0x50H: 22 AB 3D 44 55 00
D:0X60H: 00 00 00 00 00 00
After execution:
D:0x50H: 22 AB 3D 44 55 00
D:0X60H: 22 AB 3D 44 55 00

3
1. 2 BLOCK EXCHANGE

OBJECTIVE:
TO EXCHANGE TWO BLOCKS OF DATA BYTES USING 8051
PROGRAM:
MOV R0,#50H // Initialize the source memory pointer
MOV R1,#60H // Initialize the destination memory pointer
MOV R2,#05H // Initialize Iteration counter
BACK: MOV A,@R0 // Get the data from source memory pointer and Load
// into Accumulator
XCH A,@R1 // Exchange data between Accumulator and
// destination memory pointer
MOV @R0,A // Store the data into source memory pointer
INC R0 // Increment the source memory pointer
INC R1 // Increment the destination memory pointer
DJNZ R2, BACK /* Decrement iteration count and if it is not zero,
go to relative Address and repeat the same process
until count become zero*/
END
MEMORY WINDOW:
Before execution:
D:0x50H: 01 02 03 04 05 00
D:0X60H: 06 07 08 09 10 00

After execution:
D:0x50H: 06 07 08 09 10 00
D:0X60H: 01 02 03 04 05 00

1.3 LARGEST/SMALLEST ELEMENT IN AN ARRAY USING 8051

OBJECTIVE:
TO FIND THE LARGEST/SMALLEST ELEMENT IN AN ARRAY USING 8051
PROGRAM TO FIND THE LARGEST NUMBER:
MOV R0,#50H //Initialize the source memory pointer
MOV R2,#05H //Initialize Iteration counter
MOV B, @R0 /* Use B Register to store largest value
andinitialize it to the first value*/

4
BACK: MOV A,@R0 /* Get the data from source memory pointerand
Loadinto accumulator*/
CJNE A,B,LOOP /* Compare the data if not equal, go to relative
address(LOOP)*/
LOOP: JC LOOP1 // If carry generates, go to relative address LOOP1
MOV B,A // Store larger value into B-register
INC R0 // Increment the source memory pointer
DJNZ R2,BACK /* Decrement iteration count and if it is not zero,
go to relativeaddress and repeat the same process
until count become zero.*/
SJMP NEXT // Go to NEXT
LOOP1:INC R0 // Increment the source memory pointer
DJNZ R2,BACK /* Decrement iteration count and if it is not zero,
go to relativeaddress and repeat the same process
until count become zero.*/
NEXT: MOV 60H,B /*Store the largest value into memory location 60H.*/
END
MEMORY WINDOW:
Before execution:
D:0x50h: 22 AB 3D 44 55 00
D:0x60h: 00 00 00 00 00 00

After execution:
D:0x50h: 22 AB 3D 44 55 00
D:0x60h: AB 00 00 00 00 00

PROGRAM TO FIND THE SMALLEST NUMBER:


MOV R0,#50H //Initialize the source memory pointer
MOV R2,#05H //Initialize Iteration counter
MOV B, @R0 /* Use B Register to store smallest value and
initialize it to the first value*/
BACK:MOV A,@R0 /* Get the data from source memory pointer and Load
into accumulator*/
CJNE A,B,LOOP /* Compare the data if not equal, go to relative
address(LOOP)*/
LOOP: JNC LOOP1 // If carry generates, go to relative address LOOP1
MOV B,A // Store smaller value into B-register
INC R0 // Increment the source memory pointer
DJNZ R2,BACK /* Decrement iteration count and if it is not zero,
go to relative address and repeat the same process
until count become zero.*/
SJMP NEXT // Go to NEXT

5
LOOP1: INC R0 // Increment the source memory pointer
DJNZ R2,BACK /* Decrement iteration count and if it is not zero,
go to relative address and repeat the same process
until count become zero.*/
NEXT: MOV 60H,B /*Store the smallest value into memory location
60H.*/
END
MEMORY WINDOW:
Before execution:
D:0x50H: 22 AB 3D 44 55 00
D:0X60H: 00 00 00 00 00 00

After execution:
D:0x50H: 22 AB 3D 44 55 00
D:0X60H: 22 00 00 00 00 00

1.4 SORTING

OBJECTIVE:
TO ARRANGE N 8-BIT NUMBERS IN ASCENDING ORDER.
PROGRAM:
MOV R2, #05H // Initialize the iteration counter
DEC R2 // Decrement the iteration count
BACK1: MOV R0, #50H // Initialize memory pointer1
MOV R1, #51H // Initialize memory pointer2
MOV A, R2 // Store outer loop count
MOV R3, A // Store inner loop count
BACK: MOV A,@R0 // Get the data from memory pointer1
MOV B,@R1 // Get the data from memory pointer2
CJNE A, B, LOOP /* Compare if not equal go to relative address
(LOOP)*/
LOOP: JC LOOP1 /* If carry generates, go to relative address
(LOOP1)*/
MOV @R0,B // Exchange the data in memory pointer
MOV @R1, A
LOOP1: INC R0 // Increment the memory pointer1
INC R1 // Increment the memory pointer2
DJNZ R3, BACK // Decrement inner loop count if not zero go to
back

6
DJNZ R2, BACK1 // Decrement outer loop count if not zero go to
back1
END
MEMORY WINDOW:
Before execution:
D:0x50H: 06 04 03 07 02 01
After execution:
D:0x50H: 01 02 03 04 06 07
OBJECTIVE:
TO ARRANGE N 8-BIT NUMBERS IN DESCENDING ORDER.
PROGRAM:
MOV R2, #05H // Initialize the iteration counter
DEC R2 // Decrement the iteration count
BACK1: MOV R0, #50H // Initialize memory pointer1
MOV R1, #51H // Initialize memory pointer2
MOV A, R2 // Store outer loop count
MOV R3, A // Store inner loop count
BACK: MOV A,@R0 // Get the data from memory pointer1
MOV B,@R1 // Get the data from memory pointer2
CJNE A, B, LOOP // Compare if not equal go to relative address (LOOP)
LOOP: JNC LOOP1 // If carry generates, go to relative address (LOOP1)
MOV @R0,B // Exchange the data in memory pointer
MOV @R1, A
LOOP1: INC R0 // Increment the memory pointer1
INC R1 // Increment the memory pointer2
DJNZ R3, BACK /* Decrement inner loop count, if not zero go to
back*/
DJNZ R2, BACK1 /* Decrement outer loop count, if not zero go to
back1*/
END
MEMORY WINDOW:
Before execution:
D:0x50H: 06 04 03 07 02 01
After execution:
D:0x50H: 07 06 04 03 02 01

7
WORKSHEET

1. Write an ALP to generate eight Fibonacci numbers using 8051


• The first term must be zero and second term must be one
• Add the current term and previous term, store in the next term
• Repeat the same processes until count become zero.
2. Write an ALP to check the given string of data is palindrome or not
• The output will be 01 if it is palindrome.
• The output will be FF if it is not palindrome
3. Explain the difference between the following two instructions:
a. MOVC A,@R0
b. MOV A,@R0
4. Circle the invalid instructions.
a. MOV A,@R1
b. MOV A,@R2
c. MOVC A,@R0+DPTR
d. MOV @R3,A
5. Explain the difference between the following two instructions:
a. MOV A,40H
b. MOV A,#40H
6. Explain the difference between the following two instructions:
a. MOV 40H,A
b. MOV 40H,#0A
7. Give the RAM address for the following registers.
a. A = B= R0 = R2 =
b. PSW = SP = DPL = DPH =

8
EXPERIMENT NO.2.

ARITHMETIC INSTRUCTIONS - ADDITION/SUBTRACTION,


MULTIPLICATIO, SQUARE & CUBE (16 BITS ARITHMETIC
OPERATIONS)

OBJECTIVE:
TWO UNDERSTAND THE ARITHMETIC OPERATIONS AND PERFORM 8/16 BIT
ADDITION/ SUBTRACTION AND MULTIPLICATION

2.1 WRITE AN ALP TO PERFORM 16 BIT ADDITION


PROGRAM:
MOV R0,#51H //Initialize input1 memory pointer
MOV R1,#61H /* Initialize input2 memory pointer and store output
also same */
MOV R2,#02H // Initialize iteration count
CLR C
BACK: MOV A,@R0 /*Get lower bytes data in first iteration, upper
bytes data in second iteration, add them with carry
and store in memory pointer2.*/
ADDC A,@R1
MOV @R1,A
DEC R0 // Increment memory pointer1 & 2 to get upper bytes
DEC R1
DJNZ R2,BACK /* Decrement iteration count and if it is not zero,
go to relative address and repeat the same process
until count become zero.*/
JNC FINISH
MOV @R1,#01H
FINISH:SJMP $
END
MEMORY WINDOW
Before execution:
D:0x50H: FD07 00 00 00 00
D:0X60H: FF5F 00 00 00 00
After execution:
D:0x50H: FD07 00 00 00 00
D:0X5FH: 01 FC 6600 00 00

9
2. 2 WRITE AN ALP TO PERFORM 16 BIT SUBTRACTION

PROGRAM:
MOV R0,#51H //Initialize input1 memory pointer
MOV R1,#61H /* Initialize input2 memory pointer and store output
also same */
MOV R2,#02H // Initialize iteration count
CLR C
BACK: MOV A,@R0 //Get lower bytes data in first iteration, upper
bytes data in second iteration, add them with carry
and store in memory pointer2.
SUBB A,@R1
MOV @R1,A
DEC R0 // Increment memory pointer1 & 2 to get upper bytes
DEC R1
DJNZ R2,BACK /* Decrement iteration count and if it is not zero,
go to relative address and repeat the same process
until count become zero.*/
JNC POSITIVE
MOV @R1,#0FFH
JMP FINISH
POSITIVE: MOV @R1,#00H
FINISH: SJMP $
END
Eg. FAF4 - 02F5 = F7FF (ANSWER IS POSITIVE)
MEMORY WINDOW
Before execution:
D:0x50H: FA F4 00 00 00 00
D:0X60H: 02 F5 00 00 00 00
After execution:
D:0x50H: FA F4 00 00 00 00
D:0X60H: F7 FF 00 00 00 00
Eg. 0025 – 0AF6 = FFF52F (ANSWER IS NEGATIVE)
Before execution:
D:0x50H: 0025 00 00 00 00

10
D:0X60H: 0A F6 00 00 00 00
After execution:
D:0x50H: 0025 00 00 00 00
D:0x5FH: FF F52F 00 00 00

2. 3 WRITE AN ALP TO PERFORM MULTIPLICATION (16-bit by 16-bit)


First number will be in R6 and R7 while second number will be in R4 and R5. The result will be
in R0, R1, R2 and R3.
Eg:
R6 R7 = 15 FD
R4 R5 = A2 4B
R0 R1 R2 R3 = 0DF08B1F
PROGRAM:
MOV R6,#0FFH //FFFF X FFFF = FFFE 0001
MOV R7,#0FFH // input the multiplicand
MOV R4,#0FFH // input the multiplier
MOV R5,#0FFH
//Multiply R5 by R7
MOV A,R5 // Move the R5 into the Accumulator
MOV B,R7 // Move R7 into B
MUL AB // Multiply the two values
MOV R2,B // Move B (the high-byte) into R2
MOV R3,A // Move A (the low-byte) into R3
//Multiply R5 by R6
MOV A,R5 // Move R5 back into the Accumulator
MOV B,R6 // Move R6 into B
MUL AB // Multiply the two values
ADD A,R2 // Add the low-byte into the value already in R2
MOV R2,A // Move the resulting value back into R2
MOV A,B // Move the high-byte into the accumulator
ADDC A,#00h // Add zero (plus the carry, if any)
MOV R1,A // Move the resulting answer into R1
MOV A,#00h // Load the accumulator with zero
ADDC A,#00h // Add zero (plus the carry, if any)
MOV R0,A // Move the resulting answer to R0.
//Multiply R4 by R7

11
MOV A,R4 // Move R4 into the Accumulator
MOV B,R7 // Move R7 into B
MUL AB // Multiply the two values
ADD A,R2 // Add the low-byte into the value already in R2
MOV R2,A // Move the resulting value back into R2
MOV A,B // Move the high-byte into the accumulator
ADDC A,R1 // Add the current value of R1 (plus any carry)
MOV R1,A // Move the resulting answer into R1.
MOV A,#00h // Load the accumulator with zero
ADDC A,R0 // Add the current value of R0 (plus any carry)
MOV R0,A // Move the resulting answer to R1.
//Multiply R4 by R6
MOV A,R4 // Move R4 back into the Accumulator
MOV B,R6 // Move R6 into B
MUL AB // Multiply the two values
ADD A,R1 // Add the low-byte into the value already in R1
MOV R1,A // Move the resulting value back into R1
MOV A,B // Move the high-byte into the accumulator
ADDC A,R0 // Add it to the value already in R0 (plus any carry)
MOV R0,A // Move the resulting answer back to R0
// answer is now in R0, R1, R2, and R3
SJMP $
END

RESULT
REGISTER VALUES:
R6 R7 = FF FF
R4 R5 = FF FF
R0 R1 R2 R3 = FF FE 00 01

2. 3 WRITE AN ALP TO PERFORM DIVISION (16-bit by 16-bit)


First number will be in R1 and R0 while second number will be in R3 and R2.The result will be
in R2 and R3. .
Eg:
R1 R0 = D74E
R3 R2 = 00D9
R3 R2 = 00 FE

12
PROGRAM:
MOV R1,0D7H
MOV R0,4EH
MOV R3,00H
MOV R2,0D9H
div16_16:
CLR C // Clear carry initially
MOV R4,#00h // Clear R4 working variable initially
MOV R5,#00h // CLear R5 working variable initially
MOV B,#00h /*Clear B since B will count the number of left-shifted
bits*/
div1:
INC B // Increment counter for each left shift
MOV A,R2 // Move the current divisor low byte into the accumulator
RLC A /*Shift low-byte left, rotate through carry to apply highest bit
to high-byte*/
MOV R2,A // Save the updated divisor low-byte
MOV A,R3 /*Move the current divisor high byte into the accumulator*/
RLC A // Shift high-byte left high, rotating in carry from low-
byte
MOV R3,A // Save the updated divisor high-byte
JNC div1 // Repeat until carry flag is set from high-byte
div2: // Shift right the divisor
MOV A,R3 // Move high-byte of divisor into accumulator
RRC A // Rotate high-byte of divisor right and into carry
MOV R3,A // Save updated value of high-byte of divisor
MOV A,R2 // Move low-byte of divisor into accumulator
RRC A // Rotate low-byte of divisor right, with carry from high-
byte
MOV R2,A // Save updated value of low-byte of divisor
CLR C // Clear carry, we don't need it anymore
MOV 07h,R1 // Make a safe copy of the dividend high-byte
MOV 06h,R0 // Make a safe copy of the dividend low-byte
MOV A,R0 // Move low-byte of dividend into accumulator
SUBB A,R2 /*Dividend - shifted divisor = result bit (no factor, only
0 or 1)*/
MOV R0,A // Save updated dividend
MOV A,R1 // Move high-byte of dividend into accumulator

13
SUBB A,R3 /*Subtract high-byte of divisor (all together 16-bit
subtraction)*/
MOV R1,A // Save updated high-byte back in high-byte of divisor
JNC div3 // If carry flag is NOT set, result is 1
MOV R1,07h /*Otherwise result is 0, save copy of divisor to undo
subtraction*/
MOV R0,06h
div3:
CPL C // Invert carry, so it can be directly copied into result
MOV A,R4
RLC A // Shift carry flag into temporary result
MOV R4,A
MOV A,R5
RLC A
MOV R5,A
DJNZ B,div2// Now count backwards and repeat until "B" is zero
MOV R3,05h// Move result to R3/R2
MOV R2,04h// Move result to R3/R2
SJMP $
END
RESULT
REGISTER VALUES:
R1 R0 = D7 FE // D7FE/D9 = FE
R3 R2 = 00 D9
R3 R2 = 00 FE

Worksheet
1. Explain the difference between the ADD and ADDC instructions.
2. Show how to perform the subtraction: 29H - 21H.
4. True or False. "DA A" must be used for adding BCD data only.
5. Can we use the "DA A" instruction to convert data such as 9CH into BCD without
first performing an ADD instruction? Explain your answer.
6. Show a simple program to add 2345H and 56F8H.
7. Show a simple program to subtract 2345H from 56F8H.

14
EXPERIMENT NO.3
COUNTERS, BCD – BINARY, UP – DOWN COUNTER

OBJECTIVES:
TWO UNDERSTAND THE SIMULATION OF BINARY/BCD UP/DOWN
COUNTERS AND TO KNOW THE CONCEPTS OF SUBROUTINES

3. 1 WRITE A PROGRAM TO REALIZE A BINARY UP COUNTER

ORG 0000H // Organization of code memory from 0000h


CLR 50H // Clear upper byte counter
CLR 51H // Clear lower byte counter
UP: ACALL DELAY /* Call the subroutine to provide delay between two
counter value*/
// LOWER BYTE COUNTER
MOV A, 51H // Get the current lower counter
ADD A, #01H // Add 01h with previous value to get next counter
MOV 51H,A // Store the counter in lower byte
JNZ UP /* If lower count value not zero, go to relative address
(UP)*/
//UPPER BYTE COUNTER
MOV A,50H /* If lower byte reaches zero, get the current upper
counter*/
ADD A,#01H // Add 99h to previous value to get next counter
MOV 50H,A // Store the counter in upper byte
JNZ UP /* If upper count value not zero, go to relative
address (UP)*/
SJMP UP // Repeat this counter until stop running

//Provide delay between two counter value


DELAY: MOV DPTR, #04FFH // Initialize the memory pointer
L2: INC DPTR // Increment the memory pointer
MOV A, DPL // Add higher byte and lower byte address
ORL A, DPH
JNZ L2 // If it is not zero,go to relative address
(L2)
RET // Return to main program
END
RESULT

15
During execution:D:0x50H: (00 to FFH) (00 to FFH) 00 00 00 00

3. 1 WRITE A PROGRAM TO REALIZE BINARY DOWN COUNTER

MOV 50H,#0FFH // Initialize upper byte of HEX counter with FFH


MOV 51H,#0FFH // Initialize lower byte of HEX counter with FFH
UP: ACALL DELAY // Call the subroutine to provide delay
//between two Counter value
// LOWER BYTE COUNTER
DEC 51H // Decrement lower byte counter
MOV A, 51H // Store the lower byte counter into
//accumulator
JNZ UP // If lower count value not zero, go to
//relative address (UP)
// UPPER BYTE COUNTER
DEC 50H //Decrement upper byte counter
SJMP UP // Repeat this counter until stop running
// Provide delay between two counter value
DELAY:MOV DPTR, #04FFH // Initialize the memory pointer
L2: INC DPTR // Increment the memory pointer
MOV A, DPL // Add higher byte and lower byte address
ORL A, DPH
JNZ L2 // If it is not zero, go to relative
// address(L2)
RET // Return to main program
END

RESULT
During execution:
D:0x50H : (FFH to 00H ) (FFH to 00H) 00 00 00 00

3. 2 WRITE A PROGRAM TO REALIZE A BCD COUNTER

WRITE AN ALP TO PERFORM BCD UP COUNTER


ORG 0000H // Organization of code memory from 0000h
CLR 50H // Clear upper byte counter

16
CLR 51H // Clear lower byte counter
UP: ACALL DELAY // Call the subroutine to provide delay between
//two counter value
// LOWER BYTE COUNTER
MOV A, 51H // Get the current lower counter
ADD A, #01H // Add 01h to previous value to get next
counter
DA A // Convert hex value to decimal
MOV 51H, A // Store the counter in lower byte
JNZ UP // If lower count value not zero, go to
//relative address (UP)
// UPPER BYTE COUNTER
MOV A, 50H // If lower byte reaches zero, get the current
//upper counter
ADD A, #01H // Add 99h to previous value to get next
counter
DA A // Convert hex value to decimal
MOV 50H, A // Store the counter in upper byte
JNZ UP // If upper count value not zero, go to
//relative address (UP)
SJMP UP // Repeat this counter until stop running
// Provide delay between two counter value
DELAY: MOV DPTR, #04FFH // Initialize the memory pointer
L2: INC DPTR // Increment the memory pointer
MOV A, DPL // Add higher byte and lower byte address
ORL A, DPH
JNZ L2 // If it is not zero, go to relative address
(L2)
RET // Return to main program
END
RESULT
During execution: D:0x50H :(00 to 99H) (00 to 99H) 00 00 00 00

WRITE AN ALP TO PERFORM BCD DOWN COUNTER


ORG 0000H // Organization of code memory from 0000h
MOV 50H, #99H //Initialize upper byte of counter with 99h
MOV 51H, #99H //Initialize lower byte of counter with 99h
UP: ACALL DELAY /* Call the subroutine to provide delay between
two counter value*/

17
// LOWER BYTE COUNTER
MOV A, 51H // Get the current lower counter
ADD A, #99H /* Add 99h to previous value to get next
counter*/
DA A // Convert hex value to decimal
MOV 51H,A // Store the counter in lower byte
JNZ UP /* If lower count value not zero, go to
relative address (UP)*/
// UPPER BYTE COUNTER
MOV A, 50H // If lower byte reaches zero, get the current
//upper counter
ADD A, #99H /* Add 99h to previous value to get next
counter*/
DA A // Convert hex value to decimal
MOV 50H,A // Store the counter in upper byte
JNZ UP /* If upper count value not zero, go to relative
address (UP)*/
SJMP UP // Repeat this counter until stop running
// provide delay between two counter value
DELAY:MOV DPTR, #04FFH // Initialize the memory pointer
L2: INC DPTR // Increment the memory pointer
MOV A, DPL // Add higher byte and lower byte address
ORL A, DPH
JNZ L2 // If it is not zero, go to relative
//address(L2)
RET // Return to main program
END
RESULT
During Execution: D:0x50H :(99H to 00H) (99H to 00H) 00 00 00 00
Assignment:
Following is a delay subroutine. Find how much delay this subroutine provides. Insert
the delay appropriately inside the BCD COUNTER program so that each count should
happen after twice the delay
DELAY SUBROUTINE:
DELAY: NOP
MOV R2,#25
H0: MOV R3,#255
HI: MOV R4,#255
DJNZ R4,$
DJNZ R3,HI
DJNZ R2,H0
RET

18
EXPERIMENT NO.4
BOOLEAN & LOGICAL INSTRUCTIONS (BIT
MANIPULATIONS)

OBJECTIVES:
Two understand the basic logical bit and byte operations

4. 1. LOGICAL BYTE OPERATIONS


ORG 00H

MOV R0, #34H

MOV A, R0
ANL A, #0FH //and logical operation
MOV P1, A

MOV A, R0
ORL A, #0FH //or logical operations
MOV P1, A

MOV A, R0
XRL A, #0FH //exclusive or logical operations
MOV P1, A

MOV A, R0
CPL A //complement logical operations
MOV P1, A

MOV A, R0
CLR A //clear logical operations
MOV P1, A

MOV A, R0
RR A //rotate right logical operations
RR A
RR A
RR A
MOV P1, A

MOV A, R0
RL A //rotate left logical operations
RL A
RL A
RL A
MOV P1, A

MOV A, R0
SETB C

19
RRC A //rotate right with carry logical operations
RRC A
RRC A
RRC A
MOV P1, A

MOV A, R0
RLC A //rotate left with carry logical operations
RLC A
RLC A
RLC A
MOV P1, A
SJMP $
END

4. 2 LOGICAL BIT OPERATIONS


SETB 01H //set bit addressible memory 01h
CLR 01H //clear bit addressible memory 01h
CPL 01H //compliment bit addressible memory 01h
MOV C, 01H //move bit addressible memory location 01h's
content to carry
MOV 07FH, C //move carry to bit addressible memory
location 01h
SETB 01H

ANL C, 01H //logically and bit addressible memory 01h


content with carry
ORL C, 01H //logically or bit addressible memory 01h
content with carry
SJMP $
END

4.3. TWO OUT OF FIVE CODE


This is a program will check the data available at port 1 is a “2_out_of_5_code” or not. If yes it will send f0
to the port0 otherwise it will send 0f to port 0.

This program demonstrates the use of some LOGICAL instruction and


CONDITIONAL/UNCONDITIONAL JUMPS

Initializing ports and counter.

MOV P1, #0FFH


MOV P0, #00H
MOV R2, #05H

Initially Checking whether any of the last three Most Significant Bits
are high or not. If it is high it is not a “2_OUT_OF_5_CODE”

MAIN: MOV A, P1
MOV R0, A
ORL A, #01FH
CJNE A, #01FH, NOTOF

20
If all the last three MSB’s are zero then checking for how many 1’s are
present in the remaining five bits.
CHECK: MOV A, R0

AGAIN: RRC A
JNC LOOP
INC R1

LOOP: DEC R2
CJNE R2, #00, AGAIN

After completing five times rotation, R1 contains the number of 1’s in


last five bits. If the number is two it is a “2_OUT_OF_5_CODE”.

CJNE R1, #02, NOTOF


MOV P0, #0F0H //Set upper Nibble of P0 high if it is a
//“2_OUT_OF_5_CODE”
SJMP HERE

NOTOF: MOV P0, #00FH //Set lower Nibble of P0 high if it is


not a
//“2_OUT_OF_5_CODE”

HERE: SJMP HERE

END

4. 4 BOOLEAN EXPRESSIONS
1. WRITE PROGRAMS TO REALIZE SOME BOOLEAN EXPRESSIONS

TABLE : 1

BIT ADDRESSIBLE MEMORY LOCATIONS AND CORRESPONGING ADDRESSES

BIT BYTE BIT BYTE BIT BYTE BIT BYTE


00 20.0 10 21.0 20 24.0 30 26.0
01 20.1 11 22.1 21 24.1 31 26.1
02 20.2 12 22.2 22 24.2 32 26.2
03 20.3 13 22.3 23 24.3 33 26.3
04 20.4 14 22.4 24 24.4 34 26.4
05 20.5 15 22.5 25 24.5 35 26.5
06 20.6 16 22.6 26 24.6 36 26.6
07 20.7 17 22.7 27 24.7 37 26.7
08 20.0 18 22.0 28 25.0 38 27.0
09 21.1 19 23.1 29 25.1 39 27.1
0A 21.2 1A 23.2 2A 25.2 3A 27.2
0B 21.3 1B 23.3 2B 25.3 3B 27.3
0C 21.4 1C 23.4 2C 25.4 3C 27.4
0D 21.5 1D 23.5 2D 25.5 3D 27.5
0E 21.6 1E 23.6 2E 25.6 3E 27.6
0F 21.7 1F 23.7 2F 25.7 3F 27.7

21
VARIABLE DECLARATION
X EQU 10H
Y EQU 11H
Z EQU 12H

NOT_X EQU 20H


NOT_Y EQU 21H
NOT_Z EQU 22H

OUTPUT EQU P0.0

INITIALIZING PORTS P1 AS INPUT AND P2 AS OUTPUT PORT

MOV P1, #0FFH


MOV P0, #00H

INTINTIALIZING X, Y, Z AND ITS COMPLEMETS AFTER READING THE VALUES FROM


PORT 1

MOV C, P1.0
MOV X, C
CPL C
MOV NOT_X, C

MOV C, P1.1
MOV Y, C
CPL C
MOV NOT_Y, C

MOV C, P1.2
MOV Z, C
CPL C
MOV NOT_Z, C

EXPRESSION OUTPUT = X+Y+Z


CLR C
MOV C, X
ORL C, Y
ORL C, Z
MOV OUTPUT, C

EXPRESSION OUTPUT = XYZ


CLR C
MOV C, X
ANL C, Y
ANL C, Z
MOV OUTPUT, C

EXPRESSION OUTPUT = XY+ Z


CLR C
MOV C, X

22
ANL C, Y
ORL C, Z
MOV OUTPUT, C

EXPRESSION OUTPUT = X+ YZ
CLR C
MOV C, Y
ANL C, Z
ORL C, X
MOV OUTPUT, C

EXPRESSION OUTPUT =!X+ !Y+ !Z


CLR C
MOV C, NOT_X
ORL C, NOT_Y
ORL C, NOT_Z
MOV OUTPUT, C

EXPRESSION OUTPUT =!(X+Y)+ Z


CLR C
MOV C, X
ORL C, Y
CPL C
ORL C, Z
MOV OUTPUT, C

EXPRESSION OUTPUT =!(X+Y+Z)


MOV C, X
ORL C, Y
ORL C, Z
CPL C
MOV OUTPUT, C

EXPRESSION OUTPUT = XYZ + !(XY)Z + !Y!Z


CLR C
MOV C, X
ANL C, Y
ANL C, Z
MOV 00H, C
MOV C, X
ANL C, Y
CPL C
ANL C, Z
MOV 01H, C
MOV C, NOT_Y
ANL C, NOT_Z
ORL C, 01H
ORL C, 00H
MOV OUTPUT, C

HERE: SJMP HERE


END

NOTE:
Give minimum 4 input combinations and verify the results for each expression

23
Assignment: Realize the function WX+WYX + YZ

EXPERIMENT NO.5
CONDITIONAL CALL AND RETURN

OBJECTIVES:

This program keeps checking P1.0 for low signal. Once a low signal arrives, program
checks for AAh at PORT 0. If the data is AAh a counter will start.

In this program the usage of CONDITIONAL BIT JUMP, CONDITIONAL BYTE


JUMP, UNCONDITIONAL JUMP, CALL and RETURN instructions are demonstrated.

ORG 00H

INITIALIZING THE COUNTER AND PORTS

MAIN: MOV P0, #0FFH


MOV P1, #0FFH
MOV P2, #00H
MOV R0, #00H

WAITING FOR THE ARRIVAL OF LOW SIGNAL AT PORT 1.0

WAIT: JB P1.0, WAIT // CONDITIONAL BIT JUMP


ACALL VERIFY_DATA // CONDITIONAL CALL
SJMP WAIT // UNCONDITIONAL JUMP

CHECKING THE DATA AT P0 FOR 0AAh


VERIFY_DATA:
MOV A, P0
CJNE A, #0AAH, RETURN // CONDITIONAL BYTE JUMP
INC R0
MOV P2, R0
ACALL DELAY // UNCONDITIONAL CALL
RETURN:RET // RETURN

JUST A DELAY SUBROUTINE J

DELAY: NOP
MOV R2,#25
H0: MOV R3,#255
HI: MOV R4,#255
DJNZ R4,$ // CONITIONAL BYTE JUMPS
DJNZ R3,HI
DJNZ R2,H0
RET // RETURN
END

24
Worksheet

1. Upon reset, all the ports of the 8051 are configured as ______ (input, output).

2. To make all the bits of a port an input port we must write ____ hex to it.

3. Which ports of the 8051 are bits addressable?

4. What does it mean for port to be "read-modify-write"?

5. Write a program to monitor P2.4 continuously. When it becomes low, it sends 55H to P1.

25
EXPERIMENT NO.6
CODE CONVERSION
BCD – ASCII ASCII – DECIMAL DECIMAL –ASCII
HEX – DECIMAL DECIMAL – HEX

OBJECTIVES:

Two perform conversions of different number system representations by making use of


logical instructions.

6. 1 BCD – ASCII
; register a has packed BCD
; program to convert BCD to '2' ASCII numbers

ORG 0H
MOV A, #29H //mov 29 to A( packed BCD)
MOV R2, A //copy of A (BCD DATA)in R2
ANL A, #0FH //mask the upper nibble A=09h
ORL A, #30H //maske it as ASCII to A=39h
//(ASCII char 09h)
MOV R6,A //save it in R6
MOV A, R2 //A=29, get original data
ANL A, #0F0H //mask the lower nibble A=20h
RR A //rotate right
RR A //rotate right
RR A //rotate right
RR A //rotate right A=02h
ORL A, #30H //A=32h, ASCII char 2
MOV R2,A //save ASCII to char in R2
MOV P1,A
MOV P2,R6
SJMP $
END

6. 2. ASCII – DECIMAL
Students Please Complete the Program with proper comments

ORG 0

MOV R5, #"4"


MOV R6, #"7"
MOV A, R5
ANL A, #0FH
MOV R5, A
MOV A, R6
ANL A, #0FH
MOV R6, A
MOV A, R5
RL A
RL A
RL A

26
RL A
ORL A, R6
MOV P1,A
SJMP $

END

6. 3. DECIMAL – ASCII
Conversion of Decimal number 0-9 to corresponding ASCII equivalent

RAM_ADDR EQU 40H //initializing addresses


ASC_RSULT EQU 50H //for storing data and result

ORG 00H
MAIN: MOV R0,#RAM_ADDR
MOV R1,#ASC_RSULT
MOV R2,#0AH
BACK: MOV A,@R0 //stored decimal values
ORL A,#30H //are fetched ,they are ‘OR’ed with
MOV @R1,A //30 and results are placed at memory
INC R0 //pointed by R1 pointer
INC R1
DJNZ R2,BACK
MOV R1,#ASC_RSULT

MOV R2,#0AH //results are verified on


NEXT: MOV A,@R1 //PORT 1
MOV P1,A
INC R1
DJNZ R2,NEXT
SJMP $
END

6. 4. HEX - DECIMAL
Verify the Results of Minimum Five Input Combinations

ORG 0000 //start


MAIN: MOV A,#0FFH //load FF to ACC
MOV P1,A // and make P1 an input PORT
MOV A,P1 //take hex data from input port P1
MOV B,#10 //divide by 10 remainder is stored in B
DIV AB
MOV R7,B //unit place of decimal is put to R7/
MOV B,#10
DIV AB //divide again
MOV R6,B //tens place of decmial is put to R6
MOV R5,A //hundred's pplace is put to R5
LJMP MAIN
END // end

6. 5. DECIMAL – HEX

ORG 00 // start
MOV R3,#002
MOV R4,#00

27
MOV A,#251 //decmial i/p is given
MOV B,#02 //divide the no by 2

CLR C
DIV AB
MOV R5,A // ACC value is lost after div ,so move it to
R5
MOV A,B //MOV value of B to ACC
ORL A,R4 // ORing acc value with R4
MOV R4,A // store the ORed value in R4

MOV B,#02
MOV A,R5
DIV AB
MOV R5,A
MOV A,B
RL A //rotate the ACC left once ,to get first bit
ORL A,R4
MOV R4,A

MOV B,#02
MOV A,R5
DIV AB
MOV R5,A
MOV A,B
RL A // rotate the ACC twice, to get the third bit
RL A
ORL A,R4
MOV R4,A

MOV B,#02
MOV A,R5
DIV AB
MOV R5,A
MOV A,B
RL A //rotate the ACC thrice to get the fourth bit
RL A
RL A
ORL A,R4
MOV R4,A

MOV A,R5
SWAP A
ORL A,R4
MOV P1,A //display the final result on port1
HERE: SJMP HERE //end of the program
END

Worksheet
1. Find the value in A, the accumulator, after the following code.

28
MOV A, #45H
RR A
RR A
RR A

A= in hex

2. Find the value in A, the accumulator, after the following code.

MOV A, #45H
RL A
RL A
RL A

A= in hex

3. In the absence of the "SWAP A" instruction, how would you perform the operation?

4. Can the SWAP instruction work on any register?

5. Find the value in A after the following code.

CLR A
XRL A,#0FFH

A= in hex

6. Find the value in A after the following code.

CLR A
CPL A
XRL A,#0FFH

A= in hex

29
EXPERIMENT NO.7
PROGRAMS TO GENERATE DELAY,

PROGRAMS USING SERIAL PORT AND ON-CHIP TIMER /


COUNTER

OBJECTIVES:
Two understand the basics of serial port communication

Write an ALP to send a string to serial port (Software Simulation)

CR EQU 0DH ; CLEAR LINE


LF EQU 0AH ; LINE FEED
ORG 0

;Initialize the Special Function Registers Required for Serial Data


Transmission

MOV TMOD, #20H ; timer 1, mode 2(auto-reload)


MOV TH1, #-3 ; 9600 baud rate
MOV SCON,#50H ; 8-bit,1 stop, REN enabled
SETB TR1 ; start timer 1

;Clear the Serial Window(Screen) and return the curser to beginning of


the line
MOV A,#0CH ; clear screen
ACALL SENDsr
MOV A,#LF
ACALL SENDsr

;Retrieve The Data to be transmitted from the code memory

MOV DPTR,#MYDATA0 ; load pointer for message


H_1: CLR A
MOVC A,@A+DPTR ; get the character
JZ HERE ; if last character get out
ACALL SENDsr ; otherwise call transfer
ACALL DELAY
INC DPTR ; next one
SJMP H_1 ; stay in loop

; Serial data transfer. ACC has the data

SENDsr: MOV SBUF,A ; load the data


JNB TI,$ ; stay here until last bit gone
CLR TI ; get ready for next char
RET ; return to caller
; Receive data serially in Acc

;Delay Subroutine

30
DELAY: NOP
H0: MOV R2,#30
HI: MOV R3,#255
DJNZ R3,$
DJNZ R2,HI
DJNZ R1,H0
RET

; The message

MYDATA0: DB CR,LF,"Hello World" ,0


HERE: SJMP HERE
END

NOTE: MODIFY THIS PROGRAM TO SEND THE STRING IN A LOOP.

General Worksheet

31
1. Give the highest single digit for each of the number systems: decimal, binary, and hex.

2. Which of the following cannot be a number in base-2? Give the reason.

(a) 11001 (b) 113 (c) 10001

3. What is the highest 8-bit number?

(a) In binary: (b) In hex:

4. What is the highest 16-bit number?

(a) In binary: (b) In hex:

5. Convert binary 100000 to decimal and hex.

(a) Decimal: (b) Hex:

6. Convert hex number BAAD to binary and decimal.

(a) Binary: (b) Decimal:


7. Find the value of the CY flag after the execution of the following code.

(a) MOV A,#85H


ADD A,#92H

(b) MOV A,#15H


ADD A,#72H

(c) MOV A,#0F5H


ADD A,#52H

(d) MOV A,#0FF


INC A

8. Upon reset, what is the value in the SP register?

9. Upon pushing data onto the stack, the SP register is _____________ (decremented,
incremented).

10. Upon popping data from the stack, the SP register is ___________ (decremented,
incremented).

11. Can you change the value of the SP register? If yes, explain why you would want
to do that.

12. The stack uses the same area of RAM as bank ______.

13. Indicate the size (8- or 16-bit) of each of the following registers.

PC = A= B=

32
R0= R1= R2= R7=

14. For Question 1, indicate the largest value (in decimal) that each register can contain.

PC = A= B=

R0= R1= R2= R7=

15. For Question 1, indicate the largest value (in hex) that each register can contain.

PC = A= B=

R0= R1= R2= R7=

16. Who generates each of the following files and what is the use of each.

.asm .lst .obj .abs .hex

33
PART II

INTERFACING

34
Note to students:
If generic PIN name is mentioned in any of the following programs, it is to be replaced
with corresponding PIN number mentioned in the above table if required.

35
EXPERIMENT NO.1
INTERFACE A TOGGLE SWITCH AND LED

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

Hardware:
Microcontroller 89s8252
Crystal Freq 11.0592

I/O Port configuration


Port 0 output; 7 Segment Display
Port 2 input: Keypad

Figure 1. Circuit Diagram

36
Figure 2. Seven Segment Display

Program

ORG 000H // starting address


SJMP LABEL //jumps to the LABEL

ORG 003H // starting address for the ISR(INT0)


ACALL ISR // calls the ISR (interrupt service routine)
RETI // returns from the interrupt

LABEL: MOV A,#10000000B // sets the initial stage of the LEDs (D1 OFF &
D2 ON)

MAIN: // main function that sets the interrupt parameters


SETB IP.0 // sets highest priority for the interrupt INT0
SETB TCON.0 // interrupt generated by a falling edge signal at INT0
SETB IE.0 // enables the external interrupt
SETB IE.7 // enables the global interrupt control
SJMP MAIN // jumps back to the MAIN subroutine

ISR: CPL A // complements the current value in accumulator A


MOV P1,A // moves the current accumulator value to port 1
RET // jumps to RETI
END

Open End Experiment 1:



Students are expected to write another program which
toggles the LED for small duration

37
EXPERIMENT NO.2
WRITE A C PROGRAM TO (I) TRANSMIT AND (II) TO
RECEIVE A SET OF CHARACTERS SERIALLY BY
INTERFACING 8051 TO A TERMINAL.

Objective
To demonstrate the basic Interface between 8051 and Computer using RS2323 protocol

Hardware:
Microcontroller 89s8252
Crystal Freq 11.0592

Figure 2. RS 232 connection

Program

// Program to test serial communication of controller with PC using


hyper terminal
#include<reg51.h>

void initialize() // Initialize Timer 1 for serial communication


{
TMOD=0x20; //Timer1, mode 2, baud rate 9600 bps
TH1=0XFD;
SCON=0x50;
TR1=1;
}

void receive() //Function to receive serial data


{
unsigned char value;
while(RI==0);
value=SBUF;
P1=value;
RI=0;
}

38
void transmit() // Function to transmit serial data
{
P2=P1-32;
SBUF=P2;
while(TI==0);
TI=0;
SBUF=P1;
while(TI==0);
TI=0;
}

void main()
{
while(1)
{
initialize();
receive();
transmit();
}
}

39
EXPERIMENT NO.3
WRITE ASSEMBLY LEVEL PROGRAMS TO GENERATE
WAVEFORMS USING ADC INTERFACE.

Objective
This program implements the basic wave form generation using DAC. Output is displayed on a
CRO.

Hardware:
Microcontroller 89s8252
Crystal Freq 11.0592

I/O Port configuration

Port output DAC

Figure 3. DAC Interface

Program.
1. Generation of square wave.

ORG 0000H
MOV TMOD,#01H

UP:MOV P0, #00H //Generate low output by making all inputs of DAC low
LCALL DELAY //generate OFF time period by calling delay
MOV P0, #0FFH //Generate high output by making all inputs of DAC high
LCALL DELAY //generate ON time period by calling delay
SJMP UP

DELAY: //Intialise the timers and wait for the timer flag to set.
MOV TH0,#0FEH
MOV TL0,#0CH
CLR TF0
SETB TR0

40
HERE:JNB TF0,HERE
RET
END
2. Generation of triangular wave.

ORG 0000H
CLR A

UP:INC A //provide value to input of DAC


MOV P2,A
LCALL DELAY //to change the frequency of the triangular wave
CJNE A,#0FFH,UP

DOWN: DEC A
MOV P2,A //provide value to input of DAC
LCALL DELAY
CJNE A,#00,DOWN
SJMP UP

Open End Experiment 2



Students are expected to write, analyze and execute the
programs to generate sinewave, sawtooth wave and pulse
train (not square wave)

41
EXPERIMENT NO.4
WRITE ALP TO INTERFACE AN LCD DISPLAY AND TO
DISPLAY A MESSAGE ON IT.

Objective: To demonstrate the basic interface between an LCD display and 8051

Hardware:
Microcontroller 89s8252
Crystal Freq 11.0592

I/O Port configuration


Port 0 output LCD Data
Port 3 output LCD Control

Figure 4. LCD Interface

MOV A,#38H // Use 2 lines and 5x7 matrix


ACALL CMD
MOV A,#0FH // LCD ON, cursor ON, cursor blinking ON
ACALL CMD
MOV A,#01H //Clear screen
ACALL CMD
MOV A,#06H //Increment cursor
ACALL CMD
MOV A,#82H //Cursor line one , position 2
ACALL CMD
MOV A,#3CH //Activate second line
ACALL CMD

//In the following we are sending characters to display


MOV A,#73D
ACALL DISP
MOV A,#32D
ACALL DISP

42
MOV A,#97D
ACALL DISP
MOV A,#109D
ACALL DISP

MOV A,#0C1H //Jump to second line, position 1


ACALL CMND

//In the following we are sending characters to display


MOV A,#83D
ACALL DISP
MOV A,#65D
ACALL DISP
MOV A,#78D
ACALL DISP

HERE: SJMP HERE

CMD: MOV P0,A


CLR P3.7
CLR P3.6
SETB P3.5
CLR P3.5
ACALL DELY
RET

DISP: MOV P0,A


SETB P3.7
CLR P3.6
SETB P3.5
CLR P3.5
ACALL DELY
RET

DELY: CLR P3.5


CLR P3.7
SETB P3.6
MOV P1,#0FFh
SETB P3.5
MOV A,P1
JB ACC.7,DELY
CLR P3.5
CLR P3.6
RET
END

43
EXPERIMENT NO.5
STEPPER MOTOR CONTROL INTERFACE TO 8051

12.1 STEPPER MOTOR CONTROL

Objective
This program demonstrates a Stepper motor control using an H-Bridge controller. Direction and
speed are selected with hyper terminal.

Hardware:
Microcontroller 89s8252
Crystal Freq 11.0592

I/O Port configuration

Port 0 output Stepper motor

Figure 5. Stepper motor interface


A1 EQU P1.0
A2 EQU P1.1
A3 EQU P1.2
A4 EQU P1.3
ORG 00H
MOV TMOD,#00000001B

MAIN:
CLR A1
ACALL DELAY
SETB A1

CLR A2
ACALL DELAY
SETB A2

44
CLR A3
ACALL DELAY
SETB A3

CLR A4
ACALL DELAY
SETB A4
SJMP MAIN

DELAY:MOV R6,#1D
BACK: MOV TH0,#00000000B
MOV TL0,#00000000B
SETB TR0
HERE2: JNB TF0,HERE2
CLR TR0
CLR TF0
DJNZ R6,BACK
RET
END

45
OPEN END EXPERIMENT NO.3
WRITE ALP TO INTERFACE ADC-0804 AND CONVERT AN
ANALOG INPUT CONNECTED TO IT

Objective
Students are expected to write a program which implements basic temperature sensor using an
ADC. Output is displayed on a 2x16 LCD.

Hardware to be used : AT89S8252 Microcontroller Development Board, ADC 0804


Software to be used for debugging : Keil uVision

46
APPENDIX A
GENERAL QUESTIONS

1. Upon reset, all ports of the 8051 are configured as _____________ (output, input).
2. Which ports of the 8051 have internal pull-up resistors?
3. Which ports of the 8051 require the connection of external pull-up resistors in order
to be used for I/O? Show the drawing for the connection.
4. In the 8051, explain why we must write "1" to a port in order for it to be used for
input.
5. Explain why we need to buffer the switches used as input in order to avoid damaging
the 8051 port.
6. How does the LCD distinguish data from instruction codes when receiving
information at its data pin?
7. To send the instruction code 01 to clear the display, we must make RS = ___.
8. To send letter 'A' to be displayed on the LCD, we must make RS = ____.
9. What is the purpose of the E line? Is it an input or an output as far as the LCD is
concerned?
10. When is the information (code or data) on the LCD pin latched into the LCD?
11. Indicate the direction of pins WR, RD, and INTR from the point of view of the 8051.
12. Give the three steps for converting data and getting the data out of the ADC804. State
the status of the CS, RD, INTR, and WR pins in each step.
13. Assume that Vref/2 is connected to 1.28 V. Find the following.
13.1. step size
13.2. maximum range for Vin
13.3. D7 - D0 values if Vin = 1.2 V
13.4. Vin if D7 - D0 = 11111111
13.5. Vin if D7 - D0 = 10011100
14. Assume that Vref/2 is connected to 1.9 V. Find the following.
14.1. step size
14.2. maximum range for Vin

14.3. D7 - D0 values if Vin = 2.7 V

47
14.4. Vin if D7 - D0 = 11111111
14.5. Vin if D7 - D0 = 11011101
15. The ADC804 is a(n) ____-bit converter.
16. To get step size of 2 mV, what is the value for Vref/2?
17. What is a transducer?
18. What is the form of the transducer output?
19. What is preprocessing of transducer signals to be fed into an ADC called?

20. The LM35 and LM34 produce a _______ mV output for every degree of change in
temperature.
21. The LM35/LM34 is a ____________ (linear, nonlinear) device. Discuss the
advantages of linear devices and of nonlinear devices.
22. Explain signal conditioning and its role in data acquisition.
23. What is the maximum frequency that can be generated using Mode 1 if the crystal
frequency is 11.0592 MHz? Show your calculation.
24. What is the maximum frequency that can be generated using Mode 2 if the crystal
frequency is 11.0592 MHz? Show your calculation.
25. What is the lowest frequency that can be generated using Mode 1 if the crystal
frequency is 11.0592 MHz? Show your calculation.
26. What is the lowest frequency that can be generated using Mode 1 if the crystal
frequency is 11.0592 MHz? Show your calculation.
27. In mode 1, when is TFx set to high?
28. In mode 2, when is TFx set to high?
29. The 8051 TxD and RxD signals ________ (are, are not) TTL-compatible.
30. In this lab, what is the role of the MAX233 (MAX232) chip?
31. With XTAL=11.0592 MHz, what is the maximum baud rate for the 8051?
32. Show how to achieve the maximum baud rate
33. What is the role of TI and RI?
34. True or false. The 8051 can transfer data in full-duplex.
35. For full duplex, what are the absolute minimum signals needed between the 8051 and
the PC? Give their names.
36. What is a step angle? Define steps per revolution.

48
37. If a given stepper motor has a step angle of 5 degrees, find the number of steps per
revolution.
38. Give the four sequences for counter clockwise if it starts with 10011001 (binary).
39. Using the "RL A" instruction, show the four-step sequences if the initial step is 0011
(binary).
40. Give the number of times the four-step sequence must be applied to a stepper motor
to make a 100-degree move if the motor has a 5-degree step angle. Also fill in the
characteristics for your motor below.
40.1. Step angle _______Degree of movement per 4-step sequence ________
40.2. Steps per revolution _____ Number of rotor teeth ______________
40.3. What is the purpose of generating the truth table for a given keyboard?
41. What is the purpose of grounding each row in keyboard interfacing?
42. What is the input to the microcontroller from column if no key is pressed?
43. True or false. In our N x M matrix keypad program we cannot press two keys at the
same time.
44. In your program in, how is the key press detected?
45. In your program in, how is a key press identified?
46. Explain the role of the C/T bit in the TMOD register.
47. How is the 8051 used as an event counter to count an external event?
48. If timer/counter 0 is used as an event counter, what is the maximum count for the
following modes.
48.1. Mode 1
48.2. Mode 2
49. Indicate which pin is used for the following.
49.1. timer/counter 0
49.2. timer/counter 1
50. If timer/counter 0 is used in mode 1 to count an external event, explain when TF0 is
set to high.
51. If timer/counter 1 is used in mode 2 to count an external event, explain when TF0 is
set to high.

49
52. Indicate the direction of pins ALE, SC, EOC, and OE from the point of view of the
ADC808/809.
53. Give the steps for converting data and getting the data out of the ADC809. State the
status of the SC and EOC pins in each step.

54. Give the role of signals ALE, A, B, and C in selecting the ADC channel.
55. In the ADC809 assume that Vref is connected to 2.56 V. Find the following.
55.1. step size
55.2. maximum range for Vin
55.3. D7 - D0 values if Vin = 1.2 V
55.4. Vin if D7 - D0 = 11111111
55.5. Vin if D7 - D0 = 10011100
56. In the ADC809 assume that Vref is connected to 5V. Find the following.
56.1. step size
56.2. maximum range for Vin
56.3. D7 - D0 values if Vin = 2.7 V
56.4. Vin if D7 - D0 = 11111111
56.5. Vin if D7 – D0 = 11011101
57. In connecting ADC808/809 to an 8051, indicate the direction of pins ALE, SC, EOC,
and OE from the point of view of the 8051.
58. Define the following terminology in DAC.
58.1. resolution
58.2. full-scale voltage output
58.3. settling time
59. For your circuit, find Vout for the following inputs.
59.1. 11001100
59.2. 10001111
60. To get a smaller step size, we need DAC with ________ (more, less) data bit inputs.
61. In Figure 13-7 of the textbook, assume that R = 2.5 K ohms. Calculate Vout for the
following binary inputs.
61.1. 11000010

50
61.2. 01000001
61.3. 00101100
61.4. 11111111
62. Name all of the interrupts in the 8051 and their vector table addresses.
63. In timer mode 1, indicate when TF0 causes the interrupt.
64. In timer mode 2, indicate when TF0 causes the interrupt.
65. On reset, INT0 (and INT1) are _________ (edge, level) triggered.
66. On reset, which interrupt has the highest priority?
67. True or False. There is only a single interrupt for the serial data transfer.

51
52
53
54
55
56
Prepared by

Mr. Saneesh Cleatus Thundiyil, Associate Professor


Department of Electronics and Communication Engineering

Reference

§ Mazidi, Muhammad Ali, Janice Gillispie Mazidi, and Rolin D. McKinlay. The 8051
microcontroller and embedded systems: using Assembly and C. Vol. 626.
Pearson/Prentice Hall, 2006.

57

You might also like