Mechatronics - MC Lab Manual

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 82

REVA University School of ECE

Contents

Sl. PAGE
TITLE
No No.
1 COURSE OVERVIEW 2
2 Introduction To Microcontroller-8051 4
3 Introduction to Keil tool 11
 Sample Programs 24
PART-A Experiment.1
3 Data Transfer Instructions: Block Data Transfer and
Exchangebetween internal and external Data memory with 30
and without overlap, Sorting, largest and smallest number in
an array.
Experiment.2
4 Arithmetic Instructions: 32-bit multi-precision Addition,
41
Subtraction, Multiplication of 2 16-bit numbers and Division
(16-bit by 8 bit)
Experiment.3
Logical Instructions: 8x8 multiplication using shift Add
5 technique. ASCII to packed BCD and Vice versa, Code
47
Conversions. Exchange Two numbers without the use of
3rd location. Implementation of Boolean expressions (Bit
Manipulation)
Experiment.4
6 Timers: Wave form generation with varying Duty Cycle 51
using Interrupt and Polling Techniques.
Experiment.5
7 Serial Communication: Serial data transmission with Polling 59
and Interrupt technique (Regular and Look up table).
PART-B 8051 interfacing with the different Modules
Experiment.1
8 Interfacing DAC to generate various waveforms with output
63
voltage varying between -12V to 12V with Amplitude and
Frequency control.
9 Experiment.2
Stepper motor interfacing by controlling the steps and 71
direction.
10 Experiment.3
76
DC Motor speed control using external interrupt.
11 Experiment.4
78
Display the ASCII value of Key pressed on LCD.
12 Experiment.5
Count the incoming pulses using counters. 81
Challenge Experiment
13 Elevator interface to 8051. 83
14 Simple Calculator using 6 digit seven segment displays and
83
Hex Keyboard interface to 8051.

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |1


REVA University School of ECE

COURSE OVERVIEW:

Course Title Microcontrollers and Applications Lab Course Type Hard Core
Course Code B20ES0404 Credits 1 Class IV Semester
Contact Work Total Number of Assessment
TLP Credits
Hours Load Classes in
Course Theory 0 0 0 Weightage
Per Semester
Structure
Practice 1 2 2
Theory Practical IA SEE
Tutorial 0 0 0
Total 1 2 2 0 26 50 % 50 %
This course introduces 8051 microcontroller to provide basic understanding of architecture,
instruction set, assembly level programming, interfacing to various sensors, relays, motors,
actuators through various types of serial and parallel communication. Timers and interrupt
functions are illustrated through the selection and control activities using suitable
programming platforms such as Assemblers, C compilers, Kiel, , etc. This fundamental
knowledge on microcontrollers lead to explore large number of controller families like
ATMEGA, TI and PIC that are used in industrial and automation applications.

COURSE OBJECTIVES:
The objectives of this course are to:
1. Introduce Microcontroller 8051 Architecture.
2. Give an insight into instruction set of microcontroller 8051.
3. Introduce assembly and C programming for 8051.
4. Provide insight into timer, serial communication and interrupts modules of 8051.
5. Interface a microcontroller with peripheral devices.

COURSE OUTCOMES(COs)
On successful completion of this course; the student shall be able to:

CO# Course Outcomes POs PSOs


CO1 Describe the Architecture of 8051 microcontroller. 1,2 1
CO2 Describe Instruction Set of 8051 1,2 1
CO3 Write Assembly and C Programs for 8051. 1,2 1,2
CO4 Interface various peripherals. 1,2 1,2
CO5 Analyse the data transfer information through serial & parallel 1,2 1
ports
CO6 Write assembly language programs and generate the machine
code that will provide solutions real-world control problems and 1,2,9,10,12 1,2
document the results in the form of report.

BLOOM’S LEVEL OF THE COURSE OUTCOMES


CO# Bloom’s Level
Remember L1) Understand (L2) Apply (L3) Analyze(L4) Evaluate(L5) Create (6)
CO1  
CO2  
CO3   
CO4  
CO5    
CO6    

Microcontrollers and applications Lab B20ES0404 Sem:IV Page |2


REVA University School of ECE

COURSE ARTICULATION MATRIX


CO/

PSO1

PSO2

PSO3
PO10

PO11

PO12
PO1

PO2

PO3

PO4

PO5

PO6

PO7

PO8

PO9
POs

CO1 3 2 3

CO2 3 3 3

CO3 3 3 3 2
CO4 3 3 3 2
CO5 3 3 3 2
CO6 3 3 3 3 2 3 2
Average 3 2.8 3 3 2 3 2
Note:1-Low,2-Medium,3-High

COURSE CONTENT

PRACTICE
Part-A

1. Data Transfer Instructions: Block Data Transfer without overlap, Sorting


2. Arithmetic Instructions: 32-bit multi-precision Addition, Subtraction, square and
cube of 8-bit number and 8-bit Division.
3. Logical Instructions: ASCII to packed BCD and Vice versa,
Implementation of Boolean expressions (Bit Manipulation).
4. Timers: Wave form generation with varying Duty Cycle using
Interrupt and Polling Techniques.
5. Serial Communication: Serial data transmission with Polling and
Interrupt technique (Regular and Look up table).

Part-B

1. Interfacing DAC to generate various waveforms with output voltage varying between
-12V to 12V with Amplitude and Frequency control.
2. DC Motor speed control using external interrupt.
3. Stepper motor interfacing by controlling the steps and direction.
4. Display the ASCII value of Key pressed on LCD.

TEXTBOOKS:
1. Kenneth J. Ayala, “The 8051-microcontroller architecture, programming and applications”
Thomson publication, 3rd edition, 2007
2. Muhammad Ali Mazidi and Janice Gillespie Mazidi and Rollin D, McKinlay “The 8051
Microcontroller and Embedded Systems using assembly and C” PHI, 2006/Pearson 2006.
3. Sandhu, Harprit singh. “Making PIC microcontroller instruments and controllers / Harprit
Singh Sandhu.” McGraw-Hill (2009).

JOURNALS/MAGAZINES/ ADDITIONAL SOURCES:

1. https://e2echina.ti.com/group/c8df485b47/m/msp430/11060/download
2. https://www.arm.com/zh/files/word/Yiu_Ch1.pdf
3. http://ce.sharif.edu/~pourmohammadi/AVR%20Microcontroller%20and%20Embedded%20
Systems/ AV R%20Microcontroller%20and%20Embedded%20Systems.pdf

Microcontrollers and applications Lab B20ES0404 Sem:IV Page |3


REVA University School of ECE

Introduction to Microcontrollers:

A microcontroller can be compared to a small stand-alone computer; it is a


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

Nowadays, microcontrollers are so cheap and easily available that it is


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

Today, microcontrollers are an indispensable tool for the robotics hobbyist as


well as for the engineer. Starting in this field can be a little difficult, because
you usually can't understand how everything works inside that integrated
circuit, so you have to study the system gradually, a small part at a time, until
you can figure out the whole image and understand how the system works.

Comparison of Microprocessor and Microcontroller

Microprocessor and Microcontroller differ in their functionalities.


Microprocessor has more generalized functions, where as Microcontroller is
more specific to its task.

A Microprocessor may not be programmed to handle real-time tasks but


Microcontroller in devices that need to control temperature of water or perhaps
measure the temperature of a room require real time monitoring and therefore
with its inbuilt set of instructions the microcontroller works on its own.

Microprocessor requires constant input by a human such as in a personal


computer so that instructions can be booted. Microprocessor is the memory of
the computing machine

Microcontrollers and applications Lab B20ES0404 Sem:IV Page |4


REVA University School of ECE

The 8051 microcontroller architecture

The 8051 is the name of a big family of microcontrollers. The device which we
are going to use along this tutorial is the 'AT89S52' which is a typical 8051
microcontroller manufactured by Atmel™. Note that this part doesn't aim to
explain the functioning of the different components of an 89S52
microcontroller, but rather to give you a general idea of the organization of the
chip and the available features, which shall be explained in detail along this
tutorial.
The block diagram provided by Atmel™ in their datasheet showing the
architecture the 89S52 device can seem very complicated, and since we are
going to use the C high level language to program it, a simpler architecture can
be represented as the figure
This figures shows the main features and components that the designer can
interact with. You can notice that the 89S52 has 4 different ports, each one
having 8 Input/output lines providing a total of 32 I/O lines. Those ports can be
used to output DATA and orders do other devices, or to read the state of a
sensor, or a switch. Most of the ports of the 89S52 have 'dual function'
meaning that they can be used for two different functions: the first one is to
perform input/output operations and the second one is used to implement
special features of the microcontroller like counting external pulses, interrupting
the execution of the program according to external events, performing serial
data transfer or connecting the chipto a computer to update the software.
Each port has 8 pins, and will be treated from the software point of view as an
8-bit variable called 'register', each bit being connected to a different
Input/output pin. You can also notice two different memory types: RAM and
EEPROM. Shortly, RAM is used to store variable during program execution,
while the EEPROM memory is used to store the program itself, that's why it is
often referred to as the 'program memory'. The memory organization will be
discussed in detail later.
The special features of the 89S52 microcontroller are grouped in the blue box
at the bottomof figure at this stage of the tutorial, it is just important to note that
the 89S52 incorporates hardware circuits that can be used to prevent the
processor from executing various repetitive tasks and save processing power for
more complex calculations. Those simple tasks can be counting the number of
external pulses on a pin, or generating precise timing sequences.

Microcontrollers and applications Lab B20ES0404 Sem:IV Page |5


REVA University School of ECE

It is clear that the CPU (Central Processing Unit) is the heart of the
microcontrollers; it is the CPU that will Read the program from the FLASH
memory and execute it by interacting with the different peripherals discussed
above.

Features of 8051:

The main features of 8051 microcontroller are:

1) RAM – 128 Bytes (Data memory)


2) ROM – 4Kbytes (ROM signify the on – chip program space)
3) Serial Port – Using UART makes it simpler to interface for serial communication.
4) Two 16 bit Timer/Counter
5) Input/output Pins – 4 Ports of 8 bits each on a single chip.
6) Five Interrupt Sources
7) Harvard Memory Architecture – It has 16 bit Address bus (each of
RAM and ROM)and 8 bit Data Bus.
8) 8051 can execute 1 million one cycle instructions per second with a
clock frequencyof 12MHz.
This microcontroller is also called as “System on a chip” because
it has all the featureson a single chip

Microcontrollers and applications Lab B20ES0404 Sem:IV Page |6


REVA University School of ECE

AT89C51ED2 Microcontroller
Description

AT89C51RD2/ED2 is high performance CMOS Flash version of the 80C51


CMOS single chip 8- bit microcontroller. It contains a 64-Kbyte Flash memory
block for code and for data. The 64-Kbytes Flash memory can be programmed
either in parallel mode or in serial mode with the ISP capability or with
software. The programming voltage is internally generated from the standard
VCC pin.
The AT89C51RD2/ED2 retains all of the features of the Atmel 80C51 with 256
bytes of internal RAM, a 5 source 4-level interrupt controller and three
timer/counters. The AT89C51ED2 provides 2048 bytes of EEPROM for
nonvolatile data storage.
In addition, the AT89C51RD2/ED2 has a Programmable Counter Array, an
XRAM of 1792 bytes, a Hardware Watchdog Timer, SPI interface, Keyboard, a
more versatile serial channel that facilitates multiprocessor communication
(EUART) and a speed improvement mechanism (X2Mode).
The fully static design of the AT89C51RD2/ED2 allows reducing system power
consumption by bringing the clock frequency down to any value, including DC,
without loss of data.
The AT89C51RD2/ED2 has 2 software-selectable modes of reduced activity
and an 8- bit clock prescaler for further reduction in power consumption. In the
Idle mode the CPU is frozen while the peripherals and the interrupt system are
still operating. In the Power- down mode the RAM is saved and all other
functions are inoperative.
The added features of the AT89C51RD2/ED2 make it more powerful for
applications that need pulse width modulation, high speed I/O and counting
capabilities such as alarms, motor control, corded phones, and smart card
readers.

Microcontrollers and applications Lab B20ES0404 Sem:IV Page |7


REVA University School of ECE

INTERUPTS

Five sources of interrupt (both external and internal).


Two External interrupts INTO and INTI 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.

Microcontrollers and applications Lab B20ES0404 Sem:IV Page |8


REVA University School of ECE

Creating and compiling a µ vision 3 project


1) Double Click on the µ vision 3 icon on the desktop
2) If there is any project opened, close it by selecting close project option from project
menu of the Keil µ vision IDE menu bar.

3) Click on project menu and select new µ vision project.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |9


REVA University School of ECE

4) Create new project window will open. Select the location new volume (D) drive.
Create one new folder & open that folder.

5) Write file name. There is no need give any extension along with the project name, as keil
will automatically assign. Click on save.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |10


REVA University School of ECE

6) Select the device for the application through the provided Device Database in Select
device for target ‘target1’ window. (Device database – Atmel – AT89C51ED2) and
click OK.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |11


REVA University School of ECE

7) Keil µ vision IDE will ask for Copy standard 8051 startup code to project folder and
add file to project? Click on NO.

8) Observe the project workspace window, which is on the left side of the Keil IDE, It will
contain Target1 and Sourcegroup1.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |12


REVA University School of ECE

9) Select File from the menu bar and choose New.

10) Select File from menu bar and select SaveAS.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |13


REVA University School of ECE

11) A window called Save As will open and prompt for the location where to store the file.
For convenience sake, save this file in the same folder where the project has created.
12) While naming the file, if program is written in ‘C’ language save file with .C extension
(Ex---test.C), or if the program is written in Assembly language save ASM file
with .ASM extension Ex---test.ASM).

13) Write complete program. Select File click Save.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |14


REVA University School of ECE

14) Right click on Source Group 1 in project Workspace window and click on Add files
to group ‘Source Group 1’ or double click on Source Group 1 to add the required
files to the project.

15) Select the required .C or .ASM (.A51) files and click on Add & click on closebutton.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |15


REVA University School of ECE

16) From Project menu select Build target option. This will generate the output
file in OMF51 format. If there are any errors or warnings rectify them and
rebuild the target.

17) If it shows 0 errors on output window, then the program is ready toexecute.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |16


REVA University School of ECE

18) From Project menu select Options for Target ‘Target 1’option.

19) Select Target in the Options for Target ‘Target 1’ window. Change crystal frequency
Xtal (MHz) = 11.0592 &select at same window click Debug.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |17


REVA University School of ECE
20) To work in simulator mode (i.e. without hardware select Use Simulator radio button or
to downloaded to hardware, select keil monitor radio button and from drop down menu
Keil Monitor 51 Driver &click OK.

To download the program to hardware kit follows steps from step 21 to 25.
21) Check Load Application at Startup option and if it is a C program then check
Run to main () option.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |18


REVA University School of ECE

22) If mode of communication is in USB cable then Right click on My Computer icon on
desktop and select Manage option.

23) In the Computer Management window select Device manager, expand the Ports
(COM & LPT) option observe the port number assigned to ESA MCB51.

Close the Computer Management window.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |19


REVA University School of ECE

24) Open the Keil window, from Project menu and select Option for Target ‘Target 1”
option. Click Debug &select Settings.
25) Select the COM port &click OK.

26) Select Debug menu. Click Start/Stop Debug Session & click run.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |20


REVA University School of ECE
27) Once program is loaded in the hardware kit, observe the output. Before stopping the
debug session (i.e., by selecting stop debug on µ vision window) press RESET button in
ESA MCB 51kit compulsorily.

Note: To access data RAM area type address as D: 0020h.


Similarly to access the DPTR region (XRAM-present on chip in AT89C51 ED2) say 9000h
location type in X: 09000H.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |21


REVA University School of ECE

Sample programs
Write result of the following instructions below for each example

Data Movement operation


Example: 01
org 0000h
sjmp 30h
org 30h
mov r0,#34h
mov a,#24h
mov a, r0 ;Check the change in the content of A and R0 if any
Here: sjmp here
end

Example: 02

org 0000h
sjmp 30h
org 30h
mov a,00h ;(A) = and (R0) =
mov r0,#24h
mov 24h,#20h ; contents of RAM location 24h=
mov a,@r0 ; Check the change in the content of A and R0 if any
here: sjmp here
end

Example: 03

org 0000h
sjmp 30h
org 30h
mov a, #40h ; (A) =
mov dptr,#34h ;(DPTR)=
movx @dptr,a ;comments on this instruction
mov a,#00h
movx a,@dptr
here: sjmp here
end

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |22


REVA University School of ECE

Example: 04
org 0000h
mov DPTR,#1234h ; (DPTR)=
mov a,#00h ;(a)=
movc a,@a+DPTR
here:sjmp here
end ; comment on this instruction and check out the
respective
Register and memory location ontent
Addition operation
Example: 05
org 0000h
sjmp 30h
org 30h
mov r1,#27h ; (r1)=
mov a,#45h ; (a)=
add a, #34h ; (a) =
add a,r1 ;( a)= (r1)=
add a,01h ;(a)= (01)=
add a,@r1 ; observation on this instruction
addc a,#34h ;(PSW)= (a)= (r1)=
addc a,r1 ;(PSW)= (a)= (r1)=
addc a ,01h ;(PSW)= (a)= (r1)=
addc a,@r1 ; comment on this instruction
here: sjmp here
end
Example: 06
org 0000h
sjmp 30h
org 30h
mov r1,#27h ;( r1)=
mov a,#45h ; (a)=
subb a,#34h ;(a)= (psw)=
subb a,r1 ;(a)= (r1)=
subb a,01h ;(a) (psw)=
subb a,@r1 ; comment on this instruction & check the content of
The required register
here: sjmp here
end

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |23


REVA University School of ECE

Multiplication and Division operation


Example: 07
org 0000h
sjmp 30h
org 30h
mov b,#27h ;(b)=
mov a,#45h ;(a)=
Mul ab ;(a)= (b)=
here: sjmp here
End
Example: 08

org 0000h
sjmp 30h
org 30h
mov b, #27h ;(b) =
mov a, #45h ;(a)=
div ab ;(a)= (b) =
here: sjmp here
end

Rotate Operation
Example: 09

org 0000h
mov a, #66h ;(a)=
clr c
rl a ;(a)= flag C=
rlc a ;(a)= flag C=
rr a ;(a)= flag C=
rrc a ;(a)= flag C=
here: sjmp here
end

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |24


REVA University School of ECE

Boolean operation
Example: 10

AND Operation

org 0000h
mov a,#23h ;(a)=
mov r1,#45h ;(r1)=
anl a,r1 ;(a)= (r1)=
anl a,01h ; Comment on this instruction and check out of the respective
registers
here: sjmp here
end

Example: 11
OR Operation

org 0000h
mov a, #23h ;(a)=
mov r1, #41h ;(r1)=
orl a,#56h ;(a)=
orl a,r1 ;(a)= (r1)=
orl a,@r1 ; Comment on this instruction
anl a,01h ;(a)= (r1)=
here: sjmp here
end

Example: 12
EX-OR Operation

org 00h
mov a,#23h
mov r1,@45h
xrl a,@56h
orl a,r1
orl a,@r1
anl a,01h
here:sjmp here
end

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |25


REVA University School of ECE

Example: 13
COMPLIMENT instruction

org 0000h
mov a,#23h ; (a)=
cpl a ; (a)=
here: sjmp here
end

Example: 14
Clear Instruction

org 0000h
mov a,#34h ; (a)=
clr a ;(a)=
here: sjmp here
end

Example: 15
Exchange Operation
org 0000h
mov a,#23h ; (a) =
mov r1,#45h ;(r1)
xcha,@r1 ; (a) =
xch a,01h
xch a,@r1
here: sjmp here
end

Example: 16
Increment & Decrement Operations

org 0000h
sjmp 30h
org 30h
mov a,#45h ; (a)=
mov r1,#27h ; (r1)=
dec r1 ;(r1)=
dec a
dec @r1
inc r1 ; (r1)=
inc a ; (a)=
inc @r1
Inc dptr ; (dptr)=
end

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |26


REVA University School of ECE

Example: 17
Decimal Adjustment operations
org 0000h
mov b, #27h ; (b)=
mov a,#45h ; (a)=
add ab ; (a)=
da a ; (a)=
here: sjmp here
end

Example: 18
Jump Operations

org 0000h
sjmp 30h
org 30h
mov a,#55h ;(a)=
mov r1,#34h ;(r1)=
add a,r1 ; (a)=
jc loop
jnc loop1
jb c,loop
jnb c,loop1
jbc 08h,loop

Loop: clr c ; c flag =


loop1: setb c ; c flag =
mov a, #45h ;(a)=
mov r1,#45h ;(r1)=
mov 45h,#45h ; (45h)=
cjne a, 01h,loop
cjne r1,#45h,loop
cjne @r1,#45h,loop
djnz 45h,loop1
dec a ;(a)=
jz loop ;(pc)=
jnz loop1 ; (pc)=
end

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |27


REVA University School of ECE
1. A
Write an assembly language program to transfer n=05 bytes of data From Internal
Memory location 30h to location 40h (without overlap).

ORG 0000H
SJMP 30H
ORG 30H
MOV R0, #30H // Initialize R0 with lower byte of source starting address
MOV R1, #40H // Initialize R1 with lower byte of destination starting address
MOV R2, #05H // R2 is used as a counter
BACK: MOV A,@R0 //Transfer the contents of source to accumulator
MOV @R1, A //Data of accumulator stored in destination
INC R0 // Increment source pointer
INC R1 // Increment destination pointer
DJNZ R2, BACK //Decrement the counter and repeat the loop until the
Counter is zero
HERE: SJMP HERE
END

RESULT:

Before Execution: 5 locations D: 30H are filled up with data.

After Execution: 05 location D: 40H are filled up with data from 30H.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |28


REVA University School of ECE
B.
Write an External memory assembly language program to transfer n=10 bytes of data
from location 8035H to location 8050H (without overlap).

ORG 0000H
SJMP 30H
ORG 30H
MOV DPH, #80H //Initialize DPH with higher byte of source and destination address
MOV R0, #35H // Initialize R0 with lower byte of source starting address
MOV R1,#50H // Initialize R1 with lower byte of destination starting address
MOV R3,#0AH // R3 is used as a counter
BACK: MOV DPL, R0 // Load DPL with lower byte of source address
MOVX A,@dptr //Transfer the contents of source to accumulator
MOV DPL,R1 // Load DPL with lower byte of destination address

MOVX @dptr,A // Data of accumulator stored in destination


INC R0 // Increment source pointer
INC R1 // Increment destination pointer
DJNZ R3, BACK //Decrement the counter and repeat the loop until the counter is zero
HERE: SJMP HERE
END

RESULT:
Before Execution: 10 locations X: 8035H are filled up with data.

After Execution: 10 location X: 8050H are filled up with data from 8035H.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |29


REVA University School of ECE
C.
Transfer 10bytes of data from 40H address to Lower address with overlap of 4bytes
(Internal memory)

ORG 00H
SJMP 30H
ORG 30H
MOV R0, #40H // Initialize R0 with lower byte of source starting address
MOV R1, #3AH // Initialize R1 with lower byte of destination
starting address
MOV R2, #0AH // R2 is used as a counter
BACK: MOV A,@R0 //Transfer the contents of source
to accumulator
MOV @R1, A //Data of accumulator stored in destination
INC R0 // Increment source pointer
INC R1 // Increment destination pointer
DJNZ R2, BACK //Decrement the counter and repeat the loop until the
counter iszero
HERE: SJMP HERE
END

RESULT:

Before Execution: 10 locations D: 40H are filled up with data.

After Execution: 10 location D: 3AH are filled up with data from 3AH.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |30


REVA University School of ECE
D.
Transfer 10bytes of data from 8035H address to Lower address with overlap of
4bytes (External memory)

ORG 0000H
SJMP 30H
ORG 30H
MOV DPH,#80H //Initialize DPH with higher byte of source and destination address
MOV R0,#35H // Initialize R0 with higher byte of source starting address
MOV R1,#2FH // Initialize R1 with higher byte of destination starting address
MOV R3,#0AH // R3 is used as a counter
BACK: MOV DPL, R0 // Load DPL with higher byte of source address
MOVX A,@dptr //Transfer the contents of source to accumulator
MOV DPL,R1 // Load DPL with higher byte of destination address
MOVX @dptr,A // Data of accumulator stored in destination
INC R0 // increment source pointer
INC R1 // increment destination pointer
DJNZ R3, BACK //Decrement the counter and repeat the loop until the counter is zero

HERE: SJMP HERE


END
RESULT:
Before Execution: 10 locations X: 8035H are filled up with data.

After Execution: 10 location X: 802FH are filled up with data from 8035H.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |31


REVA University School of ECE
E.
Transfer 10bytes of data from 40H address to Higher address with overlap of 4bytes
(internal memory)

ORG 0000H
SJMP 30H
ORG 30H
MOV R0, #49H // Initialize R0 with lower byte of source starting address
MOV R1, #4FH // Initialize R1 with lower byte of destination starting address
MOV R2, #0AH // R2 is used as a counter
BACK: MOV A,@R0 //Transfer the contents of source to accumulator
MOV @R1, A //Data of accumulator stored in destination
DEC R0 // Decrement source pointer
DEC R1 // Decrement destination pointer
DJNZ R2, BACK //Decrement the counter and repeat the loop until the counter is zero
HERE: SJMP HERE
END

RESULT:

Before Execution:

After Execution:

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |32


REVA University School of ECE
F.
Transfer 10bytes of data from 8035H address to higher address with overlap of
4bytes (External memory)

ORG 0000H
SJMP 30H
ORG 30H
MOV DPH, #80H //Initialize DPH with higher byte of source and destination address
MOV R0, #3EH // Initialize R0 with lower byte of source ending address
MOV R1,#44H // Initialize R1 with lower byte of destination ending address
MOV R3,#0AH // R3 is used as a counter
BACK: MOV DPL, R0 // Load DPL with lower byte of source address
MOVX A,@dptr //Transfer the contents of source to accumulator
MOV DPL,R1 // Load DPL with lower byte of destination address
MOVX @dptr,A // Data of accumulator stored in destination
DEC R0 // decrement source pointer
DEC R1 // decrement destination pointer
DJNZ R3, BACK //Decrement the counter and repeat the loop until the counter is
zero

HERE: SJMP HERE


END

RESULT:
Before Execution:

After Execution:

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 and applications Lab B20ES404 Sem:IV P a g e |33


REVA University School of ECE
G.
Sort an array of 10 numbers from 30H in the internal memory in ascending order and store
the smallest element in 40 Location and in 41 location specify the memory address where the
smallest element is present in the sorted array.
ORG 00H
SJMP 30H
ORG 30H
MOV R2, #09H //initialize the phase counter with n-1
LOC3:MOV R0, #30H // initialize array address
MOV 03H, 02H //initialize the comparison counter
LOC2:MOV A,@R0 //read the first byte
INC R0
CLR C
SUBB A,@R0 // compare the data with next byte
JC LOC1 // if smaller the then exchange the 2bytes in array
XCH A,@R0
DEC R0
XCH A,@R0
INC R0
XCH A,@R0
LOC1: DJNZ R3, LOC2 // decrement comparison counter
MOV R1,#40H
DEC R0
MOV A,@R0
MOV @R1,A
INC R1
MOV A,R0
MOV @R1,A
DJNZ R2, LOC3 // after each phase repeat the process for n-1 cycles
HERE: SJMP HERE
END
RESULT:
Before Execution: Unsorted array at 30h.

After Execution: Sorted Array (Ascending order) at 30H.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |34


REVA University School of ECE
H.
Sort an array of 10 numbers from 30H in the internal memory in descending order and store
the smallest element in 40 Location and in 41 location specify the memory address where the
largest element is present in the sorted array.

ORG 00H
SJMP 30H
ORG 30H
MOV R2, #09H //initialize the phase counter with n-1
LOC3:MOV R0, #30H // initialize array address
MOV 03H, 02H //initialize the comparison counter
LOC2:MOV A,@R0 //read the first byte
INC R0
CLR C
SUBB A,@R0 // compare the data with next byte
JNC LOC1 // if smaller the then exchange the 2bytes in array
XCH A,@R0
DEC R0
XCH A,@R0
INC R0
XCH A,@R0
LOC1: DJNZ R3, LOC2 // decrement comparison counter
MOV R1,#40H
DEC R0
MOV A,@R0
MOV @R1,A
INC R1
MOV A,R0
MOV @R1,A
DJNZ R2, LOC3 // after each phase repeat the process for n-1 cycles
HERE: SJMP HERE
END
RESULT:
Before Execution: Unsorted array at D: 30h.

After Execution: Sorted Array (Descending order) at D: 30h.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |35


REVA University School of ECE

I.
Write an assembly language program to an to sort an array of n=6 bytes of data in
Ascending order stored from location 9000h. (use bubble sort algorithm)

ORG 00H
SJMP 30H
ORG 30H
MOV R0,#50H //count (N-1) array size = N
LOC1:MOV DPTR,#9000h //array stored from address 9000h
MOV R1,#04h //initialize exchange counter
LOC2:MOVX A,@DPTR //get number from array and store in B register
MOV B,A
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-1stNo.,since no compare instruction in 8051
JNC noexchg // JC -FOR ASCENDING ORDER
MOV A,B //exhange the 2 noes in the array
MOVX @DPTR,A
DEC DPL //DEC DPTR-instruction not present
MOV A,R2
MOVX @DPTR,A
INC DPTR
noexchg: DJNZ R1,LOC2 //decrement compare counter
DJNZ R0,LOC1 //decrement pass counter
End

RESULT:
Before Execution: Unsorted array at X: 9000h.

After Execution: Sorted Array (Ascending order) at X: 9000h.

Microcontrollers and applications Lab B20ES404 Sem:IV P a g e |36


REVA University School of ECE

J.
Write an assembly language program to an to sort an array of n=6 bytes of data in
Descending order stored from location 9000h. (use bubble sort algorithm)
ORG 00H
SJMP 30H
ORG 30H
MOV R0,#50H //count (N-1) array size = N
LOC1:MOV DPTR,#9000h //array stored from address 9000h
MOV R1,#05h //initialize exchange counter
LOC2:MOVX A,@DPTR //get number from array and store in B register
MOV B,A
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-1stNo.,since no compare instruction in 8051
JC noexchg // JC -FOR DESCENDING ORDER
MOV A,B //exhange the 2 noes in the array
MOVX @DPTR,A
DEC DPL //DEC DPTR-instruction not present
MOV A,R2
MOVX @DPTR,A
INC DPTR
noexchg: DJNZ R1,LOC2 //decrement compare counter
DJNZ R0,LOC1 //decrement pass counter
End
RESULT:
Before Execution: Unsorted array at X: 9000h.

After Execution: Sorted Array (Ascending order) at X: 9000h.

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |37


REVA University School of ECE

K.
Write an assembly language program to exchange n=5 bytes of data from Internal
memory location 30h to location 40h

ORG 00H
SJMP 30H
ORG 30H
MOV R0, #30H // Initialize the array1
MOV R1, #40H // Initialize the array2
MOV R2, #05H // Initialize the counter
BACK: XCH A,@R0 //exchange accumulator with array1 data
XCH A,@R1 //exchange accumulator with array2 data
XCH A,@R0 //exchange accumulator with array1 data
INC R0
INC R1 // Increment pointers
DJNZ R2, BACK // repeat till the end of array
HERE: SJMP HERE
END

RESULT:
Before Execution:

After Execution:

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |38


REVA University School of ECE

2. A
Wap to ADD 32bit No. Stored Starting from Memory Location 40H aand 50H onwards store
The result starting from 60h onwards .
N1:98795432H.
N2:89ABCD58H.

ORG 00H
SJMP 30H
ORG 30H
MOV R0,#40H
MOV R1,#60H
MOV R2,#04
SETB PSW.3
MOV R1,#50H
CLR PSW.3
CLR C
BACK:MOV A,@R0
SETB PSW.3
ADDC A,@R1
INC R1
CLR PSW.3
MOV @R1,A
INC R0
INC R1
DJNZ R2,BACK
JNC EXIT
INC @R1
EXIT:NOP
HERE:SJMP HERE
END

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |39


REVA University School of ECE

RESULT:

Before Execution:

After Execution:

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |40


REVA University School of ECE
2. B
Subtract a 32 bit number store in 40H from 50H and store the result in 60H locations
in the external memory.
N1: 89ABCD58H.
N2: 98795432H.

ORG 00H
SJMP 30H
ORG 30H
MOV R0,#40H
MOV R1,#60H
MOV R2,#04
SETB PSW.3
MOV R1,#50H
CLR PSW.3
CLR C
BACK:MOV A,@R0
SETB PSW.3
SUBB A,@R1
INC R1
CLR PSW.3
MOV @R1,A
INC R0
INC R1
DJNZ R2,BACK
JNC EXIT
INC @R1
EXIT:NOP
HERE:SJMP HERE
END

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |41


REVA University School of ECE

RESULT:

Before Execution:

After Execution:

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |42


REVA University School of ECE
2. C
A 16bit Number is Stored in Memory Location 40H onwards and an 8bit Number is
Stored in Memory Location 50H WAP to Multiply these 2 Numbers and Store the Result
From 60H Onwards.

ORG 00H
SJMP 30H
ORG 30H
MOV R0,#40H
MOV R1,#60H
MOV B,50H
MOV A,@R0
MUL AB
MOV R7,B
MOV @R1,A
MOV B,50H
INC R0
MOV A,@R0
MUL AB
MOV R6,B
ADD A,R7
INC R1
MOV @R1,A
CLR A
ADDC A,R6
INC R1
MOV @R1,A
HERE:SJMP HERE
END

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |43


REVA University School of ECE

RESULT:

Before Execution:

After Execution:

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |44


REVA University School of ECE
3. A
Convert number from BCD to ASCII stored in 50H and store the result in thenext
Location.

ORG 0000H
SJMP 30h
ORG 30h
MOV R1, #50H //get BCD data byte from RAM location 50h
MOV A,@R1
MOV R2, A //store inR2
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
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 ASCIL codes-38h 32h
HERE: SJMP HERE
END

RESULT:

The BCD code 85 at D: 0050h is converted to ASCII code-35h 38h.

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |45


REVA University School of ECE
3. B
Convert number from ASCII to BCD stored in 0035H and 0036H in the external memory
and store the result in the next location

ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR, #35H
MOVX A,@DPTR
ANL A, #0FH
MOV B, A
INC DPTR
MOVX A,@DPTR
ANL A, #0FH
SWAP A
ORL A, B
MOV DPTR, #37H
MOVX @DPTR, A
HERE: SJMP HERE
END

RESULT:

Before Execution:

After Execution:

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |46


REVA University School of ECE
3. C
Exchange the contents of accumulator and 31H location in the internal memory
Without a third location or XCHG instruction.

ORG 00H
SJMP 30H
ORG 30H
XRL A, 31H
XRL 31H, A
XRL A, 31H
HERE: SJMP HERE
END

RESULT:

Before Execution:

After Execution:

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |47


REVA University School of ECE
3. D
Calculate the output of a Boolean expression Y=ABC+A’B’C’+AB’C given A=24.0,
B=24.1, C=24.2, Y=24.3 locations in the internal memory.

ORG 00H
SJMP 30H
ORG 30H
MOV C, 20H //CARRY = A
ANL C, 21H //CARRY = AB
ANL C, 22H //CARRY =ABC
MOV 23H, C // Y=CARRY
MOV C, 20H //CARRY = A
CPL C //CARRY = A’
ANL C, /21H //CARRY = A’B’
ANL C, /22H //CARRY = A’B’C’
ORL C, 23H //CARRY=Y+CARRY=ABC+A’B’C’
MOV 23H, C //Y=CARRY=ABC+A’B’C’
MOV C, 20H //CARRY = A
ANL C, /21H //CARRY = AB’
ANL C, 22H //CARRY = AB’C
ORL C, 23H //CARRY=Y+CARRY=ABC+A’B’C’ +AB’C
MOV 23H, C //Y=CARRY=ABC+A’B’C’ +AB’C
HERE: SJMP HERE
END

RESULT:
Before Execution:

After Execution:

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |48


REVA University School of ECE
4. A
Generate a square wave of 40% duty cycle on port 1.0 of 1 KHz using polling technique.

Calculation:

ORG 00H
SJMP 30H
ORG 30H
MOV TMOD, #01H //TIMER 0 MODE1
LOC3: SETB P1.0 // SET PORT LINE
MOV TL0, #8FH
MOV TH0, #0FEH //TIMER VALUE TO GENERATE 400us delay
SETB TR0 //start timer
LOC1: JNB TF0, LOC1 //wait till time laps
CPL P1.0 //reset port line
CLR TF0 // clear timer0 flag
MOV TL0, #0D7H
MOV TH0, #0FDH //TIMER VALUE TO GENERATE 600us delay
LOC2: JNB TF0, LOC2 //wait till time laps
CLR TF0 // clear timer0 flag
SJMP LOC3 // repeat infinitely
END

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |49


REVA University School of ECE

RESULT:

ON PERIED:

OFF PERIED:

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |50


REVA University School of ECE

4. B
Generate a square wave of 40% duty cycle on port 1.0 of 1 KHz using interrupts.

Calculation:

ORG 00H
SJMP 30H
ORG 0BH //Timer 0 interrupt vector address
CPL P1.0 //compliment port line
SJMP ISR_TMR0 //timer 0 interrupt service routine
ORG 30H
MOV TMOD, #01H //timer 0 mode1
MOV IE, #82H // enable interrupts
SETB P1.0 // set port line
MOV TL0, #0BFH
MOV TH0, #0FEH //TIMER VALUE TO GENERATE 400us delay
SETB TR0 // start timer0
HERE: SJMP HERE // infinite loop
// Interrupt Service Routine
ISR_TMR0: CLR TF0 //clear flag
JB P1.0, LOC1 //if port line is set then set delay to 400us else 600us
MOV TL0, #0D7H
MOV TH0, #0FDH
RETI
LOC1: MOV TL0, #8FH
MOV TH0, #0FEH
RETI
END

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |51


REVA University School of ECE

RESULT:
ON PERIED:

OFF PERIED:

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |52


REVA University School of ECE

4.C
Generate a square wave of 5ms ON period, 15ms OFF period, 10ms on period and
20ms OFF period and repeat infinitely using polling technique and look up table.

Calculation:

ORG 00H
SJMP 30H
ORG 30H
MOV TMOD, #01H //timer0 mode1
SETB P1.0 // set timer
MOV TL0, #04H
MOV TH0, #0EEH //TIMER VALUE TO GENERATE 5ms delay
SETB TR0 // start timer
LOC3: MOV R3, #04H // 4 different delays
MOV DPTR, #ARRAY // Initialize to first location in the Look Up Table
LOC2: CLR A
MOVC A,@A+DPTR //Read data from Look up Table
MOV R2, A // Initialize counter
LOC1: JNB TF0, LOC1 //wait till the time laps
CLR TF0
MOV TL0, #04H
MOV TH0, #0EEH //TIMER VALUE TO GENERATE 5ms delay
DJNZ R2, LOC1 //check if the delay is generated the number times as specified
in Look up Table
CPL P1.0 //compliment port line
INC DPTR
DJNZ R3, LOC2 //repeat till the number of times specified in the Look up Table
SJMP LOC3 //repeat the cycle infinitely
ARRAY: DB 01H, 04H, 02H, 03H //Initialization of Look up Table
END

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |53


REVA University School of ECE

RESULT:

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |54


REVA University School of ECE

4. D
Generate a square wave of 5ms ON period, 15ms OFF period, 10ms on period and 20ms
OFF period and repeat infinitely using interrupt and look up table

Note: The algorithm is a combination of the algorithms of program 4b and 4c

Calculation:

ORG 00H
SJMP 30H
ORG 0BH
SJMP ISR_TMR0
ORG 30H
MOV TMOD, #01H
MOV IE, #82H
SETB P1.0
MOV TL0, #04H
MOV TH0, #0EEH
SETB TR0
MOV DPTR, #ARRAY
CLR A
MOVC A,@A+DPTR
MOV R2, A
INC DPTR
MOV R3, #03H
HERE: SJMP HERE
ISR_TMR0: CLR TF0
MOV TL0, #04H
MOV TH0, #0EEH
DJNZ R2, LOC1
CPL P1.0
CLR A

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |55


REVA University School of ECE

MOVC A,@A+DPTR
MOV R2, A
INC DPTR
DJNZ R3, LOC1
MOV DPTR, #ARRAY
MOV R3, #04H
MOVC A,@A+DPTR
MOV R2, A
LOC1: RETI
ARRAY: DB 01H, 04H, 02H, 03H
END

RESULT:

Microcontrollers and applications Lab B20ES0404 Sem:IV P a g e |56


REVA University School of ECE
5. A
Transfer the “REVA University” through serial port at a baud rate of 9600 using polling
technique

ORG 00H
SJMP 30H
ORG 30H
MOV TMOD, #20H //Timer1 in mode2
MOV TH1, #0FDH
MOV TL1, #0FDH // for 9600 baudrate
MOV SCON, #50H //enable serial reception in standard UART mode
SETB TR1 //start timer 1
MOV R2, #0FH // Initialize to number of characters in the LUT to be displayed
MOV DPTR, #ARRAY //Initialize the LUT address
LOC1: CLR A
MOVC A,@A+DPTR //Read the value from LUT
ACALL TRANS // Display function
INC DPTR // Point to the next data byte to be displayed
DJNZ R2, LOC1 //Repeat till all the bytes are displayed
LOC2: SJMP LOC2

TRANS: MOV SBUF, A //write data to the buffer


LOC3: JNB TI, LOC3 // wait till the data is transferred
CLR TI
RET
ARRAY: DB "REVA UNIVERSITY"
END

RESULT:

Microcontrollers and applications Lab B20ES0404 SEM-IV P a g e | 57


REVA University School of ECE
5. B
Transfer the “REVA University” through serial port at a baud rate of 9600 using SERIAL
INTERRUPT

ORG 00H
SJMP 30H
ORG 23H //Interrupt vector address of serial interrupt
CLR TI
SJMP ISR_SI // Interrupt service routine of serial interrupt
ORG 30H
MOV IE, #90H //enable serial interrupt
MOV TMOD, #20H // timer1 mode2
MOV TH1, #0FDH
MOV TL1, #0FDH //set baud rate to 9600
MOV SCON, #50H //enable serial reception
SETB TR1 //start timer1
MOV R2, #0FH // Number of characters to be displayed
MOV DPTR, #ARRAY //Initialize the pointer to LUT
CLR A
MOVC A,@A+DPTR //Read the first byte in the LUT
MOV SBUF, A // write to buffer
INC DPTR // increment the pointer
HERE: SJMP HERE

// ISR for serial interrupt


ISR_SI: DJNZ R2, LOC1 //return if all the characters are displayed else read the next
character and write it into buffer
RETI
LOC1: CLR A
MOVC A,@A+DPTR
MOV SBUF, A
INC DPTR
RETI
ARRAY: dB "REVA UNIVERSITY”
END

Microcontrollers and applications Lab B20ES0404 SEM-IV P a g e | 58


REVA University School of ECE

RESULT:

Microcontrollers and applications Lab B20ES0404 SEM-IV P a g e | 59


REVA University School of ECE

HARDWARE INTERFACING

1. Waveform generation using dual DAC.


2. Stepper motor interface.
3.3X8 hexadecimal keyboard interface.
4. DC motor interface
5. Counting of pulses.

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 make 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 the program can be executed in the desired target (say 8051).

Some of the bit manipulative instructions used are


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

Microcontrollers and applications Lab B20ES0404 SEM-IV P a g e | 60


REVA University School of ECE

EXPERIMENT: 1

Microcontrollers and applications Lab B20ES0404 SEM-IV P a g e | 61


REVA University School of ECE

Dual DAC interface to generate Square waveform Triangular waveform

 Square waveform.
 Triangular waveform.

Block diagram:

Microcontrollers and applications Lab B20ES0404 SEM-IV P a g e | 62


REVA University School of ECE

1. A
Generate a square wave of 40% duty cycle on port 0 of 1 KHz using polling technique
In C language using DAC.

Algorithm for square wave generation


Let initial amplitude of the square wave be 2.5 v (7F) and frequency count 100. Output the
values 00h (OFF) and 7fh (on) values through P0.
If amplitude key is pressed them increase the voltage in steps of 0.15v (8).
If the frequency key is pressed increment the count in steps of 50.if the count exceeds 1000
reset it back to 100.
Every time amplitude and frequency change output the value through P0 and note the
waveform on CRO.

Waveform:

Program for square wave

#include <REG51xD2.H>
main ()
{
TMOD=0X01; // timer 0 mode 1
TR0=1; // start timer1
while (1)
{
P0=0x00;
TL0=0XD7;
TH0=0XFD; //set timer delay for 400us
while (TF0==0);
TF0=0;
P0=0xFF;
TL0=0X8F;
TH0=0XFE; //set timer delay for 600us
while (TF0==0);
TF0=0;
}
}

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 63


REVA University School of ECE

1. B
Generate a triangular wave on port 0 of 100Hz using polling technique in
C language using DAC.

Waveform:

#include <REG51xD2.H>
main ()
{
unsigned char i ;
P0=0x00;
TMOD=0X02; //timer 0 mode 2
TL0=0Xdc;
TH0=0Xdc;
TR0=1;
while (1)
{
P0=0x00;
for (i=0; i< 127; i++) // 127 SAMPLES
{
while (TF0==0); // wait till 39us
TF0=0;
P0=P0+2; // increment port value by 2
}
P0=0xff;
for (i=0; i< 127; i++) // 127 SAMPLES
{
While (TF0==0);
TF0=0;
P0=P0+0xfe; // increment port value by 2
}
}
}

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 64


REVA University School of ECE

1. C
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.
Waveform:

Program for ramp waveform

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

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 65


REVA University School of ECE

1. D Algorithm for for sine wave

Compute different step values (θ = 0°, 30°, 60°, 90°, 120°, 150°, 180°, 210°, 240°, 270°,
300°, 330°, 360°.) of sine using the equation.

Output the values thro P0.


More the steps smoother will be sine wave.

The value sent to DAC is = 128d

Wave form

Program for sine wave

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

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 66


REVA University School of ECE

1. E Algorithm for square wave generation


Let initial amplitude of the square wave be 2.5 v(7F) and frequency count 100.
Output the values 00h(OFF) and 7fh (on) values through P0.
If amplitude key is pressed them increase the voltage in steps of 0.15v(8).
If the frequency key is pressed increment the count in steps of 50.if the count exceeds
1000 reset it back to 100.
Every time amplitude and frequency change output the value thro P0 and note the waveform
on CRO.

Waveform:

Program for square wave

#include <REG51xD2.H>
sbit Amp =P3^3; /* port line to change amplitude */
sbit Fre =P3^2; /*port line to change frequency */
void delay(unsigned int X) /* delay routine */
{
for(;X>0;X--);
}
main()
{
unsigned char on = 0x7f,off=0x00;
unsigned int count = 100; while(1)
{
if (!Amp) /* if user choice to change amplitude */
{
while(!Amp); /*wait for key relese */
on+=0x08; /*increase the amplitude */
}
if(!Fre) /*if user choice is to change frequency */

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 67


REVA University School of ECE

{
if(count > 1000) /*if frequency exceeds 1000 reset */
count = 100;
while(!Fre); /*wait for key release */
count+=50;} /* increase the frequency */
P0=on; /*write amplitude to port */
P1=on;
delay(count);
P0=off; /*clear port */
P1=off;
delay(count);
}}

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 68


REVA University School of ECE

2. STEPERMOTOR
Stepper motor interfacing to 8051

A stepper motor translates electrical pulses into mechanical movement. A conventional motor
(AC/DC motor) shaft run freely, whereas the stepper motor shaft moves in a fixed increment &
hence the shaft position can be controlled precisely, say move by 4° & stop. Stepper motors are
used for position control applications such as dot matrix printers, disk drivers, robotics, etc.

There are two types of stepper motors (SM) --- permanent magnet SM & variable reluctance
SM, depending on the rotor type (whether permanent magnet is used or not). The permanent
magnet SM consists of a permanent magnet rotor) also called the shaft) surrounded by a stator
as shown in fig a. Generally the stator has 4 windings that are paired with a center-tapped
common as shown in fig b. The center tap allows a change of current direction in each of the
two coils, hence changing the direction of polarity in the stator poles which return leads to a
change in the direction of rotor rotation.

The rotation of the rotor in a SM along the winding energization sequence is shown in table.
Depending on the number of teeth on the stator & rotor, the stepper motor rotates fixed steps
per revolution. The commonly available number of steps for one revolution is 500, 200, 180,
144, 72, 48, and 24. The steps angle, i.e., the movement of a single step of a stepper motor is
calculated as . Say for 200 steps per revolution. The step angle is
per step.
Similarly step angle for 72steps per revolutionis .

For the 4-step switching sequence shown above, after four steps the same two windings will be
‘ON’, i.e., the sequence repeats after every 4 steps. After completing 4 steps, the rotor moves
only one tooth pitch. Hence if the rotor has 50 teeth (each teeth is one pole), the no of steps for
one complete revolution is 4 steps x 50 rotor teeth = 200 steps / revolution. Hence for smaller
step angle (i.e., more steps / revolution), the rotor must have more teeth.

To double the number of steps/revolution, say 400 instead of 200, we follow the 8-step
sequence shown in the table. Here, with this method, step size is half the original size & hence
the 8-step sequence is called ‘half stepping’.

Normal 4-step sequence.


Windings
P0.0- Step
P0.3 # A B C D Remarks

9 1 1 0 0 1 AD-ON; BC-off

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 69


REVA University School of ECE

C 2 1 1 0 0 AB-ON; CD-off

6 3 0 1 1 0 AD-OFF; BC-on

3 4 0 0 1 1 AB-OFF; CD-on

Half step sequence.

Port pins Windings


P0.0- P0.3

Step# A B C D Remarks Action

09 1 1 0 0 1 AD = 11; BC =00 Rotor aligned with AD mid Rotor


aligned with A
A = 1; BCD =000 Rotor aligned with AB mid Rotor
08 2 1 0 0 0
aligned with B Rotor aligned with
AB = 11; CD =00 BCmid
0C 3 1 1 0 0
B = 1; A is off
04 4 0 1 0 0
AD = 00; BC = 11
06 5 0 1 1 0

02 6 0 0 1 0

03 7 0 0 1 1

01 8 0 0 0 1

9 Sequence repeats after 9thstep

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 70


REVA University School of ECE

Interfacing

Now for interfacing the stepper motor to 8051, we need to connect the 4 winding leads to 4 port
pins say P0.0 to P0.3. Since the port pins do not have the sufficient current, to drive the stepper
motor winding (needs > 10mA) a driver such as ULN 2003 consists of 4 sets of power transistors
(to supply more current) and associated diodes (to provide the freewheeling path to each
winding when it is made up off). Here separate power supplies are used. One for 8051 and
another for ULN 2003 & stepper motor. Figure c shows the interfacing of 8051 to a stepper motor.

Stepper motor unlike DC motor rotates in steps.


Stepper motor has 4 coils which from the stator the stator and a central rotor. Rotation
depends on excitation of stator coils.

Step coil A coil B coil C coil D


1 0 0 0 1
2 1 0 0 0
3 0 1 0 0
4 0 0 1 0

Anyone these value forms the initial value. To get 360 ° revolution 200 steps are required. Step
angle = 360° /200 = 1.8°. (Difference between 2 teeth).

Block diagram:

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 71


REVA University School of ECE

2.A
STEPPER MOTOR INTERFACE CLOCK WISE AND ANTICLOCKWISE

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

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 72


REVA University School of ECE

2.B
Rotate the stepper motor shaft at an angle of 30° clockwise 3seconds, 60°
anticlockwise for 6 seconds and 75° clockwise for 12 seconds and stop the
motor.

#include <REG51xD2.H>
void delay (unsigned int y)
{
unsigned int x;
for (; y>0;y--)
for(x=6121;x>0; x--); //delay of 46ms
return;
}
main ()
{
unsigned char Val,i,j,k,m,n,sav[3]={4,8,10},sd[3]={1,2,4};
P0=0x00;
for (j=0; j<4; j++)
{
for (k=0; k<3;k++)
{
for (m=sav[k];m>0; m--) //dependent on the total number of steps
{
n=k & 0x01;
if (n==0) // clockwise rotation if the index value is odd number else anticlockwise
{
Val = 0x08;
for (i=0; i<4;i++)
{
P0 = Val;
Val=Val>>1;
delay (sd[k]); //stepper delay dependent on the total time delay required
}
}
else
{
Val = 0x01;
for(i=0;i<4;i++)
{
P0 = Val;
Val=Val<<1;
delay (sd[k]); //stepper delay dependent on the total time delay required
}
}
} }}
while (1);
}

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 73


REVA University School of ECE

3. DC MOTOR

Block diagram:

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 74


REVA University School of ECE

Program for DC motor


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

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 75


REVA University School of ECE

4. 3X8 HEXKEYBOARD

Block diagram:

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 76


REVA University School of ECE

Program for 3X8 hex keypad.


#include <reg51xd2.h>
#include "lcd.h"
unsigned char getkey();
main()
{
unsigned char key;
InitLcd(); /* initialise
lcd*/
WriteString("key pressed=");
/* display msgonlcd */
while(1)
{
GotoXY(12,0); /* set cursor
position */
key=getkey(); /* call getkey
method*/
}
}
unsigned char getkey ()
{
unsigned char i,j,k,indx, t=0,
m;
P0=0x0ff;
P1=0x0ff; P2=0x00;
indx =0x00; /* index for
storing the first value of scan
line*/
i=0xfe;
for(m=0;m<3;m++) /* for 3
scan lines*/
{
P2=i; /* write data to scan
line*/
if(P0==0xff) /* if key press
is true*/
{
i<<=1;
Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 77
REVA University School of ECE

i|=1;
indx += 8; /* if no key
pressed increment index */
continue;
}
t=P0; /* read read lines
connected to p0*/
t =~t;
while
(P0!=0xff);
for(j=0;j<8;j++) /* check for 8
lines*/
{
t >>=1;
if (t==0) /* if get pressed
key*/
{
k=indx+j; /* Display
that by converting to Ascii*/
t=k>>4;
t +=0x30;
WriteChar(t); /* Write upper
nibble */
t = k & 0x0f;

if(t > 9) t+=0x37;


else t+=0x30;
WriteChar(t); /* write lower
nibble */
return(indx+j); /* Return
index of the key pressed */
}
}
}
}

Note: Add hex keypad header file & library file (lcd.h & lcd-lib- it’s available in desktop) into
the Source Group
Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 78
REVA University School of ECE

5. Count the incoming pulses using counters

#include <REG51xD2.H>
#include "lcd.h"
Unsigned char getpulse();

void main(void)
{
unsigned char key;
InitLcd ();
WriteString ("No. Pulses ="); // display the string on the LCD
TH0=0;
TL0=0;
TMOD= 0X0D; //timer0 mode2 counter mode
TR0=1; // start timer0

While (1)
{
GotoXY(12,0); /* set cursor position */
key=getpulse();
}
}
unsigned char getpulse() //Display data on LCD as in the keypad program
{
unsigned char t;
P0=0x0ff;
P1=0x0ff;
P2=0x00;
t=TL0>>4;
if (t > 9)
t+=0x37;
else
t +=0x30;
WriteChar(t);
t = TL0 & 0x0f;
if (t > 9)
t+=0x37;
else
t+=0x30;
WriteChar(t);
return(TL0);
}

Note: Add hex keypad header file & library file (lcd.h&lcd-lib- it’s available in desktop)
into the Source Group

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 79


REVA University School of ECE

Output:

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 80


REVA University School of ECE

Challenge Experiment:

• Interface 8051 to control an elevator.

• Interface 8051 to calculator.

Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 81

You might also like