0% found this document useful (0 votes)
266 views77 pages

B M S Institute of Technology Yelahanka, Bangalore-64: Laboratory Manual

This document outlines the syllabus for the Microcontrollers Lab course. It includes topics on 8051 and MSP430 assembly programming as well as interfacing programs using the 8051 microcontroller. The assembly programs cover data transfer, arithmetic, counting, logical operations, code conversion, and serial communication. The interfacing programs involve interfacing modules such as DAC, motors, LCD, ADC, temperature sensors, and calculators to the 8051. The course aims to develop proficiency in microcontroller programming and interfacing through hands-on experience.

Uploaded by

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

B M S Institute of Technology Yelahanka, Bangalore-64: Laboratory Manual

This document outlines the syllabus for the Microcontrollers Lab course. It includes topics on 8051 and MSP430 assembly programming as well as interfacing programs using the 8051 microcontroller. The assembly programs cover data transfer, arithmetic, counting, logical operations, code conversion, and serial communication. The interfacing programs involve interfacing modules such as DAC, motors, LCD, ADC, temperature sensors, and calculators to the 8051. The course aims to develop proficiency in microcontroller programming and interfacing through hands-on experience.

Uploaded by

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

DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

B M S Institute of Technology
Yelahanka, Bangalore-64

Department of Electrical & Electronics Engineering

IV SEMESTER

10ESL47 –MICROCONTROLLER LAB

8051 and MSP 430 Microcontrollers

LABORATORY MANUAL

NAME OF THE STUDENT :

BRANCH :

UNIVERSITY SEAT NO. :

SEMESTER & SECTION :

BATCH :

MICROCONTROLLERS LAB (10ESL47) 1 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

MICROCONTROLLERS LAB (10ESL47) 2 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

Vision of the Department

To emerge as one of the finest Electrical & Electronics Engineering


Departments facilitating the development of competent professionals,
contributing to the betterment of society

Mission of the Department

Create a motivating environment for learning Electrical Sciences


through teaching, research, effective use of state of the art facilities and
outreach activities.

Graduates of the program will,


Have successful professional careers in Electrical Sciences, and Information Technology
PEO1
enabled areas and be able to pursue higher education.

PEO2 Demonstrate ability to work in multidisciplinary teams and engage in lifelong learning.

PEO3 Exhibit concern for environment and sustainable development.

After the successful completion of the course, the graduate will be able to,

PO1: Apply the knowledge of mathematics, science, engineering fundamentals,


Engineering knowledge and an engineering specialization to the solution of complex engineering
problems.
PO2: Identify, formulate, review research literature, and analyze complex
Problem analysis engineering problems reaching substantiated conclusions using first
principles of mathematics, natural sciences, and engineering sciences.
PO3: Design solutions for complex engineering problems and design system
Design/development of components or processes that meet the specified needs with appropriate
solutions consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
PO4: Use research-based knowledge and research methods including design of
Conduct investigations experiments, analysis and interpretation of data, and synthesis of the
of complex problems information to provide valid conclusions.
PO5: Create, select, and apply appropriate techniques, resources, and modern
Modern tool usage engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.
PO6: Apply reasoning informed by the contextual knowledge to assess societal,
The engineer and health, safety, legal and cultural issues and the consequent responsibilities
society relevant to the professional engineering practice.
PO7: Understand the impact of the professional engineering solutions in societal
Environment and and environmental contexts, and demonstrate the knowledge of, and need
sustainability for sustainable development.
PO8: Ethics Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.
PO9: Function effectively as an individual, and as a member or leader in diverse
Individual and team

MICROCONTROLLERS LAB (10ESL47) 3 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

work teams, and in multidisciplinary settings.


PO10: Communicate effectively on complex engineering activities with the
Communication 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.
PO11: Demonstrate knowledge and understanding of the engineering and
Project management management principles and apply these to one’s own work, as a member
and finance and leader in a team, to manage projects and in multidisciplinary
environments.
PO12: Recognize the need for, and have the preparation and ability to engage in
Life-long learning independent and life-long learning in the broadest context of technological
change.

Program Specific Outcomes:

The Graduates of the Program will be able to

PSO1: Analyze and design electrical power systems.


PSO2: Analyze and design electrical machines.
PSO3: Analyze and design power electronic controllers for industrial drives.
PSO4: Analyze and design analog and digital electronic systems.

CO-PO MAPPING

CO/PO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 3  3 3  3  3         3      
10ESL47 CO2 3  3 3  3  3         3      
CO3 3  3 3  3  3         3      

Course Outcomes
At the end of the course, students will be able to,
CO1 Write assembly programs and run simulation to carryout data transfer, arithmetic, logical
and code conversion programs for 8051 microcontroller
CO2 Write C-programs and interface various modules to 8051 microcontroller and conduct
experiments.
CO3 Write assembly programs and run simulation to carryout data transfer, arithmetic, logical
programs forMSP430 microcontroller

MICROCONTROLLERS LAB (10ESL47) 4 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

CONTENTS

PAGE
Sl.
Content NO.
No.
Introduction and microcontroller basics 7

PART A: 8051 ASSEMBLY PROGRAMS


1 DATA TRANSFER INSTRUCTIONS. 16
II ARITHMETIC INSRUCTIONS 23
COUNTERS
III 27
IV BOOLEAN AND LOGICAL INSTRUCTIONS (BIT MANIPULATIONS) 30
V CODE CONVERSION 36

VI SERIAL DATA TRANSMISSION 42

PART-B: INTERFACING PROGRAMS


I DAC INTERFACE TO GENERATE WAVEFORMS 46
II STEPPER MOTOR AND DC MOTOR INTERFACE 52
III LCD DISPLAY AND 4X4 HEX KEYBOARD INTERFACE 55
IV ADC AND TEMPERATURE SENSOR 58
V ELEVATOR INTERFACE 60
VI CALCULATOR USING KEYBOARD AND 7-SEGMENT DISPLAY 62
SCHEMATICS OF INTERFACING CARDS 67
PART –C MSP 430 ASSEMBLY PROGRAMS 71
*QUESTION BANK 75
*VIVA QUESTIONS 77

MICROCONTROLLERS LAB (10ESL47) 5 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

VTU SYLLABUS

10ESL47 MICROCONTROLLERS LAB (Common to EC/TC/EE/IT/BM/ML)

Subject Code : 10EEL47 IA Marks : 25


No. of Practical Hrs./ Week : 03 Exam Hours : 03
Total No. of Practical Hrs. : 42 Exam Marks : 50

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.

Note: Programming exercise is to be done on both 8051 & MSP430.

II. INTERFACING:

Write C programs to interface 8051 chip to Interfacing modules to develop single chip
solutions.
8. Simple Calculator using 6 digit seven segment displays and Hex Keyboard interface to
8051.
9. Alphanumeric LCD panel and Hex keypad input interface to 8051.
10. External ADC and Temperature control interface to 8051.
11. Generate different waveforms Sine, Square, Triangular, Ramp etc. using DAC interface
to 8051; change the frequency and amplitude.

MICROCONTROLLERS LAB (10ESL47) 6 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

Microcontroller Basics

A microcontroller is a computer-on-a-chip, or a single-chip computer. Micro


suggests that the device is small, and controller tells that the device might be
used to control objects, processes, or events. Another term to describe a
microcontroller is embedded controller, because the microcontroller and its
support circuits are often built into, or embedded in, the devices they control.

About the 8051

The Intel 8051 is an 8-bit microcontroller which means that most available
operations are limited to 8 bits. All the ICs from a generic model in general are
called 8051 because they can all be programmed using 8051 assembly
language, and they all share certain features (although the different models all
have their own special features).

Some of the features that have made the 8051 popular are:

 8-bit data bus


 16-bit address bus
 34 general purpose registers each of 8 bits
 16 bit timers (usually 2, but may have more, or less).
 3 internal and 2 external interrupts.
 Bit as well as byte addressable RAM area of 16 bytes.
 4 8-bit ports, (short models have 2 8-bit ports).
 16-bit program counter and data pointer

8051 models may also have a number of special, model-specific features, such
as UARTs, ADC, OpAmps, etc...

MICROCONTROLLERS LAB (10ESL47) 7 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

Typical applications

8051 chips are used in a wide variety of control systems, telecom applications,
robotics as well as in the automotive industry. By some estimations, 8051
family chips make up over 50% of the embedded chip market.

Basic Pins

PIN 9: PIN 9 is the reset pin which is used reset the microcontroller’s internal
registers and ports upon starting up.

PINS 18 & 19: The 8051 has a built-in oscillator amplifier hence we need to
only connect a crystal at these pins to provide clock pulses to the circuit.

PIN 40 and 20: Pins 40 and 20 are VCC and ground respectively. The 8051
chip needs +5V 500mA to function properly, although there are lower powered
versions like the Atmel 2051 which is a scaled down version of the 8051 which
runs on +3V.

MICROCONTROLLERS LAB (10ESL47) 8 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

PINS 29, 30 & 31: As described in the features of the 8051, this chip contains
a built-in flash memory. In order to program this we need to supply a voltage
of +12V at pin 31. If external memory is connected then PIN 31, also called
EA/VPP, should be connected to ground to indicate the presence of external
memory. PIN 30 is called ALE (address latch enable), which is used when
multiple memory chips are connected to the controller and only one of them
needs to be selected. We will deal with this in depth in the later chapters. PIN
29 is called PSEN. This is "program select enable". In order to use the external
memory it is required to provide the low voltage (0) on both PSEN and EA
pins.

Ports

There are 4 8-bit ports: P0, P1, P2 and P3.

PORT P1 (Pins 1 to 8): The port P1 is a general purpose input/output port


which can be used for a variety of interfacing tasks. The other ports P0, P2 and
P3 have dual roles or additional functions associated with them based upon the
context of their usage.

PORT P3 (Pins 10 to 17): PORT P3 acts as a normal IO port, but Port P3 has
additional functions such as, serial transmit and receive pins, 2 external
interrupt pins, 2 external counter inputs, read and write pins for memory
access.

PORT P2 (pins 21 to 28): PORT P2 can also be used as a general purpose 8


bit port when no external memory is present, but if external memory access is
required then PORT P2 will act as an address bus in conjunction with PORT
P0 to access external memory. PORT P2 acts as A8-A15, as can be seen from
fig 1.1

MICROCONTROLLERS LAB (10ESL47) 9 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

PORT P0 (pins 32 to 39) PORT P0 can be used as a general purpose 8 bit port
when no external memory is present, but if external memory access is required
then PORT P0 acts as a multiplexed address and data bus that can be used to
access external memory in conjunction with PORT P2. P0 acts as AD0-AD7.

Oscillator Circuits

The 8051 requires the existence of an external oscillator circuit. The oscillator
circuit usually runs around 12MHz, although the 8051 (depending on which
specific model) is capable of running at a maximum of 40MHz. Each machine
cycle in the 8051 is 12 clock cycles, giving an effective cycle rate at 1MHz
(for a 12KHz clock) to 3.33MHz (for the maximum 40MHz clock).

Data and Program Memory

The 8051 Microprocessor can be programmed in PL/M, 8051 Assembly, C and


a number of other high-level languages. Many compilers even have support for
compiling C++ for an 8051.

Program memory in the 8051 is read-only, while the data memory is


considered to be read/write accessible. When stored on EEPROM or Flash, the
program memory can be rewritten when the microcontroller is in the special
programmer circuit.

Program Start Address

The 8051 starts executing program instructions from address 0x00 in the
program memory.

Direct Memory

The 8051 has 256 bytes of internal addressable RAM, although only the first
128 bytes are available for general use by the programmer. The first 128 bytes

MICROCONTROLLERS LAB (10ESL47) 10 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

of RAM (from 0x00 to 0x7F) are called the Direct Memory, and can be used
to store data.

Special Function Register

The Special Function Register (SFR) is the upper area of addressable


memory, from address 0x80 to 0xFF. This area of memory cannot be used for
data or program storage, but is instead a series of memory-mapped ports and
registers. All port input and output can therefore be performed by memory
mov operations on specified addresses in the SFR. Also, different status
registers are mapped into the SFR, for use in checking the status of the 8051,
and changing some operational parameters of the 8051.

General Purpose Registers

The 8051 has 4 selectable banks of 8 addressable 8-bit registers, R0 to R7.


This means that there are essentially 32 available general purpose registers,
although only 8 (one bank) can be directly accessed at a time. To access the
other banks, we need to change the current bank number in the flag status
register.

A and B Registers

The A register is located in the SFR at memory location 0xE0. The A register
works in a similar fashion to the AX register of x86 processors. The A register
is called the accumulator, and by default it receives the result of all arithmetic
operations. The B register is used in a similar manner, except that it can
receive the extended answers from the multiply and divide operations. When
not being used for multiplication and Division, the B register is available as an
extra general-purpose register.

MICROCONTROLLERS LAB (10ESL47) 11 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

Introduction
Microcontroller used is Atmel AT89S8252

8051 based Full Static CMOS controller with Three-Level Program, Memory
Lock, 32 I/O lines, 3 Timers/Counters, 9 Interrupts Sources, SPI, Watchdog
Timer, 2 DPTRs, 8K Flash Memory, 2k EEPROM, 256 Bytes On-chip RAM

MICROCONTROLLERS LAB (10ESL47) 12 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

Creating and compiling a μVision3 project

1. Double Click on the Vision3 icon on the desktop.

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

3. Start Project – New Project, and select the CPU from the device database (Database-
Atmel- AT89S8252).On clicking ‘OK’, the following option is displayed. Choose No.

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. 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, go to
debug mode

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

7. 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).

8. 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 ->

MICROCONTROLLERS LAB (10ESL47) 13 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

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 code region, say 1000h location type in C:1000H.

MICROCONTROLLERS LAB (10ESL47) 14 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

PART – A: Assembly Programs

I. DATA TRANSFER INSTRUCTIONS


1) Write an assembly language program to transfer n =10 bytes of data from location
8035h to location 8041h (without overlap).

AIM : To write an assembly language program to transfer n =10 bytes of data from
location 8035h to location 8041h (without overlap).
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is
seen as shown in the result.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPH,#80H
MOV R0,#35H //source address
MOV R1,#41H //destination address
MOV R3,#05H //count
BACK: MOV DPL, r0
MOVX A,@dptr
MOV DPL, R1
MOVX @dptr,A
INC R0
INC R1
DJNZ R3, BACK
HERE: SJMP HERE
END
RESULT:
Before Execution: 10 locations X:8035h are filled up with data.

After Execution: 10 locations X:8041h are filled up with data from 8035h.

MICROCONTROLLERS LAB (10ESL47) 15 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

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.

MICROCONTROLLERS LAB (10ESL47) 16 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

2) ASSEMBLY LANGUAGE PROGRAM TO EXCHANGE A BLOCK OF DATA.


Write an assembly language program to exchange n = 5 bytes of data at location 0027h
and at location 0041h.

AIM:To write an assembly language program to exchange n = 5 bytes of data at


location 0027h and at location 0041h.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is
seen as shown in the result.
ORG 00H
SJMP 30H
ORG 30H
MOV R0,#27H //source address
MOV R1,#41H //destination address
MOV R3,#05H //count
BACK: MOVX A,@r0
MOV r2,a
MOVX a,@r1
MOVX @r0,a
MOV a, r2
MOVX @r1,a
INC R0
INC R1
DJNZ R3, BACK
HERE: SJMP HERE
END
Using XCH command.
ORG 0000H
SJMP 30H
ORG 30H
MOV R0,#27H //source address
MOV R1,#41H //destination address
MOV R3,#05H //count
BACK: MOVX A,@r0
MOV r2,a
MOVX a,@r1
XCH a, r2
MOVX @r1,a
XCH a, r2
MOVX @r0,a
INC R0

MICROCONTROLLERS LAB (10ESL47) 17 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

INC R1
DJNZ R3, BACK
HERE: SJMP HERE
END
RESULT:
Before Execution: 5 locations at X:0027h & X:0041h are filled up with data.

After Execution: The data at X:8027h & X:8041h 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.

MICROCONTROLLERS LAB (10ESL47) 18 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

3) ASSEMBLY LANGUAGE PROGRAM TO SORT NUMBERS.


//BUBBLE SORT PROGRAM
Write an assembly language program to sort an array of n= 6 bytes of data in
ascending order stored from location 8035h.(use bubble sort algorithm).

AIM: To write an assembly language program to sort an array of n= 6 bytes of data in


ascending order stored from location 8035h.(use bubble sort algorithm).
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3, 9V- 1A adapter.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is
seen as shown in the result.
ORG 0000H
SJMP 30H
ORG 30H
MOV R0,#05 //count n-1 -ARRAY SIZE-n- Pass Counter
L1: MOV dptr, #9000h //array stored from address 9000h
MOV A,R0 //initialize exchange counter
MOV R1,A
L2: MOVX a, @dptr //GET NUMBER FROM ARRAY
MOV B, A //& STORE IN B
INC dptr
MOVX a, @dptr //next number in the array
CLR C //reset borrow flag
MOV R2, A //STORE IN R2
SUBB A, B //2nd - 1st no.—no compare instruction in 8051
JC NOEXCHG // JNC - FOR ASCENDING ORDER
MOV A,B //EXHANGE THE 2 NOES IN THE ARRAY
MOVX @dptr,a
DEC DPL //DEC dptr-INSTRUCTION NOT PTRESENT
MOV a,R2
MOVX @dptr,a
INC DPTR
NOEXCHG: DJNZ R1,L2 //decrement compare counter
DJNZ R0,L1 //decrement pass counter
here: SJMP here
END

MICROCONTROLLERS LAB (10ESL47) 19 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

Algorithm
1. Store the elements of the array from the address 9000h
2. Initialize a pass counter with array size-1 count (for number of passes).
3. Load compare counter with pass counter contents & initialize DPTR to point to the
start address of the array (here 9000h).
4. Store the current and the next array elements pointed by DPTR 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.
RESULT:
Before Execution:Unsorted Array at 9000h

After Execution: Sorted Array (Descending order) at 9000h

MICROCONTROLLERS LAB (10ESL47) 20 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

4) Write an assembly language program to find the largest element in a given string of
n = 6 bytes at location 4000h. Store the largest element at location 4062h.

AIM: To write an assembly language program to find the largest element in a given
string of n = 6 bytes at location 4000h. Store the largest element at location 4062h.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3, 9V- 1A adapter.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is
seen as shown in the result.
ORG 0000H
SJMP 30H
ORG 30H
MOV R3,#6 //length of the array
MOV DPTR,#4000H //starting address of the array
MOVX A,@DPTR
MOV r1,a
NEXTBYTE: INC DPTR
MOVX A,@DPTR
CLR C //reset borrow flag
MOV R2,A //next number in the array
SUBB A,R1 //OTHER Num - PREVIOUS LARGEST no.
JC skip // JNC for smallest element
MOV A,r2 //UPDATE larger number in r1
MOV R1,A
skip:DJNZ R3,NEXTBYTE
MOV DPL, #62H //LOCATION OF THE RESULT-4062H
MOV A,R1 //LARGEST NUMBER
MOVX @DPTR,A //STORE AT #4062H
OVER: SJMP OVER
END
Algorithm
1. Store the elements of the array from the address 4000h
2. Store the length of the array in r3 and set it as counter.
3. DPTR is loaded with starting address of the array.
4. Store the first number of the array in r1 (r1 is assigned to hold the largest number).
5. Increment DPTR.
6. Subtract the number pointed by DPTR from the contents of r1 (to compare whether
the next array element is larger than the one in r1).
7. If the element pointed by DPTR is larger then load the larger number into r1.
8. Decrement the counter and repeat steps through 5 until the counter becomes 0.
9. Store the largest number in r1 in address 4062h

MICROCONTROLLERS LAB (10ESL47) 21 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

RESULT:
Before Execution:

After Execution: Location 4062 has the largest element.

MICROCONTROLLERS LAB (10ESL47) 22 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

II. ARITHMETIC INSTRUCTIONS


1 .ASSEMBLY LANGUAGE PROGRAM ILLUSTRATING ADDITION,
SUBTRACTION, MULTIPLICATION AND DIVISION.
1) Write an ALP to perform the following: If x=0-perform w + v; else if x=1-perform
w-v; else if x=2-perform w*v; else if x=3-perform w/v, where w & v are eight bit
numbers.

AIM: To write an ALP to perform the following operations: If x=0-perform w + v; else


if x=1-perform w-v; else if x=2-perform w*v; else if x=3-perform w/v, where w & v are
eight bit numbers.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is
seen as shown in the result.
ORG 0000H
SJMP 30H
ORG 30H
MOV R0, #40H
MOVX A,@R0
MOV R1, A //R1 HAS CONDITION X
INC R0
MOVX A,@R0
MOV B, A //B HAS 1ST NUMBER-v
INC R0
MOVX A,@R0 //A HAS 2ND NUMBER-w
CJNE R1,#00,CKSUB
ADD A,B //PERFORM ADDITION
MOV B,#00 //B HAS CARRY
JNC SKIP
MOV B,#01H
SKIP:SJMP LAST
CKSUB: CJNE R1,#01,CKMUL
CLR C //RESET BORROW FLAG
SUBB A,B
MOV B,#00 //B INDICATES BORROW
JNC SKIP1
MOV B,#0FFH //FF INDICATES NEGATIVE NUMBER
SKIP1:SJMP LAST
CKMUL: CJNE R1,#02,CKDIV
MUL AB //16 bit product in AB with A having lower byte

MICROCONTROLLERS LAB (10ESL47) 23 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

SJMP LAST
CKDIV: CJNE R1,#03,OTHER
DIV AB //Quotient in A & remainder in B
SJMP LAST
OTHER:MOV A,#00
MOV B,#00
LAST: INC R0
MOVX @R0,A
INC R0
MOV A,B
MOVX @R0,A
HERE:SJMP HERE
END
Algorithm
1. Store the condition x in r1.
2. Load the first and second numbers to A and B registers respectively
3. Compare the contents of r1 and perform the operations add, sub, etc accordingly.
4. Store the result present in A and B registers to the appropriate memory locations.

RESULT:Before Execution: ADD SUB

After Execution: ADD After Execution: SUB

Before Execution: MUL After Execution: MUL

MICROCONTROLLERS LAB (10ESL47) 24 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

2. ASSEMBLY PROGRAM ILLUSTRATING SQUARE AND CUBE OPERATIONS.


//cube is an example of 16-bit arithmetic operation
//depending on flag condition, square or cube is performed
// Flag is a bit in the bit addressable RAM, say 1st bit of location 20h is used,
then bit address is 01
2) An eight bit number X is stored in external memory location 9000h. Write
an ALP to compute (i) the square of the number X if LSB of data RAM 20h
(bit address 01H) is set
(ii) the cube of the number X if LSB of data RAM 20h (bit address 01H) is
reset.
Store your result at locations 9001, 9002, 9003h.
AIM: To write an ALP for the following condition.
An eight bit number X is stored in external memory location 9000h. Write an
ALP to compute (i) the square of the number X if LSB of data RAM 20h (bit
address 01H) is set
(ii) the cube of the number X if LSB of data RAM 20h (bit address 01H) is
reset.
Store the result at locations 9001, 9002, 9003h.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC
loaded with Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.

2. After successful compilation, debugging is started.


3. The memory window is accessed and the input data are entered and the
output is seen as shown in the result.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR,#9000H
MOVX A,@DPTR //GET NUMBER-X
MOV R0,A //STORE IN R0
MOV B,A
MUL AB //SQUARE IT-X^2
CLR C //FOR STORING RESULT

MICROCONTROLLERS LAB (10ESL47) 25 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

JB 01,LAST //IF BIT 01 IS SET THEN END, ELSE DO CUBE


PUSH B //STORE UPPER PART OF SQUARE
MOV B,A //B-LOWER PART OF X^2
MOV A,R0 //A-X
MUL AB //X*LOWER X^2
INC DPTR
MOVX @DPTR,A //STORE PARTIAL RESULT
MOV A,B
MOV R2,A //UPPER PART OF X*LOWER X^2 IN R2
POP B //GET BACK UPPER PART OF SQUARE
MOV A,R0 //A-X
MUL AB //X*UPPER X^2
ADD A,R2 //ADD TO PARTIAL RESULT
LAST:INC DPTR
MOVX @DPTR,A
MOV A,B
ADDC A,#00 //ADD CARRY TO B(FOR SQUARE RESULT, C=0)
INC DPTR
MOVX @DPTR,A
HERE:SJMP HERE
END
RESULT:
CUBE OF 56H IS 9B498 WHICH IS STORED AS 98, B4, 09 (LOWER BYTE FIRST)

To get square make the D1 bit of data memory 20h high, say FF,02,06,etc. The bit address is
01. Similarly bit address 78h correspond to D0 bit 0f data ram location 2Fh.

Algorithm
1. Store the eight bit number x in A, r0 & B registers.
2. Multiply A and B registers to obtain the square (say SQH:SQL) of the number x.
3. Check if bit 01 is set. If set go to end (storing the result), else do the cube operations.
4. The high part of the square result (SQH) is stored on the stack.
5. Multiply the low part of the square result (SQL) with x (partial cube result).
6. Store the low part of the above result at 9001h & the high part in R2.
7. Retrieve the high part of the square result (SQH) stored on the stack & multiply with
x.
8. Add the low part of the above result (SQH*X) with R2 and store in 9002h.
9. Add the high part (SQH*X) with the resulting carry and store in 9003.

MICROCONTROLLERS LAB (10ESL47) 26 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

III. COUNTERS
A) ASSEMBLY PROGRAM ILLUSTRATING HEX UP/DOWN COUNTERS.
//counter program - hex/binary counters
1) Write an ALP to implement (display) an eight bit up/down binary (hex) counters on
watch window.
AIM: To write an ALP to display an eight bit up/down binary (hex) counters on watch
window.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.

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
INC a //dec a for binary down counter
JNZ BACK
HERE:SJMP HERE

DELAY: MOV r1,#0FFH


DECR1:MOV r2,#0FFH
DECR: MOV r3,#OFFH
DJNZ r3,$
DJNZ r2,DECR
DJNZ r1,DECR1
RET
END

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


…,0F,10,11,…FF

MICROCONTROLLERS LAB (10ESL47) 27 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

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(decrement for down counter)

B) ASSEMBLY PROGRAM ILLUSTRATING BCD UP/DOWN COUNTERS.


//counter program – BCD up/down counters
2) Write an ALP to implement (display) an eight bit up/down BCD counters on watch
window.
AIM : To Write an ALP to implement (display) an eight bit up/down BCD counters on
watch window.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
ORG 0H
SJMP 30H
ORG 30H
MOV a,#00
BACK:ACALL DELAY
ADD a,#99H //ADD 01 for BCD up counter
DA A //for bcd counter
JNZ BACK
HERE:SJMP HERE
DELAY:MOV r1,#0FFH
DECR1:MOV r2,#0FFH
DECR:MOV r3, #0FFH

MICROCONTROLLERS LAB (10ESL47) 28 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

DJNZ r3,$
DJNZ r2, DECR
DJNZ r1, DECR1
RET
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.

MICROCONTROLLERS LAB (10ESL47) 29 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

IV. PROGRAM ILLUSTRATING BOOLEAN AND LOGICAL (BIT


MANIPULATIONS)

1) Two eight bit numbers NUM1 & NUM2 are stored in external memory locations 8000h
& 80001h respectively. Write an ALP to compare the 2 nos.
Reflect your result as: if NUMI<NUM2, SET LSB of data RAM 2F (bit address 78H)
IF NUM1>NUM2, SET MSB OF 2F(7FH). if NUM1 = NUM2-Clear both LSB & MSB of
bit addressable memory location 2Fh.
AIM: To illustrate Boolean and logical instructions for the given conditions.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR,#8000H
MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
CLR C
SUBB A,R0
JZ EQUAL
JNC BIG
SETB 78H
SJMP END1
BIG:SETB 7FH
SJMP END1
EQUAL:CLR 77H
CLR 7FH
END1:SJMP END1
END
Algorithm:
1. Store the elements of the array from the address 4000h
2. Move the first number in r0 and the second number in register A respectively
3. Clear carry flag and subtract the two numbers, if the carry flag is 0(if the nos are
equal), Clear both LSB & MSB of bit addressable memory location 2Fh
4. If the carry bit is set then Set MSB of 2F(7FH), else LSB of data RAM 2F (bit
address 78H).

RESULT: 1) Before Execution: X:08000h = 45 & X:8001 = 35

MICROCONTROLLERS LAB (10ESL47) 30 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

After Executuion: D:02FH =01


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

MICROCONTROLLERS LAB (10ESL47) 31 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

2) ASSEMBLY PROGRAM ILLUSTRATING LOGICAL INSTRUCTIONS (BYTE


LEVEL)
3 eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM locations 20h, 21h
& 22H respectively. 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 IS 23H LOCATION
AIM: To write an ALP to illustrate logical instructions in byte level.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
ORG 0000H
SJMP 30H
ORG 30H
MOV A, 20h //donot use #, as data ram 20h is to be accessed
MOV R1,A //X IN R1
MOV A,21H //A -NUM1
CJNE R1,#0,CKOR
ANL A, 22H
SJMP END1
CKOR:CJNE R1,#01,CKXOR
ORL A, 22H
SJMP END1
CKXOR:CJNE R1,#02,OTHER
XRL A, 22H
SJMP END1
OTHER: CLR A
END1: MOV 23H,A //STORE RESULT
HERE: SJMP HERE
END

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: 1)Before Execution: D:020H =00, 21=0f, 22 = 12


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

MICROCONTROLLERS LAB (10ESL47) 32 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

4)Before Execution: D:020H =34, 21=0f, 22 = 12


After Execution D:023H = 00

The above program can also be written as shown below (using indirect addressing)
ORG 0000H
SJMP 30H
ORG 30H
mov r0,#20h
MOV A,@R0 //ON CHIP DATA RAM-DONOT USE MOVX
MOV R1,A //X IN R1
INC R0
MOV A,@R0 //A -NUM1
INC R0 // R0 POINTS TO NUM2
CJNE R1,#0,CKOR
ANL A, @R0
SJMP END1
CKOR:CJNE R1,#01,CKXOR
ORL A, @R0
SJMP END1
CKXOR:CJNE R1,#02,OTHER
XRL A, @R0
SJMP END1
OTHER: CLR A
END1:INC R0
MOV @R0,A //STORE RESULT
HERE:SJMP HERE
END

MICROCONTROLLERS LAB (10ESL47) 33 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

ASSEMBLY PROGRAM ILLUSTRATING LOGICAL INSTRUCTIONS (BIT


LEVEL)
3) 3 eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM locations 20h,
21h & 22H respectively. 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 23 H LOCATIONS.
AIM: To write an ALP to illustrate logical instructions in bit level.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.

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
MOV C,0FH //MSB OF NUM1 (21H) - BIT ADDRESS -0F
SJMP LAST
CK3:CLR C
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)

MICROCONTROLLERS LAB (10ESL47) 34 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

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

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.

MICROCONTROLLERS LAB (10ESL47) 35 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

V. CODECONVERSION PROGRAMS

1.Write an ALP to implement decimal to hex conversion


AIM: To write an ALP to implement decimal to hex conversion.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
ORG 0000H
SJMP 30h
ORG 30h
MOV DPTR,#40H //2-digit decimal number to be converted is given in data
memory 40h
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)

Algorithm
1. Move the decimal data to be converted from external memory 40h to accumulator.
2. AND A reg with 0f0h and obtain the upper MSB of the decimal digit and swap the
LSB and MSB of accumulator to bring the same to units place.
3. Move 0ah to B register and multiply with A reg to convert to hex value, store the
converted tens value in r1
4. Get the LSB of the decimal number and add to the converted tens value
5. point to the next memory location and store the result (hexadecimal).

MICROCONTROLLERS LAB (10ESL47) 36 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

3) Write an ALP to implement hex to decimal conversion


AIM: To write an ALP to implement hex to decimal conversion.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
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.

MICROCONTROLLERS LAB (10ESL47) 37 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

4) Write an ALP to implement BCD to ASCII conversion.


AIM: To write an ALP to implement BCD to ASCII conversion.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
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.

MICROCONTROLLERS LAB (10ESL47) 38 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

4) Write an ALP to implement hexadecimal to ASCII conversion


//This program also illustrates conditional branching (JNC), call and return
instructions.
AIM: To write an ALP to implement hexadecimal to ASCII conversion.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
ORG 0000H
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

MICROCONTROLLERS LAB (10ESL47) 39 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

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

5) Write an ALP to implement ASCII to hexadecimal conversion


AIM: To Write an ALP to implement ASCII to hexadecimal conversion

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
ORG 0000H
SJMP 30h
ORG 30h
MOV R1,#50H
MOV A,@R1 //get ascii byte from RAM location 50h
CLR C
SUBB A,#41H
MOV A,@R1
JC SKIP
CLR C
SUBB A,#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.

MICROCONTROLLERS LAB (10ESL47) 40 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

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
Store the converted hexadecimal number.

MICROCONTROLLERS LAB (10ESL47) 41 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

VI. SERIAL DATA TRANSMISSION

Program illustrating serial ascii data transmission (data-yE)


1) Conduct an experiment to configure 8051 microcontroller to transmit characters (yE) to a
PC using the serial port and display on the serial window.

AIM:To conduct an experiment to configure 8051 microcontroller to transmit characters


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

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
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: yE is printed on the serial window each time the program is executed.
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.

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

MICROCONTROLLERS LAB (10ESL47) 42 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

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

Program illustrating timer delay


2) Generate a 1second delay continuously using the on chip timer in interrupt mode.
AIM: To generate a 1second delay continuously using the on chip timer in interrupt mode.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
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

MICROCONTROLLERS LAB (10ESL47) 43 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

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.

MICROCONTROLLERS LAB (10ESL47) 44 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

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).

MICROCONTROLLERS LAB (10ESL47) 45 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

PART – B: Hardware Interfacing

1.Waveform Generation using DAC


2.Stepper Motor and DC motor interface.
3.4X4 hexadecimal Keyboard interface
4.Temperature sensor.
5.Calculator using Keyboard and Seven segment display.
6.Elevator control.

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

Some of the bit manipulative instructions used are


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

MICROCONTROLLERS LAB (10ESL47) 46 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

I. DAC interface to generate

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

8
0
5 P0 DAC CRO
1
μC

I.a)SQUARE WAVEFORM GENERATION

AIM :To generate square wave using DAC.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3,DAC board,CRO.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3.Output is seen in the CRO.

Algorithm for Square wave generation

 Output the values 00h(0ff) and ffh(on) Values through P0.


 After feeding the data allow a delay.
 The duty cycle and the frequency can be altered by having suitable combination of
ON and OFF period delays .
 The port P0 drives the DAC and the output waveform is displayed in CRO.

Program for square wave


#include<reg51.h>
void delay(unsigned int x)
{for(;x>0;x--);
return;
}
main()
{
while(1)
{

MICROCONTROLLERS LAB (10ESL47) 47 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

P0=0xff;
delay(500);
P0=0x00;
delay(1500);
}
}

MICROCONTROLLERS LAB (10ESL47) 48 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

I.bTRIANGULAR WAVEFORM GENERATION

AIM : To generate Triangular wave using DAC.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3, DAC board, CRO.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3. Output is seen in the CRO.

Algorithm for Triangular wave generation

 Output the initial value 00 through P0.


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

Program for triangular wave:

#include <REG51.H>
main()
{
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while(1)
{
for(i=0;i<0xff;i++){ /* Generate positive slope */
P0 = i;
}
for(i=0xfe;i>0x00;i--) /* Generate negative slope */
{P0 = i;
P1 = i;
}
}
}

1.c. RAMP/SAW TOOTH WAVEFORM GENERATION

AIM : To generate ramp/saw tooth wave using DAC.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3, DAC board, CRO.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3. Output is seen in the CRO.

MICROCONTROLLERS LAB (10ESL47) 49 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

Algorithm for Ramp wave generation

 Output the initial value 00 through P0.


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

Program for Ramp waveform

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

1.d.SINUSOIDAL WAVEFORM GENERATION

AIM : To generate sine wave using DAC.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3, DAC board, CRO.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3. Output is seen in the CRO.

Algorithm for Sine wave

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


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

Program for sine wave

#include <REG51xD2.H>
main()
{

MICROCONTROLLERS LAB (10ESL47) 50 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

static int a[13]={128,192,238,255,238,192,128,64,17,0,17,64,128};


unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)
{
for(i=0;i<13;i++) /* Output different values */
{ P0 = a[i]; }
}
}

MICROCONTROLLERS LAB (10ESL47) 51 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

II. Stepper motor and DC motor interface

a)STEPPER MOTOR

• Stepper motor unlike DC motor rotates in steps.


• Stepper motor has 4 coils which forms the stator and a central rotor.
• Rotation depends on excitation of stator coils.
step coil A coil B coil C coil D
1 0 0 0 1
2 0 0 1 0
3 0 1 0 0
4 1 0 0 0
AIM : To interface Stepper motor.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3, stepper motor.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3. Output is seen in the form of rotation of the motor.

Algorithm for Stepper Motor

• Configure P0 as output.
• Apply the initial excitation of 11 to motor coils through P0.
• Rotate right/left by one bit for clockwise /anti clockwise direction.

PS

8051µC
Stepper
Motor Stepper
P0
Interface Motor
Card

Program for stepper motor interface


#include <REG51xD2.H>
void delay (unsigned int x) /* Delay Routine */
{
for(;x>0;x--);
return;
}
Main ( )
{

MICROCONTROLLERS LAB (10ESL47) 52 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

unsigned char Val, i;


P0=0x00;
Val = 0x11;
for (i=0;i<4;i++)
{
P0 = Val;
Val = Val<<1; /* Val= Val>>1; for clockwise direction*/
delay (500);
}
}
b)DC MOTOR

AIM : To interface DC motor.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3, DC motor.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3. Output is seen in the form of rotation of the motor.

Algorithm for DC motor interface


• Configure P0 as output port and P3 as input port (P3.3 as start and P3.2 for
direction.)
• Check for start and direction bit condition.
• If start bit and direction bits are 1, load the bit pattern C0 to the port P0and call
delay.
• If start bit is 1 and direction bit is 0,load the bit pattern 50 to port0 and introduce
delay.

PS

8051µC
DC Motor
P0 Interface DC
FRC 8pin Card Motor
Cables
P3.2 (start)
P3.3 (cw)

Program for DC motor

#include<reg51xd2.h>
sbit start=P3^3;

MICROCONTROLLERS LAB (10ESL47) 53 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

sbit cw=P3^2;
main()
{
while(1)
{while(start&&cw)
{
P0=0xc0;
}
while(start&&(!cw))
{
P0=0x50;
}
P0=0x00;
}
}

MICROCONTROLLERS LAB (10ESL47) 54 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

III. LCD Display and 4X4 HEX Keyboard interface

PS

8051µC

P0 4X4 Keyboard
FRC 26pin Interface
Cable Card/LCD
Display

a) LCD Display

AIM : To interface LCD Display to display the characters.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3,LCD display.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3. Output is seen in the LCD display.

Algorithm for LCD Display.


 Configure P3 as input port and P0 as output port.
 Select the command register and send the respective commands.
 Select the display register and send the characters.

Program on LCD display


#include <reg51.h>
sbit rs= P3^7;
sbit rd =P3^6;
sbit en =P3^5;
int i;
unsigned char C[5]={0x3c,0x0e,0x06,0x01,0x80};
unsigned char D[5]="HELLO";
void command(unsigned char X){
P0=X;
rs=0;
rd=0;
en=1;
en=0;
return;
}
void display(unsigned char Y){
P0=Y;
rs=1;

MICROCONTROLLERS LAB (10ESL47) 55 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

rd=0;
en=1;
en=0;
return;
}
void delay(unsigned char X){
for (;X>0;X--);
return;}
main ()
{
for (i=0;i<5;i++){
delay(500);
command(C[i]);
}
for (i=0;i<5;i++){
delay(500);
display(D[i]);
}
while(1);
}

b)4X4 HEX Keyboard interface

AIM: To interface 4X4 HEX Keyboard.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3, 4X4 HEX Keyboard, LED interface card.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3. Key is pressed and the corresponding output is seen in LED display.

Algorithm for Keyboard Interface

• Configure P0 as input/output port to scan/read the rows and columns.


• Force 4 columns high and take the status of the rows.
• Force 4 rows high and take the status of the columns.
• Combine both the status to get the scan code of the key pressed.

Program for 4X4 hex keypad.

#include<reg51.h>
Unsigned char Temp;
Main(){
While(1){
P0=0x0f;
Temp=P0;
P0=0xf0;
Temp=Temp+P0;

MICROCONTROLLERS LAB (10ESL47) 56 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

P2=Temp;
}
}

MICROCONTROLLERS LAB (10ESL47) 57 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

IV. ADC and Temperature Controller.

PC

8051µC

P1,P2,P3 Temp Heat


FRC 16 and 8 Sensor Source
pin Interface
Cables

Heat
switch

a) TEMPERATURE CONTROLLER

AIM: To interface the temperature controller using ADC.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3,LCD display, Temperature sensor LM35,ADC board.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3. Output is seen in the LED display as ON/OFF of the switch.

Algorithm for Temperature controller

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


2. Start the conversion by making wr from low to high.
3. Wait for end of conversion.
4. Compare the data in P2 and perform the suitable operation (switch On or OFF the switch).

#include<reg51.h>
sbit rd=P1^1;
sbit wr=P1^2;
sbit hswitch=P3^0;
unsigned char mntr=0xA5;
void delay (unsigned int x){
for(;x>0;x--);
return;
}
main (){
P1=0x00;
wr=1;
delay (50000);
rd=1;

MICROCONTROLLERS LAB (10ESL47) 58 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

if(P2<mntr){
hswitch=1;
}
else hswitch=0;
}
b)VOLTAGE CONVERSION USING ADC

AIM: To interface the temperature controller using ADC.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3, LCD display, Temperature sensor LM35, and ADC board.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3. Output is seen in the LED display in hexadecimal form.

Algorithm for ADC

1. Configure P1 as input port.


2. Start the conversion by making wr from low to high.
3. Wait for end of conversion using delay.
4. Read the data in P2 and display.

#include<reg51.h>
sbit rd=P1^1;
sbit wr=P1^2;
void delay (unsigned int x){
for(;x>0;x--);
return;
}
main (){
P1=0x00;
wr=1;
delay (50000);
rd=1;
}

V. Elevator

MICROCONTROLLERS LAB (10ESL47) 59 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

AIM: To demonstrate elevator interface for 4 floors.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3, LCD display, 4X4 HEX Keyboard.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3. Depending on the request given through keypad output is seen in the LED display.

Algorithm for elevator interface

I/O Port configuration

Port 0 ----->Input, Hex Key pad


Port 2 -----> LED

• Read the floor request through input port P0.


• If the current floor and requested floor are the same no change .Display the same
floor.
• If the requested floor greater/lesser than current moving up of the lift is indicated by
glow of LED.

PS

8051µC

P0 4X4 matrix
FRC 26pin keypad and
Cable LCD Display

Program for Elevator


#include <REG52.H>

#include <reg51.h>
unsigned char temp,value=0x80,press;
int request,i,prefloor;
char getkey(){
P0=0xf0;
temp=P0;
if(temp==0x70)request=3;
else if(temp==0xb0)request=2;
else if(temp==0xd0)request=1;
else if(temp==0xe0)request=0;
//else request=0;

MICROCONTROLLERS LAB (10ESL47) 60 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

return temp;
}

void delay(){
unsigned char j,k,l;
for(j=0;j<0xff;j++){
for(k=0;k<0xff;k++){
for(l=0;l<0x0f;l++);
}
}
}

main(){
while(1){
press=getkey();
if(press!=0xf0){
//value=0x80;
i=prefloor-request;
prefloor=request;
if(i<0){
for(;i!=0;i++){
value=value>>1;
P2=value;
delay();
}
}
if(i>0){
for(;i!=0;i--){
value=value<<1;
P2=value;
delay();
}
}
}
}
}

MICROCONTROLLERS LAB (10ESL47) 61 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

VI. CALCULATOR USING KEYBOARD AND 7-SEGMENT DISPLAY

AIM: To demonstrate Calculator using keyboard and 7-segment display.

APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with


Keil Micro vision 3, LCD display, seven segment display.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, hex file is downloaded.
3. Depending on the data given through keypad output is seen in the seven segment display.

Algorithm

• Read the numbers n1 and n2 from keyboard and display them on seven segment.
• Read the operand from the keypad if key pressed is B (+), C(-),D(*),E(/) then
respective operation is performed.
• Result is displayed on 2 digit seven segment display.
• If any time the key pressed value returned as 10h then clear the LCD.

PS
PS

8051µC Keypad
P0
FRC 26pin
Cable
7 Seg
Display

#include <REG51.H>

sbit srck = P3^0 ;


sbit ser = P3^2 ;
sbit rck = P3^1 ;

//--------------Funtion declaration
void display(unsigned char); //LED Display Routine
void convert_display(unsigned char);
void MSDelay (unsigned int); //Delay Routine
void Clear (void);
unsigned char getkey (); //KeyBoard Routine

unsigned char key=0xFF; //Recieved KeyStroke

MICROCONTROLLERS LAB (10ESL47) 62 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

//---------------The message
unsigned char idata keypad[4][4]= { '1','2','3','/',
'4','5','6','*',
'7','8','9','-',
'C','0','C','+',};
unsigned char idata disp[10] = { 0x40,0xcf,0xa4,0x30,0x19,0x12,0x02,0xf8,0x00,0x10 };

void main()
{
unsigned char Data1,Data2,Ans,Funct;
Clear();
while(1)
{
do
{
getkey (); //get data from matrix key pad
Clear();
Data1 = key;
key = key & 0xF0;
}while (key!=0x30);
display(disp[Data1&0x0F]);
MSDelay(10);
do
{
getkey (); //get data from matrix key pad
Funct = key;
key = key & 0xF0;
}while (key!=0x20);
display(0xff);
MSDelay(10);
do
{
getkey (); //get data from matrix key pad
Data2 = key;
key = key & 0xF0;
}while (key!=0x30);
display(disp[Data2&0x0F]);
MSDelay(110);
Clear();

Data1= Data1 & 0x0F;


Data2= Data2 & 0x0F;

switch (Funct) //loop to respective subroutine


{
case ('+'):
{
Ans = Data1 + Data2;
convert_display(Ans);

MICROCONTROLLERS LAB (10ESL47) 63 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

break;
}
case ('-'):
{
Ans = Data1 - Data2;
convert_display(Ans);

break;
}
case ('*'):
{
Ans = Data1 * Data2;
convert_display(Ans);

break;
}
case ('/'):
{
Ans = Data1 / Data2;
convert_display(Ans);

break;
}
}
}
}

void Clear (void)


{
unsigned int x;
for (x=0;x<6;x++)
{
display(0xFF);
}
}

void MSDelay (unsigned int value) // Delay routine


{
unsigned int x,y;
for (x=0;x<900;x++)
for (y=0;y<value;y++);
}

unsigned char getkey () // Routine to read matrix keyboard


{
unsigned char colloc, rowloc;
TMOD = 0x20;
TH1 = -3;
SCON = 0x50;
TR1 = 1;

MICROCONTROLLERS LAB (10ESL47) 64 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

P2 = 0xff;
do {
P2 = 0x0f; //Wait untill all keys are released
colloc = P2;
colloc &= 0x0f;
} while (colloc != 0x0f);

do {
do
{
MSDelay (1);
colloc = P2;
colloc &= 0x0f;
} while (colloc == 0x0f); //Check whether any ket is pressed
MSDelay (1);
colloc = P2;
colloc &= 0x0f;
//Confirm whether any ket is pressed after delay
} while (colloc == 0x0f); //to aviod spikes

while(1)
{
P2 = 0xfE; //get the row presses
colloc = P2;
colloc &= 0xf0;
if (colloc != 0xf0)
{
rowloc = 0;
break;
}
P2 = 0xfd;
colloc = P2;
colloc &= 0xf0;
if (colloc != 0xf0)
{
rowloc = 1;
break;
}
P2 = 0xfb;
colloc = P2;
colloc &= 0xf0;
if (colloc != 0xf0)
{
rowloc = 2;
break;
}
P2 = 0xf7;
colloc = P2;
colloc &= 0xf0;

MICROCONTROLLERS LAB (10ESL47) 65 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

rowloc = 3;

break;
}
//get the coloum presses
if (colloc == 0xe0) key = (keypad[rowloc][0]);
else if (colloc == 0xd0) key = (keypad[rowloc][1]);
else if (colloc == 0xb0) key = (keypad[rowloc][2]);
else key = (keypad[rowloc][3]);
return(key);
}

//----------------Hex to Binary convertion routine


void convert_display(unsigned char value)
{
unsigned char x, d1, d2, d3;
x = value / 10; //divide by 10
d1 = value % 10; //save low digit (reminder of division)
d2 = x % 10;
d3 = x / 10; //divide by 10 once more
display(disp[d3]);
display(disp[d2]);
display(disp[d1]);
return;
}

//----------------7 segment display


void display(unsigned char value)
{
char m;
char buffer;
buffer = value;
for(m=0;m<8;m++)
{
if(buffer&0x80) ser=1;
else ser=0;
srck=1;
buffer<<=1;
srck=0;
}
rck=1;
;
rck=0;
}

MICROCONTROLLERS LAB (10ESL47) 66 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

MICROCONTROLLERS LAB (10ESL47) 67 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

MICROCONTROLLERS LAB (10ESL47) 68 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

MICROCONTROLLERS LAB (10ESL47) 69 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

MICROCONTROLLERS LAB (10ESL47) 70 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

PART –C: MSP 430 PROGRAMS

1. MSP430xG46x - 8x8 Unsigned Multiply

Description: Hardware multiplier is used to multiply two numbers. The calculation is


automatically initiated after the second operand is loaded. Results are stored in RESLO and
RESHI.

.cdecls C,LIST, "msp430xG46x.h"


;-------------------------------------------------------------------------------
.text ;Program Start
;-----------------------------------------------------------------------------
RESET mov.w #900,SP ; Initialize stackpointer
StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop WDT
SetupFLL bis.b #XCAP14PF,&FLL_CTL0 ; Configure load caps
mov.w #12h,&MPY ; Load first operand -unsigned mult
mov.w #56h,&OP2 ; Load second operand
bis.w #LPM4,SR ; LPM4
nop ; set BREAKPOINT here

2. MSP430xG46x - 16x16 Unsigned Multiply


;
; Description: Hardware multiplier is used to multiply two numbers. The calculation is
automatically initiated after the second operand is loaded. Results are stored in RESLO and
RESHI.

.cdecls C,LIST, "msp430xG46x.h"


;-------------------------------------------------------------------------------
.text ;Program Start
;-----------------------------------------------------------------------------
RESET mov.w #850,SP ; Initialize stackpointer
StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop WDT
SetupFLL bis.b #XCAP14PF,&FLL_CTL0 ; Configure load caps
mov.w #1234h,&MPY ; Load first operand -unsigned mult
mov.w #5678h,&OP2 ; Load second operand
bis.w #LPM4,SR ; LPM4
nop ; set BREAKPOINT here

.sect ".reset" ; POR, ext. Reset, Watchdog


.short RESET
.end

MICROCONTROLLERS LAB (10ESL47) 71 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

3. MSP430xG46x - 16x16 Addition

cdecls C,LIST, "msp430xG46x.h"


;-------------------------------------------------------------------------------
.text ;Program Start
;-------------------------------------------------------------------------------
RESET mov.w #900,SP ; Initialize stackpointer
StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop WDT
SetupP5 MOV.b #12,R6
MOV.b #01,R7
ADD.b R7,R6
;
Mainloop bis.w #LPM4+GIE,SR ; LPM4, enable interrupts
nop ; Required only for debugger
;
;-----------------------------------------------------------------------------
P1_ISR; Toggle P1.0 Output
;-----------------------------------------------------------------------------
xor.b #002h,&P5OUT ; P5.1 = toggle
bic.b #02h,&P1IFG ; P1.4 IFG Cleared
reti ; Return from ISR
;
;-----------------------------------------------------------------------------
; Interrupt Vectors
;-------------------------------------------------------------------------------
.sect ".reset" ; MSP430 RESET Vector
.short RESET
.sect ".int20" ; P1.x Vector
.short P1_ISR ;
.end

4. Block transfer:

A 16 word block from 1400-141fh is transfered to 1420h-143fh using DMA0 in a burst


block using software DMAREQ trigger. After each transfer, source, destination and DMA
size are reset to inital software setting because DMA transfer mode 5 is used. P5.1 is
toggled durring DMA transfer only for demonstration purposes.

; ** RAM location 0x1400 - 0x143f used - make sure no compiler conflict **

; Built with IAR Embedded Workbench Version: 3.41A


;
**************************************************************************
*****
#include "msp430xG46x.h"
;-------------------------------------------------------------------------------
RSEG CSTACK ; Define stack segment

MICROCONTROLLERS LAB (10ESL47) 72 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

;-------------------------------------------------------------------------------
RSEG CODE ; Assemble to Flash memory
;-------------------------------------------------------------------------------
RESET mov.w #SFE(CSTACK),SP ; Initialize stackpointer
StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop WDT
SetupP5 bis.b #002h,&P5DIR ; P1.0 output
SetupDMA0 movx.a #1400h,&DMA0SA ; Start block address
movx.a #1420h,&DMA0DA ; Destination block address
mov.w #0010h,&DMA0SZ ; Block size
mov.w
#DMADT_5+DMASRCINCR_3+DMADSTINCR_3+DMAEN,&DMA0CTL; Rpt, inc
bis.w #DMAEN,&DMA0CTL ; Enable DMA0
;
Mainloop bis.b #02h,&P5OUT ; P5.1 = 1, LED on
bis.w #DMAREQ,&DMA0CTL ; Trigger block transfer
bic.b #02h,&P5OUT ; P5.1 = 0, LED off
jmp Mainloop ; Again
;
;-------------------------------------------------------------------------------
COMMON INTVEC ; Interrupt Vectors
;-------------------------------------------------------------------------------
ORG RESET_VECTOR ; POR, ext. Reset, Watchdog
DW RESET
END

5. Addition of 16 bit numbers


NAME main ; module name

PUBLIC main ; make the main label visible outside this module
ORG 0FFFEh
DC16 init ; set reset vector to 'init' label

RSEG CSTACK ; pre-declaration of segment


RSEG CODE ; place program in 'CODE' segment

init: MOV #SFE(CSTACK), SP ; set up stack

main: NOP ; main program


MOV.W #WDTPW+WDTHOLD,&WDTCTL ; Stop watchdog timer
MOV.B #0x0FF,R6
MOV.W #0xFFFF,R7
BIC.B #0x0001,R7
BIC.B #0x0002,R6
MOV.W #0x0F0F,R7
BIS.W #0x8000,R7
BIS.W #0x4000,R7
MOV.W #1234,R9

MICROCONTROLLERS LAB (10ESL47) 73 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

ADD.B #01,R9
ADD.W R9,R10

AND.B #000Fh,R6

MOV.W #0002h,R4

MOV.W #00011000000b,R15

JMP $ ; jump to current location '$'


; (endless loop)
END

MICROCONTROLLERS LAB (10ESL47) 74 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

QUESTION BANK
1.Write an assembly language program to transfer ______ bytes of data from location
_____ to location ______ (without overlap).
2. Write an assembly language program to exchange n = ____ bytes of data at
location______ and at location _______.
3. Write an assembly language program to sort an array of n= _______bytes of data in
ascending order stored from location __________(use bubble sort algorithm).
4. Write an assembly language program to find the largest element in a given string of n
=______ bytes at location _______. Store the largest element at location ______.
5. Write an ALP to perform the following: If x=0-perform w + v; else if x=1-perform w-v;
else if x=2-perform w*v; else if x=3-perform w/v, where w & v are eight bit numbers.
6. Write an ALP to illustrate square and cube operations.
7. Write an ALP to implement (display) an eight bit up (hex) counters on watch window.
8. Write an ALP to implement (display) an eight bit down (hex) counters on watch
window.
9. Write an ALP to implement (display) an eight bit up/down BCD counters on watch
window.
10. Two eight bit numbers NUM1 & NUM2 are stored in external memory locations 8000h
& 80001h respectively. Write an ALP to compare the 2 nos.
Reflect your result as: if NUMI<NUM2, SET LSB of data RAM 2F (bit address 78H)
If NUM1>NUM2, SET MSB OF 2F(7FH). if NUM1 = NUM2-Clear both LSB & MSB of
bit addressable memory location 2Fh.
11. 3 eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM locations 20h,
21h & 22H respectively. 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 is 23H location.
12. Write an ALP to implement decimal to hex conversion.
13. Write an ALP to implement hex to decimal conversion.
14. Write an ALP to implement BCD to ASCII conversion.
15. Write an ALP to implement ASCII to BCD conversion.
16. Conduct an experiment to configure 8051 microcontroller to transmit characters (yEs) to
a PC using the serial port and display on the serial window.

MICROCONTROLLERS LAB (10ESL47) 75 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

17. Write a program in C generate sinusoidal waveform using DAC.


18. Write a program in C to generate square waveform of 50% duty cycle using DAC.
20. Write a program in C to generate triangular waveform using DAC.
21. Write a program in C to generate saw tooth waveform using DAC.
22. Write a program in C to rotate the stepper motor in clockwise direction.
23. Write a program in C to rotate the stepper motor in anticlockwise direction.
24. Write a program in C to rotate DC motor in clockwise and anticlockwise direction.
25. Write a program in C to measure the temperature using ADC.
26. Write a program in C to convert the analog voltage of_______V to equivalent digital
value.
27. Write a program in C to display the message HELLO on LCD display.
28. Write a program in C to detect the key pressed in 4X4 hex keypad.
29. Write a program in C to demonstrate elevator interface for 4 floors.
30. Write a program in C to illustrate the calculator using seven segment display and 4x4
matrix keypad.

MICROCONTROLLERS LAB (10ESL47) 76 IV SEMESTER


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERNG BMSIT

VIVA QUESTIONS
1. What is microcontroller?
2. What is the difference between microcontroller and microprocessor?
3. What is the difference between microcontroller and microcomputer?
4. Compare 8051 and MSP430 microcontrollers
5. Explain the role of watchdog timer in MSP430
6. Explain the clock system of MSP430
7. Explain the application areas of low power embedded systems.
8. List the peripherals of the microcontroller 89S8252.
9. List the special function registers of 8051 and their function
10. What is the difference between bit and byte addressable SFRs?
11. List the applications of microcontrollers.
12. What are arithmetic instructions?
13. What are logical instructions?
14. What are interrupts? List the interrupts of 8051 with their priority.
15. What do you mean by baud rate?How do to set the baud rate?
16. What is the difference between MOV and MOVC instruction?
17. What is the difference between CALL and JUMP instruction?
18. What are assembler directives?
19. What is a cross compiler?
20. What are the features of embedded C?
21. What is stack? List the instructions related to stack
22. What do you mean by data memory and code memory?
23. List out the types of memories. What do you mean by flash memory?
24. What are internal and external memories? Which instruction is used to access
external memory.
25. What is the difference between timer and counter?

.
**********

MICROCONTROLLERS LAB (10ESL47) 77 IV SEMESTER

You might also like