You are on page 1of 82

IV Semester Microcontroller Lab.

(BEE403)

Name:
USN :

(2023-24)
PREPARED BY: APPROVED BY:

Mrs. RAMYA M Asst. Professor Dr. Rekha S N


Mrs. DEEPA B Asst. Professor Prof. & HOD,
Dept. of Electrical and Electronics Engg. Dept. of Electrical and Electronics Engg,
Sapthagiri College of Engineering, Sapthagiri College of Engineering,
Bangalore – 560 057. Bangalore – 560 057.

Dept. of EEE, Sapthagiri College of Engineering, Page 1


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Vision of the Department


 To create globally competent Electrical and Electronics Engineers
who can contribute to the growth of the nation and serve the society.

Mission of the Department:

M1: To impart student centric quality education


M2: To nurture the talents and impart moral values to the students.
M3: To keep abreast the technical knowledge among students and
faculty with industry–academia interaction.
M4: To enrich research and innovation methods in students and faculty.

Dept. of EEE, Sapthagiri College of Engineering, Page 2


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Programme Outcomes (POs)

Engineering Knowledge: Apply the knowledge of mathematics, science, engineering


PO1 fundamentals, and an engineering specialization to the solution of complex engineering
problems.
Problems Analysis: Identify, formulate, review research literature, and analyze complex,
PO2 engineering problems reaching sustained conclusions using first principles of mathematics,
natural science and engineering science.
Design/development of solutions: Design solutions for complex engineering problems and
PO3 design system components of process that meet the specified needs with appropriate
consideration for the public health and safety, and cultural, societal, and environmental
consideration.
Conduct investigations of complex problems: Use research-based knowledge and
PO4 research methods including design of experiments analysis and interpretation of data, and
synthesis of information to provide valid conclusions.
Modern tool usage: create, select, and apply appropriate techniques, resource and
PO5 modern engineering and IT tools including prediction and modelling to complex engineering
activities with an understanding of the limitations.
The engineer and society: Apply the reasoning informed by the contextual knowledge to
PO6 access societal, health, safety, legal and cultural issue and the consequent responsibility
relevant to the professional engineering practice.
Environment and sustainability: Understanding the impact of the professional engineering
PO7 solution in the social and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
PO8 Ethics: Apply ethics principle and commit to professional ethic and responsibility and norms
of the engineering practice.
PO9 Individual and team work: Function effectively as an individual, and as member or leader
in diverse team, and in multidisciplinary setting.
Communication: Communicate effectively on complex engineering activities with the
PO10 engineering community and with society at large, such as, being to comprehend and write
effective reports and design documentation, make effective presentations, and give and
receive clear instructions.
Project management and finance: Demonstrate knowledge and understanding of the
PO11 engineering and management principles and apply these to one’s own work, as a member
and leader in team, to manage projects and in multidisciplinary environments.
Life-long learning: Recognizing the need for, and have the preparation and ability to
PO12 engage in independent and life-long learning in the broadest context of technological
change.

Dept. of EEE, Sapthagiri College of Engineering, Page 3


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Programme Specific Outcomes (PSOs)


The graduates of Electrical and Electronics Engineering Program should be able to
attain the following at the time of graduation.

PSO 1: Apply fundamental knowledge to identify, formulate, design and investigate


various problems of Electrical Machines, Power Electronics, Micro-Controller, Control
System, Power System, High Voltage Engineering.
PSO 2: Apply modern software tools for design, simulation and analysis of electrical and
electronics systems, engage in lifelong learning and to successfully adapt in
multidisciplinary environments.

PSO 3: Solve ethically and professionally various Electrical and Electronics


Engineering problems in societal and environmental context.

COURSE OUTCOMES:

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

1. Build assembly language programs for data transfer, arithmetic, Boolean and
logical instructions and code conversions.
2. Apply ALP subroutines for generation of delays, counters, configuration of
SFRs for serial communication and timers.
3. Demonstrate interfacing of LCD, stepper motor and dc motor for controlling the
speed
4. Develop different waveforms using DAC interface.

Dept. of EEE, Sapthagiri College of Engineering, Page 4


Bangalore
IV Semester Microcontroller Lab. (BEE403)

MICROCONTROLLER LAB.
Syllabus: [Use of 8051 Microcontroller and its advanced version AT89C51ED2 ]
A. Use of Simulation Software for writing programs .

1. Data Transfer – Writing ALPs for:


block data movement, block data exchange, sorting of data,
finding largest/smallest element in a given array etc, etc,.
2. Arithmetic Instructions – Writing ALPs for:
Addition, Subtraction, Multiplication and Division involving 8 bit
and 16 bit numbers. Finding the square and cube of a given number.
3. Writing ALPs for:
Counters: Binary Counters, Decimal Counters etc.,
4. Writing ALPs for:
Boolean and Logical Instructions (bit manipulations).
5. Writing ALPs for:
Understanding conditional CALL and RETURN instructions.
6. Writing ALPs for:
Understanding conditional CALL and RETURN instructions.
7. Writing ALPs for:
Generation of time-delay(delay programs), Generation of time
delay(use of on chip timer/counter), serial port communication.
B. Use of Simulation Software for writing programs and interfacing .

8. Writing ‘c’ program for: Stepper motor speed and direction control.
9. Writing ‘c’ program for: DC Motor speed and direction control( PW M o du la tio n ).
10. Writing ‘c’ program for: Alphanumerical display on LCD Panel.
11. Writing ‘c’ program for: Gen. of Waveforms ( Sine, Triangular, Ramp ) using DAC.
12. Writing ‘c’ program for: External ADC and Temperature control.
13. Writing ‘c’ program for: Elevator Interfacing.

**************

Dept. of EEE, Sapthagiri College of Engineering, Page 5


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Introduction to Microcontrollers
A microcontroller can be compared to a small standalone computer; it is a very powerful device, which is capable
of executing a series of preprogrammed tasks and interacting with other hardware devices. Being packed in a tiny
integrated circuit (IC) whose size and weight is usually negligible, it is becoming the perfect controller for robots or any
machines requiring some kind of intelligent automation. A single microcontroller can be sufficient to control a small
mobile robot, an automatic washer machine or a security system. Any microcontroller contains a memory to store the
program to be executed, and a number of input/output lines that can be used to interact with other devices, like reading
the state of a sensor or controlling a motor.

Nowadays, microcontrollers are so cheap and easily available that it is common to use them instead of simple
logic circuits like counters for the sole purpose of gaining some design flexibility and saving some space. Some
machines and robots will even rely on a multitude of microcontrollers, each one dedicated to a certain task. Most recent
microcontrollers are 'In System Programmable', meaning that you can modify the program being executed, without
removing the microcontroller from its place.

Today, microcontrollers are an indispensable tool for the robotics hobbyist as well as for the engineer. Starting
in this field can be a little difficult, because you usually can't understand how everything works inside that integrated
circuit, so you have to study the system gradually, a small part at a time, until you can figure out the whole image and
understand how the system works.
Features Of 8051
 40 pin DIP
 Harvard Architecture
 8 bit Arithmetic and Logical unit
 8 bit CPU with registers A and B
 16 bit PC and DPTR
 8 bit PSW
 8 bit SP with SP=07h by default
 Internal ROM or EPROM of 0 to 4K
 Internal RAM of 128 bytes
 4 banks of 8 registers(R0-R7), 8 bit each
 16 bytes which can be addressed at the bit level
 80 bytes of General purpose data memory
 2, 16 bit timers/counters with interrupts
 Four 8 bit parallel ports(P0-P3) with single line access
 Oscillator and clock circuits
2 external and 3 internal interrupt sources
 Full duplex serial data receiver/transmitter: SBUF
 Control Registers: TCON, TMOD, SCON, PCON, IP and IE

PIN DIAGRAM OF 8051

Dept. of EEE, Sapthagiri College of Engineering, Page 6


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Dept. of EEE, Sapthagiri College of Engineering, Page 7


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Dept. of EEE, Sapthagiri College of Engineering, Page 8


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Dept. of EEE, Sapthagiri College of Engineering, Page 9


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Dept. of EEE, Sapthagiri College of Engineering, Page 10


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Some basics:
1. Identify the Hexadecimal numbers:
1, 3, 5, 7, 8, A, C, D, F, FG, H, AF, FA, UG, PG.
2. What is a bit? Byte? Nibble? Word? kB = how many Bytes?
3. Convert Ch, AFh and FAh into binary. How many bit s does AFFAh has?
3. What is the difference between or “ ” or ‘ ’
and Ah?
4. ADD: i) 9h and 1h ii) 8h and 2h iii) 9h and 6h iv) Ah and Bh
5. Addition of Hexadecimal Numbers— Add 23D9h and 94BEh.
[ Note that if a carry results, the carry is always 1h(or simply 1 as 1h =
1d)]
6. Subtraction of Hexadecimal Numbers— Sub 2B8h from 59Fh. In
subtracting process if the digit to be subtracted is greater, then 16
is to be borrowed.
7. Subtraction by borrowing is complicated. Then what is the
alternative? Explain the procedure used for subtraction in
computers.
8. What is a nibble? How many nibbles make 1 byte. How many
bytes make 1 word? How many bits a word will have?
9. What is a Microprocessor?
10. What is a Microcontroller?
11. Will there be inbuilt memory in Microprocessor?
12. Why a Microcontroller is referred to as a system on chip?
13. What is likely RAM and ROM memory capacities of a Microprocessor and a
Microcontroller?
14. For the CPU to process information, the data must b e stored in RAM or ROM.
[The function of ROM is to provide information to the CPU that is fixed
and permanent. RAM is used to provide information to CPU that is not
permanent.]
15. What makes Microcontrollers to have low memory capacities?
16. A microcontroller normally has which of the following
devices on chip?
(A) RAM (B) ROM (C) I/O (D) All of these
17. A general-purpose microprocessor normally needs which of the following
devices to be attached to it?
(A) RAM (B) ROM (C) I/O (D) All of these
18. What does the term embedded system mean?
19. An embedded system is also called a dedicated system. Why?
20. Mention some of the Embedded products using microcontrollers that are

Dept. of EEE, Sapthagiri College of Engineering, Page 11


Bangalore
IV Semester Microcontroller Lab. (BEE403)

generally found in places like— Home, office and in Automobiles.


21. Who is the Originator of 8051 Microcontroller? What other versions of
8051 are available? Mention them and also the difference among
themselves. What is the Microcontroller used in the interfacing modules
used in the Lab.?
22. An embedded system is also called a dedicated system. Why?
23. Let us get into the memory of 8051 microcontoller.
[8051 μC has 4kb ROM (ie program memory) and 128bytes of RAM memory.
Another 128 bytes of RAM memory is also available for SFR * . Some books
refer that 8051 has 128 bytes of internal RAM, because it is the RAM
available for the programmer for data manipulations. SFRs space cannot
be used for data manipulations or calculations. Out of other 128 bytes
reserved for SFRs all the memory space is not used up. There are unused
locations which are reserved and must not be used by the programmer.
These spaces are reserved by the manufacturer for any future
expansions.]
24. What can we understand by memory locations?
[RAM is used to store data. It is a storage space for data, and not a
dust bin. In order to facilitate storage and retrieving the required
data RAM memory locations are named and well organized]
25. 128 bytes of RAM is divided into various locations.
128 bytes of RAM is divided into 3 regions. 32 bytes from starting for
4 Register Banks. Next 16 bytes for bit addressable RAM space. Next 80
bytes are General Purpose RAM.
26. How many Register Banks are there in 805 1 ? What are they ? Name them.
27. How many Registers are there in each Bank ? How they have been named?
28. All the register banks have got registers having same names!!
Then how to distinguish ? For ex. How can I extract data from R5 register
belonging to Bank3 specifically ? ( since R5 registers are available in
other register banks also). What do you understand by default register
bank?
29. What do you understand by bit addressable registers?
30. What is the difference between bit addressable and byte addressable
registers ?
31. Be clear about this: ’MOVE’ the Microcontroller Book from one corner
of the table to the other corner. The instruction is very clear.
But, what happens when we use instruction ‘MOV’ to move a ’ data‘ from
memory location to another memory location? The Instruction MOV simply
puts a copy of the data from source location to destination location.
(In this sense(!), ’MOVE’ the Microcontroller Book from one corner
of the table to the other corner should put a copy of the book to the
other corner, the original book will stay put in its original place.)

(i) MOV A, #40h What is expected?


Dept. of EEE, Sapthagiri College of Engineering, Page 12
Bangalore
IV Semester Microcontroller Lab. (BEE403)

(ii) MOV A, 40h What is expected?


Observe this Instruction– MOV R1, R2 (!?)
What do you expect as per above? This should mean(!) put a copy of t he data
available in Register R2 to Register R1. Will this work? If answer is
‘no’ – find what is wrong.
Try the following and find out the limitations.
MOV A, R2 ;
MOV R1, A ; what difference it has made?
Identify always the source byte and destin ation byte. To remember, always
pronounce the expression(does not matter if your neighbor is dis turbed
!). Like, MOV A, 40h ; Pronounce — move data from 40h memory location
(source is 40h RAM memory location) to Accumulator register
A(Destination is Accumulator) and so and so.
32. What are Index Registers?

MOV R0, 40h ; What is the value of R 0 ? and


Why? MOV A, @R0; What is the value of A ? and Why?
MOV R0, 47h; What is the value of R 0 ? and
Why? MOV A, @R0; What is the value of A ? and Why?
INC R 0;
MOV R2, 40h ; What is the value of R 2 ? and
Why? MOV A, @R2; What happens??
33. What is PSW register? It is byte addressable or bit addressable? Reason
out! Load value of Ch on to A and copy this value of A in all R5
registers.
34. What is PC register? (PC points to the next instruction to be executed.
As the CPU fetches the OPCODE from the ROM, the program counter is
incremented to point to the next instruction. What is width of this
register? Where does the 8051 wake-up when it is powered? Why ‘ORG 0h’
(same ‘ ORG 0000h’) is included in the beginning? 8051 expects first
OPCODE to be stored at ROM memory location 0000.
Run this single line program MOV R5,#25h. Observe
PC. Add these lines to above. MOV A, R5 ;
MOV R7, A ; run and observe PC.
35. ORG 0000h;
MOV R5,#25h;

Dept. of EEE, Sapthagiri College of Engineering, Page 13


Name:
Bangalore
USN :
IV Semester Microcontroller Lab. (BEE403)

MOV R7,#34h;

MOV A,#0;
ADD A, R5; ADD
A, R7;
ADD A,#12h;
HERE: SJMP HERE
END
Run above program to understand OPCODE, INSTRUCTION, bytes per
instruction etc. Observe PSW Register and PC during execution.
36. CPU can access data in various ways. The data (i) could be in a
register, (ii) it could be in a memory location or (iii) data can be
provided as an immediate value. Observe (i) Register (ii) Direct
(iii) Immediate addressing modes below.
MOV A, R0; CPU looks into the register R0, takes the value and puts it in A
MOV A, 0h; CPU looks into the memory location oh, takes the value
that is in memory location 0h and puts it in A.
Memory location 0 or 0h belongs to which register?
What is the memory location for Register R0? (!?) Does these two
questions amounts to one and same meaning?

MOV A,#25h; the value is provided as an immediate value; no need for the
CPU to go in search-of.
Write some more examples for above.
37. There are 4 Ports, P0, P1, P2, P3 each port has got 8 pins for 8 bits of
a byte either to input data to C or to take out data from C. In order
to establish communication with the outside world these ports are used.
The Ports can be configured to receive data for the microcontroller or
send out data from the microcontroller by initialization of values.
(how to do this?)
38. Understand the difference between movement of data within internal
memory locations, movement of data within internal memory locations
and taking data to external memory locations.
39. Waveform generation in TD programs. (What is the need of Wave form in
Time delay programs? Can we generate Triangular waveforms instead of
square wave for Time delays? What is the idea behind it?)
40. Find answers for signed number/unsigned n umber ?, Type declaration ⭢
Unsigned Integer/Unsigned Char etc

Note: Programming is an art. Various instructions or commands are available at our


disposal. The programs written by two persons need not match 1: 1. But for the
data given and final objective they must match. Also there will be limitations on
memory availability on the devices, etc. A good programmer shall obviously take
Dept. of EEE, Sapthagiri College of Engineering, Page 14
Bangalore
IV Semester Microcontroller Lab. (BEE403)

these aspects into consideration (To reach a destination point, A driver may choose a
short route or a longer one; but he should have reasons for selecting a particular route
and has to justify it is it not?).

Dept. of EEE, Sapthagiri College of Engineering, Page 15


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 1. PROGRAM FOR BLOCK DATA TRANSFER. (ALP)

ALGORITHM:

1. Initialize Register for holding source address.


2. Initialize Register for holding destination address.
3. Initialize Register for holding the count of bytes.
4. Transfer one byte of data.
5. Apply logic and transfer all the other data bytes, byte after byte.

; TRANSFER A BLOCK OF DATA FROM ONE MEMORY LOCATION TO ANOTHER


; MEMORY LOCATION.
ORG 0000h ; set the program counter to 0000h.
MOV R0, #40h ; address of the source byte (Imm. Add. Mode)
MOV R1, #50h ; address of the destination byte
MOV R2, #5h ; count for number of bytes to be
transferred REPEEET: MOV A, @R0 ; Label Name for repeating
instructions.
MOV @R1, A ; @ is applicable for Index
Registers. INC R0 ;
INC R1 ;
DJNZ R2, REPEEET ; Decrement Jump Not Zero; go to matching Label
END ;

Dept. of EEE, Sapthagiri College of Engineering, Page 16


Bangalore
IV Semester Microcontroller Lab. (BEE403)

RESULT:
BEFORE EXECUTION:
MEM. LOCATION 40h 41h 42h 43h 44h
DATA(default) 00 00 00 00 00

MEM. LOCATION 50h 51h 52h 53h 54h


DATA(default) 00 00 00 00 00

MEM. LOCATION 40h 41h 42h 43h 44h


DATA(entered) 02 07 01 09 AC

MEM. LOCATION 50h 51h 52h 53h 54h


DATA 00 00 00 00 00

AFTER EXECUTION:

MEM. LOCATION 40h 41h 42h 43h 44h


DATA(entered) 02 07 01 09 AC

MEM. LOCATION 50h 51h 52h 53h 54h

DATA 02 07 01 09 AC

Note that MOV instruction places a copy of the data in the new location,
keeping the original data intact .
GP_BLOCK DATA TRANSFER. docx

(Exercise: Transfer data to even locations only.)

Dept. of EEE, Sapthagiri College of Engineering, Page 17


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 2. PROGRAM FOR EXCHANGE OF DATA IN TWO BLOCKS. (ALP)

AFTER EXECUTION:

ALGORITHM:
1. Initialize Registers for holding source-cum-destination address.
2. Initialize Register for holding count of bytes to be exchanged.
3. Take first byte of data from BLK 2, lodge it safely in a known memory loc. R3.
4. Take first byte of data from BLK 1 and transfer it to BLK 2.
5. Take the byte of data in R3 and transfer it to BLK 1; completes 1 byte transfer.
6. Apply logic and transfer all the other data bytes, byte by byte.
; EXCHANGE DATA BETWEEN TWO MEMORY BLOCKS (say BLK 1 and BLK 2).
; Two Data blocks of 5 elements each, one start s @ 40h and another starts @ 50h.
ORG 0000h ;(Observe the use of Index Registers for
DBs ) MOV R0, #40h ;Initialize R0 with starting address of
one DB
MOV R1, #50h ;Initialize R1 with staring address of
another DB. MOV R2, #05h ;number of elements to be
exchanged.
AGAIN: MOV A, @R1 ;
MOV R3, A ;Hold this data in R3, to transfer
it later. MOV A, @R0 ;
MOV @R 1, A ;Earlier data can be overwritten.
MOV A, R3 ;note: no direct transfer between
Registers. MOV @R 0, A ;hence, roundabout procedure is
adopted.
INC R0 ;
INC R1 ;
DJNZ R2, AGAIN ; Decrement R2 first, then Jump to target if
Not ‘0’
END

Dept. of EEE, Sapthagiri College of Engineering, Page 18


Bangalore
IV Semester Microcontroller Lab. (BEE403)

RESULT:
BEFORE EXECUTION:

MEM. LOCATION 40h 41h 42h 43h 44h 45h


DATA ABh BCh CDh DEh EFh FAh

MEM. LOCATION 50h 51h 52h 53h 54h 55h


DATA AAh BBh CCh DDh EEh FFh

AFTER EXECUTION:
MEM. LOCATION 40h 41h 42h 43h 44h 45h
DATA AAh BBh CCh DDh EEh FFh

MEM. LOCATION 50h 51h 52h 53h 54h 55h


DATA ABh BCh CDh DEh EFh FFh
GP_BLOCK DATA EXCHANGE. docx

; As said earlier efficient program writing is an art to use various commands at


; your disposal. Let us write program for above using XCH command.
ORG 0000h ;
MOV R0, #40h ;
MOV R1, #50h ;
MOV R2, #5h ; Number of elements to be
exchanged(initially 5) AGAIN: MOV A, @R1 ;
XCH A, @R0 : [ Try XCH @R 0, A ; will it work? No!! A <
source] MOV @R1, A ;
2
INC R0 ; R0
INC R1 ; Acc.
DJNZ R2, AGAIN ; END

Note how data will be lodged in different


3 locations.

[Exercise: Take 8 elements; exchange data in even memory locations only.]

Dept. of EEE, Sapthagiri College of Engineering, Page 19


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 3. PROGRAM FOR ADDITION INVOLVING TWO 8 bit NUMBERS.


(don’t start writing the program. First be clear about the Instruction.
Then you can think about writing the program).
ALGORITHM:
1. (writing Algorithm is left to the
student) 2. Name:

. . . . . . . . . . . . . . . . . USN :

; Aim: Develop PROGRAM FOR ADDITION OF TWO 8 bit Numbers .


; Let 29h and FAh be the two eight bit numbers to be added.(given)
ORG 0000h ; Calculator M+
CLR C ; set the Carry Flag CY to zero. (find why this
required.) MOV A, #29h ; move one of the numbers to A.(Suppose # is
removed?) ADD A, #0FAh ; add to A the other number.(find reason for
‘0’)
MOV R0, A ; Least significant byte of the Result is stored
in R0 . JNC next ; If there is a carry from addition, store carry
in R1 .
MOV R 1, #01h ; if a carry has resulted, it will be 1h only(
possible). next: NOP ;
END ;

RESULT:
After Execution:

Content of R1 Register Content of R0 Register


01 23h
GP_ADDITION OF 2 EIGHT BIT NUMBERS. docx
Note: In the above program, Immediate addressing mode is used. If the numbers
to be added are changed, the whole process of editing the program, build target
etc are to performed again. In calculator, when two numbers are to be added,
we just enter the two numbers with a + sign in between. In– built Program is
already present and we have no access for editing that program. Same thing
can be achieved in the above case also by writing the program suitably.
Observe the use of Direct addressing mode in the program given below:

Dept. of EEE, Sapthagiri College of Engineering, Page 20


Bangalore
IV Semester Microcontroller Lab. (BEE403)

; Aim: PROGRAM FOR ADDITION OF ANY TWO 8 bit Numbers .


; The two numbers to be added are available in memory locations(say) 40h & 47h.
ORG 0000h ;
CLR C ; set the Carry Flag CY to zero. (find why this
required.) MOV A, 40h ; 40h location contain one number. (rule:
Destn, Source) ADD A, 47h ; 47h location contain the other number
MOV R0, A ; Least significant byte of the Result is
stored in R0 JNC next ; If there is a carry from addition,
store carry in R1
MOV R 1, #01h ; if a carry has resulted, it will be 1h only( possible).
next: NOP ; store the result of addition in R1 Hannu – Aloo,
Eerulli
END ;
RESULT:

Before Execution: (d:40h)

Content of 40h memory location Content of 47h memory


location
29h FAh

After Execution:

Content of R1 Register Content of R0 Register


01 23h

Now, it is simple matter, to add any two 8 bit numbers by simply entering the
numbers in appropriate memory locations and running the program(without
bothering save, built target etc, etc).

Exercise: Add the following:


ADD (i) FAh and FBh
(ii) 17h and
23h
(iii) AFh and GFh (!)

Dept. of EEE, Sapthagiri College of Engineering, Page 21


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 4. PROGRAM FOR ADDITION INVOLVING TWO 16 bit NUMBERS.


40h 41h 42h 43h 44h
9C 34 00 8A 0FA
ALGORITHM:
1. (writing Algorithm is left to the student) 2. …………
; Addition of two 16 Bit Numbers.
; Let 9C34 h and 8AFA h be the two sixteen bit numbers to be added.
; 9Ch is high byte and 34 h is low byte of first 16 bit number. Similarly,
; 8Ah is high byte and FAh is low byte of second 16 bit number.
; Let the numbers be available in memory locations (say) 40h, 41h and 43h, 44h.

ORG 0000h ; set the program counter to


0000h CLR C ; set the Carry Flag CY to zero.
MOV A, 41h ; move the low byte of first number to A.(DA
mode) ADD A, 44h ; add to A the low byte of second number. (If a
carry
; results from this addition, it will be accounted.)
MOV R0, A ; Store. This forms one of the final values. Next add high
; bytes accounting previous carry, if any.
MOV A, 40h ; (Now, it should be clear why CLR C was done
earlier). ADDC A, 43h ; Add with carry. Now A holds addition of
high bytes. MOV R1, A ;
JNC NEXT ;
MOV R 2, #01h ; R2 holds the final carry, if resulted.
NEXT: NOP ; Read the Final r esult as ⃗𝑴 ⃗→ R2 R1 R0
⃗⃗⃗⃗⃗𝑺⃗⃗⃗𝑩
END
RESULT: After Execution:

Final carry(if addition of High bytes, Addition of Low


with previous carry(if
any) any) added bytes
Content of R2 Content of Content of R0
R1
01h 27h 2Eh
(i) ) FAAF h and FBBF h Read result as: one-two- seven-two-E hexadecimal.(= 75566 d)
(ii) 1718 h and 2324 h GP_ADDITION OF TWO 16 BIT NUMBERS.docx
(ii) AF h and BFBF h

Dept. of EEE, Sapthagiri College of Engineering, Page 22


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 5. PROGRAM FOR SUBTRACTION INVOLVING TWO 8 bit NUMBERS.


ALGORITHM: 1. (writing Algorithm is left to the student) 2.

; SUBTRACTION INVOLVING TWO 8 bit Unsigned (only positive) NUMBERS.


; Let 6Eh and 4Ch be the two 8 bit numbers involved in subtraction.
; case 1: subtracting 4Ch(smaller) from 6Eh(higher) and
; case 2: subtracting 6Eh (higher) from 4Ch (smaller).
; Always, take , minuend (the umber FROM which subtraction is to be done) in A
; and , subtrahend (the umber to be subtracted) in B.

Case 1: Case 2:
40h 41h 52h 53h
6Eh 4Ch 4Ch 6Eh
ORG 0000h ;
; Part 1: case 1. Sub. a smaller num from a larger num
CLR C ; set the Carry Flag CY to zero.
MOV A, 40h ; Minuend is taken into Register A.
MOV B, 41h ; Subtrahend is taken into Register B.
SUBB A, B ; subtract with Barrow. (SUBB B, A is not allowed).
; A (new) = [previous value of A– CY– source byte in B]
MOV R0, A ;
JNC next ; if a borrow is there,
store it. MOV R 1, #01h ;
next: NOP ;
; Part 2: case 2. Sub. a larger num from a smaller num
CLR C ; set the Carry Flag CY to zero.
MOV A, 52h ; Minuend is taken into Register A.
MOV B, 53h ; Subtrahend is taken into Register B.
SUBB A,B ; subtract with Barrow.
MOV R2, A ;
JNC next1 ;
MOV R3, #01h ;
next1 : NOP ;
END ; happen? Clarify. Use Setb C and try. Use Calc i ! M- in

Dept. of EEE, Sapthagiri College of Engineering, Page 23


Bangalore
IV Semester Microcontroller Lab. (BEE403)

It can be noted that if the data is swapped in memory locations 40h and 41h t
hen Part 2 of the program is not at all required. If the programmer says ‘
enter minuend in 40h and subtrahend in 41 h ’ problem is solved; no need to go for
2 parts in the program.
RESULT:
Given: Minuend = 6Eh , Subtrahend = 4Ch
. After Execution:
Content of R1 Content of
R0
00h 22h

Given: Minuend = 4Ch , Subtrahend = 6Eh .


After Execution:
Content of R1 Content of R0
01h DEh

Exercise: Try for various numbers and understand subtraction


procedure. Q: What is Subtraction Procedure ?
Ans: Subtraction procedure involves adding the 2’s complement of the subtrahend
to the binary equivalent of minuend and then inverting the
carry. Q: How to get the 2 ’s complement of a number?
Ans: Add binary 1 to 1’s Complement of the number whose 2’s complement is
required.

Q: What is 1’s complement of a number?


Ans: 1’s complement of a given number is obtained by inverting every bit of
the binary equivalent of that number.

Q: What is binary equivalent of 4Ch? .


Solution: 4C h = 0 1 0 0 1 1 0 0 b Ans.

Q: What is 1’s complement of 6Eh? .


Solution: 6E h = 0 1 1 0 1 1 1 0 b
1’s complement of 6E h = 1 0 0 1 0 0 0 1 Ans .

Dept. of EEE, Sapthagiri College of Engineering, Page 24


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Q: Obtain the 2’s complement of 6Eh.


Soln: 1’s complement of 6 Eh = 1 0 0 1 0 0
0 1 Adding 1; +
1
∴ 2’s complement of 6 Eh = 1 0 0 1 0 0 1 0

Q: Subtract of 6Eh from 4Ch


Binary equivalent of 4Ch = 0 1 0 0 1 1
0 0 b carry = 0.
Add 2’s complement of 6Eh = 1 0 0 1 0 0 1 0

After Inverting the carry and writing,


we get,

∴ [4Ch – 6Eh] 11 1 0 1 1 1 1 0 b = 1 DE h Ans.


=

Exercise: Simplify: FEh – FFh = ?


ABh – ACh = ?
67h – 83h =
?

Dept. of EEE, Sapthagiri College of Engineering, Page 25


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 6. PROGRAM FOR SUBTRACTION INVOLVING TWO 16 BIT NUMBERS.


ALGORITHM:
1. (writing Algorithm is left to the student)
2.
40h 41h 42h 43h 44h
27 A5 00 3C FA

; SUBTRACTION INVOLVING TWO 16 BIT UNSIGNED NUMBERS.


; Let 27A5h and 3CFAh be two 16 bit numbers involved in subtractio n.
ORG 0000h ;
CLR C ;
; Performing (27A5h – 3CFAh), the number to be subtracted is 3CFAh
→B MOV A, 41h ; Take Low byte of minuend into A.
MOV B, 44h ; Take Low byte of subtrahend into B(as per rule).
SUBB A, B ; A(new) = [previous value of A– CY– source byte in
B ]MOV R0, A ; Store LB result in R0
MOV A, 40h ;
MOV B, 43h ;
SUBB A, B ; A (new) = [previous value of A– CY– source byte
in B]MOV R1, A ; Store HB result in R1
JNC next ;
MOV R 2, #01h ; Store CY/Borrow if any, in
R2. next: NOP ;
END

Minuend must be in A whereas Subtrahend can be in B or provided by


immediate or by direct addressing mode; Like SUBB A, B or SUBB A,#3Ch or SUBB
A, 43h .
Also, since Subtraction with Borrow(SUBB) is done, carry or borrow
is automatically taken care except in last leg of operation.

Dept. of EEE, Sapthagiri College of Engineering, Page 26


Bangalore
IV Semester Microcontroller Lab. (BEE403)

RESULT:
After Execution:

Content of R2 (CY) Content of R1 (HB) Content of R0 (LB)


1 EA h AB h
h
GP_SUBTRACTION OF 16 BIT NUMBERS. docx
Verification:
To find [27A5 h– 3CFAh] = ?
27A5 h 0 0 1 0 0 1 1 1 1 0 1 0 0 1 0 1
3CFA 0 0 1 1 1 1 0 0 1 1 1 1 1 0 1 0

3CFA 0 0 1 1 1 1 0 0 1 1 1 1 1 0 1 0
3CFA h 1 1 0 0 0 0 1 1 0 0 0 0 0 1 0 1
1’s
+ 1
3CFA h 1 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0
2’s

27A5 h 0 0 1 0 0 1 1 1 1 0 1 0 0 1 0 1
3CFA 1 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0
Adding 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1
Invert 1 EAh ABh
CY

∴ [27A5h– 3CFAh] = 1 EA AB h. Ans.

Exercise: Try for various numbers and understand subtraction procedure.

Dept. of EEE, Sapthagiri College of Engineering, Page 27


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 7. PROGRAM FOR MULTIPLICATION OF TWO 8 BIT NUMBERS . OR . PROGRAM FOR


FINDING SQUARE OF AN 8 BIT NUMBER
ALGORITHM:
1. (writing Algorithm is left to the
student) 2.
. . . . . . . . . . . . . . . . .
; Multiplication of two 8 bit unsigned numbers.
; Let FAh and 4Ch be two 8 bit numbers involved in multiplication. Any other
; numbers like 10dx15 , 25hx78h can also be considered.
; Here, we learn the use of MUL AB instruction. Observe the CY and OV flags.
; When the instruction is executed, CY flag will be cleared, even if it was
se t earlier. OV flag will be set if the product exceeds FFh, otherwise
the flag is cleared, even it was set earlier. Learn PSW Register at this
juncture.
40h 41h
FAh 4Ch
ORG 0000h ;
MOV A, 40h ; Take one of the numbers in A. (p x q = q x p)
MOV B, 41h ; Take the other number in B. Use of A and B is a
must. MUL AB ; After execution of this statement, A ← LB & B
← HB
MOV R0, A ; ( note → comma in ADD A, B and no comma in MUL AB )
MOV R1, B ; observe the flags CY and
OV
END ;

RESULT: FAh x 4Ch = 4A38h


After Execution:
content of R1 content of R0
(High byte of (Low byte of
Result) Result)
4Ah 38h
GP_MULTIPLICATION OF TWO 8 BIT NUMBERS. docx
Obviously, to find the square of a given 8 bit number same program can be
used. Same number is taken in both A and B registers.

Dept. of EEE, Sapthagiri College of Engineering, Page 28


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 8. PROGRAM FOR DIVISION INVOLVING TWO 8 BIT


40h 41h
NUMBERS. ?
95h 25h
ALGORITHM: 40h 41h
1. . . . . . 2. . . . . 25h 95h
(writing Algorithm is
left to the student) ?

; DIVISION INVOLVING TWO 8 BIT UNSIGNED NUMBERS.


; Let 95h and 25h be the two 8 bit numbers involved for division.
; Identify the dividend and divisor first.
; case 1: dividing 95h(higher) from 25h(smaller) [95h/25h] and
; case 2: dividing 25h(smaller) from 95h(higher) [25h/95h].
; Here, we learn the use of DIV AB instruction (DIV BA is not allowed) and
; observe CY and OV flags for changes.
; The instruction automatically clears CY flag (if set earlier) and sets OV flag, if
; the division is invalid. (ex. divide by zero) theory: explain DIV instruction
; In DIV AB Instruction, the dividend must be in A and D ivisor must be in
B.(rule) ORG 0000h;
MOV A, 50h ; rule: Dividend is taken
in A. MOV B, 51h ; rule: Divisor is
taken in B.
DIV AB ; A is divided by B. A ← quotient, B ← remainder.
Observe PSW. MOV R0, A ; Move the quotient to register R0.
MOV R1, B ; Move the remainder to register
R1.
END ;
Divisor ) Dividend ( Quotient
----------
----------
——————————
RESULT: Remainder
Quotient (A) Reminder (B)
95h ÷
25h
25h ÷
95h
GP_DIVISION OF TWO 8 BIT NUMBERS.docx

Dept. of EEE, Sapthagiri College of Engineering, Page 29


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 9. PROGRAM FOR PICKING THE LARGEST ELEMENT


IN A GIVEN ARRAY OF DATA .
.
Write an assembly language program to find the largest number from array starting from location 40H and store the
result in location 50H
Code Comment
ORG 0000h

MOV R0, #40h

MOV R2, #05h

MOV 50h, #00h

rpt: MOV A, @R0

CJNE A, 50h, here

SJMP less

here: JC less

MOV 50h, A

less: INC R0

DJNZ R2, rpt

END

Answer the following Questions


1. Brief the algorithm used to find the largest element of the array.

Dept. of EEE, Sapthagiri College of Engineering, Page 30


Bangalore
IV Semester Microcontroller Lab. (BEE403)

2. Indicate addresses of the data used in finding largest element.

3. Modify the program to find smallest element from the array of 5 numbers.

Dept. of EEE, Sapthagiri College of Engineering, Page 31


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 10. PROGRAM FOR SORTING OF DATA (ASCENDING / DESCENDING * ).


Write an assembly language program to sort the given set of numbers in ascending order, starting from
location 50H

Code Comment
ORG 00H

nxtpass : MOV R3, #04h

MOV R0, #50h

MOV R2, #04h

nxtcomp : MOV A,@R0

MOV B, A

INC R0

MOV A,@R0

CJNE A, B, notequal

SJMP SKIP

notequal: JNC SKIP

DEC R0

MOV @R0, A

INC R0

MOV @R0, B

SKIP : DJNZ R2, nxtcomp

DJNZ R3, nxtpass

END

Dept. of EEE, Sapthagiri College of Engineering, Page 32


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Answer the following Questions

1. Brief the algorithm used to sort the numbers in ascending order.

2. Indicate addresses of the data used for sorting.

3. Change the addresses of the data and carry out the programming. Indicate the address locations.

4. What does the instruction @R0, @R1 mean?

Write an assembly language program to sort the given set of numbers in descending order, starting from location 50H

Code Comment
ORG 00H

nxtpass : MOV R3, #04h


Dept. of EEE, Sapthagiri College of Engineering, Page 33
Bangalore
IV Semester Microcontroller Lab. (BEE403)

MOV R0, #50h

MOV R2, #04h

nxtcomp : MOV A,@R0

MOV B, A

INC R0

MOV A,@R0

CJNE A, B, notequal

SJMP SKIP

notequal: JC SKIP

DEC R0

MOV @R0, A

INC R0

MOV @R0, B

SKIP : DJNZ R2, nxtcomp

DJNZ R3, nxtpass

END

Answer the following Questions


1. Brief the algorithm used to sort the numbers in descending order.

Dept. of EEE, Sapthagiri College of Engineering, Page 34


Bangalore
IV Semester Microcontroller Lab. (BEE403)

2. Indicate addresses of the data used for sorting.

3. Change the addresses of the data and carry out the programming. Indicate the address locations.

4. What does the instruction DJNZ and CJNE mean?

PROGRAM 11. COUNTERS: BINARY UP COUNTER / BINARY DOWN COUNTER *


* observe minor changes between the two Prgs.
ALGORITHM:
1. . . . . .
2. . . . . (writing Algorithm is left to the student)
; In Binary Up-Counter, count upwards from 00h to FFh , after specified Time Delay
; In Binary Dn-Counter, count Dn-wards from FFh to 00h , after specified TD
; Note the use of INC and DEC instructions for the UP/DOWN counters.

Dept. of EEE, Sapthagiri College of Engineering, Page 35


Bangalore
IV Semester Microcontroller Lab. (BEE403)

; Note that the used Time-delay program provides a Time-delay of


; approximately 10 sec. for every count.
ORG 0000h ;
MOV A, #00h ; #0FFh for down counter.
COUNT: MOV P0, A ;
ACALL DELAY ;
INC A ; DEC A instruction for down counter.
JNZ COUNT ; JNZ target . Jump i f accumulator is not zero.
HERE: SJMP HERE ;
; Time Delay subroutine for approx. 10 sec.
delay DELAY: ;
MOV R1, #0AAh ; (use #0FF for longer time delays).
LOOP3: MOV R2, #0AAh ;
LOOP2: MOV R3, #0AAh ;
LOOP1: DJNZ R3, LOOP1 ; complete innermost loop first.
DJNZ R2, LOOP2 ; complete the middle loop incl. the first for every
R2 DJNZ R1, LOOP3 ;
RET ; RETURN TO THE MAIN PROGRAM.
END

Result: View PORT ‘0’ and observe counting(wait for the given time delay).
GP_COUNTERS_BINARY COUNTER. docx

Dept. of EEE, Sapthagiri College of Engineering, Page 36


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 12. COUNTERS: BCD UP COUNTER / BCD DOWN COUNTER *


* observe minor changes between the two Prgs.
ALGORITHM:
1. . . . . .
2. . . . . (writing Algorithm is left to the student)

; BCD Numbers will have only 0 tp 9 decimal digits.


; In BCD Counter programs note that INC or DEC instructions are not used.
; In BCD up counter 01h is added every time, after a specific time- delay and
; DA A is operated. Whereas in BCD Down Counter 99h is added and DA A operated.
; Note that the used TD program provides a Time-delay of ≈ 10 sec. for every count.
ORG 0000h ;
MOV A, #00h ;
COUNT: MOV P0, A ;
ACALL DELAY ;
ADD A, #01h ; ADD A, #99h for down counter.
DA A ; decimal adjust the value
JNZ COUNT ;
here: SJMP here ;
; Time Delay subroutine for approx. 10 sec.
delay DELAY: ;
MOV R1, #0AAh ; (use #0FF for longer time delays).
LOOP3: MOV R2, #0AAh ;
LOOP2: MOV R3, #0AAh ;
LOOP1: DJNZ R3, LOOP1 ; complete innermost loop first.
DJNZ R2, LOOP2 ; complete the middle loop incl. the first for every
R2 DJNZ R1, LOOP3 ;
RET ; RETURN TO THE MAIN PROGRAM.
END
Result: View PORT ‘0’ and observe counting(wait for the given time delay).

Dept. of EEE, Sapthagiri College of Engineering, Page 37


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 13. PERFORMING LOGICAL OPERATIONS; LOGICAL AND, LOGICAL OR,


LOGICAL XOR for byte variables .

Write an assembly language program to perform the following operations on 3 bytes of data stored from location X
onwards
(A) OR the lower nibble of location X with upper nibble of location X+1
(B)XOR the result of (A) with contents of location X+2.

Code Comment
ORG 0000h

MOV A, 40h

ANL A, #0Fh

MOV 50h, A

MOV A, 41h

SWAP A

ANL A, #0Fh

ORL A, 50h

XRL A, 42h

MOV 50h, A

END

Answer the following Questions

1. Explain the logical instructions AND, OR and XOR.

2. How are the logical instructions used for bit manipulation? Give examples.

Dept. of EEE, Sapthagiri College of Engineering, Page 38


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 14. PERFORMING LOGICAL OPERATIONS; LOGICAL AND, LOGICAL OR,


LOGICAL XOR for bits

Write an assembly language program to perform the following operations on a given byte of data.
Set the bits 0 & 1, Reset bits 2&3, Compliment bits 6&7.

Code Comment
ORG 00H

MOV A, 40h

MOV P2,#00H

ORL A, #03h

ANL A, #0F3h

XRL A, #0C0h

MOV 50h, A

END

Answer the following Questions


1. Give examples of other bit manipulation instructions.

PROGRAM 15. PERFORMING SERIAL DATA COMMUNICATION.


Transmit “ GP” to PC using Serial Port and display it on Serial
Window. Data given for serial communication:
baud rate: 9600, 8 bits per data, 1
stop bit. Understand what these stand

Dept. of EEE, Sapthagiri College of Engineering, Page 39


Bangalore
IV Semester Microcontroller Lab. (BEE403)

for:
TMOD register has a value of
20h. TH1 register has a value
of FDh.
SCON register has a value of
50h.
JNB means what? When Bit will be set? What is to be done next?
ORG 0000h ;
MOV TMOD, #20h
; MOV
TH1, #0FDh ;
MOV SCON, #50h ;
SETB TR1 ;

MOV A, #’G’ ;
ACALL SEND ;

MOV A, #’P’ ;
ACALL SEND ;
HERE: SJMP HERE ;

SEND: MOV SBUF, A ;


WAIT: JNB TI, WAIT ; stay in loop as long as bit is
not set.
CLR TI ; after coming out, clear the bit.
RET ;
END

Instead of checking the bit of Timer Register for ‘ 0 b ’ we could have


used a Timedelay program of certain duration. But. what is the
disadvantage?

Dept. of EEE, Sapthagiri College of Engineering, Page 40


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 16. SERIAL DATA TRANSFER


Code Comment
ORG 00H
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON ,#50H
SETB TR1
AGAIN: MOV A,#'E'
ACALL TRANS
MOV A,#'L'
ACALL TRANS
MOV A,#'E'
ACALL TRANS
MOV A,#'C'
ACALL TRANS
MOV A,#'T'
ACALL TRANS
MOV A,#'R'
ACALL TRANS
MOV A,#'I'
ACALL TRANS
MOV A,#'C'
ACALL TRANS
MOV A,#'A'
ACALL TRANS
MOV A,#'L'
ACALL TRANS
MOV A,#20H
ACALL TRANS
MOV A,#'R'
ACALL TRANS
MOV A,#'O'
ACALL TRANS
MOV A,#'C'
ACALL TRANS
MOV A,#'K'
ACALL TRANS
MOV A,#'S'
ACALL TRANS
MOV A,#9
ACALL TRANS
AGAIN1: SJMP AGAIN1
TRANS: MOV SBUF,A
HERE: JNB TI,HERE
CLR TI
RET
END

Dept. of EEE, Sapthagiri College of Engineering, Page 41


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Answer the following Questions


1. What is SBUF register?

2. What is SCON register?

Dept. of EEE, Sapthagiri College of Engineering, Page 42


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 17. OBTAING TIME DELAY USING INTERNAL TIMER.


This program can be used as a TD Program (SubRoutine prg) in any other
Program. We are generating a Square Wave of required frequen cy on P2(or on
P2. 3).
Operate Timer 0 in mode 1. Hence set TMOD register value for this.
TL0 and TH0 values are first calculated for the req uired ‘f’ and entered.
Exact Time delays can be provided using Timers; instead of repeatedly
executing certain instructions which gives only approximate Time Delay.
Question: Generate a square of 500 Hz. (Time Period: T = 1/f = 2000 sec.)
Calculation:
(1) Generation of square wave for equal on time and off
time. T = 2000 sec.
on time = 1000 sec = off time.
Num of counts for 1000  sec = 100/1. 0582 = 920 counts.
The final count for the Timer is fixed (=FFFFh or 65536d).
Hence the Timer has to start with initial value of(65536 -920)=64616d
or FC68h. these values are entered in TH0 and TL0 registers.
ORG 0000h ;

BACK: MOV TMOD, #01h ;

MOV TH0, #0FCh ; setting values for off-time


MOV TL0, #68h ;
MOV P2, #0FFh ; or SETb P2.3 PARTICULAR BIT
ACALL DELAY ; FFh value is held till...( duration set in
TD prg.)

MOV TH0, #0FCh ; setting values for on-time

MOV TL0, #68h ;


MOV P2, #00h ; CLR P2.3
ACALL DELAY ; 00h value is held till…( duration set in
TD prg.)

SJMP BACK ;

DELAY : SETB TR0 ; start the timer

AGAIN: JNB TF0, AGAIN ; ‘kept on’ till overflow occurs

Dept. of EEE, Sapthagiri College of Engineering, Page 43


Bangalore
IV Semester Microcontroller Lab. (BEE403)

CLR TR0 ; stop Timer, counting(delay) is over


CLR TF0 ; clear Timer flag(set ready for
next) RET ;
END

Note that this is a Time delay program which can be used in another program
to provide a specified and exact Time Delay between two operations. Waveform
generation is not the main objective. Ex: The program can be used to provide
Timing between two events like switching ON and OFF a Lamp in Traffic signal
light.

Dept. of EEE, Sapthagiri College of Engineering, Page 44


Bangalore
IV Semester Microcontroller Lab. (BEE403)

PROGRAM 18: Write an assembly language program to add a series of ‘n’ numbers stored from location X onwards.
Code Comment
ORG 0000h

MOV R0, #40h

MOV R2, #05h

CLR A

MOV 50h, #00h

again: ADD A, @R0

JNC nxt

INC 50h

nxt: INC R0

DJNZ R2, again

MOV 51h, A

END

Write an assembly language program to add a series of ‘n’ decimal numbers stored from location X onwards.
Code Comment
ORG 0000h

MOV R0, #40h

MOV R2, #05h

CLR A

MOV 50h, #00h

again: ADD A, @R0

DAA

JNC nxt

INC 50h

nxt: INC R0

DJNZ R2, again

Dept. of EEE, Sapthagiri College of Engineering, Page 45


Bangalore
IV Semester Microcontroller Lab. (BEE403)

MOV 51h, A

END

Answer the following Questions

1. Modify the program to find the average or mean of N numbers.

PROGRAM 19: CODE CONVERSION PROGRAMS


19(a) Write an assembly language program to convert BCD to Binary
Code Comment
ORG 0000h

MOV A, 40h

ANL A, #0Fh

Dept. of EEE, Sapthagiri College of Engineering, Page 46


Bangalore
IV Semester Microcontroller Lab. (BEE403)

MOV 50h, A

MOV A, 40h

SWAP A

ANL A, #0Fh

MOV B, #0Ah

MUL AB

ADD A, 50h

MOV 50h, A

END

LOGIC: (UD x 10d) + LD


Answer the following Questions

1. Take two different BCD number and convert it to binary.

2. What does the instruction SWAP do?

19(b) Write an assembly language program to convert Binary to BCD.


Code Comment
ORG 0000h

MOV A, 40h

MOV B, #64h

DIV AB

MOV 50h, A

Dept. of EEE, Sapthagiri College of Engineering, Page 47


Bangalore
IV Semester Microcontroller Lab. (BEE403)

MOV A, B

MOV B, #0Ah

DIV AB

SWAP A

ADD A, B

MOV 51h, A

END

LOGIC: Binary/100d =quotient---- 100’s digit


Remainder/10d =quotient----10’s digit
Remainder-----unit’s digit

Answer the following Questions

1. Modify the program to convert a series binary numbers into BCD numbers.

Dept. of EEE, Sapthagiri College of Engineering, Page 48


Bangalore
IV Semester Microcontroller Lab. (BEE403)

19(c) Write an assembly language program to convert ASCII to Binary.


Code Comment
ORG 0000h

MOV A, 50h

CJNE A, #40h, nxt

SJMP last

JC less40

SUBB A, #37h

less40: CLR C

SUBB A, #30h

last: MOV 51h, A

END

ORG 0000h

LOGIC:
ASCII Binary
30h-39h 0-9h
41h-46h A-Fh

19(d) Write an assembly language program to convert Binary to ASCII.


Code Comment
ORG 0000h

MOV A, 40h

ANL A, #0Fh

LCALL ascii

MOV 50h, A

MOV A, 40h

ANL A, #0F0h

SWAP A

LCALL ascii

Dept. of EEE,
MOVSapthagiri
51h, A College of Engineering, Page 49
Bangalore
ascii: CJNE A, #0Ah, nxt
IV Semester Microcontroller Lab. (BEE403)

SJMP last

nxt : JC lessA

ADD A, #37h

SJMP last

lessA : ADD A, #30h

last : RET

END

LOGIC:
Binary ASCII
If binary < 0Ah Add #30h
If binary >=0Ah Add #37h

Answer the following Questions


1. Take different binary number and convert it to ASCII.

2. Explain the LCALL instruction and its use in the program..

PROGRAM 20: Write an assembly language program to find the 2’s compliment of a
Dept. of EEE, Sapthagiri College of Engineering, Page 50
given 16-bit number.
Bangalore
Code Comment
ORG 0000h
IV Semester Microcontroller Lab. (BEE403)

MOV A, 41h

CPL A

MOV 51h, A

MOV A, 40h

CPL A

INC A

MOV 50h, A

END

Dept. of EEE, Sapthagiri College of Engineering, Page 51


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Part B
Interfacing

1. Write a C program to generate the sinusoidal waveform and display the same on CRO
Dept. of EEE, Sapthagiri College of Engineering, Page 52
Bangalore
IV Semester Microcontroller Lab. (BEE403)

Vout= 5V+ (5V*Sinθ)


#include <reg52.h>
void main( )
{
unsigned char i;
unsigned char idata table[180]=

{128,132,136,141,154,150,154,158,163,167,171,175,180,184,188,192,195,199,203,206,210,213,216,
220,223,226,228,231,234,236,238,241,243,244,246,247,248,249,250,251,252,253,254,255,255,255,2
55,255,254,254,253,252,251,249,246,244,243,241,238,236,234,231,228,226,223,220,216,213,210,20
6,203,199,195,192,188,184,180,175,171,167,163,158,154,150,145,141,136,132,128,123,119,114,110,
105,101,97,92,88,84,80,75,71,67,64,60,56,52,49,45,42,39,35,32,29,27,24,21,19,17,14,12,11,9,7,6,4,3,
2,1,1,0,0,0,0,0,0,0,0,1,1,2,3,4,6,7,9,11,12,14,17,19,21,24,27,29,32,35,39,42,45,49,52,56,60,64,67,71,7
5,80,84,88,92,97,101,105,110,114,119,123,128};
P0=0X00;
while(1)
{
for(i=0;i<180;i++)
P0=table[i];
}
}
Observations:

Results & Inference

Dept. of EEE, Sapthagiri College of Engineering, Page 53


Bangalore
IV Semester Microcontroller Lab. (BEE403)

2. Write a C program to generate the Square waveform and display the same on CRO
#include<reg52.h>
Void MSDelay ( unsigned int );
Void main ( )
{
P0 = 0 X 00;
While (1)
{
P0 = 0 X 00;
MSDelay(50);
P0 = 0 X ff;
MSDelay(50);
}
}
Void MSDelay ( unsigned int value)
{unsigned int y;
for ( y = 0 ; y < value ; y++ );
}

Observations:

Results & Inference

Dept. of EEE, Sapthagiri College of Engineering, Page 54


Bangalore

3. Write a C program to generate the triangular waveform and display the same on CRO
IV Semester Microcontroller Lab. (BEE403)

#include<reg52.h>
void main ( )
{
Unsigned int i;
P0 = 0 X 00;
While (1)
{
for (i = 0; i<255; i++)
{
P0 = i;
}

for (i = 255; i>0; i--)


{
P0 = i;
}
}
}

Observations:

Results & Inference

Dept. of EEE, Sapthagiri College of Engineering, Page 55


Bangalore

4. Write a C program to generate the Ramp waveform and display the same on CRO
IV Semester Microcontroller Lab. (BEE403)

#include<reg52.h>
void main ( )
{
Unsigned int i;
P0 = 0 X 00; set P0 as i/p port
While (1)
{
for (i = 0; i<255; i++)
{
P0 = i;
}
}
}

Observations:

Results & Inference

Dept. of EEE, Sapthagiri College of Engineering, Page 56


Bangalore

5.Write a C program to generate the Rectangular waveform and display the same on CRO
IV Semester Microcontroller Lab. (BEE403)

#include<reg52.h>
Void MSDelay ( unsigned int );
void main ( )
{
P0 = 0 X 00;
While (1)
{
P0 = 0 X 00;
MSDelay(50);
P0 = 0 X ff;
MSDelay(100);
}
}
Void MSDelay (unsigned int value)
{Unsigned int y;
for ( y = 0 ; y < value ; y++ );
}

Observations:

Results & Inference

Dept. of EEE, Sapthagiri College of Engineering, Page 57


Bangalore
6. Write a C program to generate the Saw tooth waveform and display the same on CRO
IV Semester Microcontroller Lab. (BEE403)

#include<reg52.h>
void main ( )
{
Unsigned int i;
P0 = 0 X 00;
While (1)
{
for (i = 0; i<255; i = i+5)
{
P0 = i;
}

for (i = 255; i>1; i--)


{
P0 = i;
}
}
}

Observations:

Results & Inference

Dept. of EEE, Sapthagiri College of Engineering, Page 58


Bangalore
IV Semester Microcontroller Lab. (BEE403)

7.Stepper motor and DC motor speed control interface with 8051 microcontroller

7a.To rotate Stepper motor in clockwise 7b.To rotate Stepper motor in anticlockwise
direction direction
#include<reg52.h>
Void MSDelay(unsigned char value); #include<reg52.h>
void main( ) Void MS Delay(unsigned char value);
{ void main( )
While (1) {
{ While (1)
P0=0X66; {
MSDelay(1); P0=0X33;
P0=0Xcc; MSDelay(1);
MSDelay(1); P0=0X99;
P0=0X99; MSDelay(1);
MSDelay(1); P0=0Xcc;
P0=0X33; MSDelay(1);
MSDelay(1); P0=0X66;
} MSDelay(1);
} }
Void MSDelay(unsigned char value) }
{ Void MSDelay(unsigned char value)
Unsigned int y,x; {
for(x=0;x<500;x++); Unsigned int y,x;
for(y=0;y<value;y++); for(x=0;x<500;x++)
} for(y=0;y<value;y++);
}

Dept. of EEE, Sapthagiri College of Engineering, Page 59


Bangalore
IV Semester Microcontroller Lab. (BEE403)

8a. To rotate DC motor in clockwise 8b. To rotate DC motor in anticlockwise


direction direction

#include<reg52.h> #include<reg52.h>
Void MSDelay (unsigned char value); Void MSDelay (unsigned char value);
sbit enablr=P0^6; sbit enablr=P0^6;
sbit mtr_1=P0^7; sbit mtr_1=P0^7;
sbit mtr_2=P0^4; sbit mtr_2=P0^4;
void main( ) void main( )
{ {
while(1) while(1)
{ {
mtr_1=1; mtr_1=0;
mtr_2=0; mtr_2=1;
MSDelay(50); MSDelay(50);
mtr_1=0; mtr_1=0;
} }
} }
Void MSDelay(unsigned char value) Void MSDelay(unsigned char value)
{ {
Unsigned int y,x; Unsigned int y,x;
for(x=0;x<40;x++) for(x=0;x<40;x++)
for(y=0;y<value;y++); for(y=0;y<value;y++);
} }

Dept. of EEE, Sapthagiri College of Engineering, Page 60


Bangalore
IV Semester Microcontroller Lab. (BEE403)

9. Write a C Program to display a given message on LCD Panel using 8051 microcontroller
LCD Panel Details:
• The size of LCD is 16 characters and 2 lines.

• The address of the first line is 80H and second line is C0H.

Dept. of EEE, Sapthagiri College of Engineering, Page 61


Bangalore
IV Semester Microcontroller Lab. (BEE403)

#include<reg52.h>
Sfr ldata=0x80;
Sbit rs=P3^7;
Sbit rw=P3^6;
Sbit en=P3^5;
sbit busy=P0^7;
void MSDelay(unsigned char);
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void lcdready( );
unsigned char i;
unsigned char idatamsg[15]={"REVA University"} ;
void main( )
{
lcdcmd(0x38);
lcdcmd(0x0e);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x83);
for(i=0;i<12;i++)
lcddata(msg[i]);
while(1)
{
lcdcmd(0xc3);
}
}
Void MSDelay(unsigned char value)
{
Unsigned int x,y;
for(x=0;x<900;x++)
for(y=0;y<value;y++);
}
Void lcdcmd(unsigned char value)
{
lcdready( );
ldata=value;
rs=0;
rw=0;
en=1;

Dept. of EEE, Sapthagiri College of Engineering, Page 62


Bangalore
IV Semester Microcontroller Lab. (BEE403)

MSDelay(1);
en=0;
return;
}
Void lcddata(unsigned char value)
{
lcdready( );
ldata=value;
rs=1;
rw=0 ;
en=1 ;
MSDelay(1);
en=0;
return;
}
Void lcdready( )
{
busy=1;
rs=0;
rw=1;
while(busy==1)
{
en=0;
MSDelay(1);
en=1;
}
return;
}

Results & Inference

Dept. of EEE, Sapthagiri College of Engineering, Page 63


Bangalore / Critical thinking Questions:
Challenge
1. Write code to add #1 to the contents of accumulator in 5 different ways
IV Semester Microcontroller Lab. (BEE403)

Challenging/Critical thinking questions:


1. Write code to add #1 to the contents of accumulator in 5 different ways.
2. Consider the following code segment
MOV A,#47H
MOV B,#24H
ADD A,B
DA A
What would be the contents of the accumulator after the ADD? After DA A Instruction
3. Write ALP to multiply the numbers in R2 and Port 2and store in external RAM location 20H(LSB)
and 21H(MSB)
4. Write a program to find the 2’s complement for a given number and store the result in 47H
5. Writean ALP to mask the MSB of the given number
6. Consider that a switch SW is connected to pin P2.3. Monitor the SW status and if SW =0, send
“HELLO” and if SW=1,send “WORLD” serially.Assumebaud rate is 9600,8 bitdata and 1 stop bit.
7. Write an ALP to exchange the data between two registers using stack instructions.
8. Find the delay in the subroutine, if the crystal frequency is 12MHz
DELAY:MOV R1,#0F3H
MOV R0,#00H
LOOP:DJNZ R0, LOOP
DJNZ R1,LOOP
RET
9. What are the ways to create a time delay? Discuss the factors affecting accuracy of the time delay.
10. Write a program to read the status of switch shown in the figure. If S is ON ,turn on the LED
else turn off the LED.

Dept. of EEE, Sapthagiri College of Engineering, Page 64


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Sample Viva Questions


1. What do you mean by Embedded System? Give examples.
2. Why are embedded Systems useful?
3. What are the segments of Embedded System?
4. What is Embedded Controller?
5. What is Microcontroller?
6. List out the differences between Microcontroller and Microprocessor.
7. How are Microcontrollers more suitable than Microprocessor for Real Time
Applications?
8. What are the General Features of Microcontroller?
9. Explain briefly the classification of Microcontroller.
10. Explain briefly the Embedded Tools.
11. Explain the general features of 8051 Microcontroller.
12. How many pin the 8051 has?
13. Differentiate between Program Memory and Data Memory.
14. What is the size of the Program and Data memory?
15. Write a note on internal RAM. What is the necessity of register banks? Explain.
16. How many address lines are required to address 4K of memory? Show the
necessary calculations.
17. What is the function of accumulator?
18. What are SFR’s? Explain briefly.
19. What is the program counter? What is its use?
20. What is the size of the PC?
21. What is a stack pointer (SP)?
22. What is the size of SP?
23. What is the PSW? And briefly describe the function of its fields.
24. What is the difference between PC and DPTR?
25. What is the difference between PC and SP?
26. What is ALE? Explain the functions of the ALE in 8051.
27. Describe the 8051 oscillator and clock.
28. What are the disadvantages of the ceramic resonator?
29. What is the function of the capacitors in the oscillator circuit?
30. Show with an example, how the time taken to execute an instruction can be
calculated.
31. What is the Data Pointer register? What is its use in the 8051?
32. Explain how the 8051 implement the Harvard Architecture?
33. Explain briefly the difference between the Von Neumann and the Harvard
Architecture.
34. Describe in detail how the register banks are organized.
35. What are the bit addressable registers and what is the need?
36. What is the need for the general purpose RAM area?
37. Write a note on the Stack and the Stack Pointer.
38. Why should the stack be placed high in internal RAM?
39. Explain briefly how internal and external ROM gets accessed.
40. What are the different addressing modes supported by 8051 Microcontroller ?
41. Explain the Immediate Addressing Mode.
42. Explain the Register Addressing Mode.
43. Explain the Direct Addressing Mode.
Dept.
44. of EEE,
Explain Sapthagiri
the Indirect College
Addressing Mode.of Engineering, Page 65
Bangalore
45. Explain the Code Addressing Mode.
46. Explain in detail the Functional Classification of 8051 Instruction set
47. What are the instructions used to operate stack?
IV Semester Microcontroller Lab. (BEE403)

48. What are Accumulator specific transfer instructions?


49. What is the difference between INC and ADD instructions?
50. What is the difference between DEC and SUBB instructions?
51. What is the use of OV flag in MUL and DIV instructions?
52. What are single and two operand instructions?
53. Explain Unconditional and Conditional JMP and CALL instructions.
54. Explain the different types of RETURN instructions.
55. What is a software delay?
56. What are the factors to be considered while deciding a software delay?
57. What is a Machine cycle?
58. What is a State?
59. Explain the need for Hardware Timers and Counters?
60. Give a brief introduction on Timers/Counter.
61. What is the difference between Timer and Counter operation?
62. How many Timers are there in 8051?
63. What are the three functions of Timers?
64. What are the different modes of operation of timer/counter?
65. Give a brief introduction on the various Modes.
66. What is the count rate of timer operation?
67. What is the difference between mode 0 and mode 1?
68. What is the difference Modes 0,1,2 and 3?
69. How do you differentiate between Timers and Counters?
70. Explain the function of the TMOD register and its various fields?
71. How do you control the timer/counter operation?
72. What is the function of TF0/TF1 bit
73. Explain the function of the TCON register and its various fields?
74. Explain how the Timer/Counter Interrupts work.
75. Explain how the 8051 counts using Timers and Counters.
76. Explain Counting operation in detail in the 8051.
77. Explain why there is limit to the maximum external frequency that can be
counted.
78. What’s the benefit of the auto-reload mode?
79. Write a short note on Serial and Parallel communication and highlight their
advantages and disadvantages.
80. Explain Synchronous Serial Data Communication.
81. Explain Asynchronous Serial Data Communication.
82. Explain Simplex data transmission with examples.
83. Explain Half Duplex data transmission with examples.
84. Explain Full Duplex data transmission with examples.
85. What is Baud rate?
86. What is a Modem?
87. What are the various registers and pins in the 8051 required for Serial
communication? Explain briefly.
88. Explain SCON register and the various fields.
89. Explain serial communication in general (synchronous and asynchronous). Also
explain the use of the parity bit.
90. Explain the function of the PCON register during serial data communication.

Dept. of EEE, Sapthagiri College of Engineering, Page 66


Bangalore
IV Semester Microcontroller Lab. (BEE403)

91. How the Serial data interrupts are generated?


92. How is data transmitted serially in the 8051? Explain briefly.
93. How is data received serially in the 8051? Explain briefly.
94. What are the various modes of Serial Data Transmission? Explain each mode
briefly.
95. Explain with a timing diagram the shift register mode in the 8051.
96. What is the use of the serial communication mode 0 in the 8051?
97. Explain in detail the Serial Data Mode 1 in the 8051.
98. Explain how the Baud rate is calculated for the Serial Data Mode 1.
99. How is the Baud rate for the Multiprocessor communication Mode calculated?
100. Explain in detail the Multiprocessor communication Mode in the 8051.
101. Explain the significance of the 9th bit in the Multiprocessor communication
Mode.
102. Explain the Serial data mode 3 in the 8051.
103. What are interrupts and how are they useful in Real Time Programming?
104. Briefly describe the Interrupt structure in the 8051.
105. Explain about vectored and non-vectored interrupts in general.
106. What are the five interrupts provided in the 8051?
107. What are the three registers that control and operate the interrupts in 8051?
108. Describe the Interrupt Enable (IE) special function register and its various bits.
109. Describe the Interrupt Priority (IP) special function register and its need.
110. Explain in detail how the Timer Flag interrupts are generated.
111. Explain in detail how the Serial Flag interrupt is generated.
112. Explain in detail how the External Flag interrupts are generated.
113. What happens when a high logic is applied on the Reset pin?
114. Why the Reset interrupt is called a non-maskable interrupt?
115. Why do we require a reset pin?
116. How can you enable/disable some or all the interrupts?
117. Explain how interrupt priorities are set? And how interrupts that occur
simultaneously are handled.
118. What Events can trigger interrupts, and where do they go after getting
triggered?
119. What are the actions taken when an Interrupt Occurs?
110. What are Software generated interrupts and how are they generated?
111. What is RS232 and MAX232?
112. What is the function of RS and E pins in an LCD?
113. What is the use of R/W pin in an LCD?
114. What is the significance of DA instruction?
115. What is packed and unpacked BCD?
116. What is the difference between CY and OV flag?
117. When will the OV flag be set?
118. What is an ASCII code?

Dept. of EEE, Sapthagiri College of Engineering, Page 67


Bangalore
IV Semester Microcontroller Lab. (BEE403)

8051 Instructions
8051 has about 111 instructions. These can be grouped into the following categories
1. Arithmetic Instructions
2. Logical Instructions
3. Data Transfer instructions
4. Boolean Variable Instructions
5. Program Branching Instructions

The following nomenclatures for register, data, address and variables are used while write instructions.
A: Accumulator

B: "B" register

C: Carry bit

Rn: Register R0 - R7 of the currently selected register bank

Direct: 8-bit internal direct address for data. The data could be in lower 128bytes of RAM (00 - 7FH) or it
could be in the special function register (80 - FFH).

@Ri: 8-bit external or internal RAM address available in register R0 or R1. This is used for indirect
addressing mode.

#data8: Immediate 8-bit data available in the instruction.

#data16: Immediate 16-bit data available in the instruction.

Addr11: 11-bit destination address for short absolute jump. Used by instructions AJMP & ACALL. Jump
range is 2 kbyte (one page).

Addr16: 16-bit destination address for long call or long jump.

Rel: 2's complement 8-bit offset (one - byte) used for short jump (SJMP) and all conditional jumps.

bit: Directly addressed bit in internal RAM or SFR

Dept. of EEE, Sapthagiri College of Engineering, Page 68


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Arithmetic Instructions
Mnemonics Description Bytes Instruction Cycles
ADD A, Rn A A + Rn 1 1
ADD A, direct A A + (direct) 2 1
ADD A, @Ri A A + @Ri 1 1
ADD A, #data A A + data 2 1
ADDC A, Rn A A + Rn + C 1 1
ADDC A, direct A A + (direct) + C 2 1
ADDC A, @Ri A A + @Ri + C 1 1
ADDC A, #data A A + data + C 2 1
DA A Decimal adjust accumulator 1 1
DIV AB Divide A by B
A quotient 1 4
B remainder
DEC A A A -1 1 1
DEC Rn Rn Rn - 1 1 1
DEC direct (direct) (direct) - 1 2 1
DEC @Ri @Ri @Ri - 1 1 1
INC A A A+1 1 1
INC Rn Rn Rn + 1 1 1
INC direct (direct) (direct) + 1 2 1
INC @Ri @Ri @Ri +1 1 1
INC DPTR DPTR DPTR +1 1 2
MUL AB Multiply A by B
A low byte (A*B) 1 4
B high byte (A* B)

SUBB A, Rn A A - Rn - C 1 1
SUBB A, direct A A - (direct) - C 2 1
SUBB A, @Ri A A - @Ri - C 1 1
SUBB A, #data A A - data - C 2 1

Dept. of EEE, Sapthagiri College of Engineering, Page 69


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Logical Instructions
Mnemonics Description Bytes Instruction Cycles
ANL A, Rn A A AND Rn 1 1
ANL A, direct A A AND (direct) 2 1
ANL A, @Ri A A AND @Ri 1 1
ANL A, #data A A AND data 2 1
ANL direct, A (direct) (direct) AND A 2 1
ANL direct, #data (direct) (direct) AND data 3 2
CLR A A 00H 1 1
CPL A A A 1 1
ORL A, Rn A A OR Rn 1 1
ORL A, direct A A OR (direct) 1 1
ORL A, @Ri A A OR @Ri 2 1
ORL A, #data A A OR data 1 1
ORL direct, A (direct) (direct) OR A 2 1
ORL direct, #data (direct) (direct) OR data 3 2
RL A Rotate accumulator left 1 1
RLC A Rotate accumulator left through 1 1
carry
RR A Rotate accumulator right 1 1
RRC A Rotate accumulator right through 1 1
carry
SWAP A Swap nibbles within Acumulator 1 1
XRL A, Rn A A EXOR Rn 1 1
XRL A, direct A A EXOR (direct) 1 1
XRL A, @Ri A A EXOR @Ri 2 1
XRL A, #data A A EXOR data 1 1
XRL direct, A (direct) (direct) EXOR A 2 1
XRL direct, #data (direct) (direct) EXOR data 3 2

Dept. of EEE, Sapthagiri College of Engineering, Page 70


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Data Transfer Instructions


Mnemonics Description Bytes Instruction
Cycles
MOV A, Rn A Rn 1 1
MOV A, direct A (direct) 2 1
MOV A, @Ri A @Ri 1 1
MOV A, #data A data 2 1
MOV Rn, A Rn A 1 1
MOV Rn, direct Rn (direct) 2 2
MOV Rn, #data Rn data 2 1
MOV direct, A (direct) A 2 1
MOV direct, Rn (direct) Rn 2 2
MOV direct1, (direct1) (direct2) 3 2
direct2
MOV direct, @Ri (direct) @Ri 2 2
MOV direct, #data (direct) #data 3 2
MOV @Ri, A @Ri A 1 1
MOV @Ri, direct @Ri (direct) 2 2
MOV @Ri, #data @Ri data 2 1
MOV DPTR, DPTR data16 3 2
#data16
MOVC A, A Code byte pointed by A + DPTR 1 2
@A+DPTR
MOVC A, A Code byte pointed by A + PC 1 2
@A+PC
MOVC A, @Ri A Code byte pointed by Ri 8-bit address) 1 2
MOVX A, A External data pointed by DPTR 1 2
@DPTR
MOVX @Ri, A @Ri A (External data - 8bit address) 1 2
MOVX @DPTR, @DPTR A(External data - 16bit address) 1 2
A
PUSH direct (SP) (direct) 2 2
POP direct (direct) (SP) 2 2
XCH Rn Exchange A with Rn 1 1
XCH direct Exchange A with direct byte 2 1
XCH @Ri Exchange A with indirect RAM 1 1
XCHD A, @Ri Exchange least significant nibble of A with 1 1
that of indirect RAM

Dept. of EEE, Sapthagiri College of Engineering, Page 71


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Boolean Variable Instructions


Mnemonics Description Bytes Instruction Cycles
CLR C C-bit 0 1 1
CLR bit bit 0 2 1
SET C C 1 1 1
SET bit bit 1 2 1
CPL C C 1 1
CPL bit bit 2 1
ANL C, /bit C C. 2 1
ANL C, bit C C. bit 2 1
ORL C, /bit C C+ 2 1
ORL C, bit C C + bit 2 1
MOV C, bit C bit 2 1
MOV bit, C bit C 2 2

Dept. of EEE, Sapthagiri College of Engineering, Page 72


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Program Branching Instructions


Mnemonics Description Bytes Instruction
Cycles
ACALL addr11 PC + 2 (SP) ; addr 11 PC 2 2
AJMP addr11 Addr11 PC 2 2
CJNE A, direct, rel Compare with A, jump (PC + rel) if not 3 2
equal
CJNE A, #data, rel Compare with A, jump (PC + rel) if not 3 2
equal
CJNE Rn, #data, rel Compare with Rn, jump (PC + rel) if not 3 2
equal
CJNE @Ri, #data, rel Compare with @Ri A, jump (PC + rel) if 3 2
not equal
DJNZ Rn, rel Decrement Rn, jump if not zero 2 2
DJNZ direct, rel Decrement (direct), jump if not zero 3 2
JC rel Jump (PC + rel) if C bit = 1 2 2
JNC rel Jump (PC + rel) if C bit = 0 2 2
JB bit, rel Jump (PC + rel) if bit = 1 3 2
JNB bit, rel Jump (PC + rel) if bit = 0 3 2
JBC bit, rel Jump (PC + rel) if bit = 1 3 2
JMP @A+DPTR A+DPTR PC 1 2
JZ rel If A=0, jump to PC + rel 2 2
JNZ rel If A ≠ 0 , jump to PC + rel 2 2
LCALL addr16 PC + 3 (SP), addr16 PC 3 2
LJMP addr 16 Addr16 PC 3 2
NOP No operation 1 1
RET (SP) PC 1 2
RETI (SP) PC, Enable Interrupt 1 2
SJMP rel PC + 2 + rel PC 2 2
JMP @A+DPTR A+DPTR PC 1 2
JZ rel If A = 0. jump PC+ rel 2 2
JNZ rel If A ≠ 0, jump PC + rel 2 2
NOP No operation 1 1

Dept. of EEE, Sapthagiri College of Engineering, Page 73


Bangalore
IV Semester Microcontroller Lab. (BEE403)

8051 Microcontroller Special Function Registers

All the 21 8051 Microcontroller Special Function Registers (SFRs) along with their functions and
Internal RAM Address is given in the following table.

Dept. of EEE, Sapthagiri College of Engineering, Page 74


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Annexure-A

MCS-51 Instruction set

Dept. of EEE, Sapthagiri College of Engineering, Page 75


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Dept. of EEE, Sapthagiri College of Engineering, Page 76


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Dept. of EEE, Sapthagiri College of Engineering, Page 77


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Annexure -B
8051 Special Function Registers:
1. Timer Mode Control Register (TMOD):
TMOD can be considered to be two duplicate 4-bit registers, each of which controls
the action of one of the timers. The “Timer” or “Counter” function is selected by control bits
C/T, and in different operating modes, which are selected by bit-pairs (M1, M0) in TMOD.
MSB LSB
GATE C/T M1 M0 GATE C/T M1 M0

Timer 1 Timer 0
Gating control when set. Counter “x” is enabled only while “INTx” pin is
GATE high and “TRx” control pin is set. When cleared Timer “x” is enabled
whenever “TRx” control bit is set.
Timer or Counter Selector cleared for Timer operation (input from internal
C/T system clock.) Set for Counter operation (input from “Tx” input pin).
M1 M0 OPERATI0N
0 0 13-bit Timer/Counter 5-bits of “TLx” and 8-bits of “THx” are used.
0 1 16-bit Timer/Counter 8-bits of “TLx” and 8-bits of “THx” are cascaded.
8-bit auto-reload Timer/Counter “THx” holds a value which is to be
1 0
reloaded into “TLx” each time it overflows.
(Timer 0) TL0 is an 8-bit Timer/Counter controlled by the standard Timer
1 1 0 control bits. TH0 is an 8-bit timer only controlled by Timer 1 control bits.
Timer/Counter 1 stopped.
2. Interrupt Enable (IE) Register :

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0


EA x x ES ET1 EX1 ET0 EX0

Symbol Name and Function


Enable All. If 0, Disables all interrupts and no interrupt is acknowledged. If
EA 1, each interrupt can be individually enabled or disabled by programming
appropriate bit.
x Reserved
x -
ES Enable Serial Interrupt. If 1, enables TI or RI to generate interrupt.

ET1 Enable Timer 1 interrupt. If 1, Enables the TF1 to generate the interrupt.
EX1 Enable External interrupt 1. If 1, Enables the INT1 to generate the interrupt.
ET0 Enable Timer 0 interrupt. If 1, Enables the TF0 to generate the interrupt.
EX0 Enable External interrupt 0. If 1, Enables the INT0 to generate the interrupt.

Dept. of EEE, Sapthagiri College of Engineering, Page 78


Bangalore
IV Semester Microcontroller Lab. (BEE403)

3.Timer Control Register (TCON):


TCON has control bits and flags for the timers in the upper nibble, and control bits and
flags for the external interrupts in lower nibble.
MSB LSB

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

Bit Symbol Function


Timer 1 overflow flag. Set by hardware on Timer/Counter
TCON.7 TF1 overflow. Cleared by hardware when processor vectors to
interrupt routine, or clearing the bit in software.
Timer 1 Run control bit. Set/cleared by software to turn
TCON.6 TR1
Timer/Counter on/off.
Timer 0 overflow flag. Set by hardware on Timer/Counter
TCON.5 TF0 overflow. Cleared by hardware when processor vectors to
interrupt routine, or by clearing the bit in software.
Timer 0 Run control bit. Set/cleared by software to turn
TCON.4 TR0
Timer/Counter on/off.
Interrupt 1 Edge flag. Set by hardware when external interrupts
TCON.3 IE1
edge detected. Cleared when interrupt processed.
Interrupt 1 type control bit. Set/cleared by software to specify
TCON.2 IT1
falling edge/low level triggered external interrupts.
Interrupt 0 Edge flag. Set by hardware when external interrupts
TCON.1 IE0
edge detected. Cleared when interrupt processed.
Interrupt 0 Type control bit. Set/cleared by software to specify
TCON.0 IT0
falling edge/low Level triggered external interrupts.
4.Interrupt Priority (IP) Register:
Each source of the interrupt can be individually programmed to be in either of the two
priority levels. The priorities can be assigned to each interrupt by programming appropriate
bits in the SFR Interrupt Priority Register.

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0


x x x PS PT1 PX1 PT0 PX0

Symbol Name and Function


x Reserved
PS Priority of Serial Interrupt. If 1, Priority of Serial Interrupt is higher
PT1 Priority of Timer 1 interrupt. If 1, Priority of Timer 1 interrupt is higher
PX1 Priority of External interrupt 1. If 1, Priority of the INT1 is higher
PT0 Priority of Timer 0 interrupt. If 1, Priority of Timer 0 Interrupt is higher
PX0 Priority of External interrupt 0. If 1, Priority of the INT0 is higher

Dept. of EEE, Sapthagiri College of Engineering, Page 79


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Serial Port Control Register (SCON):


The serial port control and status register is the Special Function Register SCON. This
register contains not only the mode selection bits, but also the 9th data bit for transmit and
receive (TB8 and RB8) and the serial port interrupt bits (TI and RI).
MSB LSB

SM0 SM1 SM2 REN TB8 RB8 TI RI

Where SM0, SM1 specify the serial port mode, as follows:

SM0 SM1 Mode Description Baud Rate


0 0 0 Shift register f osc / 12
0 1 1 8-bit UART Variable
1 0 2 9-bit UART f osc / 64 or fosc /32
1 1 3 9-bit UART variable

Enables the multiprocessor communication feature in Modes 2 and 3. In Mode 2 or


3, if SM2 is set to 1, then Rl will not be activated if the received 9th data bit (RB8)
SM2
is 0. In Mode 1, if SM2=1 then RI will not be activated if a valid stop bit was not
received. In Mode 0, SM2 should be 0.
Enables serial reception. Set by software to enable reception. Clear by software to
REN
disable reception.
The 9th data bit that will be transmitted in Modes 2 and 3. Set or clear by software
TB8
as desired.
In Modes 2 and 3, is the 9th data bit that was received. In Mode 1, it SM2=0, RB8 is
RB8 the stop bit that was received. In Mode 0, RB8 is not used.
Transmit interrupt flag. Set by hardware at the end of the 8th bit time in Mode 0, or
TI at the beginning of the stop bit in the other modes, in any serial transmission. Must
be cleared by software only.
Receive interrupt flag. Set by hardware at the end of the 8th bit time in Mode 0, or
RI halfway through the stop bit time in the other modes, in any serial reception (except
see SM2). Must be cleared by software only.

Dept. of EEE, Sapthagiri College of Engineering, Page 80


Bangalore
IV Semester Microcontroller Lab. (BEE403)

Additional Programs

1. Program to generate arithmetic progression. //Tn=a+(n-1)d clr c

mov r1,#03h //d


mov r2,#01h //a mov r3,#08h
mov r0,#30h mov 30h,r2 mov
r4,#01h
next: mov a,r1
mov b,r4 mul ab addc a,r2
inc r0
mov @r0,a inc r4
djnz r3,next end
2. Program to find the 2 out of 5 code.

mov r0,#30h ; r0 as pointer


mov a,@r0 ; no. to the Acc.
ANL a,#0e0h
jnz notvalid ; checks the 1st 3bits
mov a,@r0
mov r2,#5 ; r2 as counter
mov r1,#00h ; r0 stores no. of ones
clr c
check: rrc a
jnc skip
inc r1 ; if carry increment r1
skip: djnz r2,check
mov A,r1
clr c
subb A,#02h
jnz notvalid ; (a)!= 0 ===> (r2)!= 2 no. is not valid.
inc r0
mov @r0,#0ffh ; valid 2 out of 5 code sjmp exit
notvalid: inc r0
mov @r0,#11h ; in valid 2 out of 5 code
exit: sjmp exit end

Dept. of EEE, Sapthagiri College of Engineering, Page 81


Bangalore
IV Semester Microcontroller Lab. (BEE403)

3. Program to generate first ten Fibonacci numbers.

Mov dptr, #9000h Mov r3, #08h


Movx a, @dptr Mov r0,a
Inc dptr
Movx a, @dptr Back: xch a, r0
Add a,r0 Inc dptr
Movx @dptr,a Djnz r3,back
Lcall 0003h
.
4.Program to add multibyte numbers.

Mov dptr,#9000h Mov r1,#04h


Mov r2,#90h Mov r3,#91h Mov
r4,#92h
Clr c
Mov dph,r2
Back: movx a, @dptr Mov r5,a
Mov dph,r3 Movx a,@dptr
Addc a,r5 //Note:For multibyte subtraction put subb a,r5 Mov dph,r4
Movx @dptr,a Inc dptr
Djnz r1,back Jnc end1 Mov
a,#01h
Movx @dptr, a End1:lcall 0003h
End

Dept. of EEE, Sapthagiri College of Engineering, Page 82


Bangalore

You might also like