Micro Controller Lab Manual 2020
Micro Controller Lab Manual 2020
Micro Controller Lab Manual 2020
LABORATORY
( 18EEL57 - For V Semester EEE )
Prepared by
Mrs. D Beula
Mrs. R Subha
To stimulate in students a spirit of inquiry and desire to gain knowledge and skills to
meet the changing needs that can enrich their lives.
To provide opportunity and resources for developing skills for employability and
entrepreneurship, nurturing leadership qualities, imbibing professional ethics and
societal commitment.
To create an ambience and nurture conducive environment for dedicated and quality
staff to up-grade their knowledge & skills and disseminate the same to students on a
sustainable long term basis.
Facilitate effective interaction with the industries, alumni and research institutions.
2
PROGRAM OUTCOMES
Engineering Graduates will be able to:
3
PROGRAM SPECIFIC OUTCOMES
Engineering Graduates will be able to:
PSO1: Comprehend the breadth and depth of electrical and electronics engineering and apply
their knowledge in the fields of power system, power electronics and drives.
PSO2: Enhance their career by adapting contemporary tools and techniques to augment
electrical and electronic systems.
PEO1: Graduates of the program will have a successful career with sound base in domain
specific engineering skills.
PEO2: Graduates of the program will be capable of succeeding in diverse engineering fields
providing innovative solution with ethical and social responsibility.
PEO3: Graduates of the program will continue to pursue professional development and
engage in life-long learning.
4
B.E ELECTRICAL AND ELECTRONICS ENGINEERING(EEE)
CHOICE BASED CREDIT SYSTEM (CBCS)
SEMESTER -V
MICROCONTROLLER LABORATORY - 1
Subject Code 18EEL57 IA Marks 40
Number of Practical Hours/Week 03 Exam Hours 03
Total Number of Practical Hours 42 Exam Marks 60
Credits - 02
Course objectives:
To explain writing assembly language programs for data transfer, arithmetic, Boolean and
logical instructions.
To explain writing assembly language programs for code conversions.
To explain writing assembly language programs using subroutines for generation of delays,
counters, configuration of SFRs for serial communication and timers.
To perform interfacing of stepper motor and dc motor for controlling the speed.
To explain generation of different waveforms using DAC interface. ∎
Sl. no Experiments
Course outcomes:
At the end of the course the student will be able to:
Write assembly language programs for data transfer, arithmetic, Boolean and logical instructions.
Write ALP for code conversions.
Write ALP using subroutines for generation of delays, counters, configuration of SFRs for
serial communication and timers.
Perform interfacing of stepper motor and dc motor for controlling the speed.
Generate different waveforms using DAC interface.
Work with a small team to carryout experiments using microcontroller concepts and prepare reports that
present lab work. ∎
5
B.E ELECTRICAL AND ELECTRONICS
ENGINEERING(EEE) CHOICE BASED CREDIT
SYSTEM (CBCS) SEMESTER -V
15EEL57 MICROCONTROLLER LABORATORY – 1(continued)
Conduct of Practical Examination:
1. All laboratory experiments are to be included for practical examination.
2. Breakup of marks and the instructions printed on the cover page of answer script to be strictly
adhered by the examiners.
3. Students can pick one experiment from the questions lot prepared by the examiners.
4. Change of experiment is allowed only once and 15% Marks allotted to the procedure part to be made zero
Learning beyond the syllabus: To acquire a wide variety of skills and to develop society friendly
applications mini projects can be practiced by referring to “Microcontroller Based Projects” Second
Edition, An EFY (Electronics For You) Enterprise Pvt Ltd, 2013.
6
18EEL57 MICROCONTROLLERS LAB
LIST OF EXPERIMENTS
S. No Experiment Page No.
CYCLE 1 - Assembly language programs with 8051
1 Data Transfer Instructions
A. Block Data Transfer Without Overlap
B. Block exchange
C. Sorting – Ascending and Descending
D. Finding Largest and Smallest number in an array
2 Arithmetic Instructions
A. Arithmetic operations
B. Finding Square of a number using LUT
C. Finding Cube of an eight bit number
3 Counters
A. Hexadecimal up counter
B. Hexadecimal down counter
C. Decimal up counter
D. Decimal down counter
E. Generation of one second delay using on chip timer
4 Boolean and Logical Instructions
A. Program illustrating bit manipulations
B. Byte level logical operations
C. Bit level logical operations
5 Code Conversion Programs
A. Decimal to hexadecimal code conversion
B. Hexadecimal to decimal code conversion
C. Decimal to ASCII code conversion
D. ASCII to decimal code conversion
E. Hexadecimal to ASCII code conversion
F. ASCII to hexadecimal code conversion
CYCLE 2 - Interfacing with 8051 (C Programming)
1 Stepper Motor Interface
7
2 DC Motor Interface
3 Alphanumeric LCD Panel Interface
4 Generation of Waveforms using DAC Interface
A. Square wave of 1KHz having 50% duty cycle.
B. Square wave of 60% duty cycle
C. Triangular wave
D. Ramp wave
E. Sine wave
5 Elevator Interface
Beyond Syllabus Experiments
1 Interfacing LCD with PIC 16F877A Microcontroller
Temperature measurement using PIC 16F877A Microcontroller
2
and Sensor
8
CYCLE 1
Assembly language programs with 8051
9
Program
Mnemonics Comments
ORG 0000H
MOV DPH,#80H
MOV R0,#35H
MOV R1,#41H
MOV R3,#0AH
MOVX A,@DPTR
MOV DPL, R1
MOVX @DPTR,A
INC R0
INC R1
END
10
1. DATA TRANSFER INSTRUCTIONS
Aim
Write an assembly language program to transfer 10 bytes of data from location 8035h
to location 8041h (without overlap).
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.
Result:
11
Program
Mnemonics Comments
ORG 0000H
MOV R2,#05H
MOV R0,#30H
MOV R1,#40H
XCH A,@R1
MOV @R0,A
INC R0
INC R1
DJNZ R2,REPEAT
END
12
B. BLOCK EXCHANGE
Aim
Write an assembly language program to exchange 5 bytes of data at internal RAM
locations 30h and 40h
Algorithm
1. Get the count of the number of elements in the blocks
2. Initialize pointer1 to point to the starting address of the block1
3. Initialize pointer2 to point to the starting address of the block2
4. Exchange the data pointed to by pointer1 and the data pointed to by pointer2
through a temporary variable
5. Increment pointer1 and pointer2. Decrement count. Go to step 4 if count is not
equal to 0.
Result:
13
Program
Mnemonics Comments
ORG 0000H
MOV R0,#05
MOV A,R0
MOV R1,A
MOV B, A
INC DPTR
MOVX A,@DPTR
CLR C
MOV R2, A
SUBB A, B
MOV A,B
MOVX @DPTR,A
DEC DPL
14
C. SORTING – ASCENDING AND DESCENDING
Aim
Write an assembly language program to sort an array of n = 6 bytes of data in
ascending and descending order stored from location 9000h (Use bubble sort
Algorithm).
Algorithm
1. Store the elements of the array from the address 9000h
2. Initialize a pass counter with array size-1 count (for number of passes).
3. Load compare counter with pass counter contents & initialize DPTR to point
to the start address of the array (here 9000h).
4. Store the current and the next array elements pointed by DPTR in registers B
and r2 respectively.
5. Subtract the next element from the current element.
6. If the carry flag is set (for ascending order) then exchange the 2 numbers in
the array.
7. Decrement the compare counter and repeat through step 4 until the counter
becomes 0.
8. Decrement the pass counter and repeat through step 3 until the counter
becomes 0.
Ascending Order
Input Before Execution
15
Mnemonics Comments
MOV A,R2
MOVX @DPTR,A
INC DPTR
DJNZ R0,L1
END
16
Descending Order
Input Before Execution
Result:
17
Program
Mnemonics Comments
ORG 0000H
MOV R3,#05
MOV DPTR,#9100H
MOVX A,@DPTR
MOV R1,A
MOVX A,@DPTR
CLR C
MOV R2,A
SUBB A,R1
MOV A,R2
MOV R1,A
18
D. FINDING LARGEST AND SMALLEST NUMBER IN AN ARRAY
Aim
Write an assembly language program to find the largest element in a given string of 6
bytes at location 9100h. Store the largest element at location 910Ah.
Algorithm
1. Store the elements of the array from the address 9100h
2. Store the length of the array in r3 and set it as counter.
3. DPTR is loaded with starting address of the array.
4. Store the first number of the array in r1 (r1 is assigned to hold the largest
number).
5. Increment DPTR.
6. Subtract the number pointed by DPTR from the contents of r1 (to compare
whether the next array element is larger than the one in r1).
7. If the element pointed by DPTR is larger then load the larger number into r1.
8. Decrement the counter and repeat steps through 5 until the counter becomes 0.
9. Store the largest number in r1 in address 910Ah
Largest Number
Input before execution
Address 910A
Data
19
Mnemonics Comments
MOV A,R1
MOVX @DPTR,A
END
20
Smallest Number
Input before execution
Address 910A
Data
Result:
21
Program
Mnemonics Comments
ORG 0000H
MOV R1,40H
MOV A,41H
MOV B,42H
CJNE R1,#00,CKSUB
ADD A,B
MOV B,#00
JNC SKIP
MOV B,#01H
CLR C
SUBB A,B
MOV B,#00
JNC SKIP1
MOV B,#0FFH
22
2. ARITHMETIC INSTRUCTIONS
A. ARITHMETIC OPERATIONS
Aim
Write an ALP to perform the following: If x=0 perform addition(a+b); else if x=1
perform subtraction (a-b); else if x=2 perform multiplication (a*b); else if x=3
perform division (a/b), where a & b are eight bit numbers. Condition to perform the
specific operation and the two bit numbers are stored at RAM location starting from
40h.
Algorithm
1. Store the condition x in r1.
2. Load the first and second numbers to A and B registers respectively
3. Compare the contents of r1 and perform the operations add, sub, etc
accordingly.
4. Store the result present in A and B registers to the appropriate memory
locations.
23
Mnemonics Comments
MUL AB
SJMP LAST
DIV AB
SJMP LAST
MOV B,#00
MOV 44H,B
END
24
Multiplication (without higher byte) Multiplication (with higher byte)
Input before Output after Input before Output after
execution execution execution execution
40h 43h (LB) 40h 43h (LB)
41h 44h (HB) 41h 44h (HB)
42h 42h
Result:
25
Program
Mnemonics Comments
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR
MOV DPTR,#8050H
MOVC A,@A+DPTR
MOV DPTR,#9001H
MOVX @DPTR,A
ORG 0050H
DB 00H
DB 01H
DB 04H
DB 09H
DB 10H
DB 19H
DB 24H
26
B. FINDING SQUARE OF A NUMBER USING LUT
Aim
Write an ALP to compute the square of an eight bit number(00h to 0Fh) stored in
external memory location 9000h. Store the result at location 9001h.
Algorithm
1. Store the number in A from the address 9000h
2. Initialize the DPTR to point to the starting of the LUT
3. Fetch the square from the LUT address DPTR+A
4. Store the result in location 9001
Sample 1:
Input before execution Output after Execution
Sample 2:
Input before execution Output after Execution
27
Mnemonics Comments
DB 31H
DB 40H
DB 51H
DB 64H
DB 79H
DB 90H
DB 0A9H
DB 0C4H
DB 0E1H
END
28
Result:
29
Program
Mnemonics Comments
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR
MOV R0,A
MOV B,A
MUL AB
PUSH B
MOV B,A
MOV A,R0
MUL AB
INC DPTR
MOVX @DPTR,A
MOV A,B
MOV R2,A
POP B
MOV A,R0
30
C. FINDING CUBE OF AN EIGHT BIT NUMBER
Aim
Write an ALP to compute the cube of an eight bit number stored in external
memory location 9000h. Store the result at locations 9001, 9002 and 9003h.
Algorithm
1. Store the eight bit number x in A, R0 & B registers.
2. Multiply A and B registers to obtain the 16 bit square(SQH and SQL)
3. The high part of the square result (SQH) is stored on the stack.
4. Multiply the low part of the square result (SQL) with x (partial cube result).
5. Store the low part of the above result at 9001h & the high part in R2.
6. Retrieve the high part of the square result (SQH) stored on the stack &
multiply with X.
7. Add the low part of the above result (SQH*X) with R2 and store in 9002h.
8. Add the high part (SQH*X) with the resulting carry and store in 9003.
Sample 1:
Input before execution Output after Execution
Sample 2:
Input before execution Output after Execution
31
Mnemonics Comments
MUL AB
ADD A,R2
MOVX @DPTR,A
MOV A,B
INC DPTR
MOVX @DPTR,A
END
32
Result:
33
Program
Mnemonics Comments
ORG 0000H
MOV A,#00H
ACALL DELAY
INC A
JNZ BACK
DJNZ R1,LOOP1
RET
END
34
3. COUNTERS
A. HEXADECIMAL UP COUNTER
Aim
Write an ALP for eight bit hexadecimal up counter and display the count value in
data field.
Algorithm
1. Move 00 to A register
2. Call the display subroutine to display in the data field.
3. Call the delay subroutine, in delay program move FFH to registers r1 & r2
loop and decrement until 0.
4. Increment A register
5. Go to step 2 till A=00.
Result:
35
Program
Mnemonics Comments
ORG 0000H
MOV A,#0FFH
DEC A
ACALL DELAY
JNZ BACK
DJNZ R1,LOOP1
RET
END
36
B. HEXADECIMAL DOWN COUNTER
Aim
Write an ALP for eight bit hexadecimal down counter and display the count value in
data field.
Algorithm
1. Move 00 to A register
2. Decrement A register
3. Call the display subroutine to display in the data field.
4. Call the delay subroutine, in delay program move FFH to registers r1 & r2
loop and decrement until 0.
5. Go to step 2 till A=00.
Result:
37
Program
Mnemonics Comments
ORG 0000H
MOV A,#00H
ACALL DELAY
ADD A,#01H
DA A
JNZ BACK
DJNZ R1,LOOP1
RET
END
38
C. DECIMAL UP COUNTER
Aim
Write an ALP for decimal up counter and display the count value in data field.
Algorithm
1. Move 00 to A register
2. Call the display subroutine to display in the data field.
3. Call the delay subroutine, in delay program move FFH to registers r1 & r2
loop and decrement until 0.
4. Add 1 to accumulator and decimal adjust accumulator to convert the sum to
decimal.
5. Go to step 2 till A=00.
Result:
39
Program
Mnemonics Comments
ORG 0000H
MOV A,#99H
ACALL DELAY
ADD A,#99H
DA A
JC BACK
DJNZ R1,LOOP1
RET
END
40
D. DECIMAL DOWN COUNTER
Aim
Write an ALP for decimal down counter and display the count value in data field.
Algorithm
1. Move 99 to A register
2. Call the display subroutine to display in the data field.
3. Call the delay subroutine, in delay program move FFH to registers r1 & r2
loop and decrement until 0.
4. Add 99 to accumulator (subtraction by 1) and decimal adjust accumulator to
convert the sum to decimal.
5. Go to step 2 till A=00.
Result:
41
Program
Mnemonics Comments
ORG 0000H
MOV TMOD,#01H
MOV A,#00H
MOV R1,#0EH
CLR TF0
CLR TR0
DJNZ R1,L1
INC A
JNZ L2
END
42
E. GENERATION OF ONE SECOND DELAY USING ONCHIP TIMER
Aim: Write an ALP to generate a 1 second delay continuously using the on chip
timer.
Algorithm
1. Move 00 to A register
2. Set up timer0 in mode 1 operation
4. Load register R1 with 0Eh
5. Display A in the data field
6. start the timer and wait for overflow
6. decrement R1 and go to step 5 till R1 becomes zero.
7. Increment A register and go to step 4 till A becomes 00.
Timer Calculations:
Timer 1 is configured in mode 1
Maximum overflow time = maximum count x12/oscillator frequency
= 65536 x 12/(11.0596x106)
= 0.071 Sec
To get a delay of 1 sec, the timer must overflow 14 times
Result:
43
Program
Mnemonics Comments
ORG 0000H
MOV A, 20H
MOV R1,A
MOV A,21H
CJNE R1,#0,CKOR
ANL A, 22H
SJMP END1
ORL A, 22H
SJMP END1
XRL A, 22H
SJMP END1
OTHER: CLR A
END
44
4. BOOLEAN AND LOGICAL INSTRUCTIONS
Algorithm
1. Point to the data RAM register 20h and store the condition x.
2. Point to 21h and 22h and move the first number to A register.
3. Compare the contents of r1 and perform the operations accordingly.
4. The result will be stored in 23H register.
AND Operation
Input before execution Output after Execution
OR Operation
Input before execution Output after Execution
45
46
XOR Operation
Input before execution Output after Execution
Result:
47
Program
Mnemonics Comments
ORG 0000H
MOV R0,20H
CJNE R0,#0,CK1
MOV C,08H
ANL C,10H
SJMP LAST
MOV C,0FH
ORL C,17H
SJMP LAST
CPL 0FH
MOV C,0FH
SJMP LAST
CK3: CLR C
END
48
B. BIT LEVEL LOGICAL OPERATIONS
Algorithm
1. Move the condition X (from 20h location) into R0 register.
2. If X=0; then move LSB bit of 21h to carry flag and ‘AND’ Carry flag with
LSB bit of 22h. Go to step5
3. If X=1; then move MSB bit of 21h to carry flag and ‘OR’ Carry flag with
MSB bit of 22h. Go to step5
4. If X=0; then complement MSB bit of 21h and move it to carry flag. Go to
step5
5. Store Carry flag at MSB bit of 23h location.
AND Operation
Input before execution Output after Execution
OR Operation
Input before execution Output after Execution
49
50
NOT Operation
Input before execution Output after Execution
Result:
51
Program
MNEMONICS COMMENTS
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
CLR C
SUBB A,R0
JZ EQUAL
JNC BIG
SETB 7FH
SJMP END1
SJMP END1
52
C. PROGRAM ILLUSTRATING BIT MANIPULATIONS
Aim : Write an ALP to compare the 2 nos. Reflect your result as:
If NUMI<NUM2, SET LSB of data RAM 2F (bit address 78H)
IF NUM1>NUM2, SET MSB OF 2F(7FH).
If NUM1 = NUM2 Clear both LSB & MSB of bit addressable memory
location 2Fh. Two eight bit numbers NUM1 & NUM2 are stored in external memory
locations 9000h & 9001h respectively.
Algorithm
1. Store the elements of the array from the address 9000h
2. Move the first number in r0 and the second number in register A respectively
3. Clear carry flag and subtract the two numbers, if the carry flag is 0(if the nos
are equal), Clear both LSB & MSB of bit addressable memory location 2Fh.
53
MNEMONICS COMMENTS
CLR 7FH
END1: NOP
END
54
NUM1 = NUM2
Input before execution Output after Execution
Result:
55
Program
Mnemonics Comments
ORG 0000H
MOVX A,@DPTR
ANL A,#0FH
MOV R0,A
MOVX A,@DPTR
ANL A,#0F0H
SWAP A
MOV B,#0AH
MUL AB
ADD A,R0
MOV P1,A
END
56
5. CODE CONVERSION PROGRAMS
Algorithm
1. Get the decimal number in the range 00 to 99 as input
2. Separate the higher and lower nibble of the two digit number
3. Bring the higher nibble to the ones position and multiply it by 0Ah
4. Add the result to the lower nibble and store the result
Sample 1:
Input before execution Output after Execution
Sample 2:
Input before execution Output after Execution
Result:
57
Program
Mnemonics Comments
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR
MOV B,#64H
DIV AB
MOV P1,A
MOV A,B
MOV B,#0AH
DIV AB
SWAP A
ADD A,B
MOV P2,A
END
58
B. HEXADECIMAL TO DECIMAL CODE CONVERSION
Algorithm
1. Get the hex number in the range 00 to FFh as input
2. Divide the number by 64h and store the quotient
3. Divide the remainder by 0Ah and store the quotient and remainder
4. Bring the quotient to the tens position and add the remainder to it.
5. The quotient from the first division and the result of step4 form the 16-bit
decimal number
Sample 1:
Input before execution Output after Execution
Sample 2:
Input before execution Output after Execution
Result:
59
Program
Mnemonics Comments
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR
ANL A,#0FH
ADD A,#30H
MOV P1,A
MOVX A,@DPTR
ANL A,#0F0H
SWAP A
ADD A,#30H
MOV P2,A
END
60
C. DECIMAL TO ASCII CODE CONVERSION
Aim: Write an ALP to convert a decimal number to its equivalent Ascii code and
display the result in the address field.
Algorithm
1. Get the decimal number in the range 00 to 99 as input
2. Separate the higher and lower nibble of the two digit number
3. Add 30h to the lower nibble and store the result
4. Bring the higher nibble to the ones position, add 30h to it and display the
result.
Sample 1:
Input before execution Output after Execution
Sample 2:
Input before execution Output after Execution
Result:
61
Program
Mnemonics Comments
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR
CLR C
SUBB A,#30H
MOV P1,A
END
62
D. ASCII TO DECIMAL CODE CONVERSION
Aim: Write an ALP to convert a Ascii to its equivalent decimal number and display
the result in the data field.
Algorithm
1. Get the Ascii code in the range 30 to 39 as input
2. Clear carry bit
3. Subtract with borrow 30h from the input and display the result
Sample 1:
Input before execution Output after Execution
Sample 2:
Input before execution Output after Execution
Result:
63
Program
Mnemonics Comments
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR
ANL A,#0FH
ACALL SUB
MOV P1,A
MOVX A,@DPTR
ANL A,#0F0H
SWAP A
ACALL SUB
MOV P2,A
64
E. HEXADECIMAL TO ASCII CODE CONVERSION
Aim: Write an ALP to to convert a 8bit Hexa-decimal number into ASCII Code and
store the result in the address field.
Algorithm
1. Move the hexadecimal data to be converted to accumulator.
2. Get the lower nibble & call ASCII subroutine
3. Store the converted ASCII value
4. Get the higher nibble & call ASCII routine
5. Store the converted ASCII value
ASCII subroutine
1. If digit greater than 09,(for A-F) add 07h & 30h
2. Else (i.e., for 0-9) add only 30h
Sample 1:
Input before execution Output after Execution
Sample 2:
Input before execution Output after Execution
65
Mnemonics Comments
SUB: CLR C
CJNE A,#0AH,LOOP
SJMP LOOP2
LOOP: JC LOOP1
RET
END
66
Result:
67
Program
Mnemonics Comments
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR
CLR C
CJNE A,#41H,LOOP
SJMP LOOP1
LOOP: JC SKIP
CLR C
SKIP: CLR C
SUBB A,#30H
MOV P1,A
END
68
F. ASCII TO HEXADECIMAL CODE CONVERSION
Aim: Write an ALP to convert a 8bit ASCII Code into Hexa-decimal number and
store the result in the data field.
Algorithm
1. Move the ASCII character to be converted to accumulator.
2. If character is greater than 41h,(for A-F), then subtract 07h & 30h
3. Else (i.e., for 0-9) subtract only 30h
4. Display the converted hexadecimal number into data field .
Sample 1:
Input before execution Output after Execution
Sample 2:
Input before execution Output after Execution
Result:
69
70
CYCLE 2
Interfacing with 8051 (C Programming)
71
Program
#include <Reg51.h>
#define phasea 0x0D
#define phaseb 0x0E
#define phasec 0x07
#define phased 0x0B
void delay(unsigned int);
void main( )
{
P2=0x00;
while(1)
{
P2=phasea;
delay(5000);
P2=phaseb;
delay(5000);
P2=phasec;
delay(5000);
P2=phased;
delay(5000);
}
}
Note :
1. For changing the speed change the delay time
2. For changing the direction, change the excitation sequence order
72
1. STEPPER MOTOR INTERFACE
Aim: To interface a stepper motor and to write a C program to control its speed and
direction of rotation.
Hardware interfacing
• Stepper motor unlike DC motor rotates in steps.
• Stepper motor has 4 coils which forms the stator and a central rotor.
• Rotation depends on excitation of stator coils.
PS
PS
8051µC
P2 Stepper
Motor Stepper
FRC 26pin
Interface Motor
Cable
Card
Algorithm
• Configure P2 as output.
• For anticlockwise motion excitation sequence to motor coils are 0B,07,0E,0D.
Output through P2 with delay.
• For clockwise motion excitation sequence to motor coils are 0D,0E,07.0B.
Output through P2 with delay.
Result:
73
Program
#include<reg51.h>
sbit ibit=P2^0;
void msdelay(unsigned int value);
void main( )
{
ibit=0;
while(1)
{
ibit=0;
msdelay(25)
ibit=1;
msdelay(75)
}
}
Note: To change the speed of the DC motor, change the ON time and OFF time
of the PWM pulse.
74
2. DC MOTOR INTERFACE
Aim: To interface a DC motor and to write a C program to control its speed using
PWM.
Algorithm
1. Configure P2 bit 0 as output pin
2. Output value 00 through P2.1.Call delay to remain ON for 25ms.
3. Output value 01 through P2.1. Call delay to remain 75ms
4. Repeat step 2&3 continuously.
Result:
75
Program
#include<reg51.h>
sbit rs = P2^4; //register select rs=0
sbit rw = P2^5; //read/write
sbit en = P2^6; //enable pin
void lcdcmd(unsigned char);
void lcddata();
void tdelay(unsigned int);
unsigned char msg[16]={"ELECTRICAL DEPT."};
unsigned char x,temp,value;
void main()
{
lcdcmd(0x38); //initialize lcd lines 5x7 matrix
lcdcmd(0x0e); //display on and cursor on
lcdcmd(0x06); //shift cursor right
lcdcmd(0x01); //clear display
lcdcmd(0x80); //starting address of line1
tdelay(5);
for(x=0;x<8;x++)
{
temp=msg[x];
lcddata ();
}
lcdcmd(0xC0); //starting address of line2
for(x=8;x<16;x++)
{
temp=msg[x];
lcddata();
}
}
76
3. ALPHANUMERIC LCD PANEL INTERFACE
LCD details:
The LCD used is an alphanumeric 16*1 LCD.
P0.0-P0.7 are 8 bit data lines.
P2.4, P2.5 & P2.6 are RS, R/W and Enable pins
Character construction is 5*7dots.
77
void lcdcmd (unsigned char value)
{
P0=value;
rs=0; //select instruction register
rw=0; //write
en=1;
tdelay(1);
en=0;
}
void lcddata (void)
{
P0=temp;
rs=1; //select data register
rw=0; //write
en=1;
tdelay(1);
en=0;
}
78
LCD Interfacing with 8051
Result:
79
80
5. GENERATION OF WAVEFORMS USING DAC INTERFACE
Aim: To write a C program to generate the following waveforms. Interface DAC and
observe the waveform on CRO
A) Square wave of 1KHz having 50% duty cycle.
B) Square wave of 60% duty cycle
C) Triangular wave
D) Ramp wave
E) Sine wave
Interfacing Diagram
8 CRO
0 Dual
5 P0 Xout
DAC
1 P1
μC
81
Program
#include <Reg51.h>
void tdelay( );
void main( )
{
while(1)
{
P0=0xFF;
tdelay( );
P0=0x00;
tdelay( );
}
}
void tdelay()
{
unsigned int i;
for(i=0;i<450;i++);
}
82
A. Square wave of 1 kHz Frequency and 50% Duty Cycle
Algorithm
1. Configure P0 as output port (Output the initial value 00 through P0).
2. Output value FFh(on) through P0.Call delay to remain ON for .5ms. (ie.count
450 in the for loop)
3. Output value 00h(off) through P0. Call delay to remain ON for .5ms. (ie.count
450 in the for loop)
4. Repeat step 2&3 continuously.
5. Note the waveform on CRO.
83
Program
#include <Reg51.h>
void tdelay( );
void main( )
{
while(1)
{
P0=0xFF;
tdelay( );
tdelay( );
tdelay( );
P0=0x00;
tdelay( );
tdelay( );
}
}
void tdelay( )
{
unsigned int i;
for(i=0;i<450;i++);
}
84
B. Square wave of 60% Duty Cycle
Algorithm
1. Configure P0 as output port (Output the initial value 00 through P0).
2. Output value FFh(on) through P0.Call delay three times
3. Output value 00h(off) through P0. Call delay two times.
4. Repeat step 2&3 continuously.
5. Note the waveform on CRO.
85
Program
#include <Reg51.h>
void main( )
{
unsigned char i ;
while(1)
{
for(i=0;i<0xFF;i++)
P0 = i ;
for(i=0xFE;i>0x00;i--)
P0 = i ;
}
}
86
C. Triangular Waveform
Algorithm
1. Output the initial value 00 through P0.
2. Increment it in steps of 1 until a count value of FFh (5V) is reached. Every
time output the value through P0.
3. Decrement it in steps of 1 until a zero value is reached. Every time output the
value through P0.
4. Repeat step 2&3 continuously.
87
Program
#include <Reg51.h>
void main( )
{
unsigned char i ;
while(1)
{
for(i=0;i<0xFF;i++)
P0 = i ;
}
}
88
D. Ramp Waveform
Algorithm
1. Output the initial value 00 through P0.
2. Increment it in steps of 1 until a count value of FFh (5V) is reached. .
Every time output the value through P0.
3. Repeat step 2 continuously.
89
Program
#include <Reg51.h>
void main( )
{
static int a[25]={128,161,
95,128}
;
unsigned char i ;
while(1)
{
for(i=0;i<25;i++)
P0 = a[i] ;
}
}
90
E. Sine Waveform
Algorithm
1. Compute different step values (θ = 0o,15o,30…360) using the equation
128+128 sin . Output the values through P0.
2. More the steps smoother will be sine wave.
Result:
91
Program
#include<reg51.h>
void delay_ms(unsigned int);
unsigned char reqflr,curflr,i,flr;
unsigned int r1;
void main()
{
P1=0xff;
P0=0x0F;
curflr=0x00;
while(1)
{
reqflr=P1 & 0x0f;
switch(reqflr)
{
case 0x07:
flr=0x09;
break;
case 0x0b:
flr=0x06;
break;
case 0x0d:
flr=0x03;
break;
case 0x0e:
flr=0x00;
break;
}
if(curflr<flr)
{
for(i=curflr;i<flr;i++)
92
5. ELEVATOR INTERFACE
Hardware Details
The control and operation of an elevator functioning within a building, having four
floors is simulated. A key and a corresponding LED indicator serve as a request
button and request status indicator. The green LED’s when lit indicate that the
elevator is at a floor while the amber LED’s goes on, indicating the elevator to be in
between 2 floors. The serving of a request can be indicated by clearing the request. A
four line to ten line decoder (7442) is used to specify the location of the elevator
within the shaft. Two dual D flip-flops are used to sense requests and output the
status.
The connection diagram for the elevator control is as shown.
93
{
P0=0xf0|i;
delay_ms(25000);
}
}
else
{
for(i=curflr;i>flr;i--)
{
P0=0xf0|i;
delay_ms(25000);
}
}
curflr=flr;
switch(flr)
{
case 0x00:
P0=0xe0;
break;
case 0x03:
P0=0xd3;
break;
case 0x06:
P0=0xb6;
break;
case 0x09:
P0=0x79;
break;
}
}
}
void delay_ms(unsigned int r)
{
for(r1=0;r1<=r;r1++); }
94
Result:
95