0% found this document useful (0 votes)
39 views49 pages

Micro Controller Lab Manual AMC Updated

The Microcontroller Lab Manual outlines the curriculum for the Microcontroller Lab course at AMC Engineering College for the academic year 2024-25, detailing various experiments and their objectives. Key topics include data transfer, arithmetic instructions, interfacing with motors and displays, and the 8051 instruction set. The manual serves as a comprehensive guide for students to understand and implement microcontroller programming and interfacing techniques.

Uploaded by

bhavanask16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views49 pages

Micro Controller Lab Manual AMC Updated

The Microcontroller Lab Manual outlines the curriculum for the Microcontroller Lab course at AMC Engineering College for the academic year 2024-25, detailing various experiments and their objectives. Key topics include data transfer, arithmetic instructions, interfacing with motors and displays, and the 8051 instruction set. The manual serves as a comprehensive guide for students to understand and implement microcontroller programming and interfacing techniques.

Uploaded by

bhavanask16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Microcontroller Lab Manual

AMC ENGINEERING COLLEGE


18th K.M.,Bannerghatta Road Bangalore-560083

(Affiliated to VTU,Belgaum and recognized by AICTE,New Delhi)

DEPARTMENT OF ELECTRICAL & ELECTRONICS


ENGINEERING

IV SEMESTER B.E.
LABORATORY MANUAL

BEE 403 – MICROCONTROLLER LAB


Academic year: 2024-25

Staff in charge:

Mr.ANANTHAKUMAR
Asst. Professor

NAME OF THE STUDENT :

BRANCH :

UNIVERSITY SEAT NO. :

SEMESTER & SECTION :

1
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

LIST OF EXPERIMENTS

Date of Faculty Total


KEIL
Sl.No Name of the Experiment completion Sign Marks
Marks
Data Transfer
a. Block Move
1 b. Exchange
c. Sorting
d. Find largest element in an array
Arithmetic Instruction
a. Addition
Subtraction
2 Multiplication
Division
b. Square
Cube
Counters
a. Hex / Binary UP Counter
3
b. Hex / Binary down counter
c. BCD UP counter
Boolean & Logical Instruction
4
(Bit manipulation)
5 Conditional CALL & RETURN
Code Conversion
a. BCD – ASCII
b. ASCII-BCD
6 c. ASCII – Decimal
d. Decimal – ASCII
e. Hex – Decimal
f. Decimal – Hex
Serial port communication & on
7
chip timer / counter
INTERFACE EXPERIMENTS

8
Stepper Motor Interface
DC motor interface for
9
direction and speed control
a. Sine
b. Square
10
c. Triangular
d. Ramp generator
11 Elevator

2
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

MICROCONTROLLER LABORATORY-1

SUBJECT: MICROCONTROLLERS LAB EXAM HOURS: 03


(Common to EE, EC, IT, TC, BM and ML) EXAM MARKS: 60

Experiments
Note: For the experiments 1 to 6, 8051 assembly programming is to be used.
1 Data transfer – Program for block data movement, sorting, exchanging, finding largest element
in an array.
2 Arithmetic instructions: Addition, subtraction, multiplication and division. Square and cube
operations for 16 bit numbers.
3 Counters
4 Boolean and logical instructions (bit manipulation).
5 Conditional call and return instructions.
6 Code conversion programs – BCD to ASCII, ASCII to BCD, ASCII to decimal, Decimal to
ASCII, Hexa decimal to and Decimal to Hexa.
7 Programs to generate delay, Programs using serial port and on-chip timer/counters.

Note: Single chip solution for interfacing 8051 is to be with C Programs for the following
experiments.
II. INTERFACING

8 Stepper motor interface.


9 DC motor interface for direction and speed control using PWM.
10 Alphanumerical LCD panel interface.
11 Generate different waveforms: Sine, Square, Triangular, Ramp using DAC interface.
12 External ADC and Temperature control interface.
13 Elevator interface.

3
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

The 8051 Instruction Set


The process of writing program for the microcontroller mainly consists of giving instructions
(commands) in the specific order in which they should be executed in order to carry out a specific task. As
electronics cannot “understand” what for example an instruction “if the push button is pressed- turn the light
on” means, then a certain number of simpler and precisely defined orders that decoder can recognize must
be used. All commands are known as INSTRUCTION SET. All microcontrollers compatible with the 8051
have in total of 255 instructions, i.e. 255 different words available for program writing.

At first sight, it is imposing number of odd signs that must be known by heart. However, it is not as
complicated as it looks like. Many instructions are considered to be “different”, even though they perform
the same operation, so there are only 111 truly different commands. For example: ADD A, R0, ADD A,
R1... ADD A, R7 are instructions that perform the same operation (addition of the accumulator and register).
Since there are 8 such registers, each instruction is counted separately. Taking into account that all
instructions perform only 53 operations (addition, subtraction, copy etc.) and most of them are rarely used
in practice, there are actually 20-30 abbreviations to be learned, which is acceptable.

Types of instructions
Depending on operation they perform, all instructions are divided in several groups:

 Arithmetic Instructions
 Branch Instructions
 Data Transfer Instructions
 Logic Instructions
 Bit-oriented Instructions

The first part of each instruction, called MNEMONIC refers to the operation an instruction performs
(copy, addition, logic operation etc.). Mnemonics are abbreviations of the name of operation being executed.
For example:

INC R1 - Increment register R1 (increment register R1)


LJMP LAB5 - Long Jump LAB5 (long jump to the address marked as LAB5)
JNZ LOOP - Jump if Not Zero LOOP (if the number in the accumulator is not 0,
jump to the address marked as LOOP)
The other part of instruction, called OPERAND is separated from mnemonic by at least one white
space and defines data being processed by instructions. Some of the instructions have no operand, while
some of them have one, two or three. If there is more than one operand in an instruction, they are separated
by a comma. For example:

RET - return from a subroutine


JZ TEMP - if the number in the accumulator is not 0, jump to the address marked
as TEMP
ADD A,R3 - add R3 and accumulator
CJNE A,#20,LOOP - compare accumulator with 20. If they are not equal, jump to the
address marked as LOOP

4
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Arithmetic instructions

Arithmetic instructions perform several basic operations such as addition, subtraction, division,
multiplication etc. After execution, the result is stored in the first operand. For example: ADD A,R1 - The
result of addition (A+R1) will be stored in the accumulator.

Arithmetic Instructions
Mnemonic Description Byte Cycle
ADD A,Rn Adds the register to the accumulator 1 1
ADD A,direct Adds the direct byte to the accumulator 2 2
ADD A,@Ri Adds the indirect RAM to the accumulator 1 2
ADD A,#data Adds the immediate data to the accumulator 2 2
ADDC A,Rn Adds the register to the accumulator with a carry flag 1 1
ADDC
Adds the direct byte to the accumulator with a carry flag 2 2
A,direct
ADDC
Adds the indirect RAM to the accumulator with a carry flag 1 2
A,@Ri
ADDC Adds the immediate data to the accumulator with a carry
2 2
A,#data flag
SUBB A,Rn Subtracts the register from the accumulator with a borrow 1 1
SUBB Subtracts the direct byte from the accumulator with a
2 2
A,direct borrow
Subtracts the indirect RAM from the accumulator with a
SUBB A,@Ri 1 2
borrow
SUBB Subtracts the immediate data from the accumulator with a
2 2
A,#data borrow
INC A Increments the accumulator by 1 1 1
INC Rn Increments the register by 1 1 2
INC Rx Increments the direct byte by 1 2 3
INC @Ri Increments the indirect RAM by 1 1 3
DEC A Decrements the accumulator by 1 1 1
DEC Rn Decrements the register by 1 1 1
DEC Rx Decrements the direct byte by 1 1 2
DEC @Ri Decrements the indirect RAM by 1 2 3
INC DPTR Increments the Data Pointer by 1 1 3
MUL AB Multiplies A and B 1 5
DIV AB Divides A by B 1 5
Decimal adjustment of the accumulator according to BCD
DA A 1 1
code

Branch Instructions: There are two kinds of branch instructions:

5
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Unconditional jump instructions: upon their execution a jump to a new location from where the program
continues execution is executed.

Conditional jump instructions: a jump to a new program location is executed only if a specified
condition is met. Otherwise, the program normally proceeds with the next instruction.

Branch Instructions
Mnemonic Description Byte Cycle
ACALL
Absolute subroutine call 2 6
addr11
LCALL
Long subroutine call 3 6
addr16
RET Returns from subroutine 1 4
RETI Returns from interrupt subroutine 1 4
AJMP addr11 Absolute jump 2 3
LJMP addr16 Long jump 3 4
Short jump (from –128 to +127 locations relative to the
SJMP rel 2 3
following instruction)
JC rel Jump if carry flag is set. Short jump. 2 3
JNC rel Jump if carry flag is not set. Short jump. 2 3
JB bit,rel Jump if direct bit is set. Short jump. 3 4
JBC bit,rel Jump if direct bit is set and clears bit. Short jump. 3 4
JMP
Jump indirect relative to the DPTR 1 2
@A+DPTR
JZ rel Jump if the accumulator is zero. Short jump. 2 3
JNZ rel Jump if the accumulator is not zero. Short jump. 2 3
CJNE Compares direct byte to the accumulator and jumps if not
3 4
A,direct,rel equal. Short jump.
CJNE Compares immediate data to the accumulator and jumps if
3 4
A,#data,rel not equal. Short jump.
CJNE Compares immediate data to the register and jumps if not
3 4
Rn,#data,rel equal. Short jump.
CJNE Compares immediate data to indirect register and jumps if
3 4
@Ri,#data,rel not equal. Short jump.
DJNZ Rn,rel Decrements register and jump if not 0. Short jump. 2 3
DJNZ Rx,rel Decrements direct byte and jump if not 0. Short jump. 3 4
NOP No operation 1 1

Data Transfer Instructions


Data transfer instructions move the content of one register to another. The register the content of which is
moved remains unchanged. If they have the suffix “X” (MOVX), the data is exchanged with external
memory.

6
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Data Transfer Instructions


Mnemonic Description Byte Cycle
MOV A,Rn Moves the register to the accumulator 1 1
MOV A,direct Moves the direct byte to the accumulator 2 2
MOV A,@Ri Moves the indirect RAM to the accumulator 1 2
MOV A,#data Moves the immediate data to the accumulator 2 2
MOV Rn,A Moves the accumulator to the register 1 2
MOV
Moves the direct byte to the register 2 4
Rn,direct
MOV
Moves the immediate data to the register 2 2
Rn,#data
MOV direct,A Moves the accumulator to the direct byte 2 3
MOV
Moves the register to the direct byte 2 3
direct,Rn
MOV
Moves the direct byte to the direct byte 3 4
direct,direct
MOV
Moves the indirect RAM to the direct byte 2 4
direct,@Ri
MOV
Moves the immediate data to the direct byte 3 3
direct,#data
MOV @Ri,A Moves the accumulator to the indirect RAM 1 3
MOV
Moves the direct byte to the indirect RAM 2 5
@Ri,direct
MOV
Moves the immediate data to the indirect RAM 2 3
@Ri,#data
MOV
Moves a 16-bit data to the data pointer 3 3
DPTR,#data
MOVC Moves the code byte relative to the DPTR to the
1 3
A,@A+DPTR accumulator (address=A+DPTR)
MOVC Moves the code byte relative to the PC to the accumulator
1 3
A,@A+PC (address=A+PC)
MOVX
Moves the external RAM (8-bit address) to the accumulator 1 3-10
A,@Ri
MOVX Moves the external RAM (16-bit address) to the
1 3-10
A,@DPTR accumulator
MOVX
Moves the accumulator to the external RAM (8-bit address) 1 4-11
@Ri,A
MOVX Moves the accumulator to the external RAM (16-bit
1 4-11
@DPTR,A address)
PUSH direct Pushes the direct byte onto the stack 2 4
POP direct Pops the direct byte from the stack/td> 2 3
XCH A,Rn Exchanges the register with the accumulator 1 2

7
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

XCH A,direct Exchanges the direct byte with the accumulator 2 3


XCH A,@Ri Exchanges the indirect RAM with the accumulator 1 3
XCHD Exchanges the low-order nibble indirect RAM with the
1 3
A,@Ri accumulator

Logic Instructions
Logic instructions perform logic operations upon corresponding bits of two registers. After execution, the
result is stored in the first operand.

Logic Instructions
Mnemonic Description Byte Cycle
ANL A,Rn AND register to accumulator 1 1
ANL A,direct AND direct byte to accumulator 2 2
ANL A,@Ri AND indirect RAM to accumulator 1 2
ANL A,#data AND immediate data to accumulator 2 2
ANL direct,A AND accumulator to direct byte 2 3
ANL
AND immediate data to direct register 3 4
direct,#data
ORL A,Rn OR register to accumulator 1 1
ORL A,direct OR direct byte to accumulator 2 2
ORL A,@Ri OR indirect RAM to accumulator 1 2
ORL direct,A OR accumulator to direct byte 2 3
ORL
OR immediate data to direct byte 3 4
direct,#data
XRL A,Rn Exclusive OR register to accumulator 1 1
XRL A,direct Exclusive OR direct byte to accumulator 2 2
XRL A,@Ri Exclusive OR indirect RAM to accumulator 1 2
XRL A,#data Exclusive OR immediate data to accumulator 2 2
XRL direct,A Exclusive OR accumulator to direct byte 2 3
XORL
Exclusive OR immediate data to direct byte 3 4
direct,#data
CLR A Clears the accumulator 1 1
CPL A Complements the accumulator (1=0, 0=1) 1 1
SWAP A Swaps nibbles within the accumulator 1 1
RL A Rotates bits in the accumulator left 1 1
RLC A Rotates bits in the accumulator left through carry 1 1
RR A Rotates bits in the accumulator right 1 1
RRC A Rotates bits in the accumulator right through carry 1 1

Bit-oriented Instructions

8
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Similar to logic instructions, bit-oriented instructions perform logic operations. The difference is that these
are performed upon single bits.

Bit-oriented Instructions
Mnemonic Description Byte Cycle
CLR C Clears the carry flag 1 1
CLR bit Clears the direct bit 2 3
SETB C Sets the carry flag 1 1
SETB bit Sets the direct bit 2 3
CPL C Complements the carry flag 1 1
CPL bit Complements the direct bit 2 3
ANL C,bit AND direct bit to the carry flag 2 2
ANL C,/bit AND complements of direct bit to the carry flag 2 2
ORL C,bit OR direct bit to the carry flag 2 2
ORL C,/bit OR complements of direct bit to the carry flag 2 2
MOV C,bit Moves the direct bit to the carry flag 2 2
MOV bit,C Moves the carry flag to the direct bit 2 3
Here is a list of the operands and their meanings:

A- accumulator;
Rn - is one of working registers (R0-R7) in the currently active RAM memory bank;
Direct - is any 8-bit address register of RAM. It can be any general-purpose register or a SFR (I/O
port, control register etc.);
@Ri - is indirect internal or external RAM location addressed by register R0 or R1;
#data - is an 8-bit constant included in instruction (0-255);
#data16 - is a 16-bit constant included as bytes 2 and 3 in instruction (0-65535);
addr16 - is a 16-bit address. May be anywhere within 64KB of program memory;
addr11 - is an 11-bit address. May be within the same 2KB page of program memory as the first byte
of the following instruction;
rel - is the address of a close memory location (from -128 to +127 relative to the first byte of the
following instruction). On the basis of it, assembler computes the value to add or subtract
from the number currently stored in the program counter;
bit - is any bit-addressable I/O pin, control or status bit; and
C- is carry flag of the status register (register PSW).

Addressing Modes

Instructions tell the processor which operation to carry out. For example, MOV A, #5EH tells the
processor to move the data 5EH to the accumulator.
Instructions result in the processor performing some operation on some data (the data being #5EH in the
example above). The instruction above is an example of what's known as immediate addressing. The
reason for this is because the data immediately follows the instruction in code memory.
However, it is not always adequate to store the data in code memory itself. If we needed to read the
information from a keyboard, for example, we would need to use something other than immediate
addressing.

9
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

There are eight different addressing modes in the 8051.

Opcode and Operand

The first byte of an instruction is known as the opcode (operation code) because this is the byte that is
decoded by the processor - from this code the processor can work out what operation it must perform.
For a one-byte instruction there is only the opcode.
For a two-byte instruction the second byte is the operand. The operand can be data or an 8-bit address.
For a three-byte instruction the second and third bytes make up the operand. A two-byte operand is usually
a 16-bit address, as we shall see when we look at long addressing.

The eight addressing modes are:

 Immediate
 Register
 Direct
 Indirect
 Relative
 Absolute
 Long
 Indexed

Immediate Addressing
If the operand is a constant then it can be stored in memory immediately after the opcode. Remember,
values in code memory (ROM) do not change once the system has been programmed and is in use in the
everyday world. Therefore, immediate addressing is only of use when the data to be read is a constant.
For example, if your program needed to perform some calculations based on the number of weeks in the
year, you could use immediate addressing to load the number 52 (34H) into a register and then perform
arithmetic operations upon this data.
MOV R0, #34
The above instruction is an example of immediate addressing. It moves the data 34H into R0. The
assembler must be able to tell the difference between an address and a piece of data. The has symbol (#)
is used for this purpose (whenever the assembler sees # before a number it knows this is immediate
addressing).
This is a two-byte instruction.

Register Addressing
Often we need to move data from a register into the accumulator so that we can perform arithmetic
operations upon it. For example, we may wish to move the contents of R5 into the accumulator.
MOV A, R5

10
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

This is an example of register addressing. It moves data from R5 (in the currently selected register bank)
into the accumulator.
ADD A, R6
The above is another example of register addressing. It adds the contents of R6 to the accumulator, storing
the result in the accumulator. Note that in both examples the destination comes first. This is true of all
instructions.

Direct Addressing
Direct addressing is used for accessing data in the on-chip RAM. Since there are 256 bytes of RAM (128
bytes general storage for the programmer and another 128 bytes for the SFRs). That means the addresses
go from 00H to FFH, any of which can be stored in an 8-bit location.
MOV A, 67
The above instruction moves the data in location 67H into the accumulator. Note the difference between
this and immediate addressing. Immediate addressing uses the data, which is immediately after the
instruction. With direct addressing, the operand is an address. The data to be operated upon is stored in
that address. The assembler realises this is an address and not data because there is no hash symbol before
it.
ADD A, 06
The above instruction adds the contents of location 06H to the accumulator and stores the result in the
accumulator. If the selected register bank is bank 0 then this instruction is the same as ADD A, R6.

Indirect Addressing
Register addressing and direct addressing both restrict the programmer to manipulation of data in fixed
addresses. The address the instruction reads from (MOV A, 30H) or writes to (MOV 30H, A) cannot be
altered while the program is running.
There are times when it is necessary to read and write to a number of contiguous memory locations. For
example, if you had an array of 8-bit numbers stored in memory, starting at address 30H, you may wish
to examine the contents of each number in the array (perhaps to find the smallest number). To do so, you
would need to read location 30H, then 31H, then 32H and so on.
This can be achieved using indirect addressing. R0 and R1 may be used as pointer registers. We can use
either one to store the current memory location and then use the indirect addressing instruction shown
below.
MOV A, @Ri
where Ri is either R0 or R1.
Now, we can read the contents of location 30H through indirect addressing:
MOVR0,#30H
MOV A, @R0
The first instruction is an example of immediate addressing whereby the data 30H is placed in R0. The
second instruction is indirect addressing. It moves the contents of location 30H into the accumulator.
If we now wish to get the data in location 31H we use the following:
INC R0
MOV A, @R0

11
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Once we see how to write a loop in assembly language, we will be able to read the entire contents of the
array.

Relative Addressing
Relative addressing is used only with certain jump instructions. The system executes a jump by changing
the contents of the PC to the address of the next instruction to be executed. For example, if we wished to
jump to the instruction stored at location 4EH in code memory, the PC would be loaded with 4EH. Then,
during the next execution cycle the contents of the PC (4EH) are placed on the address bus and the
instruction at 4EH is retrieved.
A relative address (or offset) is an 8-bit signed value, which is added to the PC to form the address of the
next instruction to be executed.
With 8-bit signed numbers, the MSB is used to determine whether the number is positive or negative. If
the MSB is 0 then the number is positive, while if the MSB is 1 the number is negative.
The instruction below shows how to jump six locations ahead.
SJMP 06H
SJUMP is an unconditional jump and is a 2-byte instruction. The number following it is an offset address.
If this instruction were stored in code memory at locations 100H and 101H, as shown below:
100H80H
101H06H
The opcode for SJMP is 80H. The operand is the offset address. If this instruction were executed the PC
would get the value 108H. This is what happens:
 The PC contains 100H, therefore the instruction 80H is read into the IR.
 The instruction is decoded as the 2-byte SJMP instruction.
 The PC is incremented so that the operand may be retrieved.
 The operand is read from code memory and the PC is incremented again (because this is a 2-bye
instruction).
 The operand (06H) is added to the PC (102H + 06H = 108H).
 The next instruction (at 108H) is executed.

Once we deal with 2's compliment and how negative numbers are dealt with in the CPU, we will look at
a backward jump.
The S in SJMP stands for short. The range of signed 8-bit numbers is -127 to 128. Therefore, using SJMP
allows us to jump 127 locations forward or 128 locations backward. Hence the nameshort jump.
When writing assembly programs we do not need to calculate the offset when using SJMP. Instead, we
use labels. If we wished to jump to the instruction at 108H we would simply label the instruction with an
appropriate name, for example THERE. We would then write the assembly code SJMP THERE. The
assembler does the work of replacing the label with the correct offset.

Absolute Addressing
Absolute addressing is only used with the ACALL and AJMP instructions.
ACALL - subroutine call (2 byte instruction)
AJMP - unconditional jump (2 byte instruction)
These instructions allow you to move to any instruction within the same 2K of memory.

12
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

We will look at the AJMP instruction only (at a later date, when we begin dealing with subroutines we
will deal with the ACALL instruction).

The operation of the AJMP instruction is detailed below:


AJMP address
(PC) <- (PC) + 2
(PC10-PC0) <- address10 - address0

Note that only the eleven least significant bits of the PC are altered. The five most significant bits remain
the same. This means the AJMP will only allow you to jump to a location in the same 2K page as the
instruction directly after the jump.

For example:

If the label THERE represents an instruction at address 0F46H and the instruction AJMP THERE is in
memory at locations 0900H and 0901H, the assembler will encode the instruction as
11100001 1st byte (A10 - A8 + opcode)
010001102nd byte (A7 - A0)
The underlined bits are the low-order 11 bits of the destination address, 0F46H = 0000111101000110B.
The upper five bits in the program counter will not change when this instruction executes. Note that both
the AJMP instruction and the destination are within the 2K page bounded by 0800H and 0FFFH, and
therefore have the upper five address bits in common.

SOFTWARE DESCRIPTION

PROCESSOR used is Atmel AT89C51ED2 - micro controller that has 64Kbytes of on chip program
memory. It is a version of 8051 with enhanced features.
AT 89C51ED2 operates at 11.0592 MHz.

PROCESSOR FEATURES

ON-CHIP MEMORY: CODE MEMORY: 64K Bytes of flash.

DATA MEMORY: 256 Bytes of RAM, 1792 Bytes of XRAM, 2K Bytes of EEPROM.

ON-CHIP PERIPHERALS
3 16-bit Timers/Counters, Watch Dog Timer, Programmable Counter Array (PCA) on Port1 i.e. PWM and
Capture & Compar

INTERRUPTS
Nine sources of interrupt (both external and internal).

13
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Two External interrupts INT0 and INT1 are provided with push button switches; these can also be used as
general-purpose switches.

I/O (Port) Lines


Four 10-pin connectors for all the 32 I/O lines. P0, P1 and P2 Port lines are available on a 26-pin
connector, 16X2 LCD & SERIAL I/O are also available.

Creating project

1. Double Click on the icon on the desktop.

2. Close any previous projects that were opened using – Project->Close.

3. Start Project – New Project, and select the CPU from the device database (Database-Atmel-
AT89C51ED2). (Select AT89C51ED2 or AT89C51RD2 as per the board).On clicking ‘OK’, the following
option is displayed. Choose Yes.

4. Create a source file (using File->New), type in the assembly or C program and save this (filename.asm/
filename.c) and add this source file to the project using either one of the following two methods.
(i) Project-Components,Environmentand Books->addfiles->browse to the required file -> OK “OR”
(ii) right click on the Source Group in the Project Window and the Add Files to Group option.

5. Set the Target options using ->Project – Options for Target


Vision4 Options for Target – Target configuration dialog. Set the crystal frequency as 11.0592 Mhz, and
also the Options for Target – Debug – use either Simulator / Keil Monitor- 51 driver. If Keil Monitor-
51 driver is used click on Settings ->COMPort settings select the COMPort to which the board is connected
and select the baud rate as 19200 or 9600 (recommended). Enable Serial Interrupt option if the user
application is not using on chip UART, to stop program execution.

6. Build the project; using Project -> Build Project.


Any errors in the code are indicated by – “Target not created” in the Build window, along with the error
line. Debug the errors. After an error free build, go to Debug mode.
7. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog or by clicking in
the icon.

8. The program is run using the Debug-Run command & halted using Debug-Stop Running. Also the
(reset, run, halt) icons can be used. Additional icons are (step, step over, step into, run till cursor).

9. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led status etc. If it is a
part A program, the appropriate memory window is opened using View ->memory window (for data RAM
& XRAM locations), Watch window (for timer program),serial window, etc.
Note: To access data RAM area type address as D:0020h.
Similarly to access the DPTR region (XRAM-present on chip in AT89C51ED2) say 9000h location type
in X:09000H.

Creating and compiling a μVision3 project

1. Double Click on the μVision3 icon on the desktop.

2. Close any previous projects that were opened using – Project->Close.


14
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

3. Start Project – New Project, and select the CPU from the device database (Database-Philips-
89C51RD2+). On clicking ‘OK’, the following option is displayed. Choose Yes.

4. Create a source file (using File->New), type in the assembly or C program and save this (filename.asm/
filename.c) and add this source file to the project using either one of the following two methods.

(i) Project-Components,Environmentand Books->addfiles-> browse to the required file -> OK


“OR”
(ii) right click on the Source Group in the Project Window and the Add Files to Group option

5. Build the project; using Project -> Build Project.


Any errors in the code are indicated by – “Target not created” in the Build window, along with the
error line. Debug the errors. After an error free build, go to Debug mode.

7. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog or by clicking in
the icon.

8 .The program is run using the Debug-Run command & halted using Debug-Stop Running. Also the

(reset, run, halt) icons can be used. Additional icons are (step, step
over, step into, run till cursor).

15
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

1. (a) DATA TRANSFER – BLOCK MOVE

Write an ALP to transfer 10 bytes of data from memory location 8035H to 8062H

Program algorithm:
1. Initialize register to hold source, destination addresses, data and count values.
2. Obtain data from source location into accumulator and from accumulator into the destination
location.
3. Decrement the count of register and repeat step 2 till the count value reads zero.

Label Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV DPH,#80H Load the DPH with 8 bit constant
MOV RO,#35H Move immediate data to register
MOV R1,#62H Move immediate data to register
MOV R3,#05H Move immediate data to register
BACK: MOV DPL,RO Load DPL with R0
MOVX A,@DPTR Move data from ext.RAM to acc
MOV DPL,R1 Load DPL with R1
MOVX @DPTR,A Move acc data to ext.RAM
INC R0 Increment register
INC R1 Increment register
DJNZ R3,BACK Decrement register & jump if not
zero
HERE: SJMP HERE Short jump – relative address
END End

PROCEDURE:

1. Create the project and save it.


2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Enter 5 bytes of data in the sourcr memory starting from address 8035H.
9. Run the program to view the data in the destination address 8062H.
10. Exit debug mode.

16
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Result:TRIAL I:
Source:
X:8035H 8036H 8037H 8038H 8039H
2 4 6 8 9

Destination:
X:8062H 8063H 8064H 8065H 8066H
2 4 6 8 9

TRIAL II:

1 (b) DATA TRANSFER – EXCHANGE

Write an ALP to exchange 5 bytes of data from memory location 32H to 62H

Program algorithm:
1. Initialize register to hold source, destination addresses, data and count values.
2. Obtain data from source location into accumulator and save in a register.
3. Obtain data from destination location into accumulator.
4. Exchange the data at the memory location.
5. Decrement the count of register and repeat from step 2 to 4 till the count value reads zero.

Label Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV DPH,#32H Load the DPH with 8 bit constant
MOV RO,#32H Move immediate data to register
MOV R1,#62H Move immediate data to register
MOV R3,#05H Move immediate data to register
BACK: MOVX A,@R0 Move ext. RAM to A
MOV R2,A Move acc to reg
MOVX A,@R1 Move ext. RAM R1 to A
MOVX @R0,A Move A to ext.RAM
MOV A,R2 Move R2 to A
MOVX @R1,A Move A to ext RAM
INC R0 Increment register
INC R1 Increment register
DJNZ R3,BACK Decrement register & jump if not zero
HERE: SJMP HERE Short jump – relative address
END End

17
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

PROCEDURE:

1. Create the project and save it.


2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Enter 5 bytes of data A at 32H and data B at 62H.
9. Run the program to view the data A at 62H & data B at 32H.
10. Exit debug mode.

Result: TRIAL I: Before execution

Source:
X:32H 33H 34H 35H 36H
2 4 6 8 9
Destination:
X:62H 63H 64H 65H 66H
4 7 8 9 E

After execution
Source:
X:32H 33H 34H 35H 36H
4 7 8 9 E

Destination:
X:62H 63H 64H 65H 66H
2 4 6 8 9

1 (C) DATA TRANSFER – BUBBLE SORTING

Write an ALP to sort 5 bytes of data in descending order stored at memory location 9000H

Program algorithm:
1. Store the elements of the array from the address 9000H
2. Initialize a pass counter with array size -1 count.
3. Load compare counter with pass counter contents and initialize DPTR to point to the start address
4. Store the current and the next array elements pointed by DPTR in reg B & R2
5. Subtract the next element from the current element.
6. If the carry flag is not set then exchange the 2 no’s in the array.
7. Decrement the compare counter and repeat through step 4 until the counter becomes zero.
8. Decrement the pass counter and repeat through step 3 until the counter becomes zero.

18
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Assembly program:

Label Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV R0,#05H Mov R0 = 05H
L1: MOV DPTR,#9000H DPTR=9000H
MOV A,R0 Move R0 to A
MOV R1,A Move A to R1
L2: MOVX A,@DPTR Move ext.DPTR to A
MOV B,A Move A to B
INC DPTR Increment DPTR
MOVX A,@DPTR Move DPTR to A
CLR C Clear carry flag
MOV R2,A Move A to R2
SUBB A,B Subtract borrow
JNC for ascending JC noexchg Jump if carry to label
MOV A,B Move B to A
MOVX @DPTR,A Move A to DPTR
DEC DPL Decrement DPL
MOV A,R2 Move R2 to A
MOVX @DPTR,A Move A to DPTR
INC DPTR Increment DPTR
noexchg: DJNZ R1,L2 Decrement jump if non zero
DJNZ R0,L1 Decrement jump if non zero
HERE: SJMP HERE Short jump – relative address
END End

PROCEDURE:

1. Create the project and save it.


2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Load 5 bytes of data in random at address 9000H.
9. Run the program to view stored data in descending at 9000H.
10. Exit debug mode.

19
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Result: TRIAL I: before execution

X:9000H 9001H 9002H 9003H 9004H


12 34 08 FA 0B
After execution:

X:9000H 9001H 9002H 9003H 9004H


FA 34 12 0B 08

1(D) DATA TRANSFER – LARGEST

Write an ALP to find the largest number in an array of 6 bytes of data at location 5000H.Store the
largest element at location 5052H.

Program algorithm:
1. Store the elements of the array from the address 5000H
2. Store the length of the array in R3 and set it as counter.
3. DPTR is loaded with starting address of the array.
4. Store the first number of the array in R1 where R1 is assigned to hold the largest number.
5. Increment DPTR.
6. Subtract the number pointed by DPTR from the contents of R1 for comparison if the next
array element is larger than the one in R1.
7. If the element pointed by DPTR is larger then load the larger number intoR1.
8. Decrement the counter and repeat steps through 5 until the counter becomes zero.
9. Store the largest number in R1 in address 5052H

Label: Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV R3,#06H Mov R3 = 06H
MOV DPTR,#5000H DPTR=5000H
MOVX A,@DPTR Move DPTR to A
MOV R1,A Move A to R1
ABOVE: INC DPTR Increment DPTR
MOVX A,@DPTR Move DPTR to A
CLR C Clear carry
MOV R2,A Move A to R2
SUBB A,R1
JNC for smallest JC SKIP
MOV A,R2
MOV R1,A
SKIP: DJNZ R3,ABOVE
MOV DPL,#52H
MOV A,R1
MOVX @DPTR,A
HERE: SJMP HERE Short jump – relative address
END End

20
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

PROCEDURE:

1. Create the project and save it.


2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Load 5 bytes of data in random at address 5000H.
9. Run the program to view the largest data stored at 5052H.
10. Exit debug mode.

Result:

TRIAL I: before execution

X:5000H 5001H 5002H 5003H 5004H


34 23 FA 08 11

After execution:

5052H
FA

2 (a) ARITHMETIC OPERATIONS (addition,subtraction,multiplication,division)


Write an ALP to perform the following
If x=0 perform w + v If x=1 perform w - v
If x=2 perform w * v If x=3 perform w / v where w & v are 8 bit number’s

PROGRAM ALGORITHM:
1. Store the condition x in R1.
2. Load the first & second numbers to A & B registers.
3. Compare the contents of R1 and perform the operations.
4. Store the result present in A & B reg. to the appropriate memory locations.

Label: Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV R0,#40H Move immediate data to R0
MOVX A,@R0 Move ext R0 to A
MOV R1,A Move A to R1
INC R0 Increment R0
MOVX A,@R0 Move R0 to A
MOV B,A Move A to B
INC R0
MOVX A,@R0 Move ext RAM R0 to A

21
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

CJNE R1,#00,CKSUB
ADD A,B Add B to A
MOV B,#00 Move imm data to B
JNC SKIP Jump
MOV B,#01H Move imm data to B
SKIP: SJMP LAST Short jump
CKSUB: CJNE R1,#01,CKMUL
CLR C Clear carry
SUBB A,B Subtract borrow
MOV B,#00 Movimm data to B
JNC SKIP1
MOV B,#0FFH
SKIP1: SJMP LAST
CKMUL: CJNE R1,#02,CKDIV
MUL AB Multiply AB
SJMP LAST
CKDIV: CJNE R1,#03,OTHER
DIV AB Divide AB
SJMP LAST
OTHER: MOV A,#00
MOV B,#00 Move imm data to B
LAST: INC R0 Increment R0
MOVX @R0,A Move A to ext RAM
INC R0 Increment R0
MOV A,B Move B to A
MOVX @R0,A Move A to ext RAM
HERE: SJMP HERE
END

PROCEDURE:

1. Create the project and save it.


2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.

TRAIL I: before execution - ADD

0040H 0041H 0042H 0043H


00 10 40 00

After execution
0040H 0041H 0042H 0043H
00 10 40 50

22
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Before execution – SUBTRACT

0040H 0041H 0042H 0043H


01 10 40 00

After execution
0040H 0041H 0042H 0043H
01 10 40 30

Before execution - multiply


0040H 0041H 0042H 0043H
02 02 22 00

After execution
0040H 0041H 0042H 0043H
02 02 22 44

Before execution - divide


0040H 0041H 0042H 0043H
03 02 22 00

After execution
0040H 0041H 0042H 0043H
03 02 22 11

2(b) SQUARE & CUBE OF A NUMBER


An 8 bit number x is stored in external memory location 9000H.Write an ALP to compute (a) square
of the no. X if LSB of data RAM 20H is set.(b) cube of the number x if LSB of data RAM 20H is reset.
Store the result at locations 9001H, 9002H, 9003H.

Label: Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV DPTR,#9000H Move DPTR = 9000H
MOVX A,@DPTR Move DPTR to A
MOV R0,A Move A to R0
MOV B,A Move B to A
MUL AB Multiply A and B
CLR C Clear carry
JB 01,LAST
PUSH B
MOV B,A Move A to B
MOV A,R0 Move R0 to A
MUL AB
INC DPTR

23
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

MOVX @DPTR,A
MOV A,B
MOV R2,A
POP B
MOV A,R0
MUL AB
ADD A,R2
LAST: INC DPTR
MOVX @DPTR,A
MOV A,B
ADDC A,#00
INC DPTR
MOVX @DPTR,A
HERE: SJMP HERE
END

PROCEDURE:

1. Create the project and save it.


2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.

Trial 1: Before execution: D:020H = FFH (square)


After execution: X:9000H

9000H 9001H 9002H 9003H


02 04 00 00

Before execution: D:020H = 00H(cube)


After execution: X: 9000H

9000H 9001H
02 08

Trial 2:

24
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

3 (A) HEXA UP/DOWN COUNTER

Write an ALP to implement an 8 bit up/down hexa counter

Program algorithm:
1. Move 00 to A register.
2. Call the delay subroutine for 1Sec. Move FFH to registers R1,R2 & R3.
3. Decrement the value of registers nested one within the other such that they create a
delay of 1Sec.
4. Increment value of A
5. Stop the counter after it reaches a maximum value of FFH

Label Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV A,#00 Move 00 to A
BACK: ACALL DELAY Absolute subroutine call
INC A (DEC A) Increment A
JNZ BACK Jump if A is not zero
HERE: SJMP HERE Short jump
DELAY: MOV R1,#0FFH Move 0FFH to R1
DEC1: MOV R2,#0FFH Move 0FFH to R2
DEC0: MOV R3,#0FFH Move 0FFH to R3
DEC2: DJNZ R3,DEC2 Decrement R3 and jump if not zer0
DJNZ R2,DEC0 Decrement R2 and jump if not zer0
DJNZ R1,DEC1 Decrement R1 and jump if not zer0
RET Return from subroutine
END End

PROCEDURE:

1. Create the project and save it.


2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Enable the watch window
9. Type ‘A’ in the watch window with an initial value of 00.
10. Run the program & watching the counter value varying from 00H to FFH and back to 00H.

RESULT:

Accumulator A is incremented in binary form 00, 01, 02……..0A , 0B……FF

25
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

3 (B) BCD UP/DOWN COUNTER

Write an ALP to implement an 8 bit BCD up/down counter

Program algorithm:
1. Move 00 to A register.
2. Call the delay subroutine for 1Sec. Move FFH to registers R1,R2 & R3.
3. Decrement the value of registers nested one within the other such that they create a
delay of 1Sec.
4. Increment value of A
5. Stop the counter after it reaches a maximum value of FFH and rolls back to 00H.

Label Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV A,#00 Move 00 to A
BACK: ACALL DELAY Absolute subroutine call
#01h for down counter ADD A,#99H Increment A
DA A Jump if A is not zero
JNZ BACK Jump back if it is not zero
HERE: SJMP HERE Short jump
DELAY: MOV R1,#0FFH Move 0FFH to R1
DEC1: MOV R2,#0FFH Move 0FFH to R2
DEC0: MOV R3,#0FFH Move 0FFH to R3
DEC2: DJNZ R3,DEC2 Decrement R3 and jump if
not zer0
DJNZ R2,DEC0 Decrement R2 and jump if
not zer0
DJNZ R1,DEC1 Decrement R1 and jump if
not zer0
RET Return from subroutine
END End

PROCEDURE:

1. Create the project and save it.


2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Enable the watch window
9. Type ‘A’ in the watch window with an initial value of 00.
10. Run the program & watching the counter value varying from 00H to 99 and back to 00H.

RESULT:
Accumulator A is incremented in BCD form 00, 01, 02……..99.

26
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

4 (a) PROGRAM ILLUSTRATING BIT MANIPULATIONS

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

Algorithm:
1. Store the elements of the array from the address 4000h
2. Move the first number in r0 and the second number in register A respectively
3. Clear carry flag and subtract the two numbers, if the carry flag is 0(if the nos are equal), Clear both LSB
& MSB of bit addressable memory location 2Fh
4. If the carry bit is set then Set MSB of 2F(7FH), else LSB of data RAM 2F (bit address 78H).
PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.

Label Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV DPTR,#8000H
MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
CLR C
SUBB A,R0
JZ EQUAL
JNC BIG
SETB 78H
SJMP END1
BIG: SETB 7FH
SJMP END1
EQUAL: CLR 77H
CLR 7FH
END1: SJMP END1
END
RESULT:

1) Before Execution: X:08000h = 45 & X:8001 = 35 After Executuion: D:02FH =01


2) Before Execution: X:08000h = 25 & X:8001 = 35 After Executuion: D:02FH =80
3) Before Execution: X:08000h = 45 & X:8001 = 45 After Executuion: D:02FH =00

27
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

4 (b). LOGICAL INSTRUCTIONS

ASSEMBLY PROGRAM ILLUSTRATING LOGICAL INSTRUCTIONS (BYTE LEVEL)


Write an ALP to compute the following. 3 eight bit numbers X, NUM1 & NUM2 are stored in
internal data RAM locations 20h, 21h & 22H IF X=0; THEN NUM1 (AND) NUM2, IF X=1; THEN NUM1
(OR) NUM2, IF X=2; THEN NUM1 (XOR) NUM2, ELSE RES =00, RES IS 23H LOCATION

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

Label Mnemonics Comments


ORG 0000H
SJMP 30H
ORG 30H
MOV A, 20h donot use #, as data ram 20h is to be accessed
MOV R1,A X IN R1
MOV A,21h A -NUM1
CJNE R1,#0,CKOR
ANL A, 22H
SJMP END1
CKOR: CJNE
R1,#01,CKXOR
ORL A, 22H
SJMP END1
CKXOR: CJNE R1,#02,OTHER
XRL A, 22H
SJMP END1
OTHER: CLR A
END1 MOV 23H,A STORE RESULT
HERE: SJMP HERE
END

RESULT:

1) Before Execution: D:020H =00, 21=0f, 22 = 12 After Execution: D:023H = 02


2) Before Execution: D:020H =01, 21=0f, 22 = 12 After execution: D:023H = 1F
3) Before Execution: D:020H =02, 21=0f, 22 = 12 After Execution: D:023H = 1D
4) Before Execution: D:020H =03, 21=0f, 22 = 12 After Execution D:023H = 00

28
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

5. CALL & RETURN

Algorithm:
1) Check for set bit of P1.0
2) If set, toggle port2 with data FFh & 00h with a delay.
3) If not set, toggle port2 with data D3h & 78h with delay.

ORG 00H
JB P1.0, LOOP1
LOOP2: MOV A,#0FFh
MOV P2,A
ACALL DELAY
MOV A,#00h
MOV P2,A
ACALL DELAY
SJMP LOOP2
LOOP1: MOV A,#78h
MOV P2,A
ACALL DELAY
MOV A,#0D3h
MOV P2,A
ACALL DELAY
SJMP LOOP1
DELAY: MOV R0,#0FFh
HERE1: DJNZ R1, HERE1
HERE2: DJNZ R0, HERE2
RET
END

RESULT:

Before execution: P2 – 0Xff

After execution: P2 – 0x78h, 0xD3h

29
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

6 CODE CONVERSIONS

(A) ALP program to convert BCD – ASCII

Algorithm:

1. Move the BCD byte into 2 ASCII characters.


2. Get the lower nibble (BCD digit) and ORL (add) with 30H.
3. Store the converted ASCII value.
4. Get the higher nibble (10’s BCD digit) and ORL (add) with 30H.
5. Store the converted ASCII value in the specified memory locations.

PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Load the BCD value at address 50H
9. The ASCII output is seen at 51H & 52H.
Label Mnemonics Comments
ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV R1,#50H Move #50H to R1
MOV A,@R1 Move the value in R1 to A
MOV R2,A Move A to R2
ANL A,#0FH AND #0FH to A
ORL A,#30H ORL #30H to A
INC R1 Increment R1
MOV @R1,A Move A to indirect RAM
MOV A,R2 Move R2 to A
SWAP A Swap nibbles within A
ANL A,#0FH AND #0FH to A
ORL A,#30H ORL #30H to A
INC R1 Increment R1
MOV @R1,A Move A to indirect RAM
HERE: SJMP HERE Short jump
END End
RESULT:

TRIAL I: D: 050H

050H 051H 052H


44 34 34

30
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

B) Write an ALP to implement ASCII to decimal conversion.

Algorithm:

1) Get the ASCII code in the range 30-39 as an input.


2) Clear carry bit.
3) Subtract with borrow 30h from the input & display the result.

ORG 00H
ORG 00H
MOVX A,@DPTR
CLR C Clear carry flag
SUBB A,#30H
MOV R7,A
END

RESULT:
Before execution x:9000h – 45
After execution
Project window
R7 – 0x15
A – 0x15

C) Write an ALP program to implement decimal to ASCII conversion.

Algorithm:
1) Get the decimal number in the range 0-99 as an input.
2) Separate the higher and lower nibble of the two digit number.
3) Add 30h to the lower nibble & store the result.
4) Bring the higher nibble to the ones position, add 30h to it & display the result.
ORG 00H
LJMP 9000H
ORG 9000H
MOV DPTR,#9000H
MOVX A,@DPTR
ANL A,#0FH Mask the upper nibble
ADD A,#30H
MOV R6,A
MOVX A,@DPTR
ANL A,#0F0H Mask the lower nibble
SWAP A Bring it to the ones place
ADD A,#30H
MOV R7,A
END

31
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

RESULT:
Before execution x:9000h – 39
After execution
Project window
R6 – 0x39
R7 – 0x33

(D) ALP program to convert HEX – DECIMAL

Algorithm:

1. Move the hexa data to be converted to A.


2. Move 10 to B register and divide with A register to ASCII value.
3. Exchange contents of quotient and remainder and transfer the quotient to one memory location.
4. Increment DPTR and exchange.
5. Once again divide value by 10 and exchange.
6. Store the quotient in 2nd memory location.
7. Exchange number and once again divide by a value of 10.
8. Store the final remainder in the 3rd memory location which in total with all other memory location
gives the complete decimal number.

PROCEDURE:

1. Create the project and save it.


2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Load the hexa decimal value in external RAM location 9000H.
9. See the output in hexadecimal in memory locations 9001H,9002H & 9003H.
10. Exit debug mode.

Label Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV DPTR,#9000H Move 9000H to DPTR
MOVX A,@DPTR Move the value in DPTR
to A
MOV B,#10 Move #10 to B
DIV AB Divide A by B
INC DPTR Increment DPTR
XCH A,B Exchange B with A
MOVX @DPTR,A Move A to external RAM
XCH A,B Exchange B with A
MOV B,#10 Move #10 to B
DIV AB Divide A by B

32
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

INC DPTR Increment DPTR


XCH A,B Exchange B with A
MOVX @DPTR,A Move A to external RAM
XCH A,B Exchange B with A
INC DPTR Increment DPTR
MIOVX @DPTR,A Move A to external RAM
HERE: SJMP HERE Short jump
END End

TRIAL I:

X:9000H

9000H 9001H 9002H 9003H


FF 05 05 02

(E) ALP program to convert DECIMAL - HEX


Algorithm:
1. Move the decimal no. to be converted from ext mem loc 40H to A.
2. ‘AND’ A register with 0F0H and obtain the upper MSB of the decimal digit and swap the LSB and
MSB of A to bring the same to unit’s place.
3. Move 0AH to B reg and multiply with A reg to convert to Hex value and store the converted 10’s
value in R1.
4. Get LSB of the decimal no and add it to the converted 10’s value.
5. Point to the next mem location and store the result in hexadecimal.
PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Load the decimal value in external RAM location 40H.
9. See the output in hexadecimal in memory locations 41H.
10. Exit debug mode.

Label Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV DPTR,#40H Move 40H to DPTR
MOVX A,@DPTR Move the value in DPTR to
ANL A,#0F0H Add operation between A & 0F0H
SWAP A Swap
MOV B,#0AH Move 0AH to B
MUL AB Multiply A & B
MOV R1,A Move A to R!

33
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

MOVX A,@DPTR Move ext DPTR to A


ANL A,#0FH AND between A & 0FH
ADD A,R1 Add R1 & A
INC DPTR Increment DPTR
MOVX @DPTR,A Move the value A to DPTR
HERE: SJMP HERE Short jump
END End

RESULT:
Trial 1: Trial 2:
Before execution
X: 040H : 45
After execution
X: 041H : 2D

34
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

7 (a). PROGRAM TO GENERATE DELAY


Write an ALP using TIMER0 to create 10 kHz square wave on P1.0.

Algorithm:
1) Load TMOD with 02h i.e timer0 mode2 8-bit auto reload.
2) Load TH0 with -3 to set the baud rate to 9600.
3) Start timer0 by setting TR0 to 1.
4) Monitor the value of TF0 flag with the use of the jump instruction.
5) Clear flag TF0 & compliment the 1st bit of the port1.
6) This makes the last bit of port1 to toggle.

MOV TMOD,#02h
MOV TH0,-3
SETB TR0
LOOP: JNB TF0,LOOP
CLR TF0
CPL P1.0
SJMP LOOP
END

RESULT:

Before execution: P1 = 0Xff

After execution: P1.0 toggles


P1 = 0xFE

35
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

7 (B). ALP FOR SERIAL DATA TRANSMISION

Algorithm:

1) The TMOD register is loaded with the value 20h, indicating the use of timer1 in mode2 to set baud
rate.
2) TH1 is loaded with one of the value -3 to set the baud rate.
3) The SCON register is loaded with the value 50h, indicating serial mode1, where 8-bit data is framed
with start & stop bit.
4) TR1 is set to start timer1.
5) The character byte to be transferred serially is written into the SBUFF register.
6) The TI flag is monitored with the use of the jump instructions, to see if the character has been
transferred completely.
7) Clear TI flag to assure the transmition of the next byte.
8) To transfer the next byte, go to step5.

MOV TMOD,#20h
MOV TH1,#-3
MOV SCON,#50h
SETB TR1
MOV A.#’H’
ACALL TRANS
MOV A,#’I’
ACALL TRANS
AGAIN: SJMP AGAIN
TRANS: MOV SBUF,A
HERE: JNB TI,HERE
CLR TI
RET
END

RESULT:

UART#1

HI

36
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Hardware Interfacing

Features of Embedded C:

• C is a simple programming language and so very easy to code.


• Embedded C has most features of C-language with more stress on certain bit manipulative
instructions.
• This feature makes it easy to write program for μC and μP.
• Keil is a versatile software with a cross compiler that will convert the C program to assembly
language and thus the program can be executed on the desired target (say 8051).

Some of the bit manipulative instructions used are

Symbol Operation
& Bitwise AND
| Bitwise OR
~ Bitwise NOT
>> Shift Right
<< Shift Left
^ P0.0

37
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Experiment No. 8

Write a C program to control the speed of rotation and direction of rotation of a Stepper Motor.

Stepper motor unlike DC motor rotates in steps. Stepper motor has 4 coils which forms the stator and a
central rotor. Rotation depends on excitation of stator coils.
Step Coil A Coil B Coil C Coil D
1 0 0 0 1
2 1 0 0 0
3 0 1 0 0
4 0 0 0 1

Anyone of these values forms the initial value. To get 360o revolution 200 steps are required. Step angle=
3600 /200 = 1.80. (difference between 2 teeth).

Algorithm:
1. Configure P0 as output.
2. Apply the initial excitation of 11 to motor coils through P0.
3. For clockwise motion -Rotate right once the excitation and repeat step 2.
4. For anticlockwise motion -Rotate left once the excitation and repeat step 2.

Fig 1: Stepper Motor interfaced to 8051 Microcontroller via ULN2003

38
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Fig 2: Stepper Motor Timing Diagram

Program:
#include <REG51xD2.H>
void delay (unsigned int x) /* Delay Routine */
{
for(;x>0;x--);
return;
}
main ( )
{
unsigned char Val, i;
P0=0x00;
Val = 0x11;
for (i=0;i<4;i++)
{
P0 = Val;
Val = Val<<1; /* Val= Val>>1; for clockwise direction*/
delay (500);
}
}

39
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Experiment No. 9

Write a C program to control the speed of rotation of DC Motor.

Algorithm:

1. Configure P0,P1 as output port and P3 as input port.


2. Let initially the motor rotate with half speed count 7fh.
3. If “INR” button is pressed reduce the count because the speed is inversely proportional to count.
4. If “DEC” button is pressed increase the count.

Fig 3. DC motor interface to 8051 Microcontroller

Program:

#include <REG51xD2.H>
sbit inr= P3^2; /*speed increment switch*/
sbit dcr= P3^3 /*speed decrement switch*/
main()
{
unsigned char i=0x80;
P0 = 0x7f; /*Run the motor at half speed.*/
while (1)
{
if (!inr)
{
while (!inr);
if(i>10)
i=i-10; /*increase the DC motor speed*/
}
if(!dcr)

40
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

{
while(!dcr);
if(i<0xf0)
i=i+10; /*decrease the DC motor speed*/
}
P0=i;
}
}

41
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Experiment No. 10

Dual DAC Interface is used to generate:

a. Square waveform
b .Triangular Waveform
c. Ramp waveform
d. Sine waveform

Fig 4: 8051 Microcontroller interfaced to Dual DAC 0808

42
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Write a C program to generate square wave.

Algorithm:

1. Let initial, amplitude of the square wave be 2.5v (7F) and frequency count 100.
2. Output the values 00h (0ff) and 7fh (on) Values through P0.
3. If amplitude key is pressed then increase the voltage in steps of 0.15v (8).
4. If the frequency key is pressed increment the count in steps of 50. If the count exceeds 1000
reset it back to 100.
5. Every time amplitude and frequency changes output the value through P0 and note the
waveform on CRO.

Program:

#include <REG51xD2.H>
void delay(unsigned int x) /* delay routine */
{
for (;x>0;x--);
}
main()
{
unsigned char on = 0xFF,Off=0x00;
P0=on;
delay(100);
P0 = Off; /* clear port */
delay(100);
}

Output Waveform at Port-0 (Logic Analyzer Window):

43
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Write a C program to generate Triangular wave.

Algorithm:

1. Output the initial value 00 through P0.


2. Increment it in steps of 1 until a count value of FFh (5V) is reached. Every time repeat step
1.
3. Decrement it in steps of 1 until a zero value is reached and repeat step 1.

Program:

#include <REG51xD2.H>
main()
{
unsigned char i=0;
while(1)
{
for(i=0;i<0xff;i++)
{ /* Generate ON pulse */
P1 = i;
}
for(i=0xfe;i>0x00;i--) /* Generate OFF pulse */
{
P1 = i;}
}
}

Output Waveform at Port-1 (Logic Analyzer Window):

44
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Write a C program to generate Ramp wave.

Algorithm:

1. Output the initial value 00 through P0.


2. Increment it in steps of 1 until a count value of FFh (5V) is reached. Every time repeat
step 1.
3. Repeat step 1 & 2 continuously.

Program:

#include <REG51xD2.H>
main ()
{
Unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)
{
for (i=0;i<0xff;i++) /* Generate ON pulse */
{
P0 = i;
}
}

Output Waveform at Port-1 (Logic Analyzer Window):

45
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Write a C program to generate Sine wave.

Algorithm:

1. Compute different step values (θ = 20o,15o…) of sine using the equation


V= 2.5V +2.5Vsinθ. Output the values thro P0.
2. More the steps smoother will be sine wave.
3. E.g.: θ = 0o, V= 2.5V +2.5Vsinθ = 2.5V. The value sent to DAC is 25.6 x5V= 128.

Program:

#include <REG51xD2.H>
main()
{
static int a[13]={128,192,238,255,238,192,128,64,17,0,17,64,128};
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)
{
for(i=0;i<13;i++) /* Output different values */
{
P0 = a[i];
}
}
}

Output Waveform at Port-0 (Logic Analyzer Window):

46
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Experiment No. 11

Write a C program to interface a Elevator to 8051 Microcontroller.

Algorithm:

1. Read the floor request through input port P1.


2. If the current floor and requested floor are the same no change light up the corresponding
LED through P0.
3. If the requested floor greater than current moving up of the lift is indicated by glowing of
LED’s from current floor to the requested.
4. If the requested floor lesser than current moving down of the lift is indicated by glowing of
LED’s from current floor to the requested.

Program:

#include <REG51F.H>
void delay(unsigned int);
main()
{
unsigned char Flr[9] = {0xff,0x00,0x03,0xff,0x06,0xff,0xff,0xff,0x09};
unsigned char FClr[9] = {0xff,0x0E0,0x0D3,0xff,0x0B6,0xff,0xff,0xff,0x79};
unsigned char ReqFlr,CurFlr = 0x01,i,j;
P0 = 0x00;
P0 = 0x0f0;
while(1)
{
P1 = 0x0f;
ReqFlr = P1 | 0x0f0;
while(ReqFlr == 0x0ff)
ReqFlr = P1 | 0x0f0; /* Read Request Floor from P1 */
ReqFlr = ~ReqFlr;
if(CurFlr == ReqFlr) /* If Request floor is equal to Current Floor */
{
P0 = FClr[CurFlr]; /* Clear Floor Indicator */
continue;
} /* Go up to read again */
else if(CurFlr > ReqFlr) /* If Current floor is > request floor */
{
i = Flr[CurFlr] - Flr[ReqFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];
for(;i>0;i--) /*Move the indicator down */
{
P0 = 0x0f0 | j;
j--;
delay(25000);
}
}

47
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

else /* If Current floor is < request floor */


{
i = Flr[ReqFlr] - Flr[CurFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];
for(;i>0;i--) /*Move the indicator Up */
{
P0 = 0x0f0 | j;
j++;
delay(25000);
}
}
CurFlr = ReqFlr; /* Update Current floor */
P0 = FClr[CurFlr]; /* Clear the indicator */
}
}
void delay(unsigned int x)
{
for(;x>0;x--);
}

48
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual

Microcontroller beyond the Syllabus Lab Experiments


1. Traffic Light Controller
2. LCM and GCD

49
Department of EEE/AMCEC/Bangalore

You might also like