You are on page 1of 20

LABORATORY

MANUAL
The 8051 microcontroller
Based
Embedded Systems

Manish K Patel
INDEX

SR.No Laboratory
Introduction to Keil vision 4.0 Integrated development environment
1
(IDE).
2 Data transfer instructions and addressing modes in 8051

3 Arithmetic and logical instructions

4 Bit processing instructions

5 Program flow control instructions

6 Simple 8051 board design and port operations

7 The 8051 Programming in C

8 Programming the timer/counters of 8051

9 Serial communications using 8051

10 Interrupts in the 8051

11 Keyboard interfacing

12 LCD and 7 segment display interfacing

13 Interfacing ADCs and DACs

14 Interfacing DC and stepper motors with 8051


LABORATORY 1:

Introduction to Keil vision 4.0 Integrated development environment (IDE).

Objectives:

To be familiar with vision 4.0 IDE and basics of development tools


To understand program development process

Refer Appendix B: Using Keil vision 4.0 IDE

It is complete tutorial based on vision 4.0 IDE. It explains how to use vision 4.0 to develop
and test 8051 programs in assembly as well as C language. To support and ease the
understanding, a step wise explanation along with screen shots of vision 4.0 IDE windows is
given for a sample programs.

Laboratory exercise:

Execute the sample program given in the tutorial as explained and answer the following
questions.

1. What is meant by Building a project?


2. List the types of files created by IDE after building a project.
3. Discuss the usefulness of information contained in list (.lst) file.
4. How to observe and modify the contents of internal RAM, code and data memory?
5. What is difference between assembler and compiler?
6. What is the difference between debug commands like Run, step, step over, step out and
run to the cursor line?
7. List the features of vision 4.0 IDE.
8. How to select target device and set crystal oscillator frequency for it?
9. What is meant by breakpoint? How it is useful for debugging?
10. Explain how to download application programs to Flash ROM.
LABORATORY 2:

Data transfer instructions and addressing modes in 8051

Objectives:
To understand the data transfer operations in the 8051
To be familiar with different addressing modes used in instructions

Consider following sample program.

ORG 0000H
MOV R0, #20H
MOV R2, #30H
MOV A, #10H
MOV 50H, R2
MOV @ R0, A
MOV DPTR, #0100H
MOVX @DPTR, A
MOV 7FH, A
MOV A, #03H
MOVC A,@A+DPTR
PUSH A

ORG 100H
DB 25H, 30H
DB 50H, 55H
END

Laboratory exercise:

Answer the following questions with respect to above sample program.

1. Write operation performed by each instruction.


2. Write size of each instruction in terms of number of bytes.
3. Find address of each instruction when the program is stored in code memory.
4. Identify addressing mode used by each instruction.
5. Identify size of registers involved in a program.
6. Write hex code of each instruction and identify the opcode and operands.
7. What will be the content of the PC when instruction MOVX @DPTR, A is being
executed?
8. Identify task(s) performed by program.
9. What is the size of program and how many machine cycles are required to execute the
program? Find time required to execute the program if crystal frequency is 12MHz.
10. Modify the program to reduce its size so as to perform same operation(s).
11. Identify the assembler directives used in the program.
12. Can we write in to code memory using instructions? Why?
LABORATORY 3:

Arithmetic and logical instructions

Objective

To understand arithmetic and logical operations of the 8051 and their limitations.

Consider following sample program.

ORG 0000H
MOV R2, #50H
MOV R3, #0F0H
MOV 50H, # 30H
MOV A, @R2
ADD R3, A
ADDC A, 50H
MOV B, R3
MUL AB
ANL A, #0FH
XRL A, #0FFH
CPL A
CLR C
RL A
RLC A
RR A
END

Laboratory exercise:

Answer the following questions with respect to above sample program.

1. Check validity of all instructions. Mention reason for invalid instructions


2. Rewrite the invalid instructions such that the new instruction provides the same function
as intended by invalid instruction.
3. Identify the task(s) performed by the program.
4. Identify the role of ANL A,#0FH and XRL A,#0FFH instructions
5. Identify the instructions which affect the flags and also mention which flags are affected.
6. Find the contents of A and B registers and C and OV flag after execution of the program.
Give comment on status of C and OV flags.
7. Write a program to perform addition/subtraction of two 32-bit numbers. Assume that the
first 32 bit numbers is stored at internal RAM address 10H (LSByte) onwards and second
number is stored at internal RAM address 15H (LSByte) onwards. Store the result at
internal RAM address 20H (LSByte) onwards. Also consider status of carry/borrow flag.
8. Discuss the role of overflow flag in multiply and divide instructions.
9. Discuss the use of rotate and swap instructions.
10. What is meant by masking? How it is done? What are its uses?
11. Write the program to complement the lower nibble and set the higher nibble to 1111 of
the byte at external RAM address 100H. Store the resulting bytes in same location.
12. Write program instructions to perform following operations
-1d + 45d, 99d + 35d, -35d + (-35d), -60d + (-70d).
13. Write program instructions to perform following BCD additions
99d + 01d, 75d+45d
LABORATORY 4:

Bit processing instructions

Objectives:

Identify bit addressable address spaces in the 8051


To understand importance and use of bit processing instructions.
To compare the bit level and byte level operations of 8051

Refer example 6.2 and 6.3, Write byte level and bit level instructions for specified operations.
Assemble the instructions and observe the list file and prove that use of bit processing
instructions reduces the memory requirements for the program and makes program execution
faster.

Laboratory exercise:

Answer the following questions after completion of above exercise.

1. Carry flag is also referred as Boolean accumulator. Why?


2. How status of OV flag can be monitored?
3. How does microcontroller differentiate between bit and byte processing instructions?
4. Implement the following function using bit level instructions.
Y = XYZ + X YZ+XYZ
5. List all bit addressable SFRs with addresses assigned to each bit of SFR
LABORATORY 5:

Program flow control instructions

Objectives:

To understand decision making capability of 8051 instructions and transferring program


execution within a program.
To understand looping techniques and software time delay generation
To understand the importance of subroutines and the significance of stack with reference
to subroutines

Execute one or more programs from examples 9.8 to 9.16 and answer the following questions.

Laboratory exercise:

1. Identify the instruction(s) which makes the decision and/or transfers the program
execution to a new address.
2. What do we require to implement the loops?
3. When do we need the infinite loops?
4. Show how loops can be implemented using CJNE and DJNZ instructions.
5. How to implement if-else, while, do-while and for structures of high level language using
assembly language instructions of 8051

Refer example 7.14, modify the program to generate the square wave of 1 KHz on P1.0 and
discuss the factors affecting delay generated by a program.

Refer example7.12, modify this example convert array of 10 binary numbers in to BCD
numbers. Execute the program and answer the following questions

1. What is the contents of SP register and stack before and after ACALL BIN2BCD and
RET instructions.
2. What would be the size of program without using subroutine?
3. Comment on speed of execution of program which uses subroutines.
LABORATORY 6:

Simple 8051 board design and port operations

Objectives

Design simple 8051 based board using minimum components


To illustrate the use of input/output ports

Implement the circuit shown in Figure 11.8 (89C51 based simplest system) on bread board or
PCB, switch on the power supply and observe the waveforms at pins XTAL 2 and ALE and find
the frequency of waveforms and justify that why you got those values.

Write programs to:

1. Read the status of the key and send it to the LED (LED should glow when key is pressed,
otherwise off)
2. Blink the LED every 1second
3. Generate the square wave of 10KHz on P1.0 (use delay generation using software)
4. Generate the square wave of maximum possible frequency on P1.0

Laboratory exercise:

Load the programs (one by one) in to microcontroller on-chip code memory using the
programmer device and observe the output on board. Answer the following questions.

1. What is advantage of using 89C51 over 8051 chip?


2. Why
EA pin is connected to VCC?[ Refer topic 11.2]
3. How brightness of LED can be increased?[Refer topic 18.1]
4. What are the factors affecting the generation of maximum frequency of square wave?
5. What are the other methods (except using programmer device) to load a program in to
on-chip code memory? [Refer topic 3.9]
6. How a port pin can be configures as an input? [Refer topic 13.1.1]
7. What are the current sourcing/sinking capabilities of all port pins of 89C51?[ Refer
datasheet of 89C51]
LABORATORY 7:

The 8051 Programming in C

Objectives:

To develop programs in C using vision 4.0 IDE


To be familiar with additional data types supported by Cx51 compiler to exploit the
features of 8051

Refer Appendix B: Using Keil vision 4.0 IDE (Refer topic Developing and testing program
in C language in second half)

Laboratory exercise:

Execute the sample program given in the Appendix B as explained (and few other programs
from chapter 12) and answer the following questions.

1. Why do we include reg51.h in our C program?


2. What is meant by native word size and list advantages of using variables having
native word size?
3. What are additional data types supported by Cx51 compiler to use features of 8051?
4. What is difference between bit and sbit data types?
5. How to store an array of 10 bytes in code memory?
6. How to implement infinite loops in C language?
7. Show how to access bit addressable area of 8051.
8. What is meant by memory type specifiers and when they are used?
9. How to write interrupt service routines in C program? Discuss the significance of
using attribute.[Refer topic 12.4]
10. Consider example12.26. Write equivalent assembly language program and compare
size of hex file for both programs. [Hex files are created by selecting create hex file
option in Project Options for target Output menu]. Give comment the size of
both files.
11. Discuss briefly how to write efficient C programs.
12. Refer topic 12.12 (Performance comparison between assembly and c programs).
Consider any example from example 12.38 to 12.42, execute it and compare
assembly and C programs for memory requirements and execution time.
LABORATORY 8:

Programming the timer/counters of 8051

Objective:

To understand operation of timer/counters of 8051


To develop programs and applications based on timer/counter.

Timing applications

Execute programs of example 14.5, 14.7, 14.9 and 14.12 to understand operation of timer as
interval timer to generate square wave or rectangular wave.

Observe the corresponding output waveform of square wave on logic analyzer window in
simulator mode [See vision help for details of logic analyzer window and how to use it]
Load the above programs on the 8051 board you have and observe the output (on corresponding
pins) on CRO.

Counting and frequency measurement applications

Execute programs of examples 14.21, 14.24 and 14.25 in vision IDE. Note that external pulse
can be applied in simulator by clicking twice on box of P3.4 for T0 (or P3.5 for T1) of port 3
peripheral window. See that contents of timer registers are incremented after pair of every click
on corresponding box.

Load the program 14.25 on the 8051 board, apply square wave of unknown frequency on T0 pin
and observe the result on port1 and port2.

Laboratory exercise:

1. List actions taken by 8051 when TF flag is raised.


2. Find minimum and maximum time delay that can be generated by all modes of timers
3. What is the maximum frequency that can be measured using timer/counters?
4. Modify program 14.25 such that the unknown frequency is measured using only one
timer. [Hint: use GATE bit to run the timer, apply pulse of 1s on corresponding INT pin
and signal of unknown frequency on T0 pin]
5. List the applications of timers/counters.
LABORATORY 9:

Serial communications using 8051

Objectives

To understand operation of serial port of 8051


To develop programs to transfer data using UART to other devices and execute the
programs using simulator.

Simulation of data transmission

Refer example 15.8, 15.10 and 15.11, execute (single step) these programs and observe output
i.e. transmitted data bytes in serial window (View serial window UART#1) in Vision 4.0
IDE

Simulation of data reception

Refer example 15.9 and 15.13 to simulate the reception of data byte use virtual registers i.e. to
simulate reception of data byte 45H, type command SIN= 0x0045 in command window when
program is waiting for reception of a byte.

Serial data transfer on 8051 based board.

It is considered in laboratory 10 (Refer Testing on hardware in laboratory 10)

Laboratory exercise:

1. Serial communication using UART is asynchronous communication. Why?


2. List common baud rates used for serial communication.
3. What is the significance of using crystal frequency to be 11.0592 MHz?
4. List the steps to select baud rate for serial communications
5. What action will be taken by 8051 when RI or TI is raised?
LABORATORY 10:

Interrupts in the 8051

Objectives

To understand interrupt handling mechanism in 8051


To develop the programs to handle external, timer and serial interrupts.

Timer interrupts

Simulation: Refer example16.2 and 16.4. Execute these programs in simulator and observe
the output in logic analyzer.
Testing on hardware: Load above programs in 8051 based board you have, and execute the
programs and observe the output (square waves) on CRO.

External interrupts

Simulation: Refer program 16.10 and execute it in simulator. External interrupts can be
) and P3.3 (INT1
simulated by clicking on box of P3.2 (for INT0 ) in peripheral window of
port 3.
Testing on hardware: Make connections on 8051 board you have (or on bread board,
include power on reset, crystal connections and EA ) as shown in figure 16.5. Load the
program in on-chip code memory and execute the program.

Serial interrupts

Simulation: Refer examples 16.12, 16.14, 16.15 and execute the them in simulator. Refer
laboratory 9 (Serial communications) to know how to simulate serial data reception and
transmission.
Testing on hardware:
Connect the serial port of 8051 based board with COM port of PC. Execute the programs and
observe the data transmitted by 8051 in Hyper terminal (or Xtalk or equivalent application or
C program) window in PC.

Observe the data frame on CRO.

Implement the figure 16.11 (of Project: Full duplex system at the end of chapter 16) using
two 8051 based boards. Load the programs in both boards and observe the result on LEDs.

Laboratory exercise:

1. Discuss the advantages of using interrupts.[Refer topic 16.1]


2. List the steps taken by 8051 when any of the interrupt is asserted.[Refer topic 16.3]
3. How priority of interrupt sources can be changed?[Refer topic 16.8]
4. Discuss the importance of interrupt response time and interrupt latency. What is
minimum and maximum interrupt latency in 8051? [Refer topic 16.11]
5. Discuss the significance of context saving and switching while dealing with interrupts.
How it is supported by 8051?[Refer topic 16.13]
LABORATORY 11:

Keyboard interfacing

Objectives:

To design and interface 4 x4 matrix keyboard with 8051


To understand key identification and key code generation techniques

Design 4 x 4 matrix keyboard and interface it with 89C51 (or any other variant) as shown in
schematic diagram shown in figure 17.8 (or use the 8051 based board you have). Load the
program given in interfacing example 17.3 in on-chip code memory of 89C51. Press the key and
observe the key code generated for pressed key on port 3.

Laboratory exercise:

Modify the program to generate ASCII code for pressed key.[Refer ex 17.4]
Rewrite the program of interfacing example for 5 x 4 matrix keyboard.
Discuss the importance of human factors that should be considered for key identification
program.
Discuss the significance of debouncing when interfacing mechanical switches. [Observe
the output code of the pressed key without including debounce delay in above program]
LABORATORY 12:

LCD and 7 segment display interfacing

Objective

To understand interfacing of LCD module with 8051


To develop the programs to display message on LCD module

Refer interfacing example 18.8 and interface the LCD module as shown in schematic diagram of
figure18.10. Load the program in the on-chip memory of microcontroller. Observe the output on
LCD module.

Laboratory exercise:

1. Modify above program to display Microcontrollers from beginning of second line of


LCD module.
2. Modify above program so that busy flag is monitored before sending new
command/data.[Refer example 18.11]
3. Interface LCD module in 4 bit mode and write a program to display message 4 bit mode
[Refer figure 18.11]

Refer Project: Simple burglar alarm system at the end of chapter 18 and interface 7 segment
module with 8051 as shown in schematic diagram of figure 18.12. Load the program (given for
the project) in the 8051 and observe the output after pressing input switches.
LABORATORY 13:

Interfacing ADCs and DACs

Objectives

To understand interfacing of ADC and DAC with 8051


To develop the programs to perform analog to digital and digital to analog conversions

Interfacing ADCs

Refer interfacing example 19.4 and interface the ADC 0804chip as shown in schematic diagram
of figure19.2. Load the program in the memory of microcontroller. Apply different analog
voltages using potentiometer and note down equivalent digital output.

Note: These days all new variants of 8051 have on-chip ADCs. Check the datasheet of the
microcontroller you have and modify the above program to perform analog to digital conversion
using on-chip ADC. [Refer user manual of the board you have in your laboratory]

Laboratory exercise:

1. Change the VREF/2 and observe the digital output and see how the input voltage range
(and resolution) of ADC can be varied by VREF/2.
2. Repeat above experiment for multichannel ADC like ADC 0808.[Refer example 19.7]
3. Repeat the experiment for serial ADC like MAX1112/13[Refer interfacing example 19.8]
4. Write a program to read analog value at the interval of 1 second and store it in internal
RAM location 10H onwards. Store total 20 values. Apply low frequency sinewave as
analog input
5. Discuss the factor affecting selection of ADC.

Interfacing DACs

Refer Interfacing example 19.14 and interface the DAC AD557 chip as shown in schematic
diagram of figure19.8. Load the program in the memory of microcontroller. Observe the output
of DAC on CRO.
OR
Refer Interfacing example 19.16 and interface the DAC 0808 chip as shown in schematic
diagram of figure19.11. Load the program in the memory of microcontroller. Observe the output
of DAC on CRO.

Laboratory exercise:

1. Modify the program of interfacing example 19.14 to generate more accurate sine
wave.[Hint: make lookup table for more samples of sine wave.
2. Modify above program to change the frequency of sine (or sawtooth) wave.
3. Modify above program to generate different waveforms [Refer Project: Function
generator at the end of the chapter 19]
LABORATORY 14:

Interfacing DC and stepper motors with 8051

Objectives:

To understand interfacing of DC and stepper motors with 8051


To develop the programs to rotate the motors in both directions with different speeds

Interfacing DC motors

Refer interfacing example 20.9 and interfacing example 20.11. Interface the DC motors as shown
in schematic diagram of figure 20.11 and figure 20.14 Load the programs in the memory of
microcontroller. Run the programs.

Note: H-bridge is available in single IC form such as L293 (Quadruple half H drives, output
current 1A per channel). IC A3952 (Full bridge PWM motor driver) can also be used for
direction control of DC motor. Refer datasheets of these chips for their connections with DC
motors. Choose suitable driver circuit as per ratings of the motor.

Interfacing stepper motors

Refer interfacing example 20.1 and interface stepper motor with 8051 as shown in figure 20.8.
Load the program in to code memory of microcontroller and run the program. Choose suitable
driver circuit as per ratings of the motor.

Laboratory exercise:

1. Modify program of interfacing example 20.1 to rotate motor in anticlock wise


direction[Refer example 20.3]
2. Modify above program to rotate motor with wave drive sequence[Refer example 20.5]
3. Modify above program to rotate motor with half step sequence[Refer example 20.7]
4. Write a program to rotate stepper motor by fixed angle [Refer example 20.8]

You might also like