MC Manual 2022 2023 MS
MC Manual 2022 2023 MS
MC Manual 2022 2023 MS
VISION
• To be one of the premier Institutes of Engineering and Management education in the
country.
MISSION
• To provide Engineering and Management education that meets the needs of human
resources in the country.
• To develop leadership qualities, team spirit and concern for environment in students.
MISSION
• To provide teaching/learning facilities in Electrical & Electronics Engineering for easy
adaptation to industry and higher learning.
• Provide environment for self learning to meet the challenges of changing technology
and inculcate team spirit and leadership qualities to succeed in professional career.
• Empathize with the societal needs and environmental concerns in Electrical &
Electronics Engineering practices.
• Analyze, design and propose solutions in the field of Electrical & Electronics
Engineering and adapt to changes in technology by self learning.
• Work effectively as an individual and exhibit leadership qualities in a team to meet the
goals of the program or the organization.
• Work with professionalism and concern for environment to meet the societal needs.
Course Objectives and Course outcome for the course: Microcontroller lab 17EEL57
Course objectives:
1. To write ALP for data transfer, arithmetic, logical operations.
2. To write ALP for delay, code conversions.
3. To write ALP for Serial interface, counters
4. To write C program for waveform generation, ADC, Stepper motor, Speed control of
DC motor, LCD interface, Elevator interface.
5. Develop programs for the given requirement.
6. To work as team and individual to perform lab experiments
7. To prepare reports of the experiments conducted.
Course Outcomes:
• Analyze and design controllers for electrical system using analog and digital
electronics circuits and systems..
CONTENT SHEET
1. Introduction 04
2. Square of a number 08
3. Evaluation of an expression 08
4. 16-bit binary subtraction 09
5. 16-bit binary addition 10
6. Block movement without overlap 11
7. Block interchange program 12
8. ASCII to binary conversion 13
9. Binary to ASCII conversion 14
10. Counting no. of 1’s and 0’s 15
11. Hexadecimal up-down counter 16
12. Decimal up counter 17
13. Average of 5 numbers 18
14. Logical operation demonstration 19
15. Bit operation demonstration program 20
16. Addition of n, 1-byte binary numbers 21
17. Largest/Smallest in an array 22
18. BCD – Binary conversion 23
19. Binary – BCD conversion 24
20. Square wave on Port 1 25
21. Bubble sort implementation 26
22. Introduction to use of FLASHMAGIC for uploading prg. 27
23. DAC I/F --- Square wave 70% duty cycle 29
24. DAC I/F --- Sawtooth and triangular wave generation 30
25. DAC I/F --- Sine wave generation 31
26. DAC I/F --- Square wave of 50% duty cycle 32
27. DC motor interface and speed control using PWM 33
28. Stepper motor – Anticlockwise rotation 35
29. Stepper motor – Clockwise rotation 35
30. Stepper motor – Clockwise/anticlockwise based on switch 36
31. Elevator interface – Program to simulate lift movement 37
32. LCD interface - Program to display message 40
33. ADC interface – Program to convert analog to digital 42
34. Serial programming to display name using internal port 44
35. Additional program- RTC 45
36. FAQ 46
37. Instruction set of 8051 49
CHAPTER – 1
INRODUCTION TO USE OF KEIL SOFTWARE FOR ASSEMBLY PROGRAM
The assembly experiments of 8051 are done with the help of KEIL software platform.
The following session discusses use of the software for program execution.
The steps involved in creating a project, opening an assembler file,
& building executable file and data entering & result viewing:
➢ On the DESKTOP double click on <KEIL uvsion3>. On the next screen that
appears, select <Project> and <New>.
➢ On the next screen that appears give a name of the project, typically as your
name with last 3 digits of the USN eg. Kumar013 and SAVE. On the next
screen select PHILLIPS and microcontroller as <P89c61X2> and press
<OK>. In the next screen press <YES>. The screen will appear as below:
➢ Then select <FILE> menu and <NEW> & type your program.
➢ Then select <FILE> menu and <SAVE AS> & in the window that appears
create a folder. Save file by giving suitable name with extension as .asm.
➢ Then on the project workspace area right click on <Source Group 1> and select
<Add files to source group 1>. In the next screen select Asm source files and
from the list displayed select the program you have saved. The press <ADD>
and <CLOSE>.
The next screen to appear will be as below:
➢ Select <PROJECT> & <Build Target File>. The next screen will be seen as
below:
Confirm ZERO errors at the output window at the bottom of the screen. If
any errors rectify them and repeat the step.
➢ The select <DEBUG> & select <START/STOP Debug Session>. In the popup
that appears select <OK>. The next screen will be as below:
➢ Type your desired data in the location as specified in the program. The entries
are to be in hexadecimal only.
➢ Now select RUN option in the top command bar to execute the program.
Reset Step out
➢ STOP execution using<HALT> icon on the top command bar which will
appear in RED colour.
Logic: The input number is assumed at location 40h of internal RAM. To find
the square the number is multiplied by itself.
Program:
ORG 0
SJMP main
ORG 30h
main: MOV A,40H ; (A) = number.
MOV 0F0H, A ; (B) = (A) = number
MUL AB ; (B_A) = n2
MOV 50H,B
MOV 51H,A
here: SJMP here ; Program termination
END
Program:
ORG 0
SJMP main
ORG 30H
main: MOV A,40H ;(A) = X
CLR C
SUBB A,41H ;(A) = X - Y
MOV 0F0H,A ;(B) = X - Y
MOV A,40H ; (A) = X
ADD A,41H ; (A) = X + Y
DIV AB
here: SJMP here
END
1. Write a program to subtract 16-bit binary number stored from location X with
another 16-bit binary number stored from location Y. Store the result at Z
and Z+1.
Logic: The 16-bit number is stored as lower 8-bits first and then upper 8-bits.
i .e Location X = L81 location Y = L82
Location X+1 = U81 location Y+1 = U82
The required result is: Location Z = L81 – L82
Location Z+1 = U81 – U82 – Cy.
Program: ORG 0
SJMP main
ORG 30H
Result : (50) = 16
(51) = 22
(52) = 00
Result : (50) = DD
(51) = EA (52) = 01
Logic: The 16-bit number is stored as lower 8-bits first and then upper 8-bits.
i.e Location X = L81 location Y = L82
Location X+1 = U81 location Y+1 = U82
The required result is: Location Z = L81 + L82
Location Z+1 = U81 + U82 + Cy.
Program: ORG 0
SJMP main
ORG 30H
main: MOV A,40h ; (acc) = l81
ADD A,42h ;(acc) = l81 + l82
MOV 50h,A ; (50h) = l81 + l82
MOV A,41h ; (acc) = U81
ADDC A,43h ;(acc) = U81+U82+CY
MOV 51h,A ; (51h) = U81+U82+CY
MOV A,#00
ADDC A,0E0H ;(A) = (A) +(A) +Cy.
MOV 52H,A
HERE:SJMP HERE
END
Result : (50) = 16
(51) = 46
(52) = 00
Logic: Initialize 2 pointers P1 and P2 to location X and Y. The count ‘n’ is stored in
CNT. The content of location pointed by P1 is moved to P2 location. The pointers are
incremented by 1 and the counter CNT is decremented by 1. If CNT != 0 repeat the
process until the CNT = 0.
Program :
ORG 0
SJMP MAIN
ORG 30H
Inputs: outputs:
(40) = 11 (50) = 11
(41) = 12 (51) = 12
(42) = 13 (52) = 13
(43) = 14 (53) = 14
(44) = 15 (54) = 15
NOTE: Students are required to modify the above program for external memory
data.
Logic: Initialize 2 pointers P1 and P2 to location X and Y. The count ‘n’ is stored in
CNT. The content of location pointed by P1 is moved to P2 location and the content of P2
is moved to location P1 through Accumulator and B-register. The pointers are incremented
by 1 and the counter CNT is decremented by 1. If CNT != 0 repeat the process until the
CNT = 0.
Program :
ORG 0
SJMP MAIN
ORG 30H
MAIN: MOV R3,#05 ; Cnt in C-register.
MOV R0,#40H ; pointer P1 initialization
MOV R1,#50H ; pointer P2 initialization
Rpt: MOV A,@R0 ; source data moved to acc
XCH A,@R1 ; and exchanged with destination
MOV @R0,A ; through A-reg
INC R0 ; increment pointer P1
INC R1 ; increment pointer P2
DJNZ R3,Rpt ; Decrement count
HERE:SJMP HERE
END
Inputs: outputs:
Note: Students are required to modify the above program for external memory data.
If ASCII value is < 40h then subtract 30h to get the Binary value.
If ASCII value is > 40h then subtract 37h to get the Binary value.
Implementation:
The ASCII input stored in location 50h. The resultant binary number stored in
location 60h.
Program:
ORG 0
SJMP MAIN
ORG 30H
MAIN: MOV A,50H ; (A) = data
CJNE A,#40H,Nxt ;Check if no <= 40
LJMP Last ; if = 40h goto last.
Nxt: JC LESS ; if no. <40h goto less
SUBB A,#37H ; if no.>40h sub 37h
LJMP Res
LESS: CLR C
SUBB A,#30H ; if no<40h sub 30h
Res: MOV 60H,A ;
Last: SJMP Last
END
Homework: Students are required to modify the above program such that it does not
take any input < 30h and >46h
Logic: The main program the 2 digits are separated and the subroutine for conversion
is called with each digit. This is achieved by ANDing the number with 0Fh to get the
lower digit and then AND the number with 0F0h and swap to get the higher digit.
A subroutine is used for conversion of the digit to ASCII since there are 2 digits in a
hexadecimal number. The logic of the subroutine is :
If the binary digit is < 0Ah add 30h and
If the binary digit is >= 0Ah add 37h .
Program:
ORG 0
SJMP MAIN
ORG 30H
MAIN:MOV A,40H ; (A) = number
ANL A,#0FH ; (A) =lower digit
LCALL ASCII ; (A) = ascii of LD.
MOV 50H,A ; (50) = ascii of LD
MOV A,40H ; (A) = number
ANL A,#0F0H ;
SWAP A ; (A) = upper digit
LCALL ASCII ; (A) = ascii of UD
MOV 51H,A ; (51) = ascii of UD
HERE:SJMP HERE
Logic: Get the byte into accumulator. Initialize a counter c1 for number of bits.i.e.08.
Initialize another counter c2 to hold number of 1’s as zero.
Rotate the given number right and check the carry. If carry is ‘set’
increment c2 and then decrement c1. If a carry is ‘reset’, then decrement c1 without
altering c2. Repeat the above steps until c1!= 00.
Program:
ORG 0
SJMP Main
ORG 30h
Note: For finding the numbers of zeros in the data replace JNC by JC.
Inputs:
Home work: Modify the above program to get 0’s and 1’s from a single program.
Logic: Initialize accumulator as zero. Increment accumulator by 1 and repeat in loop until
(A) = 0fh and then start to decrement by 1 until (A) = 0.
Program:
ORG 0
SJMP Main
ORG 30h
Note: To run this program, after selecting <DEBUG> in the main menu use <View
→Watch & Call Stack window>. Select Watch#1 or Watch#2. Press F2 and enter
‘ACC’ (for accumulator). After this adjustment run the program in step into mode.
Observe the count in the watch window as you continuously press Step into icon
Reset Step out
Program:
ORG 0
SJMP Main
ORG 30h
Note: To run this program, after selecting <DEBUG> in the main menu use <View
→Watch & Call Stack window>. Select Watch#1 or Watch#2. Press F2 and enter
‘ACC’ (for accumulator). After this adjustment run the program in RUN MODE
Reset Step out
Logic: The array of 5 numbers starts from location 40h. First add the 5 numbers,
assuming result <=255d, store the result in A-register. The count 05 is loaded in B-
register. Divide the result in A-reg by count to get the average. The quotient from A-
reg is stored at location 50h and remainder from B-reg to location 51h.
Implementation: The array begins from 40h. R0 is made pointer to the array for the
addition.
Program:
ORG 0
SJMP MAIN
ORG 30h
Outputs: (50) = 0A
(51) = 00
Logic: The upper nibble of the content of X is masked to retain the lower nibble. Then
the lower nibble of X+1 is masked and the number is rotated 4 times to bring the upper
nibble of X+1 to lower position. Then OR the two values. Then XOR the result with
the content of location X+2.
Program:
ORG 0
SJMP MAIN
ORG 30h
Inputs: (40) = 03
(41) = C3
(42) = 24
Outputs:
(50 ) = 2B.
12. Write a bit manipulation program for performing the following operation: bit 0 and
bit1 of a memory to be set, bit 2 and bit 3 of the memory to be reset, bit 6 and bit
7 of the memory to be complimented. Use bit instructions.
Program:
ORG 0
SJMP MAIN
ORG 30h
13. Write a program to add ‘n’ bytes of data stored from location X. The count is
stored at location Y. Display the result in the address field.
Logic: Initialize a pointer P1 to the array. Clear accumulator and clear another register
to accumulate the carry in the addition process. Initialize a counter C1 to the number of
elements in the array.
Add the 1st number to accumulator and if no carry go to next number else if there is
a carry increment the register to accumulate the carry and then go to next number. The
next number is reached by incrementing the pointer P1 and decrement C1, Repeat the
above until count C1 != 0.
Implementation:
R0 register pair is used as a pointer P1.
The data is stored in location 40 and count is 05.
R2-register is used as counter C1
50h-register is used as carry accumulate register.
Program:
ORG 0
SJMP MAIN
ORG 30h
MAIN: MOV R0, #40H; (R0)=pointer to the array
MOV R2, #05H ; (R2) = COUNT
CLR A
MOV 50h,#00 ; (50) upper 8-bits of result
Ag: ADD A,@R0
JNC Nxt
INC 50H
Nxt: INC R0
DJNZ R2, Ag
MOV 51H, A
HERE:SJMP HERE
END
Input: (40) = 3d
(41) = FC
(42) = 47
(43) = 77
(44) = 2D
Output:
(50) = 02. (51) = 24
Note: For BCD addition use DA A instruction after ADD instruction in above
program. (Input decimal Numbers should be such that result < 1000d).
14. Write a program to display the largest/smallest in the given array of ‘n’
bytes in the data field.
Logic: Initialize a pointer P1 to the array. Load the count in register C. Assume
the 1st number to be the largest and bring it into Acc. Increment pointer P1 and
compare the first number in acc with the next number and if the second number is
greater than the first bring the 2nd number into the accumulator. Repeat the above until
count becomes equal to zero.
Implementation:
R0-register pair is used as a pointer P1.
The array is stored from location 40 and count is 05.
R2-register is used as counter C1
Program:
ORG 0
SJMP MAIN
ORG 30h
Inputs: (40) = 04
(41) = 12
(42) = 07
(43) = 16
(44) = 14
Output:
(50) = 16.
15. Write a program to convert a 2-digit BCD number into binary. Display
the result in the data field.
Logic: First the given BCD number is unpacked. The upper digit is multiplied by
10d and then the lower digit is added to get the binary equivalent.
Eg: Input = 26d expected output = 1Ah
1) Unpack number [06 and 02]. Store lower digit, 06, at memory location..
2) Upper digit 02 * 10d = 20d = 14h
3) Result = ud*10d + ld = 02*10d+06 = 1Ah
Program: ORG 0H
SJMP MAIN
ORG 30H
MAIN:MOV A,40H ;(A) = no.
ANL A,#0Fh ;(A) = lower digit ,ld
MOV 50H,A ;(50) = LD
MOV A,40H
ANL A,#0F0h
SWAP A ;(A)=upperdigit, ud
MOV 0F0h,#0AH ; (B) = 10d
MUL AB ;(A) = UD * 10D
ADD A,50H ;(A) = UD*10D + LD
MOV 50H,A
HERE:SJMP HERE
END
Input: (40) = 23
Output: (50) = 17h, which is the hexadecimal equivalent of input number 23d.
Logic: Divide the given number by 100d. The quotient of the division is hundreds
digit. Divide the remainder by 10d. The quotient is the tens position digit and the
remainder is the units position digit. The division is done using successive
subtraction logic.
To get packed result ---- Retain the hundreds digit without any change and rotate
the tens digit 4 times and add the units digit to get the packed BCD number.
Program: ORG 0H
SJMP MAIN
ORG 30H
Output: (50) = 02
(51) = 54
Logic: Make A as 00. Send to P1. After a delay corresponding to Toff (= 5ms)
make A as FF and send to P1. Take a delay corresponding to Ton (=5ms). Repeat
the process infinite no of times
0 0 0 1 0 0 0 0 =10h
Program :
Output:
Write a program to sort a given array in ascending order using BUBBLE SORT
algorithm.
Logic: In BUBBLE sort logic there are n-1 numbers of passes and in the first pass
there will be n-1 comparisons, in the 2nd pass there will be n-2 comparisons and so on.
The last pass will have 1 comparison. The first number is compared with the second. If
the first is > than the second then the 2 numbers are exchanged otherwise they are
retained. The above process is repeated until all the comparisons and all the passes are
completed.
Program: ORG 0H
SJMP MAIN
ORG 30H
Self work: Write a program to find the second largest element in an array.
To create a new project and a C-file using Keil software:
1. On the DESKTOP double click on <KEIL uvsion3>. On the next screen that
appears, select <Project> and <New>.
2. On the next screen that appears give a name of the project, typically as your name
with last 3 digits of the USN eg. Kumar013 and SAVE. On the next screen select
PHILLIPS and microcontroller as <P89c61X2> and press <OK>. In the next
screen press <YES>. The screen will appear as below:
3. Then select <FILE> menu and <NEW> and type your program.
4. Then select <FILE> menu and <SAVE AS>. In the window that appears create a
folder and save the file by giving suitable name with extension as .c
5. Then on the project workspace area right click on <Source Group 1> and select
<Add files to source group 1>. In the next screen select c-source files and from
the list displayed select the program you have saved. Then press <ADD> and
<CLOSE>.
6. Select <Project> and select <Build all>. Right click on <Target > icon. Select
<Create Hex File>. Then go to <Project> and press the icons <Build> and
<Rebuild>
9. Select the microcontroller as 89c61X2, baud rate of 9600, and select memory
blocks to erase, browse to select HEX file from the folder created by you and give
<START>.
10. Wait until the purple bar at the bottom of FLASHMAGIC screen is turned white.
Now the HEX file from the system has been downloaded into the microcontroller.
11. Connect the hardware, select run mode on the microcontroller board and see the
program execution.
DAC INTERFACE
18. Write a program generate a square wave of frequency 1kHz and a duty
cycle of 0.7 using DAC interface. Show the waveform on the CRO and verify
the frequency and duty cycle.
Interface diagram:
Program:
#include<reg51.h>
void delay1( ),delay2( );
void main( )
{ while(1)
{ P2 = 0x00;
delay1( );
P2 = 0xFF;
delay2( );
}
void delay1( )
{ TMOD=0x01; TH0=0xFE; TL0=0xEb;
TR0=1; while(TF0==0);
TR0=0; TF0=0;
}
void delay2( )
{ TMOD=0x01; TH0=0xFd; TL0=0x7b;
TR0=1; while(TF0==0);
TR0=0; TF0=0;
}
19. Write a program generate a saw tooth / triangular wave using DAC
interface. Show the waveform on the CRO and measure the slope.
Logic: a) Saw tooth waveform: Initialize acc as 00 and send to DAC. Increment by one
until it reaches FF and the next increment will bring the value to 00
b) Triangular waveform: Initialize acc as 00 and send to DAC. Increment by
one until it reaches 255 and then decrement it to bring to 00 and repeat the loop.
Command word: The command word is same as that for the previous program.
b) Triangular waveform:
#include<reg51.h>
void main( )
{ unsigned char i;
while(1)
{ for(i=0;i<255;i++)
P2 = i;
for(i=255;i>=0;i--)
P2=i;
}
}
Output:
20. Write a program generate a sine wave using DAC interface. Show the
waveform on the CRO and measure the slope.
Logic: Look up table technique is used for generation of sine wave. A table is generated
with 128d,i.e.,digital equivalent of 2.5v, as base value in steps of 10 each. Thus the count
will be 37d. The table is calculated based on the formula
f(x) = 128 + 128 * sin(x) where x = 0 - 360 in steps of 10. Eg.
a) x = 0 f(x) = 128 b) x = 10 f(x) = 150etc.
X 0 10 20 30 40 50 60 …. … … … 350 360
f(x) 128 150 172 192 210 226 239 …. … … … 106 128
Program:
#include<reg51.h>
void main( )
{ unsigned char arr[37] = {128, 150, 172, 192, 210, 226, 239, 248, 254, 255,
254, 248, 239, 226, 210, 192, 172, 150, 128, 106, 84, 64, 46, 30, 17, 8, 2, 0, 2, 8, 17, 30, 46,
64, 84, 106, 128};
unsigned char i;
while(1)
{ for(i=0,i<37;i++)
P2 = arr[i] ;
}
}
Waveform expected:
21. Write a program generate a square wave of frequency 1kHz and a duty
cycle of 0.5 using DAC interface. Show the waveform on the CRO and verify
the frequency and duty cycle.
Program:
#include<reg51.h>
void delay1( );
void main( )
{ while(1)
{ P2 = 0x00;
delay1( );
P2 = 0xFF;
delay1( );
}
}
void delay1( )
{ TMOD=0x01; TH0=0xFE; TL0=0x33;
TR0=1; while(TF0==0);
TR0=0; TF0=0;
}
The main program based on the switch selects the multiplier for the Ton and Toff.
The delay function is called using for loop for the number of times as selected based on
the switch.
The delay function gives 5ms delay using Timer 0, mode 1.
Program:
#include<reg51.h>
sbit P24=P2^4;
sbit P16=P1^6;
void delay1( );
unsigned char Toff, Ton, i;
void main()
{
TMOD=0x01;
P16=1;
while(1)
{
if(P16= =1)
{
Toff=10;
Ton=30;
}
else
{
Toff=28;
Ton=12;
}
P24=0;
for(i=0;i<Toff;i++)
delay1( );
P24=1;
for(i=0;i<Ton;i++)
delay1( );
}
}
void delay1( )
{
TR0=1;TL0=0x00;TH0=0xee;
while(TF0==0);
TF0=0;
TR0=0;
}
Logic: In case of a stepper motor, the rotor is organized as four phases. Each phase is
activated one at a time. When the phase is activated, the rotor rotates. When the rotor
field aligns with the stator field, rotation stops and so on. A phase is activated by
sending a 0 on the control pins Pins P2.3 – P2.0 are used for activating the phase. The
phase activation for 1.8° per step is given below:-
Program:
#include<reg51.h>
void delay1( );
sbit P16 = P1^6;
void main( )
{ unsigned char i, arr[]={0x0e,0x0d,0x0b,0x07};
P16 = 1;
while(1)
{ if(P16 = = 0)
{ for(i=0;i<4;i++)
{ P2 = arr[i];
delay1( ) ;
delay1( );
}
}
}
}
void delay1( )
{ unsigned int i;
for(i=0;i<40000;i++);
}
30. Write a program to realize clockwise rotation from a stepper motor. The
stepper motor should stop when a switch connected to P1.6 is ON.
31. Write a program to rotate the stepper motor clockwise if the switch at P1.6 = 1
and to rotate in anticlockwise direction if the switch at P1.6 = 0.
Program:
#include<reg51.h>
void delay1( ), clk( ), aclk( );
sbit P16 = P1^6;
void main( )
{ unsigned char i;
P16=1;
while(1)
{ if(P16 = = 1)
clk( );
else
aclk( );
}
}
void delay1( )
{ unsigned int i;
for(i=0;i<40000;i++);
}
void clk( )
{ unsigned char i, arr[]={0x07, 0x0b, 0x0d, 0x0e};
for(i=0;i<4;i++)
{ P2 = arr[i];
delay1( );
}
}
void aclk( )
{ unsigned char i, arr[]={0x0e, 0x0d, 0x0b, 0x07};
for(i=0;i<4;i++)
{ P2 = arr[i];
delay1( );
}
}
Note:
While executing based on the switch at P1.6 the direction of the motor can be
controlled. This is an example of a real time implementation using microcontroller.
When a floor request is made, the corresponding P1.X pin will be zero. The lift is
simulated by the movement of LEDs connected to 7442 which is a 4:10 decoder. P0.7 to
P0.4 is used for clearing the floor request. A zero causes the respective floor request to
be cleared. The decoder is an active low output decoder, i.e. with input as 1111 the LED
at the output zero will be activated. The floor request is identified using Port-1 pins as
given below:-
Floor P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0 Value
TF 0 0 0 0 0 1 1 1 07
SF 0 0 0 0 1 0 1 1 0b
FF 0 0 0 0 1 1 0 1 0d
GF 0 0 0 0 1 1 1 0 0e
Program:
#include<reg51.h>
void delay1( ), gf( ), ff( ), sf( ), tf( );
unsigned char i, req;
void main()
{
while(1)
{
P0=0x0f;
delay1( );
P0=0xf0;
P1=0xff;
req=P1;
req=req&0x0f;
if(req = = 0x0e) gf( );
if(req = = 0x0d) ff( );
if(req = = 0x0b) sf( );
if(req = = 0x07) tf( );
}
}
void gf( )
{
P0=0xf0;
delay1( );
P0=0x0f;
}
void ff( )
{
for(i=0xf0;i<=0xf3;i++)
{
P0=i;
delay1( );
The lift is made to move to first
}
floor by the first ‘for’ loop. After a
delay1( );
stay at first floor the lift moves to
delay1( );
ground floor. The request is cleared
for(i=0xf3;i>=0xf0;i--)
by the last statement
{
P0=i;
delay1( );
}
P0=0x0f;
}
void sf( )
{
for(i=0xf0;i<=0xf6;i++)
{
P0=i;
delay1( ); The lift is made to move to second
} floor by the first ‘for’ loop. After a
delay1( ); stay at second floor the lift moves to
delay1( ); ground floor. The request is cleared
for(i=0xf6;i>=0xf0;i--) by the last statement
{
P0=i;
delay1( );
}
P0=0x0f;
}
void tf( )
{
for(i=0xf0;i<=0xf9;i++) The lift is made to move to third
{ floor by the first ‘for’ loop. After a
P0=i; stay at third floor the lift moves to
delay1( ); ground floor. The request is cleared
} by the last statement
delay1( );
delay1( );
for(i=0xf9;i>=0xf0;i--)
{
P0=i;
delay1( );
}
P0=0x0f;
}
void delay1( )
{
unsigned int x,y;
for(x=0;x<5;x++)
for(y=0;y<60000;y++);
}
Logic: A LCD interface is used as a display device. It consumes very less power and
hence preferred to be used for a battery operated portable devices.
The basic pins of LCD interface are D0 to D7 which carries the data to be displayed
and the commands. The LCD interface diagram is given below:-
P0 D0 – D7
P2.4 RS
P2.5 RW
P2.6 EN
The various commands for LCD are given in the table below:
Command Description
38 Selects 2 line display with (5 X 7) matrix fro the character display
0F Indicates “Display on and Cursor blinking”
06 Causes the cursor to be shifted to the right after display.
01 To clear display
80 Cursor is set at the beginning of first line.
Program:
#include<reg51.h>
void lcddata( ), lcdcmd( ), delay1( );
sbit RS=P2^4;
sbit RW=P2^5;
sbit EN=P2^6;
unsigned char temp,i,x;
unsigned char comnd[]={0x38,0x0f,0x06,0x01,0x80};
unsigned char msg[]="BNMITEE";
void main()
{ delay1( );
delay1( );
for(i=0;i<=4;i++)
{
temp=comnd[i]; The LCD commands are sent to
lcdcmd( ); command register of LCD.
}
for(i=0;i<7;i++)
{
temp=msg[i]; The message to be displayed is sent to
lcddata( ); data register of LCD.
}
while(1);
}
void lcdcmd( )
{ P0=temp;
RS=0;
RW=0;
EN=1;
delay1( );
EN=0;
delay1( );
}
void lcddata( )
{ P0=temp;
RS=1;
RW=0;
EN=1;
delay1( );
EN=0;
delay1( );
}
void delay1( )
{
for(x=0;x<100;x++);
}
Logic: It is used to convert an analog signal to digital. The basic block diagram of
ADC interface to the microcontroller and the LED display of the digital value is given
below:-
The LED uses common cathode configuration i.e., 1 - causes LED to glow and 0 –
causes LED to be off. Thus the coding for 1 is given below
H g f E d C b a
0 0 0 0 0 1 1 0 = 06
Program:
#include<reg51.h>
void delay1( );
sbit soc = P3^5;
sbit chsel=P3^6;
sbit displsd=P1^0;
sbit dispmsd=P1^1;
sbit eoc = P3^3;
unsigned char arr[ ]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7b,0x39,0x5e,0x79,0x71};
unsigned char adcval, lsd, msd;
void main()
{
P2=0xff;
chsel=0x01;
while(1)
{
soc=1;
delay1( );
soc=0;
while(eoc = =0);
delay1( );
adcval=P2;
msd = arr[adcval/0x10]; Identify 7 seg code of upper digit.
lsd = arr[adcval%0x10]; Identify 7 seg code of lower digit.
displsd = 1;
dispmsd = 1;
P0 = msd;
displsd = 1;
dispmsd = 1;
P0 = lsd;
}
}
void delay1( )
{
unsigned char i;
for(i=0;i<200;i++);
}
35. Write a program to display a message your name serially at a baud rate of 9600
using serial port.
Logic: Timer 1, mode 2 is used to program the baud rate. For the given baud rate the
value to be loaded in TH1 is FDh. The command word for serial control SCON is 50h.
The result can be viewed in the <view> option, <hardware peripherals> and selecting
<UART>. Execute the program after making the UART window selection.
Program:
ORG 0h
SJMP MAIN
ORG 30H
MAIN: MOV TMOD, #20H
MOV TH1, #0FDH
MOV SCON, #50H
SETB TR1
MOV DPTR, #0100H
Ag: CLR A
MOVC A,@A+DPTR
JZ Fin
MOV SBUF, A
Here: JNB TI, Here
CLR TI
INC DPTR
SJMP Ag
Fin: SJMP Fin
ORG 0100H
db ‘KUMAR’, 0
END
Note: After selecting the UART window and executing the program the UART window
buffer will have the string specified (in this case ‘KUMAR’) displayed.
Logic: Timer0 is configured for delay of 50ms and repeated 20 times to realize 1
sec. Memory location 40h is used for minutes and memory location 41h is used for
seconds. Initially minutes can be set to any desired value. After every second,
location 41h representing, seconds is incremented. When the value reaches 60,
minutes is incremented by one and seconds is reset to zero. When the value of
minutes reaches 60 both minutes and seconds are reset to zero. The operation
executed in an infinite loop.
Program:
ORG 0
SJMP 30h
ORG 30h
MOV 40h,#58h; initial value for minutes
SJMP rpt2
ag1: MOV 40h,#00
rpt2: MOV 41h,#00; initial value for seconds
rpt1: MOV R2,#20;
ag: LCALL delay; Delay of 1 sec
DJNZ R2,ag;
MOV A,41h;
ADD A,#01;
DA A incrementing seconds to 1 and adjusting
MOV 41h,a; to a decimal value. Checking for 60 and if
CJNE A,#60h,rpt1; not equal go to next second.
MOV A,40h
ADD A,#01 incrementing minute to 1 and adjusting
DA A to a decimal value. Checking for 60 and if
MOV 40h,A not equal go to next minute
CJNE A,#60h,rpt2
SJMP ag1
FAQ
1. What is a microprocessor?
2. What is a microcontroller?
3. What are the differences between microprocessor and microcontroller?
4. What are the various types of processor architectures?
5. What are the advantages of Harvard architecture over Von-Neumann architecture?
6. What is a RISC system and what is a CISC system?
7. What is a program counter?
8. What is a stack pointer?
9. What is meant by embedded microcontroller?
10. What are registers and what are its uses?
11. Why external memory interface is required in a microcontroller?
12. Mention features of 8051 microcontroller?
13. Why do you call 8051 as an 8-bit microcontroller?
14. What is the internal memory type and capacity in 8051?
15. What is the organization of internal data memory of 8051?
16. Why 8051 is called Boolean processor?
17. What is PSW?
18. Explain the use of CY and Ov – Flag.
19. How many register banks are there in 8051 internal RAM?
20. How do you select between the various register banks of 8051?
21. Mention the value of stack pointer on reset.
22. How many IO ports are there is 8051?
23. Mention the uses of the various IO ports of 8051.
24. What are the various addressing modes of 8051?
25. How do you identify the use of immediate addressing mode?
26. Can external memory be accessed using direct addressing mode?
27. What is the addressing that is supported by external memory?
28. Which registers can be used as pointers for external memory access?
29. Which registers can be used as pointers for internal memory access?
30. Mention the instruction to access the data from external data memory.
31. Mention the instruction to access the data from external code memory.
32. Explain the working of instructions like – SWAP A, DA A etc….
33. What are SFR? Why are they required?
34. Explain the logic of the program written by you.
35. What is meant by ASCII code?
36. Where do you find the use of ASCII code?
37. Mention the difference between BCD and Binary codes.
38. Mention any two bit manipulation instructions.
39. What is the capacity of external memory that can be connected to 8051?
40. What the different types of CALL and JUMP’s?
41. Mention the ranges of branching with short, absolute and long branch.
42. Explain the instruction DJNZ, CJNE, JB, JBC etc..
43. What is the difference between branching and subroutine?
44. What is the difference in execution between LJMP and LCALL?
45. How do you handle real time subroutine call?