Microcontroller Lab Manual PDF
Microcontroller Lab Manual PDF
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.
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.
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
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
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
8
EXPERIMENT NO.2.
OBJECTIVE:
TWO UNDERSTAND THE ARITHMETIC OPERATIONS AND PERFORM 8/16 BIT
ADDITION/ SUBTRACTION AND MULTIPLICATION
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
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
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
15
During execution:D:0x50H: (00 to FFH) (00 to FFH) 00 00 00 00
RESULT
During execution:
D:0x50H : (FFH to 00H ) (FFH to 00H) 00 00 00 00
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
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
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
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
END
4. 4 BOOLEAN EXPRESSIONS
1. WRITE PROGRAMS TO REALIZE SOME BOOLEAN EXPRESSIONS
TABLE : 1
21
VARIABLE DECLARATION
X EQU 10H
Y EQU 11H
Z EQU 12H
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
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
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.
ORG 00H
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.
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:
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
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
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
6. 4. HEX - DECIMAL
Verify the Results of Minimum Five Input Combinations
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
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?
CLR A
XRL A,#0FFH
A= in hex
CLR A
CPL A
XRL A,#0FFH
A= in hex
29
EXPERIMENT NO.7
PROGRAMS TO GENERATE DELAY,
OBJECTIVES:
Two understand the basics of serial port communication
;Delay Subroutine
30
DELAY: NOP
H0: MOV R2,#30
HI: MOV R3,#255
DJNZ R3,$
DJNZ R2,HI
DJNZ R1,H0
RET
; The message
General Worksheet
31
1. Give the highest single digit for each of the number systems: decimal, binary, and hex.
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=
15. For Question 1, indicate the largest value (in hex) that each register can contain.
PC = A= B=
16. Who generates each of the following files and what is the use of each.
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
36
Figure 2. Seven Segment Display
Program
LABEL: MOV A,#10000000B // sets the initial stage of the LEDs (D1 OFF &
D2 ON)
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
Program
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
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
DOWN: DEC A
MOV P2,A //provide value to input of DAC
LCALL DELAY
CJNE A,#00,DOWN
SJMP UP
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
42
MOV A,#97D
ACALL DISP
MOV A,#109D
ACALL DISP
43
EXPERIMENT NO.5
STEPPER MOTOR CONTROL INTERFACE TO 8051
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
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.
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
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
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