Microcontroller Manual

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 91

BANGALORE INSTITUTE OF TECHNOLOGY

K.R.ROAD, V.V.PURA Bangalore-560004

DEPARTMENT OF TELECOMMUNICATION
ENGINEERING

MICROCONTROLLER
LABORATORY MANUAL

SUBJECT CODE: 18ECL47

[2018 SCHEME]

IV SEMESTER

Lab In-Charge: H.O.D


Dr.M. RAJESWARI Dr.S.SHANTHALA
MICROCONTROLLERS LAB-18ECL47 2019-20

VISION OF THE DEPARTMENT

Empower every student to be Creative and Productive in the field of Electronics and
Telecommunication Engineering by imparting excellent Technical Education and inculcating
Human Values.

MISSION OF THE DEPARTMENT

1. To make our Students acquaint with the Global requirements such as Problem Solving
Skills, Cultural Sensitivity, Ethical behavior and Social Responsibility.

2. To motivate our Students to pursue Higher Education and engage in continuous


upgradation of their Professional Skills.

3. To encourage students to develop Communication Skills, Professional Values and Positive


Attitude that in turn leads to fostering Leadership Qualities.

PROGRAMME EDUCATIONAL OBJECTIVES (PEOs)

PEO1: To prepare our Graduates with strong foundations in Mathematical, Scientific and
Problem Solving Skills.
PEO2: To prepare our Graduates to adapt to fast Technical changes in the domain area of
core Electronics and Telecommunication Engineering by interacting and working closely
with other Premier Technological Institutions / Industries having similar Vision.
PEO3: To enable our Graduates to develop the Knowledge and Skills across the range of
disciplines.
PEO4: To prepare our Graduates to meet the Global challenges by inculcating traits such as
Communication Skills, Ethical Values and ability to work in a team.

PROGRAMME SPECIFIC OUTCOMES (PSOs)

PSO1: Specify, design, build and test analog and digital systems for signal processing
including multimedia applications, using suitable components or simulation tools.

PSO2: Understand and architect wired and wireless analog and digital communication
Systems including networking protocols and determine their performance.

Dept of Telecommunication Engineering, B.I.T 2


PROGRAM OUTCOMES (POs)

Graduates will be able to:

1. Apply the knowledge of mathematics, science, engineering fundamentals, and an


engineering specialization to the solution of complex engineering problems.
2. Identify, Formulate, Review Research literature, and analyze complex engineering problems
reaching substantiated conclusions using first principles of mathematics, natural sciences.
3. Design solutions for complex engineering problems and design system components or
processes that meet the specified needs with appropriate consideration for the public health
and safety, and the cultural, societal, and environmental considerations.
4. Use research-based knowledge and research methods including design of experiments,
analysis and interpretation of data, and synthesis of the information to provide valid
conclusions.
5. Create, select, and apply appropriate techniques, resources, and modern engineering and IT
tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
6. Apply reasoning informed by the contextual knowledge to assess societal, health, safety,
legal and cultural issues and the consequent responsibilities relevant to the professional
engineering practice.
7. Understand the impact of the professional engineering solutions in societal and
environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.
9. Function effectively as an individual, and as a member or leader in diverse teams, and in
multidisciplinary settings.
10. Communicate effectively on complex engineering activities with the engineering community
and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
11. Demonstrate knowledge and understanding of the engineering and management principles
and apply these to one’s own work, as a member and leader in a team, to manage projects
and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
VTU SYLLABUS
MICROCONTROLLERS LAB
(Common to EC/TC/EE/IT/BM/ML)
Sub Code: 18EC L47 IA Marks: 40
Hrs/ Week: 03 Exam Hours: 03
Total Hrs: 42 Exam Marks: 60
I. PROGRAMMING
1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in
an array.

2. Arithmetic Instructions - Addition/subtraction, multiplication and division,


square, Cube – (16 bits Arithmetic operations – bit addressable).

3. Counters.

4. Boolean & Logical Instructions (Bit manipulations).

5. Conditional CALL & RETURN.

6. Code conversion: BCD – ASCII; ASCII – Decimal; Decimal - ASCII;HEX -


Decimal and Decimal - HEX .

7. Programs to generate delay, Programs using serial port and on-Chip timer
/counter.

II. INTERFACING:
Write C programs to interface 8051 chip to Interfacing modules to develop
single chip solutions.

8. Interface a simple toggle switch to 8051 and write an ALP to generate an interrupt which
switches on an LED (i) continuously as long as switch is on and (ii) only once for a small
time when the switch is turned on.

9. Write a C program to (i) transmit and (ii) to receive a set of characters serially by
interfacing 8051 to a terminal.

10. Write ALPs to generate waveforms using ADC interface.

11. Write ALP to interface an LCD display and to display a message on it.

12. Write ALP to interface a Stepper Motor to 8051 to rotate the motor.

13. Write ALP to interface ADC-0804 and convert an analog input connected to it.
MICROCONTROLLER LAB
Cycles of Experiments

Subject: Microcontroller Lab Course Code: 18ECL47


Semester: IV CIE Marks: 40
Total no. of Lecture Hours: 03 SEE Marks: 60

Cycle –I Software Programming using 8051

1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in an array.
2. Arithmetic Instructions - Addition/subtraction, multiplication and division, square, Cube –
(16 bits Arithmetic operations – bit addressable).
3. Counters.
4. Boolean & Logical Instructions (Bit manipulations).
5. Conditional CALL & RETURN.

Cycle –II Software Programming using 8051

6. Code conversion: BCD – ASCII; ASCII – Decimal; Decimal - ASCII; HEX - Decimal and
Decimal - HEX .
7. Programs to generate delay, Programs using serial port and on-Chip timer /counter.

Cycle –III Interfacing Using 8051

8. Interface a simple toggle switch to 8051 and write an ALP to generate an interrupt which
switches on an LED (i) continuously as long as switch is on and (ii) only once for a
small time when the switch is turned on.
9. Write a C program to (i) transmit and (ii) to receive a set of characters serially by
interfacing 8051 to a terminal.
10. Write ALPs to generate waveforms using ADC interface.
11 Write ALP to interface an LCD display and to display a message on it.
12. Write ALP to interface a Stepper Motor to 8051 to rotate the motor.
13. Write ALP to interface ADC-0804 and convert an analog input connected to it.

BANGALORE INSTITUTE OF TECHNOLOGY


DEPARTMENT OF TELECOMMUNICATION
ENGINEERING
MICROCONTROLLERS LAB -QUESTIONS

1. Write an ALP using 8051to transfer a data block of 16 bytes from memory location
40h to 60h.
2. Write an ALP using 8051to transfer a data block of 16 bytes from external memory
location 0500h to internal memory location 60h.
3. Write an ALPusing 8051to exchange the contents of two data blocks stored at data
memory locations 50h and 60h.
4. Write an ALPusing 8051 to sort an array of 8 numbers stored at data memory
location 50h in increasing / decreasing order (using bubble sort).
5. Write an ALP using8051to determine the largest number in an array of N=10 numbers.
6. Write an ALP using8051 to perform addition of N, 8/16 bit numbers stored from data
memory location 50h ( N can be variable). Store the result from data memory location
60h.
7. Write an ALP using8051 to perform subtraction of N, 8/16 bit numbers stored from
data memory location 50h ( N can be variable). Store the result from data memory
location 60h
8. Write an ALP using 8051 to multiply an 16 bit binary number by an 8 bit
binarynumber stored at data memory location 50h, 51h and 52h and store theresult at
53h, 54h.
9. Write an ALP using 8051 to divide an 16 bit binary number by an 8 bit binary number
stored at data memory location 50h, 51h and 52h and store the result at 53h, 54h.

10. Write a an ALP using 8051 to determine the square of the number stored at memory
50h and store the result at memory location 60h.

11. Write a an ALP using 8051 to determine the cube of the number stored at memory
50h and store the result at memory location 70h.
12. Write an ALP to compare two eight bit numbers NUM1 & NUM2 are stored in
external memory locations 8000h & 8001h respectively.
if NUMI<NUM2, set LSB of data RAM 2F (bit address 78H)
if NUM1>NUM2, set MSB OF 2F(7FH). I
if NUM1 = NUM2-Clear both LSB & MSB of bit addressable memory location 2Fh

13. Write an ALP to compute the following .Two eight bit numbers num1&num2 are
stored in internal data RAM Locations 21h & 22h respectively and X is stored in
location 20h.. If X=0, Then num1 (AND) num2.
If X=1,Then num1 (OR) num2.
If X=2,Then num1 (XOR) num2.
Else result=00
Result is stored in location23h.

14. Write an ALP to compute the following.


if x=0; then LSB of NUM1 (and) LSB of NUM2.
if x=1; then MSB of NUM1 (or)MSB of NUM2.
if x=2; then complement MSB of NUM1.
15. Write an ALP using 8051to output the binary (up/ down) counting sequence on port1
and observe the output on logic analyzer window and live watch.
Excellent Good Satisfactory Unsatisfactory
4<E<5 3<G≤4 2<S≤3 1<U≤2
16. Write an ALP using 8051 to output the decimal (up/ down)counting sequence on port
1 and observe the output on logic analyzer window and live watch.
17. Write an ALP using 8051to convert BCD to ASCII data.
18. Write an ALP using 8051to convert ASCII to decimal data.
19. Write an ALP using 8051 to convert Hexadecimal to ASCII data.
20. Write an ALP using 8051to convert ASCII to Hexadecimal data.
21. Write an ALP using 8051 to convert Decimal to Hexadecimal data.
22. Write an ALP using 8051 to convert Hexadecimal to decimal data.
23. Write an ALP to generate a 1 second delay in software.
24. Write an ALP using 8051 to display “TELECOM” on serial window using serial port
and Timer at 9600 baud rate.
25. Write an ALP to generate a 1 second delay continuously using the on chip timer.
26. Write an ALP using 8051 to Interface a simple toggle switch to generate an interrupt
Which switches on an LED (i) continuously as long as switch is on and (ii) only once
for a small time when the switch is turned on.

27. Write a C program to (i) Transmit & (ii) To receive a set characters serially by
interfacing 8051 to a terminal.
28. Write an 8051ALP to generate following waveforms of 5V amplitude using DAC (on
Port0/Port1).(i) Ramp up/down. (ii) Triangular (iii) Square wave of frequency 400
hz& amplitude 2.5V iv) Sine
29. Write ALP to interface an LCD display and to display a message on it.
30. Write ALP to interface a Stepper Motor to 8051 to rotate the motor.
31. Write ALP to interface ADC-0804 and convert an analog input connected to it.

RUBRICS FOR LABORATORY WORK


A: Laboratory work evaluation in two levels

1. Lab performance evaluation.


2. Lab record evaluation.
1. Lab performance evaluation.

Pre- Observation
Discipline and
Rubrics preparation and Participatio Communication
ethics
conduction n

Indicators A1 B1 C1 D1 E1

Relevance
in 20 % 20 % 20 % 20 % 20 %
evaluation

2. Lab record evaluation.

Experimental
Design
Record /Program Data analysis Technical
Rubrics Variables
submission (Procedures &Interpretation Conclusion
&
Materials)

Indicators A2 B2 C2 D2 E2

Relevance in 20 %
15 % 15 % 25 % 25 %
evaluation

B: Performance / category grades:

C: Final evaluation:
Final Marks is in considering 40 % of grading in performance evaluations &
60 % of grading in record evaluations

1. LAB PERFORMANCE EVALUATION RUBRIC DETAILS

Sl No performance Excellent Good Satisfactory Unsatisfactory Relevance


in
Evaluatio
n
Demonstrate Moderate Small effort No pre-
knowledge of understanding preparation
A Pre- experiment and of basic 20 %
1 preparation related concepts
concepts. related to expt.
Neat and Complete Incomplete No complete
complete observation observation observation
Observation observation. with Moderate and and
B1 and Stepwise knowledge some effort no 20 %
conduction approach about the to execution.
to conduct the conduction. conduct the
experiment. experiment.
Used time Used time Did the lab Participation
well in lab pretty well. but did not was minimal
and focused Stayed appear OR student
attention on focused on the very was
the experiment interested. hostile about
experiment. most of the Sometimes participating.
Participatio Routinely time. provides Rarely
C1 provides Usually provides 20 %
n useful ideas
useful ideas provides when useful ideas
when useful participating when
participating ideas when in the group participating
in the participating and in in
group. in the group classroom the group
discussion.
Entering the Entering the Coming bit Coming late
lab on time. lab on time. late to lab. to
Following the Adequately Following the lab. Not
instructions in following the the following
the lab and instructions in instructions the
maintaining the lab and but not instruction
the discipline. maintaining maintaining as
Outstanding the discipline. the well as
Discipline job on Good job on discipline. discipline.
D1 cleaning up cleaning up Had to be Had 20 %
and ethics
working working area reminded to to be asked
area, and and clean up area by
equipment. equipment. and teacher to
Returned all Returned all equipment; clean
components components and to return up and
appropriately appropriately. components. return
and components.
responsibly.
Communicate Communicate Less effort Unable to
s all s thoughts to communicat
thoughts moderately communicat e
clearly with with group e ideas even
E1 Communication
every member members. ideas with with
20 %
of group group
group and members. members.
teachers.
2. LAB RECORD EVALUATION RUBRIC DETAILS

Sl Relevanc
No e
performance Excellent Good Satisfactory Unsatisfactory
In
Evalu
ation
Submission Submission Submission Submitting
of of of record the
A Record completed completed incomplete 15 %
2 submission record on recordbut record.
date not on date.
Clear step- Step-by- Step-by- Description
by-step step step lacks
description description description more than
of that that two key
experimenta misses not misses not details; no
l more more than mention
Experimenta procedures; than one two key of
lDesign labeled key detail; details; apparatuses/
B 20 %
/program diagrams/dr diagrams/dr apparatuses/ devices
2 (Procedures awings of awings devices used to
&content) any included mentioned carry out
apparatuses/ but not but not the
devices used labeled shown.
to carry out
the
experiment.
All All Most Variables
variables variables variables are not
and are are described
control are clearly clearly OR the
C Variables clearly described described majority 15 %
2 described with most with most lack
with all relevant relevant sufficient
relevant details. details. detail.
details.
Professiona Accurate Accurate Data are not
l looking representati representati shown
and on of the on of the OR are
accurate data in data in inaccurate.
representati tables written
Data on of the and/or form,
D analysis data in graphs. but no
tables Graphs and graphs or 25 %
2 &Interpret
ation and/or tables are tables is
graphs. labeled presented.
Graphs and and titled.
tables are
labeled
and titled.
Student Student Student Student
E Technical displays an displays a displayed clearly has
excellent an no idea why 25%
2 Conclusion
the
understandi good understandi experiment
ng of the understandi ng of the was even
importance ng purpose for performed.
of the of the performing No
experiment, importance the conclusions
addresses of experiment. on any
all major the Conclusions major
points and experiment, were points are
objectives addresses drawn on made and
well, and all major most of the there is no
supports all points and major tie to the
conclusions objectives points but objectives
. In well, and could have of the lab
addition, supports all been and/or the
student conclusions stated material
makes good . and/or covered in
suggestions supported lecture. The
for better conclusion
improving is a series
the of rambling,
experiment irrelevant
and/or statements
ties the or simply a
experiment re-
al statement of
results to a the
practical procedure.
application
that was
not
presented
in
lecture.
Introduction to 8051

MICROCONTROLLER DEVELOPMENT TOOLS:


To develop an assembly language program we need certain program development
tools. An assembly language program consists of Mnemonics which are nothing but short
abbreviated English instructions given to the controller. The various development tools
required for Microcontroller programming are explained below.
1. Editor : An Editor is a program which allows us to create a file containing the assembly
language statements for the program.. As we type the program the editor stores the ACSII
codes for the letters and numbers in successive RAM locations. If any typing mistake is done
editor will alert us to correct it. If we leave out a program statement an editor will let you
move everything down and insert a line. After typing all the program we have to save the
program . This we call it as source file. The next step is to process the source file with an
assembler.
Ex: Sample. asm
2.Assembler : An Assembler is used to translate the assembly language mnemonics into
machine language( i.e binary codes). When you run the assembler it reads the source file of
your program from where you have saved it. The assembler generates a file with the
extension
.hex. This file consists of hexadecimal values encoding a sequence of data and their starting
offset or absolute address.
3.Compiler : A compiler is a program which converts the high level language program like
“C” into binary or machine code. Using high level languages it is easy to manage complex
data structures which are often required for data manipulation. Because of its ease , flexibility
and debug options now a days the compilers have become very popular in the market.
Compilers like Keil ,Ride and IAR workbench are very popular.
3. Debugger/Simulator : A debugger is a program which allows execute the program, and
troubleshoot or debug it. The debugger allows to look into the contents of registers and
memory locations after the program runs. We can also change the contents of registers and
memory locations and rerun the program. Some debuggers allows to stop the program after
each instruction so that you can check or alter memory and register contents. This is called
single step debug. A debugger also allows to set a breakpoint at any point in the program. If
we insert a break point , the debugger will run the program up to the instruction where the
breakpoint is put and then stop the execution.
A simulator is a software program which virtually executes the instructions similar to a
microcontroller and shows the results. This will help in evaluating the results without
committing any errors. By doing so we can detect the possible logic errors

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

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 & Compare, SPI (Serial Peripheral Interface) on Port1, Full
duplex enhanced UART.
INTERRUPTS
Nine sources of interrupt (both external and internal).
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 and compiling 8051 assembly programs using Keil
μ Vision3

1. Double Click on the  Vision3 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, Environment and Books->add files->
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 opens the Vision2 Options
for Target – Target configuration dialog. Set the Xtal 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 -> COM Port settings select the COM
Port 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.  Vision translates all the user application
and links. 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, goto Debug mode

8974 . 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 8000h
location type in X:08000H.
1a. Write an assembly language program to transfer n =5 bytes of data from location
40h to location 30h (without overlap).

org 0h

Start1:

mov r0,#40h ;r0 pointed to internal RAM 40h

mov r1,#30h ;r1 pointing to internal RAM 030h

mov r2,#5 ;r2 loaded with no. of elements in the array

Start:

mov a,@r0 ;data transfer

mov @r1,a;

inc r0;increment r2 register

inc r1;increment the register r1

djnz r2,start ;decrement r2,if not equal to 0,continue with data transfer process.

Sjmp Start1 ;short jump to the loc where the label start1 is pointing to

End; end of the program

RESULT:
Before Execution: 5 locations C:40h are filled up with data.

After Execution: 5 locations d:30h are filled up with data from 40h.
Algorithm
1. Initialize registers to hold count data & also the source & destination addresses.
2. Get data from source location into accumulator and transfer to the destination location.
3. Decrement the count register and repeat step 2 till count is zero.
Note: For data transfer with overlap start transferring data from the last location of source
array to the last location of the destination array.

Flow Chart

1b. Write an ALP block data transfer from external RAM to internal RAM enter the
elements from location 0500h (External RAM)

org 0h

start1: mov dptr,#0500h //data pointer pointed to external memory 0500h

mov r1,#30h //r1 pointing to internal RAM 030h

mov r2,#5 //r2 loaded with no. of elements in the array

Start:

movx A,@dptr ;the contents of the memory pointed by dptr is moved to acc

mov @r1,a; data present in acc is moved to the loc pointed by R1

inc dptr;increment the data pointer

inc r1

djnz r2,start ;decrement and jump if R2 is not equal to zero

Sjmp Start1 ;short jump

End;end of the program

Before Execution: 5 locations X:500h are filled up with data.

After Execution: 5 locations d:30h are filled up with data from X:500h.
2. Write an assembly language program to exchange n = 5 bytes of data at location
0030h and at location 0040h.

org 0000h

mov r0,#30h ;r0 pointing to 030h(int. RAM)

mov r1,#40h ;r1 pointing to 040h(int. RAM)

mov r2,#0ah ;r2 loaded with no. of elements to be exchanged

start: mov a,@r0 ;data @r0 is stored in temporary reg r3

mov r3,a

mov a,@r1 ;data @r1 is moved to @r0

mov @r0,a

mov a,r3 ;data from r3 is moved to @r1

mov @r1,a

inc r0 ;increment the registers

inc r1

djnz r2,start ;decrement counter r2,repeat the process if r2 is not zero

nop; no operation

here: sjmp here

end

RESULT:
Before Execution: 5 locations at d:30h & d: 40h are filled up with data.

After Execution: The data atd:30h & d:40h are exchanged.


Algorithm
1. Initialize registers to hold count data (array size) & also the source & destination
addresses.
2. Get data from source location into accumulator and save in a register.
3. Get data from the destination location into accumulator.
4. Exchange the data at the two memory locations.
5. Decrement the count register and repeat from step 2 to 4 till count is zero.
3. Write an ALP for sorting the elements in ascending/Descending order in a given
array in the internal RAM
org 0000h

num equ 040h; Directive Equate is used for assigning the name to the constant

back1: mov r0,#50h ;store n elements(say n=5) from 50h

mov a,r0 ;r0 and r1 are used as pointers

mov r1,a

mov r3,#04h ;load (n-1) to r3 (no. of passes)

mov a,r3

mov r2,a ;load r3 to r2(no. of comparison in each pass)

back: mov a,@r0 ;compare no. pointed to by r0 with no. pointed to by r1

inc r1

mov num,@r1; move the incremented data or say next data to the loc

cjne a,num,loop; compare and jump if the values are not equal

sjmp next

loop: jc next ;if num at r0<no. at r1 continue with comparison process

//jnc next for descending order

mov r4,a ;else exchange the two numbers

mov a,@r1

mov @r0,a

mov a,r4

mov @r1,a

next: inc r0

djnz r2,back ;decrement no. of comparison

djnz r3,back1 ;decrement no. of passes

end

Algorithm
1. Store the elements of the array from the address 50h
2. Initialize a pass counter with array size-1 count (for number of passes).
3. Load compare counter with pass counter contents & initialize to point to the start
address of the array (here 50h).
4. Store the current and the next array elements in registers B and r2 respectively.
5. Subtract the next element from the current element.
6. If the carry flag is set (for ascending order) then exchange the 2 numbers in the array.
7. Decrement the compare counter and repeat through step 4 until the counter becomes 0.
8. Decrement the pass counter and repeat through step 3 until the counter becomes 0.
MICROCONTROLLERS LAB-18ECL47 2019-20

Dept of Telecommunication Engineering, B.I.T 27


MICROCONTROLLERS LAB-18ECL47 2019-20

RESULT:
Before Execution: Unsorted Array at 50h

After Execution: Sorted Array (Ascending order) at 50h

4. Write an ALP to find the largest element in an array stored in the internal //RAM.

org 0000h

lar equ 40h ;location lar stores the largest element in the array

start1:

mov r0,#50h ;array location is 50h pointed to by r0

mov r2,#4h ;no. of elements in the array

mov lar,@r0 ;no. pointed to by r0 loaded to lar

start: inc r0

mov a,@r0 ;no. pointed to by r0+1 loaded to acc.

cjne a,lar,big ;compare the two no.s

sjmp next

big: jc next ;jump to next if a<lar

mov lar,@r0 ;else acc loaded to lar

Dept of Telecommunication Engineering, B.I.T 28


next: djnz r2,start ;decrement count

here: sjmp here

end

Algorithm
1. Store the elements of the array from the address 50h
2. Store the first number in r0
3. Store the length of the array in r2 and set it as counter.
4. Store the first number of the array in lar (memory assigned to hold the largest number).
5. Increment r0.
6. Move the number pointed by r0 to accumulator
7. compare whether the next array element is larger than the one in lar).
8. If the element pointed is larger then load the larger number into r1.
9. Decrement the counter and repeat steps through 5 until the counter becomes 0.
10. Store the largest number in r1 in address 40h
5a. Write an Assembly Language Program (ALP) to perform the Addition
of two eight bit numbers

org 0000h

sjmp 30h

org 30h

mov dptr,#8000h

movx a,@dptr

mov b,a

inc dptr

movx a,@dptr

add a,b

inc dptr

movx @dptr,a

mov a,#00h

rlc a

inc dptr

movx @dptr,a

here: sjmp here

end

ALGORITHM:
1. Get data from the external memory by pointing the DPTR to the source and
storing it in the accumulator
2. Move the data into register B, and get one more data into the accumulator by
incrementing DPTR.
3. Now add both data, and store it in the external ram by incrementing DPTR
and pointing towards the destination
4. Clear all bits of accumulator and rotate accumulator left with carry and store the
carry in the required destination
RESULT: (memory window)
Before Execution: After Execution
:
Address: x:8000h Address: x:8000h
X:0x008000: ff 2d 00 00 X:0x008000: ff 2d 2c 01
00 00
X:0x008006: 00 00 00 00 X:0x008006: 00 00 00 00
00 00
X:0x00800C: 00 00 00 00 X:0x00800C: 00 00 00 00
00 00
5b. Write an Assembly Language Program (ALP) to perform the
Subtraction
of two eight bit numbers

org 0000h

sjmp 30h

org 30h
mov dptr,#8000h

movx a,@dptr

mov b,a

inc dptr

movx a,@dptr

clr c

subb a,b

inc dptr

movx @dptr,a

here: sjmp here

end

ALGORITHM:
1. Point the DPTR to the source and get the data from there into the accumulator
2. Move that data in to the register B, get the other byte to be subtracted
3. Now subtract the two bytes and store the result in external memory.

RESULT: (memory window)


Before Execution: After Execution:
Address: x:8000h Address: x:8000h
X:0x008000: ff 2d 00 00 X:0x008000: ff 2d d2 00
00 00
X:0x008006: 00 00 00 00 X:0x008006: 00 00 00 00
00 00
X:0x00800C: 00 00 00 00 X:0x00800C: 00 00 00 00
00 00
5c. Write an Assembly Language Program (ALP) to perform the Multiplication
of two eight bit numbers

org 0000h

sjmp 30h

org 30h

mov dptr,#8000h

movx a,@dptr

mov b,a

inc dptr

movx a,@dptr

mul ab

inc dptr

movx @dptr,a

inc dptr

mov a,b

movx @dptr,a

here: sjmp here

end

ALGORITHM:
1. Get the data from the source using DPTR and store it in the B register
2. Get the other byte of data, store it in the accumulator
3. Multiply both the bytes and store it the external memory
RESULT: (memory window)
Before Execution: After Execution:
Address: x:8000h Address: x:8000h
X:0x008000: 99 99 00 00 X:0x008000: 99 99 71 5B
00 00
X:0x008006: 00 00 00 00 X:0x008006: 00 00 00 00
00 00
X:0x00800C: 00 00 00 00 X:0x00800C: 00 00 00 00
00 00
5d. Write an Assembly Language Program (ALP) to perform the Division
of two eight bit numbers

org 0000h

sjmp 30h

org 30h

mov dptr,#8000h

movx a,@dptr

mov b,a

inc dptr

movx a,@dptr

div ab

inc dptr

movx @dptr,a

inc dptr

mov a,b

movx @dptr,a

here: sjmp here

end

ALGORITHM:
a. Get first byte from source using DPTR and store it in the register B
b. Get the second byte and store it in accumulator
c. Now divide both the bytes and put the result in the external memory locations
RESULT: (memory window)
Before Execution: After Execution:
Address: x:8000h Address: x:8000h
X:0x008000: 99 99 00 00 X:0x008000: 99 99 01 00
00 00
X:0x008006: 00 00 00 00 X:0x008006: 00 00 00 00
00 00
X:0x00800C: 00 00 00 00 X:0x00800C: 00 00 00 00
00 00
6a. Write an ALP to compute the square of the number and store your result at locations
60H, 61H
ORG 0000h
SJMP 30h
ORG 30h

MOV R0,#50H ;move address 50h to R0


MOV R1,#60H ;move address 60h to R1
MOV A,@R0 ;move data from location 50h to accumulator
MOV B,A ;move data from accumulator to B register
MUL AB ;square
MOV R2,A
MOV A,B ;mov the contents of B to A
MOV @R1,A ;store the higher nibble in 60H address location
INC R1 ;increment R1 register to point to 61h location
MOV A,R2
MOV @R1,A ;store the lower nibble in 61H address location
HERE:SJMP HERE ;jump to current location
END

RESULT:

Square
Before Execution: After Execution:

Address: i:50h Address:i:6 0h Address: i:50h Address: i:60h


I:0x20: 01 00 00 X:0x008000: ff 00 00 00 I:0x20: 01 00 00 X:0x008000: ff 01 fe 00
I:0x23: 00 00 00 00 I:0x23: 00 00 00 00
I:0x26: 00 00 00 X:0x008006: 00 00 00 00 I:0x26: 00 00 00 X:0x008006: 00 00 00 00
00 00
X:0x00800C: 00 00 00 00 X:0x00800C: 00 00 00 00
00 00
6b.Write an ALP to compute the cube of the number and store your result at locations
60H,61H,62H.

ORG 0000h
SJMP 30h
ORG 30h
MOV R0,#50H ;move address 50h to R0
MOV R1,#60H ;move address 60h to R1
MOV A,@R0 ;move number from location 50h to accumulator
MOV B,A ;move number from accumulator to B register
MUL AB ;square the number
PUSH B ;store the higher nibble of square in stack
MOV B,A ;move lower nibble from accumulator to B register
MOV A,@R0 ;move number from location 50h to accumulator
MUL AB ;multiply number with lower nibble of square

MOV @R1,A ;store partial result in 60H address location


MOV A,B ;mov the contents of B to A
MOV R2,A ;store upper part in R2 register

POP B ;mov the higher nibble of square into B register


MOV A,@R0 ;move number from location 50h to accumulator
MUL AB ;multiply number with higher nibble of square
ADD A,R2 ;add the partial result
INC R1
MOV @R1,A ;store the higher part in 61H address location
MOV A,B
ADDC A,#00H ;add carry to contents of B register
INC R1
MOV @R1,A ; store in
HERE:SJMP HERE ;jump to current location
END

Cube
Before Execution: After Execution:
Address: i:50h Address: i:60h
I:0x20: 00 00 00 X:0x008000: ff 00 00 00 Address: i:50h Address: i:60h
I:0x23: 00 00 00 00 I:0x20: 00 00 X:0x008000: ff ff 02 fd 00
I:0x26: 00 00 00 X:0x008006: 00 00 00 00 00 X:0x008006: 00 00 00 00 00
00 I:0x23: 00 00 X:0x00800C: 00 00 00 00 00
X:0x00800C: 00 00 00 00 00
00 I:0x26: 00 00
00
7). Write an ALP to compare the two eight bit numbers NUM1 & NUM2 are stored in
external memory locations 8000h & 8001h respectively.
IF NUM1<NUM2, set LSB of data RAM 2F (bit address 78H)
if NUM1>NUM2, set MSB OF 2F(7FH). I
if NUM1 = NUM2-Clear both LSB & MSB of bit addressable memory location 2Fh

org 0000h

sjmp 30h

org 30h

mov dptr,#8000h; initializing dptr reg

movx a,@dptr

mov r0,a

inc dptr

movx a,@dptr

clr c; clear carry

subb a,r0; subtract with borrow

jz equal; jump if acc equal to zero

jnc big; jump to the loc big if there’s no carry

setb 78h; set lsb of data ram

sjmp end1

big:setb 7fh;set msb of bit addressable ram

sjmp end1

equal:clr 78h;clear both lsb and msb of bit addressable ram

clr 7fh

end1:sjmp end1

end

Algorithm:
1. Store the elements of the array from the address 8000h
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).

RESULT: 1. NUM1>NUM2

Before Execution: X:8000h = 0ffh & X:8001 = 00

Address: i:2fh Address: x:8000h


I:0x2F: 00 00 00 X:0x008000: ff 00 00 00
I:0x32: 00 00 00 00
I:0x35: 00 00 00 X:0x008006: 00 00 00 00
00
X:0x00800C: 00 00 00 00
00
After Execution:
Address: i:2Fh Address: x:8000h
I:0x2F: 80 00 00 X:0x008000: ff 00 00 00
I:0x32: 00 00 00 00
I:0x35: 00 00 00 X:0x008006: 00 00 00 00
00
X:0x00800C: 00 00 00 00
00

2. NUM1<NUM2

Before Execution: X:08000h = 00h & X:8001 = 0ffh

Address: i:2Fh Address: x:8000h


I:0x2F: 00 00 00 X:0x008000: 00 FF 00 00
I:0x32: 00 00 00 00
I:0x35: 00 00 00 X:0x008006: 00 00 00 00
00
X:0x00800C: 00 00 00 00
00

After Execution:

Address: i:2Fh Address: x:8000h


I:0x2F: 01 00 00 X:0x008000: 00 FF 00 00
I:0x32: 00 00 00 00
I:0x35: 00 00 00 X:0x008006: 00 00 00 00
00
X:0x00800C: 00 00 00 00
00

3. NUM1=NUM2

Before Execution: X:08000h = 11h & X:8001 = 11h

Address: i:2Fh Address: x:8000h


I:0x2F: 00 00 00 X:0x008000: 11 11 00 00
I:0x32: 00 00 00 00
I:0x35: 00 00 00 X:0x008006: 00 00 00 00
00
X:0x00800C: 00 00 00 00
00
After Execution:
Address: i:2Fh Address: x:8000h
I:0x2F: 00 00 00 X:0x008000: 11 11 00 00
I:0x32: 00 00 00 00
I:0x35: 00 00 00 X:0x008006: 00 00 00 00
00
X:0x00800C: 00 00 00 00
00
8) Write an ALP to compute the following.
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 I 23H LOCATION
Note:3 eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM locations 20h,
21h & 22H respectively.

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; and the data with 22h

sjmp end1
ckor:cjne r1,#01,ckxor

orl a, 22h; perform or logic

sjmp end1

ckxor:cjne r1,#02,other

xrl a, 22h; perform xor logic

sjmp end1

other: clr a

end1: mov 23h,a //store result in 23h loc of bit adrresable mem

here: sjmp here;short jump

end;terminate the prgm

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.
RESULT:

X=0
Before Execution: After Execution:
Address: I:20h Address: I:20h
I:0x20: 00 36 45 I:0x20: 00 36 45
I:0x23: 00 00 00 I:0x23: 04 00 00
I:0x26: 00 00 00 I:0x26: 00 00 00
X=1
Before Execution: After Execution:
Address: I:20h Address: I:20h
I:0x20: 01 36 45 I:0x20: 01 36 45
I:0x23: 00 00 00 I:0x23: 77 00 00
I:0x26: 00 00 00 I:0x26: 00 00 00

X=2
Before Execution: After Execution:
Address: I:20h Address: I:20h
I:0x20: 02 36 45 I:0x20: 02 36 45
I:0x23: 00 00 00 I:0x23: 73 00 00
I:0x26: 00 00 00 I:0x26: 00 00 00

X=3,4…..FF
Before Execution: After Execution:
Address: I:20h Address: I:20h
I:0x20: 08 36 45 I:0x20: 08 36 45
I:0x23: 00 00 00 I:0x23: 00 00 00
I:0x26: 00 00 00 I:0x26: 00 00 00

1) Before Execution: D:020H =00, 21=36, 22 = 45 After


Execution D:023H = 04
2) Before Execution: D:020H =01, 21=36, 22 = 45
After Execution D:023H = 77
3) Before Execution: D:020H =02, 21=36, 22 = 45
After Execution D:023H = 73
4) Before Execution: D:020H =34, 21=36, 22 = 45
After Execution D:023H = 00

9. Write an ALP to compute the following.


if x=0; then LSB of NUM1 (and) LSB of NUM2,
if x=1; then MSB of NUM1 (or)MSB of NUM2 ,
if x=2; then complement MSB of NUM1
store the bit result in res, where res is MSB of 23h location
Note:3 eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM locations
20h, 21h & 22H respectively.

org 00h

sjmp 30h

org 30h

mov r0,20h //r0-x

cjne r0,#0,ck1

mov c,08h //lsb of num1 (21h) - bit address -08


anl c,10h //lsb of num2 (22h) - bit address -10

sjmp last

ck1:cjne r0,#1,ck2

mov c,0fh //msb of num1 (21h) - bit address -0f

anl c,17h //msb of num2 (22h) - bit address -17

sjmp last

ck2:cjne r0,#2,ck3

cpl 0fh; complement the msb of num1

mov c,0fh //msb of num1 (21h) - bit address -0f

sjmp last

ck3:clr c; clear carry

last:mov 1fh,c //res is msb of 23h location -1fh

here:sjmp here

end

RESULT: 20h = 00 => AND OF LSBs=1 (hence 80 in 23h location)

20h = 01 => OR of MSBs = 0 (hence 00 in 23h location)

20h = 01 =>complement of MSB of 21h location. Hence 21h is changed to A1 and 23h
location has 80h
Before Execution After Execution

RESULT: (memory window)


X=0
Before Execution: After Execution:
Address: I:20h Address: I:20h
I:0x20: 00 36 45 I:0x20: 00 36 45
I:0x23: 00 00 00 I:0x23: 00 00 00
I:0x26: 00 00 00 I:0x26: 00 00 00

X=1
Before Execution: After Execution:
Address: I:20h Address: I:20h
I:0x20: 01 36 45 I:0x20: 01 36 45
I:0x23: 00 00 00 I:0x23: 00 00 00
I:0x26: 00 00 00 I:0x26: 00 00 00

X=2
Before Execution: After Execution:
Address: I:20h Address: I:20h
I:0x20: 02 36 45 I:0x20: 02 36 45
I:0x23: 00 00 00 I:0x23: 80 00 00
I:0x26: 00 00 00 I:0x26: 00 00 00

Algorithm:
1. Move the condition X (from 20h location) into R0 register.
2. If X=0; then move LSB bit of 21h to carry flag and ‘AND’ Carry flag with LSB bit
of 22h. Goto step5
3. If X=1; then move MSB bit of 21h to carry flag and ‘OR’ Carry flag with MSB bit
of 22h. Goto step5
4. If X=0; then complement MSB bit of 21h and move it to carry flag. Goto step5
5. Store Carry flag at MSB bit of 23h location.
10. Write an ALP to implement (display) an eight bit up/down binary (hex) counters on
watch window.

Note: to run this program, after selecting DEBUG session in the main menu use

View-> Watch& call Stack window, in the Watches select watch 1(or 2) and

press F2 and enter a (for accumulator A)

org 0h

sjmp 30h

org 0h

mov a,#00
back: acall delay; call the subroutines

inc a //dec a for binary down counter

jnz back

here:sjmp here

delay: mov r1,#0ffh,load the values to generate delay

decr1:mov r2,#0ffh

decr: mov r3,#0ffh

djnz r3,$; decrement and jump if not equal to zero

djnz r2,decr

djnz r1,decr1

ret; return of the prgm

end

RESULT:
Accumulator A is incremented in binary from 00, 01,02…09,0A, 0B,…,0F,10,11,…FF

Algorithm:
1. Move 00 to A register
2. Call the delay subroutine for 1 second, in delay program move FFH to registers r1, r2
and r3, loop and decrement until 0.
3. Increment A register(decremant for down counter)
11. Write an ALP to implement (display) an eight bit up/down BCD counters on watch
window.

org 0h

sjmp 30h

org 30h

mov a,#00

back:acall delay; call the delay subroutine

add a,#99h //add 01 for bcd up counter

da a //for bcd counter

jnz back
here:sjmp here

delay:mov r1,#0ffh; generating the delay

decr1:mov r2,#0ffh

decr:mov r3, #0ffh

djnz r3,$

djnz r2, decr

djnz r1, decr1

ret; return to the main prgm

end

Algorithm:
4. Move 00 to A register
5. Call the delay subroutine for 1 second (in delay program move FFH to registers r1, r2
and r3, loop and decrement until 0).
6. Increment A register(add 99h for down counter)
7. Decimal adjust accumulator for the BCD up/down counter.

RESULT: Accumulator A is incremented in BCD from 00, 01, 02…09, 10, 11,…99.
SERIAL DATA TRANSMISSION

Theory: In serial transmission as opposed to parallel transmission, one bit at a time is

transmitted. In serial asynchronous transmission, the data consists of a Start bit (high),

followed by 8 bits of data to be transmitted and finally the stop bit. The byte character to be

transmitted is written into the SBUF register. It transmits the start bit. The 8-bit character is

transferred one bit at a time. The stop bit is transferred. After the transmission, the TI flag = 1

indicating the completion of transmission. Hence in the subroutine wait until TI is set. Later

clear the TI flag and continue with transmission of the next byte by writing into the SBUF

register. (The program can also be written in interrupt mode). The speed of the serial

transmission is set by the baud rate which is done with the help of timer 1. (Refer Ayala).

Timer1 must be programmed in mode 2 (that is, 8-bit, auto reload).

Baud rate Calculation: Crystal freq/ (12*32) = (11.0592MHz)/(12*32) = 28800.

Serial communication circuitry divides the machine cycle frequency(11.0592MHz)/(12) by

32 before it is being used by the timer to set the baud rate.

To get 9600, 28800/3 is obtained by loading timer1 with -3 (i.e., FF – 3 = FD) for further

clock division. For 2400 baud rate, 28800/12 => -12 = F4 in TH1.

12. Conduct an experiment to configure 8051 microcontroller to transmit


characters (hi) to a PC using the serial port and display on the serial window.

Note-to use result of this program, after selecting DEBUG session in the main menu use
View-> serial window #1. On running & halting the program, the data is seen in the serial
window.

org 0h

sjmp 30h

org 30h

mov tmod,#20h //timer 1; mode 2

mov th1,#-3 //-3=fd loaded into th1 for 9600 baud, 11.0592mhz.
mov scon,#50h //8-bit, 1 stop bit, ren enabled

setb tr1 //start timer 1

again:mov a,#’y’ //transfer “y”

acall trans

mov a,#’e’ //transfer “e”

acall trans

again1:sjmp again1

trans: mov sbuf,a //load sbuf

here: jnb ti,here //wait for last bit to transfer

clr ti //get ready for next byte

ret

end

RESULT: hi is printed on the serial window each time the program is executed.
Algorithm:
1. Initialize timer 1 to operate in mode 2 by loading TMOD register.
2. load TH1 with -3 to obtain 9600 baud.
3. Initialize the asynchronous serial communication transmission (SCON) register.
4. Start timer1 to generate the baud rate clock.
5. Transmit the characters “y” & “E” by writing into the SBUF register and waiting for
the TI flag.
The above program can also be written as shown below for displaying the message
"Telecom"

org 0000h
sjmp 30h
org 30h

mov tmod,#20h

mov th1,#0fah

mov scon,#50h

setb tr1

against:mov dptr,#mes

again:mov a,#00h

movc a,@a+dptr

jz skip

acall trans

inc dptr

sjmp again

trans:mov sbuf,a

here:jnb ti,here

clr ti

ret

skip:sjmp against

mes:db"Telecom" ;0h:message

end
TIMER DELAY
Timerdelay = 12*(257-delay)/frequency
Timerdelay=0.05msec
Delay=256-((timerdelay * frequency)/12) =256-(0.05*10 -3 * 33*106)/12
=256-137.5 =118.5 //loaded in TH0
To get 1sec delay
1/0.05msec = 200*100 in the ISR
(assuming 33 MHZ crystal frequency. For 11 MHz, the calculations change).

13. Write an Alp to generate a 1 second delay continuously using the on chip timer in
interrupt mode.

org 0h //reset vector

sjmp 30h

org 0bh //tf0 vector

sjmp isr

org 30h

mov a,#00

mov r0,#0

mov r1,#0

mov tmod,#02h //00000010-run timer0 in mode 2

mov th0,#118 //set up timer 0 to overflow in 0.05msec


mov ie,#82h //%10000010 – enable timer0 interrupt

setb tcon.4 //start the timer0

here:sjmp here

isr: clr tcon.4 //disable timer0

inc r1 //r1*r2 = 100*200 = 20000 * 0.05msec = 1sec

cjne r1,#100,skip

mov r1,#00

inc r0

cjne r0,#200,skip

mov r0,#00h

inc a

skip: setb tcon.4 //enable timer

reti //return from interrupt subroutine

end

RESULT: Accumulator A is incremented in binary from 00, 01,02…09,0A, 0B, …, 0F, 10,
11, …FF every 1 second (for 33MHz clock setting & every 3 seconds for 11.0598MHz)

Algorithm:
1. Set up timer0 in mode 2 operation
2. Load TH1 with 118 to generate an interrupt every 0.05msec.
3. Reset registers a, r1 & r0.
4. Repeat step 4 continuously
5. On interrupt; ISR at 000B location goes to step 6
6. disable timer0
7. Update r1 & r0
8. Check if 20000 interrupts (=1 sec) over. Yes –increment accumulator a.
9. enable timer & return from ISR.
14. Write an ALP to implement decimal to hex conversion

org 0000h

sjmp 30h

org 30h

mov dptr,#40h //2-digit decimal number to be converted is given in data memory40h


movx a, @dptr

anl a, #0f0h //obtain upper decimal digit

swap a //bring to the units place

mov b,#0ah //multiply tens digit with #0a-toget tens in hex

mul ab

mov r1,a //temporarily store the converted tens value

movx a,@dptr //get the decimal number again

anl a,#0fh //obtain the units digit

add a,r1 //add to the converted tens value

inc dptr //increment data address

movx @dptr,a //converted hexadecimal number in next location

here:sjmp here

end

RESULT: before execution- X:0040H = 45 (Decimal/BCD)


After Execution: X:0041h = 2D (hex value)
15. Write an ALP to implement hex to decimal conversion
org 0000h

sjmp 30h

org 30h

mov dptr,#9000h

movx a,@dptr //get hex number

mov b,#10

div ab //divide by 10 (0ah)

inc dptr

xch a,b

movx @dptr,a //store the remainder (in b) in units place

xch a,b

mov b,#10 //divide the quotient in a by 10

div ab

inc dptr

xch a,b

movx @dptr,a //store the remainder (in b) in tens place

xch a,b

inc dptr

movx @dptr,a //store the quotient (in a) in hundreds place

here:sjmp here

end

RESULT: 9000H – FF (HEX NUMBER)


9001 to 9003 – unpacked BCD number (decimal)- 5,5,2 (i.e., 255 stored Lower digit first)
Algorithm
1. Move the hex data to be converted to accumulator.
2. Move 10 to B register and divide with A reg to convert to ascii value
3. Store the converted LSB value in r7
4. Repeat the step 2 to obtain the converted MSB value
5. Store the same in r6
Flow Chart

16. Write an ALP to implement BCD to ASCII conversion

org 0000h

sjmp 30h

org 30h

mov r1,#50h

mov a,@r1 //get bcd data byte from ram location 50h
mov r2,a //store in r2

anl a,#0fh //get the lower nibble

orl a,#30h //add/or with 30h i.e., 0-9 converted to 30-39h

inc r1

mov @r1,a //store the lower digit's ascii code

mov a,r2 //get back the number

swap a //swap nibbles in a

anl a,#0fh //get the upper bcd digit

orl a,#30h //convert to ascii

inc r1

mov @r1,a //store the upper digit's ascii code

here: sjmp here

end

RESULT: The BCD code 28 at D:0050h is converted to 2 ASCII codes-38h 32h

Algorithm :
//Converts the BCD byte in A into two ASCII characters.
1. Move the BCD data to be converted to accumulator.
2. Get the lower nibble(BCD digit) & ADD (or ORL) with 30h
3. Store the converted ASCII value
4. Get the higher nibble(tens BCD digit) & ADD (or ORL) with 30h
5. Store the converted ASCII value
Flow Chart

17. Write an ALP to implement hexadecimal to ASCII conversion

sjmp 30h

org 30h

mov r1,#50h

mov a,@r1 //get hexadecimal data byte from ram location 50h

mov r2,a //store in r2

anl a,#0fh //get the lower nibble

acall ascii //convert to ascii


inc r1

mov @r1,a //store the lower digit's ascii code

mov a,r2 //get back the number

swap a //swap nibbles in a

anl a,#0fh //get the upper bcd digit

acall ascii

inc r1

mov @r1,a //store the upper digit's ascii code

here: sjmp here

ascii:mov r4,a //store a

clr c

subb a,#0ah //check if digit >=0a

mov a,r4

jnc skip

add a,#07h //add 07 if >09

skip:add a,#30h //else add only 30h for 0-9

ret

end

RESULT: The BCD code 2C at D:0050h is converted to 2 ASCII codes-43h(for 0B) & 32h
(for 02) Another Example-BA

Algorithm :
//Converts the hexadecimal byte in A into two ASCII characters.
1. Move the hexadecimal data to be converted to accumulator.
2. Get the lower nibble & call ASCII routine
3. Store the converted ASCII value
4. Get the higher nibble & call ASCII routine
5. Store the converted ASCII value
ASCII subroutine
1. If digit greater than 09,(for A-F) add 07h & 30h
2. Else (i.e., for 0-9) add only 30h
3. return
Flow Chart

18. Write an ALP to implement ASCII to hexadecimal conversion

org 0000h

sjmp 30h

org 30h

mov r1,#50h

mov a,@r1 //get ascii byte from ram location 50h

clr c
subb a,#41h; subtract 41h from ascii value

mov a,@r1

jc skip

clr c

subb a,#07h; if there’s no carry generated then again subtract 07h

skip:clr c

subb a,#30h

inc r1

mov @r1,a //store the hex code

here: sjmp here

end

RESULT: The ASCII code 45 at D:0050h is converted to hexadecimal -0E at 51h

Note: For this program the input data should be only in the range 30h-39h & 41h to 46h.

Algorithm :
//Converts the ASCII characters into hexadecimal number.
1. Move the ASCII character to be converted to accumulator.
2. If character is greater than 41h,(for A-F), then subtract 07h & 30h
3. Else (i.e., for 0-9) subtract only 30h
4. Store the converted hexadecimal number.
Flow Chart

8051-Viva Questions
1. What is meant by micro controller?
Microcontroller is a small chip that has in-build Micro processor, memory, ports, timers
and converter. Micro controllers are designed for specific use. For instance, micro
controller in TV remote is mainly designed for controlling TV.

2. List out the features of 8051 micro


controller? 40 Pin IC.
128 bytes of RAM.
4K ROM.
2 Timers (Timer 0 and Timer 1).
32 Input/ Output pins.
1 serial port.
6 Interrupts (Including Reset).

3. What are the types of interrupts in 8051?


External interrupt 0 (IE0) has highest priority among interrupts.
Timer interrupt 0 (TF0)
External interrupt 1 (IE1)
Timer interrupt 1 (TF1) has lowest priority among other interrupts.
Serial port Interrupt
Reset

4. What is an interrupt?
Hardware or software can communicate to micro controller through interrupts. Interrupts
are external signal that controls the micro processor. Interrupt signals are generated by
sources like software programs or hardware controls.

5. What is an Interrupt service routine?


When micro controller is under sudden interrupt, it will call ISR (Interrupt service
routine) that will store the address of current memory address and takes the control to
new interrupt memory address. After the interrupt, the control will transfer back to its
previous address.

6.what are the different addressing modes in


8051? Direct addressing
Register addressing
Register indirect addressing
Implicit addressing
Immediate addressing
Index addressing

7. Difference between JUMP and CALL instruction?


JUMP: Takes the control to newly specified address.
CALL: Takes the control to newly specified address but control will resume back to its
current address once its call is finished.

8. Draw the pin diagram of 8051 micro controller?


1) What is the difference between microprocessor and microcontroller?

The microprocessor are general purpose computer with CPU, memory addressing circuit,
interrupt handling circuits and its models vary in data size from 4 to 32 bits.

The microcontrollers are special purpose digital controller with above features of
microprocessor plus it has timer parallel and serial I/O and internal RAM and ROM.

2) Describe the internal Ram of 8051?

The internal RAM of 8051 is generally 128 bytes. It is classified as 4 register banks of 8
registers each. 16 byte addressable at bit level & 8 bytes of general purpose data memory.

3) How many timers are available in 8051?

There are two 16 bit timer/counter option. They are T0 and T1.

4) What are the different control registers in 8051?

The following control register are present in 8051:


TCON, TMOD, SCON, PCON, IP, & IE.

5) How many interrupt sources are made available in 8051?

There are two external & three internal interrupt sources.

6) Which register of PSW are used to select register bank?


The register R50 and R51 are used to select register bank.

7) Which flag is used in arithmetic instruction?

The overflow flag (OV) is used in arithmetic instruction.

8) Which flag is used for BCD arithmetic?

The auxiliary carry flag is used BCD arithmetic.

9) The carry flag is used for which instruction?

The carry flag is used for arithmetic, jump, rotate and Boolean instruction.

10) What is the position of the port during power on or power reset?

The port P0 to P3 will be high during power ON condition.

11) List the different register priority wise?

The register are listed as per the following ranking based on priority to response an
interfacing.
1) IE0 2) TF0 3) IE1 4) TF1 5) SERIAL (T1 OR R1)

12) Name the features of 8051?

The following are the 3 main features of 8051.


128 byte RAM, $k ON-CHIP ROM and 48 bit I/O ports.

13) What is the major difference between 8051 and 8052 microcontroller?

The 8052 has everything that the 8051 has. It has an extra timer and the on-chip ROM is
8k instead. The RAM in 8052 is 256 byte instead of 128 bytes.

14) List different 8-bit register of 8051?

A, B, R0 TO R7, SP.

15) List 16 bit register of 8051?

DPTR, PC.

16) Why is assembly language caused as low level language?

The assembly language is called as low level language as it directly deals with the
internal structure of CPU.

17) What is a compiler?

A compiler is a program that translates high level language into machine code.
18) Which directive is used for ASCII string?

The ‘DB’ directive is used for ASCII string.

19) What is the advantage in using the ‘EQU’ directive to define a constant value?

If the value are to be changed later, it can be easily done once in one place instead of
every occurrence.

20) What is the flag register in 8051 called as?

The flag register 8051 is called as program status word i.e., PSW.

21) What is the size of flag register in 8051?

The size of flag register in 8051 is 8 bit.

22) Which bits of the PSW registers are users definable?

D1 and D5 i.e., PSW.1 and PSW.5 are user definable.

23) When is the carry flag (CF) set?

The carry flag is set whenever there is carry out from the d7 bit. This flag is affected after.

24) Is it possible to have immediate data as destination?

No, it is not possible to have immediate data as destination.

25) What should be the number in the register RP?

It must be always the ram address.

26) Which register are used for indirect addressing?

Only register R0 and R1 may be used for indirect addressing.

27) All external data moves must involve which register?

All external data moves must involve the accumulator.

28) How many bytes can register RP address?

Rp can address 256 bytes.

29) How many bytes can DPTR address?

DPTR can address 64k bytes.


30) What happens to the PC before it is added to A to form the final address of the
code byte?

The PC is incremented by 1 to point to the next instruction.

31) What precaution one has to take to handle the subtract operation?

The carry flag is set to 0 if is not to be included as part of the subtraction operation.

32) Which register bank is used if we alter RS0 and RS1 of the PSW by the
following 2 instruction?
SETB psw.3
SETB psw.4

The register bank 3 is used.

33) Name 16 bit register in 8051?

PC and DPTR.

34) What is the result of the following code and where it is kept?
MOV A, #15H
MOV R2, #13H
ADD A, R2

The result is 28H and is stored in accumulator.

35) In the ADD instruction, when CY is raised?

The CY is raised or set whenever there is a carry out from the D7 bit.

36) In the ADD instruction, when is AC flag set?

If there is a carry from D3 to D4 during an ADD or SUB operation, this bit is set,
otherwise it is cleared.

37) Which flag is used to perform BCD arithmetic?

The AC flag is used to perform the BCD arithmetic.

38) In the 8051, which register bank conflict with the stack?

Bank1.

39) In ‘JZ = NEXT’ which register content is checked to see if it zero?

The accumulator.
40) What do the mnemonic LCALL and ACALL stand for?

LCALL is termed as Long Call.


ACALL is termed as Absolute Call.

41) Show the instruction to enable EX0 and Timer0 interrupts?

MOV IE, # 1000011b

42) Which pin of the 8051 is assigned to the external hardware interrupt INT?

Port pin P3.3 which is pin 13 on the 40 pin DIP package is assigned as external hardware
interrupt.

43) What address in the interrupt vector table is assigned t the INT1 and Timer1
interrupts?

0013 for INT1 and 001b for timer1.

44) What address in the interrupt vector table is assigned to timer 0?

0000Bh is assigned as address to timer 0.

45) Which bit of IE belong to the Timer interrupt? Show how both are enabled?

Bits D1 and D3 belong to the timer interrupts. They are enabled by the instruction: MOV IE #
10001010b

46) What address in the interrupt vector table is assigned to INT0 and INT1? What
at=re their pin number?

The address are 0003h and 0013h. pins are 12(p3.2) and 13(p3.3).

47) which bit of ie register belong to the external hardware interrupt? Show how both
are enabled.

Bits d0 and d2 belong to the external hardware interrupt and are enabled by the instruction
MOV IE,#10000101B.

48) Assume that IE bit for external hardware interrupt EX1 is enabled and is active low
explain how these interrupt works when it is activated?

Upon application of a low pulse(min 4 machine cycle wide), to pin p3.3, the 8051. is
interrupted in whatever it is doing and jump to ROM location 00BH to execute the ISR.

49) Explain the role that each of the two bits TCON.0 and TCON.2 play in the execution
of external interrupts 0?
TCON.0 is set to high to make INT0 an edge triggered. If INT 0 is edge triggered(i.e TCON.0
is set ) whenever high to low pulse is applied to the INT0 is latched and kept by the
TCON.0 bit by making TCON.2 high. While the ISR for INT0 is being serviced,TCON.2
stays high. No matter how many times a high to low pulse is applied to pin INT0.
Upon the execution of the last instruction of the ISR, which is RETI the TCON.2 bit is
cleared indicating that the INT0 pin can respond to another interrupt.

50) What address in the interrupt vector table is assign to the serial interrupt?

23H is the address assigned for serial interrupt.

51) How are interrupts better than Polling?

In polling the processor needs to continuously monitor the i/o device whether it is ready or
not. This continuous monitoring of the device may lead to undue waste of time. Whereas the
device may interrupt the processor as and when required ,in the mean time the processor my
carry on with some other piece of work.

52) Explain the working of the enable pin of the LCD

The data written on to the data pins is taken in only when a high to low pulse is given on
this pin or to understand the data written comes into action only when high to low pulse is
given.

53) What is the role of SBUF register during serial communication?

Any data that needs to be transmitted or received is passed through the SBUF register. the
data being transmitted is moved to SBUF and the data received through serial communication
is collected in SBUF.

54) A stepper with a step angle of 5 degrees has how many steps per revolution?
a) 144
b) 72
c) 48
d) 24

Answer is 72.

55) What is the total number of steps needed to rotate one complete rotation?

The total number of steps are 180 steps * 20 = 360

56) What is the purpose of LM336 Zener diode around pot setting Vref/2?

The purpose of LM336 Zener diode around pot setting Vref/2 is to overcome any fluctuation
in power supply.

57) LM34 provides 10mv_ for each degree of _ Fahernheit,_(Fahernheit, Celsius)


temperature.
58) LM35 provides _10mv for each degree of _ Celsius (Fahernheit, Celsius)
temperature.

59) A stepper with a step angle of 7.5 degrees has how many steps per revolution?
a) 144
b) 72
c) 48
d) 24

Answer is 48

60) A stepper with a step angle of 15 degrees has how many steps per revolution?
a) 144
b) 72
c) 48
d) 24

Answer is 24

61) A stepper with a step angle of 2.5 degrees has how many steps per revolution?
a) 144
b) 72
c) 48
d) 24

Answer is 144

62) What will be the order of 4 step sequence of stepper motor if we start with 0110?
a) 1,2,4
b) 4,1,2
c) 1,4,2
d) 2,1,4

Answer is 4,1,2.

63) What will be the order of 4 step sequence of stepper motor if we start with 0011?
a) 1,2,4
b) 4,1,2
c) 1,4,2
d) 2,1,4
Answer is 1,2,4.

64) What will be the order of 4 step sequence of stepper motor if we start with 1100?
a) 1,2,4
b) 4,1,2
c) 1,4,2
d) 2,1,4

Answer is 2,1,4.
65) What is the direction (out, in) for each of following pins of ADC 808/809.
A) A,B,C B) SC C)EOC.

The direction for A,B,C is in, for SC is also in and for EOC is out .

66) In ADC804, the INTR signal is


a) Input Signal b) Output signal

It is output signal

67) Which pin of ADC804 indicates end of conversion?


a) INTR b) CS c) RD d) Vcc.

The pin of ADC804 which indicates end of conversion is INTR.

68) Which pin of ADC804 indicates start of conversion?


a) INTR b) CS c) RD d) Vcc.

The pin of ADC804 which indicates start of conversion is RD

69) Both the ADC 804 and ADC 808 are 8 bit converter

70) For an ADC804 chips, find the step size of Vref values
a) Vref/2 = 1.28 Volt
b) Vref/2 = 1 Volt

The step size for 1.28 Volt is Vref = 2.56/ 256 = 10


The step size for 1Volt is Vref = 2/ 256 = 7.81

71) Is self clocking present in ADC 808/809?


No

72) On what factor speed of conversion depends?

Speed of conversion depends on frequency of clock connected to the clock pin.

73) What is the relation between digital output voltage and analog input voltage and
step size?

Dout = Vin/ step-size.

74) DAC 808 is an 8 bit D to A Converter

75) To get the full scale output what should be the inputs for DAC?

All inputs are high.

76) Assume that R=5k and Iref= 2mA, calculate Vout for 10011001?
Iout = 2 mA * 153/ 255 = 1.195 mA.

Vout = 1.195 * 5 = 5.975

77) Assume that R=5k and Iref= 2mA, calculate Vout for 11001000?

Iout = 2 mA * 200/ 256 = 1.562 mA

Vout = 1.562 * 5 = 7.8125.

78) What will be the value of column of keypad matrix if no key is pressed?

The value of column of keypad matrix if no key is pressed is all 1’s

79) To detect the key press which of following is grounded?


A) All rows B) One row at a time C) both (A) and (B)

Answer is A

80) To identify the key press which of following is grounded?


A) All rows B) One row at a time C) both (A) and (B)

Answer is B

1. Intel 8051 follows which architecture: Harvard or Newman?


2. What is the difference between Harvard and Newman architecture?
3. 8051 was developed using which technology nmos or pmos?
4. Why 8051 is called 8 bit microcontroller?
5. What is the width of data bus?
6. What is the width of address bus?
7. How many memory locations address bus can access (also tell in kb)?
8. The memory locations that the address bus can access will be of RAM or ROM or
both?
9. How much on-chip RAM is available?
10. On-chip RAM is also called memory?
11. How much on-chip ROM is available?
12. On-chip ROM is also called memory?
13. How many byte is of the bidirectional input/output port?
14. What is UART?
15. The original 8051 core runs at how many clock cycles per machine cycle?
16. With 12 MHz clock frequency how many instructions(of 1 machine cycle and 2
machine cycle) it can execute per second?
17. What are the four distinct types of memory in 8051?
18. Internal RAM is located from address 0x00 to ?
19. Tell the addresses which can be accessed directly?
20. Tell the addresses which are bit addressable?
21. Tell the addresses which can be accesssed only by indirect addressing mode?
22. Tell the addresses where the special function registers are present and also which
addressing mode is used to access them?
23. Are SFR bit addressable or byte addressable?
24. What type of memory is Program Memory? Read only/Read-write/write only?
25. *Does the program memory can be only used for storing code only, if not then how
that data is accessed?
26. What is the address of external data memory from which it starts?
27. How the external data memory is accessed?
28. The internal RAM memory of the 8051 is: 128 bytes
29. This program code will be executed continuously:
STAT: MOV A, #01H
JNZ STAT

30. The 8051 has 2 16-bit counter/timers


31. The address space of the 8051 is divided into four distinct areas: internal
data, external data, internal code, and external code. True
32. Data transfer from I/O to external data memory can only be done with the MOVX
command. True
33. The 8051 can handle 5 interrupt sources.
34. The special function registers are maintained in the next 128 locations after
the general-purpose data storage and stack. True
35. This statement will set the address of the bit to 1 (8051 Micro-controller):
SETB 01H
36. MOV A, @ R1 will: copy the contents of memory whose address is in R1 to
the accumulator
37. A label is used to name a single line of code. True
38. The following program will receive data from port 1, determine whether bit 2 is high,
and then send the number FFH to port 3: True
READ: MOV A,P1
ANL A,#2H
CJNE A,#02H,READ
MOV P3,#FFH
39. Device pins XTAL1 and XTAL2 for the 8051 are used for connections to an external
oscillator or crystal. True
40. When the 8051 is reset and the line is HIGH, the program counter points to the
first program instruction in the: internal code memory
41. An alternate function of port pin P3.4 in the 8051 is: Timer 0 Both registers TL0 and
TL1 are needed to start Timer 0. False
42. The I/O ports that are used as address and data for external memory are: ports 0 and 2
43. The last 96 locations in the internal data memory are reserved for general-purpose
data storage and stack. False
44. Microcontrollers often have: CPUs, RAM, ROM
45. The 8051 has 4 parallel I/O ports.
46. The total external data memory that can be interfaced to the 8051 is: 64K
47. Which instruction will load the value 35H into the high byte of timer 0? MOV TH0,
#35H
48. Bit-addressable memory locations are: 20H through 2FH
49. The 8-bit address bus allows access to an address range of: 00 to FFH
50. The contents of the accumulator after this operation
MOV A,#0BH
ANL A,#2CH
will be 00001000
51. The start-conversion on the ADC0804 is done by using the:
52. Which of the following instructions will move the contents of register 3 to the
accumulator? MOV A, R3
53. Which of the following statements will add the accumulator and register 3? ADD A,
R3
54. Data transfer from I/O to external data memory can only be done with the MOV
command. False
55. Which of the following commands will move the number 27H into the
accumulator? MOV A, #27H
56. This program code will read data from port 0 and write it to port 2, and it will
stop looping when bit 3 of port 2 is set: True

STAT: MOV A, PO
MOV P2,A
JNB P2.3, STAT

57. Which of the following commands will move the value at port 3 to register 2? MOV
R2, P3
58. The number of data registers is: 32
59. When the 8051 is reset and the EA line is LOW, the program counter points to the
first program instruction in the: external code memory
60. The designs of a centigrade thermometer and a PWM speed-control circuit can be
implemented by the 8051. True
61. What is the difference between the 8031 and the 8051? The 8031 is ROM-less.
62. The I/O port that does not have a dual-purpose role is: port 1
63. To interface external EPROM memory for applications, it is necessary to
demultiplex the address/data lines of the 8051.
64. The following command will copy the accumulator to the location whose address is
23H:
MOV 23H,A True
65. The special function registers can be referred to by their hex addresses or by their
register names. True
66. The contents of the accumulator after this operation :
MOV A,#2BH
ORL A,00H
will be: 2B H
67. The following program will cause the 8051 to be stuck in a loop: False
LOOP: MOV A, #00H
JNZ LOP
68. Which of the following commands will copy the contents of RAM whose address is in
register 0 to port 1? MOV P1, @ R0
69. The statement CALL READ passes control to the line labeled READ. True
70. Which of the following commands will copy the contents of location 4H to the
accumulator? MOV A, 04H
71. The microcontroller is useful in systems that have nonvariable programs for
dedicated applications. True
72. The total amount of external code memory that can be interfaced to the 8051 is: 64K
73. The ADC0804 has 8-bit resolution
74. A HIGH on which pin resets the 8051 microcontroller? RST
75. An alternate function of port pin P3.1 in the 8051 is: serial port output
76. Which of the following instructions will move the contents of the accumulator to
register 6? MOV R6, A
77. The following command will rotate the 8 bits of the accumulator one position to
the left: RL A: True
78. An alternate function of port pin P3.0 (RXD) in the 8051 is: serial port input

You might also like