Professional Documents
Culture Documents
ECE MC Lab Manual 2023
ECE MC Lab Manual 2023
ECE MC Lab Manual 2023
Communication Engineering
Program
B.Tech. in ECE
Microcontroller
and Applications
Lab
LABORATORY MANUAL
B20EN0407
IV Semester
2021-25
www.reva.edu.in
Vision of the University
‘’REVA University aspires to become an innovative university by developing excellent human
resources with leadership qualities, ethical and moral values, research culture and innovative skills through
higher education of global standards”
PEO -1: To have successful professional careers in industry, government, academia, and military as innovative
engineers.
PEO -2: To successfully solve engineering problems associated with the lifecycle of Electronics and
Communication Systems either leading a team or as a team member.
PEO -3: To continue to learn and advance their careers through activities such as participation in professional
organizations, attainment of professional certification for lifelong
learning and seeking higher education.
PEO -4: To be active members ready to serve the society locally and internationally and will undertake
entrepreneurship for the growth of economy and to generate employment.
Program Outcomes (POs)
PSO-1: Isolate and solve complex problems in the domains of Electronics and Communication Engineering
using latest hardware and software tools and technologies, along with analytical and managerial skills to arrive
at cost effective and optimum solutions either independently or as a team.
PSO-2: Implant the capacity to apply the concepts of electronics, communications, signal processing, VLSI,
embedded systems, etc. in the design, development and implementation of application oriented engineering
systems.
PSO-3: Design, Model, Analyze and Build Electronics and Communication Systems to solve real life and
industry problems.
Contents
Sl. No TITLE PAGE No.
Introduction To Microcontroller-8051 6
Introduction to Keil tool 24
sample Programs
PART-A Experiment.1 30
Data Transfer Instructions: Block Data Transfer and Exchange
between internal and external Data memory with and without
1 overlap, Sorting, largest and smallest number in an array.
Experiment.2 46
2 Arithmetic Instructions: 32-bit multi-precision Addition,
Subtraction, Multiplication of 2 16-bit numbers and Division
(16-bit by 8 bit)
Experiment.3 53
Logical Instructions: 8x8 multiplication using shift Add technique.
3 ASCII to packed BCD and Vice versa, Code Conversions. Exchange
Two numbers without the use of 3rd location. Implementation of
Boolean expressions (Bit Manipulation)
Experiment.4 57
4 Timers: Wave form generation with varying Duty Cycle using
Interrupt and Polling Techniques
Experiment.5 62
5 Serial Communication: Serial data transmission with Polling and
Interrupt technique (Regular and Look up table)
PART-B
8051 interfacing with the different Modules
Experiment.1 68
6 Interfacing DAC to 8051 and generating the following wave forms:
Triangle wave
Square wave with varying amplitude and
frequency
Sine wave
Ramp wave
7 Experiment.2 71
Stepper control interface to 8051 and
8 Experiment.3 76
DC motor control interface to 8051.
REVA University School of ECE
9 Experiment.4 78
Interfacing Alphanumeric LCD panel and Hex keypad input to 8051.
10 Experiment.5 81
Count the incoming pulses using counters.
Challenge Experiment 83
SYLLABUS
Sub Code: B20EN0407 IA Marks: 20
Hrs/Week: 03 Exam Hours: 03
Total Hours: 42 Exam Marks: 30
Section-A
(Assembly Language Programming)
1. Data Transfer Instructions: Data Transfer between internal and external RAM
with and without overlap, Sorting, largest and smallest number in an array and
exchange.
2. Arithmetic Instructions: 32 bit multi-precision Addition, Subtraction,
Multiplication of 2 16 bit numbers and Division (16-bit by 8 bit).
3. Logical Instructions: 8x8 multiplication using shift Add technique. ASCII to
packed BCD and vice versa. Exchange 2 numbers without the use of 3rd
location. 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).
Section-B
(Embedded C Programming)
1. Interfacing DAC to generate various waveforms with output voltage
varying between -12V to 12V with Amplitude and Frequency control.
2. Stepper motor interfacing by controlling the steps and direction.
3. DC Motor speed control using external interrupt.
4. Display the ASCII value of Key pressed on LCD.
5. Count the incoming pulses using counters.
CHALLENGING EXPERIMENT
1. Elevator interface to 8051.
2. Simple Calculator using 6 digit seven segment displays and Hex
Keyboard interface to 8051.
Course Objectives:
Course outcomes:
After completion of the course a student will be able to: \Analyze a given problem and
design a suitable embedded system using microcontroller 8051.
Introduction to Microcontrollers:
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.
Whereas the microcontroller integrates the entire computer in a single chip. Not only does it have
the memory embedded in it but also has input and output ports plus peripherals such as timers and
converters. All this can be handled with a single touch.
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 chip to 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 bottom of 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.
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:
8) Harvard Memory Architecture – It has 16 bit Address bus (each of RAM and ROM) and 8
bit Data Bus.
9) 8051 can execute 1 million one cycle instructions per second with a clock frequency of
12MHz.
This microcontroller is also called as “System on a chip” because it has all the features on a
single chip
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.
INTERUPTS
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.
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.
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.
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).
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.
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.
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.
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.
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.
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.
Sample programs
Write result of the following instructions below for each example
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
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
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, ;(a)=
#66h 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
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
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
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
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:
After Execution: 05 location D: 40H are filled up with data from 30H.
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
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.
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:
After Execution: 10 location D: 3AH are filled up with data from 3AH.
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.
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:
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
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
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.
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,#05H //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.
ORG 00H
SJMP 30H
ORG 30H
MOV R0,#05H //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.
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:
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
RESULT:
Before Execution:
After Execution:
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
RESULT:
Before Execution:
After Execution:
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
RESULT:
Before Execution:
After Execution:
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:
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:
ORG 00H
SJMP 30H
ORG 30H
XRL A, 31H
XRL 31H, A
XRL A, 31H
HERE: SJMP
HERE END
RESULT:
Before Execution:
After Execution:
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:
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
RESULT:
ON PERIED:
OFF PERIED:
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
RESULT:
ON PERIED:
OFF PERIED:
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
RESULT:
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
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:
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
RESULT:
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
RESULT:
HARDWAREINTERFACING
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).
EXPERIMENT: 1
Square waveform.
Triangular waveform.
Block diagram:
Waveform:
#include <REG51xD2.H>
main ()
{
TMOD=0X01; // timer 0 mode 1
TR0=1; // start timer1
while (1)
{ P0=0x0
0;
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;
}
}
1. 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=0x0
0;
for (i=0; i< 127; i++) // 127 SAMPLES
{
while (TF0==0); // wait till
39us TF0=0;
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:
#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;
}}}
Compute different step values (θ = 0°, 30°, 60°, 90°, 120°, 150°, 180°, 210°, 240°, 270°,
300°, 330°, 360°.) of sine using the equation.
Wave form
#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];
}}}
Waveform:
#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 */
{
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);
}}
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’.
9 1 1 0 0 1 AD-ON; BC-off
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
02 6 0 0 1 0
03 7 0 0 1 1
01 8 0 0 0 1
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 astepper
motor.
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:
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);
}
}
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 B20EN0407 SEM-IV
P a g e | 75
REVA University School of ECE
3. DC MOTOR
Block diagram:
4. 3X8 HEXKEYBOARD
Block diagram:
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;
Note: Add hex keypad header file & library file (lcd.h&lcd-lib- it’s available in desktop) into
the Source Group
#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
Output:
Challenge Experiment:
Follow us on
/REVAUniversity