Mechatronics - MC Lab Manual
Mechatronics - MC Lab Manual
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.
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:
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
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).
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
Introduction to Microcontrollers:
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.
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:
AT89C51ED2 Microcontroller
Description
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, #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
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
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,#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.
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.
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:
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
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:
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
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:
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
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
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:
HARDWARE INTERFACING
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:
1. A
Generate a square wave of 40% duty cycle on port 0 of 1 KHz using polling technique
In C language using DAC.
Waveform:
#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;
}
}
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
}
}
}
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
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.
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);
}
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
Microcontrollers and applications Lab B20ES404 SEM-IV P a g e | 78
REVA University School of ECE
#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: