MC Manual 2022 2023 MS

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

MICROCONTROLLER LAB MANUAL - 17EEL57

B.N.M. Institute of Technology


Approved by AICTE, Affiliated to VTU, Accredited as Grade A Institution by NAAC.
All UG branches – CSE, ECE, EEE, ISE & Mech.E Accredited by NBA for academic years 2018-19 to 2020-21 & valid upto 30.06.2021
Post box no. 7087, 27th cross, 12th Main, Banashankari 2nd Stage, Bengaluru- 560070, INDIA
Ph: 91-80- 26711780/81/82 Email: [email protected], www. bnmit.org

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.

DEPARTMENT OF ELECTRICAL & ELECTRONICS ENGINEERING


VISION

• To be one of the premier department for education in Electrical & Electronics


Engineering in the state of Karnataka, moulding students into professional Engineers.

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.

PROGRAM EDUCATIONAL OBJECTIVES (PEO)


After 2/3 years of graduation, the students will have the ability to:

• 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.

• Excel in professional career by acquiring higher education.

DEPT OF EE, B.N.M.I.T. PAGE NO: 1


MICROCONTROLLER LAB MANUAL - 17EEL57

DEPARTMENT OF ELECTRICAL & ELECTRONICS ENGINEERING

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:

At the end of the course the students should be able to:


1. Write ALP for data transfer, arithmetic, logical operations.
2. Write ALP for delay, code conversions.
3. Write ALP for Serial interface, counters
4. Write C program for waveform generation, ADC, Stepper motor, Speed control of
DC motor, LCD interface, Elevator interface.
5. Develop self learning programs and algorithm for home automation or home
security.

PROGRAM SPECIFIC OUTCOMES (PSO)


EEE Graduates will:
• Analyze, design and provide an engineering solution in the areas involving electrical
machines, power generation, power electronics, transmission & distribution and
energy management.

• Analyze and design controllers for electrical system using analog and digital
electronics circuits and systems..

DEPT OF EE, B.N.M.I.T. PAGE NO: 2


MICROCONTROLLER LAB MANUAL - 17EEL57

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

DEPT OF EE, B.N.M.I.T. PAGE NO: 3


MICROCONTROLLER LAB MANUAL - 17EEL57

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:

Expand the icon <Target 1>

➢ 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:

DEPT OF EE, B.N.M.I.T. PAGE NO: 4


MICROCONTROLLER LAB MANUAL - 17EEL57

➢ 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.

DEPT OF EE, B.N.M.I.T. PAGE NO: 5


MICROCONTROLLER LAB MANUAL - 17EEL57

➢ The select <DEBUG> & select <START/STOP Debug Session>. In the popup
that appears select <OK>. The next screen will be as below:

➢ In the memory window at the bottom right use address as


• D:nn for internal data memory entries.
• C:nnnn for code memory entries.
• X:nnnn for external memory entries.

➢ 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

Run to cursor line


Run Halt Step into Step over

➢ Observe the result at the desired location.

DEPT OF EE, B.N.M.I.T. PAGE NO: 6


MICROCONTROLLER LAB MANUAL - 17EEL57

➢ STOP execution using<HALT> icon on the top command bar which will
appear in RED colour.

DEPT OF EE, B.N.M.I.T. PAGE NO: 7


MICROCONTROLLER LAB MANUAL - 17EEL57

1. Write a program to find the square of a given 8-bit number.

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

Sample input and output:

Input: (40h) = 05 Output: (50H) = 00 (51H) = 19h


------------------------------------------------------------------------------------------------

2. Write a program to evaluate an expression Z = ( X + Y ) / ( X - Y)

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

Sample input and output:

Input: (40h) = X = 05 Quotient=02---stored in A Register


(41H) = Y = 02 Remainder=01 ---- stored in B Register

Output: (B_A) = 01 02h

DEPT OF EE, B.N.M.I.T. PAGE NO: 8


MICROCONTROLLER LAB MANUAL - 17EEL57

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.

Implementation : Location X is 40h, location Y is 42h and location Z is 50h.

Program: ORG 0
SJMP main
ORG 30H

main: CLR C ;(Cy) = 0


MOV A,40h ; (acc) = l81
SUBB A,42h ;(acc) = l81 – l82
MOV 50h,A ; (50h) = l81 – l82
MOV A,41h ; (acc) = U81
SUBB 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

Inputs and results :


(i) Let n1 = 3416h and n2 = 1200h (n1>n2) EXPECTED O/P: 00 22 16
Input: (40) = 16 (42) = 00
(41) = 34 ( 43 ) = 12

Result : (50) = 16
(51) = 22
(52) = 00

(ii) Let n1 = 1200h and n2 = 3416h (n1<n2) EXPECTED O/P : 01 DD EA


Input: (40) = 00 (42) = 16
(41) = 12 ( 43 ) = 34

Result : (50) = DD
(51) = EA (52) = 01

DEPT OF EE, B.N.M.I.T. PAGE NO: 9


MICROCONTROLLER LAB MANUAL - 17EEL57

2. Write a program to ADD 2, 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.

Implementation: Location X is 40h, location Y is 42h and location Z is 50h.

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

Inputs and results : Let n1 = 3416h and n2 = 1200h


Input: (40) = 16 (42) = 00
(41) = 34 (43 ) = 12

Result : (50) = 16
(51) = 46
(52) = 00

DEPT OF EE, B.N.M.I.T. PAGE NO: 10


MICROCONTROLLER LAB MANUAL - 17EEL57

3. Write a program to move a block of ‘n’ bytes of data from location X to


location Y without overlap. The block length ‘n’ is stored in suitable location.

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.

Implementation: R0 is used as P1. R1 is used as P2.


R3 is used as count CNT.
The location X is taken as 40h and location Y as 50h.

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
MOV @R1,A ; and moved to destination.
INC R0 ; increment pointer P1
INC R1 ; increment pointer P2
DJNZ R3,Rpt ; Decrement count
HERE:SJMP HERE
END

Sample inputs and results :

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.

DEPT OF EE, B.N.M.I.T. PAGE NO: 11


MICROCONTROLLER LAB MANUAL - 17EEL57

4. Write a program to interchange ‘n’ bytes of data from location X to location Y.

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.

Implementation: R0 is used as P1. R1 is used as P2.


R3 is used as count CNT.
The location X is taken as 40h and location Y as 50h.

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

Sample inputs and results :

Inputs: outputs:

(40) =11 (50) =21 (40) =21 (50) =11


(41) =12 (51) =22 (41) =22 (51) =12
(42) =13 (52) =23 (42) =23 (52) =13
(43) =14 (53) =24 (43) =24 (53) =14
(44) =15 (54) =25 (44) =25 (54) =15

Note: Students are required to modify the above program for external memory data.

DEPT OF EE, B.N.M.I.T. PAGE NO: 12


MICROCONTROLLER LAB MANUAL - 17EEL57

5. Write a program to convert an ASCII code to Binary. Display the


result in the data field.

Logic: ASCII values BINARY values


30h – 39h 0-9
41h – 46h A-F

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

Inputs: 1. (50) = 36 2. (50) = 43

Outputs: 1. (60) = 06 2. (60) = 0Ch

Homework: Students are required to modify the above program such that it does not
take any input < 30h and >46h

DEPT OF EE, B.N.M.I.T. PAGE NO: 13


MICROCONTROLLER LAB MANUAL - 17EEL57

6. Write a program to convert a BINARY number to ASCII. Display the


result in Data field.

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 .

Implementation: Location 40h is used for the input data.

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

ASCII : CJNE A,#0Ah,Nxt ; compare digit with 0A


LJMP Big0A ; If == go to Big0A
Nxt: JC Less0A ; if <0A go to Less0A
Big0A: ADD A,#37h ; if > = 40 add 37h
RET
Less0A: ADD A,#30h ; if < 40 add 30h
RET
END

Inputs : 1. (40) = 35 2, (40) = 2C

Outputs: 1. (50) = 35 ; (51) = 33 2. (50) = 43 ; (51) = 32.

DEPT OF EE, B.N.M.I.T. PAGE NO: 14


MICROCONTROLLER LAB MANUAL - 17EEL57

7. Write a program to count number of 1’s / 0’s in a given byte. Display


the count on the data field.

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.

Implementation: R2-register is used as c1.


R3-register is used as c2.
Data is stored in location 40h.

Program:
ORG 0
SJMP Main
ORG 30h

Main: MOV A,40H ;(A) = data


MOV R2,#08h ; (R2) = Count c1 for the bit count.
MOV R3,#00 h; (R3) = count c2 for result.
Rpt: RLC A ; Rotate no. left to send the bit to Cy
JNC Next ; If Cy = 0 go to decr c1 skipping c2.
INR R3 ; else increment c2.
Next: DJNZ R2,Rpt ;
MOV 50H,R3; move result into loch 50h
Here: SJMP Here
END

Note: For finding the numbers of zeros in the data replace JNC by JC.

Inputs:

(40) = C3h i.e. 1100 0011b


Output:

(50) = 04, which is the no. of 1’s in data.

Home work: Modify the above program to get 0’s and 1’s from a single program.

DEPT OF EE, B.N.M.I.T. PAGE NO: 15


MICROCONTROLLER LAB MANUAL - 17EEL57

8. Write a program to realize hexadecimal up-down counter.

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

Main: MOV A,#00h


AG: INC A
CJNE A,#0FH,AG
AG1: DEC A
JNZ AG1
HERE:SJMP HERE
END

Result: On execution the number 00 – 0F & 0F-00 is seen in the accumulator.

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

Run to cursor line


Run Halt Step into Step over

DEPT OF EE, B.N.M.I.T. PAGE NO: 16


MICROCONTROLLER LAB MANUAL - 17EEL57

9. Write a program to realize decimal up counter.

Logic: Initialize accumulator as zero. Increment accumulator by 1 and repeat in loop


until (A) = 10 and then start to decrement by 1 until (A) = 0. Use instruction for
decimal adjustment to get decimal result.

Program:
ORG 0
SJMP Main
ORG 30h

Main: MOV A,#00h


AG: LCALL DELAY
ADD A,#01h or INC A
DA A
SJMP AG

DELAY: MOV R1,#0FFH


B3: MOV R2,#0FFH
B2: MOV R3,#0FFH
B1: DJNZ R3,B1
DJNZ R2,B2
DJNZ R1,B3
END

Result: On execution the number 00- 99 is seen in the accumulator.

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

Run to cursor line


Run Halt Step into Step over

DEPT OF EE, B.N.M.I.T. PAGE NO: 17


MICROCONTROLLER LAB MANUAL - 17EEL57

10. Write a program to find average of 5 numbers.

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

MAIN:MOV R0, #40H;(R0) = Pointer the array


MOV R2, #05 ;(R2) = Count
CLR A ;(A) = 00
Ag: ADD A, @R0; Add number pointed by R0 to A
INC R0 ; increment pointer and
DJNZ R2, Ag ; Repeat until count = 0
MOV 0F0, #05; (B) = 05
DIV AB ; (A) = Avg quo; (B) – AVG REM
MOV 50H, A ;
MOV 51H, 0F0H
HERE: SJMP HERE
END

Inputs and Outputs:


Inputs: (40) = 0C ; (41) = 0E ; (42) = 0A; (43) = 06; (44) = 08

Outputs: (50) = 0A
(51) = 00

DEPT OF EE, B.N.M.I.T. PAGE NO: 18


MICROCONTROLLER LAB MANUAL - 17EEL57

11. Write a program to perform the following operation on 3 bytes of data


stored from location X:
a. OR the lower nibble of X with the upper nibble of X+1.
b. Result of a) is Ex-OR with X+2.
Display the result of the above operation in the data field.

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

MAIN:MOV A, 40H ; (A) = (X).


ANL A, #0FH;
MOV 50, A ; (50) = lower digit of X
MOV A, 41H ;
ANL A, #0F0H;
SWAP A ; (A) = Upper digit of X+1
ORL A, 50H
XRL A, 42H
MOV 50H, A
HERE:SJMP HERE
END

Inputs and outputs:

Inputs: (40) = 03
(41) = C3
(42) = 24
Outputs:
(50 ) = 2B.

DEPT OF EE, B.N.M.I.T. PAGE NO: 19


MICROCONTROLLER LAB MANUAL - 17EEL57

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.

Implementation: The input data byte is assumed at location 40h.

Program:
ORG 0
SJMP MAIN
ORG 30h

MAIN:MOV A,40H ; (A) = NO.


SETB 0E0H
SETB 0E1H
CLR 0E2H
CLR 0E3H
CPL 0E6H
CPL 0E7H
MOV 50H,A
HERE: SJMP HERE
END

Input : (40) = 37h ( 0011 0111)

Output: (50) = 1111 0011 (F3)

DEPT OF EE, B.N.M.I.T. PAGE NO: 20


MICROCONTROLLER LAB MANUAL - 17EEL57

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

Inputs and outputs:

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).

DEPT OF EE, B.N.M.I.T. PAGE NO: 21


MICROCONTROLLER LAB MANUAL - 17EEL57

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

MAIN: MOV R0, #40H ; (R0) – pointer to array


MOV R2,#05 ; (R2) – COUNT
MOV 50H,@R0 ;Assume 1st no. as largest
Ag1: DJNZ R2,Ag
LJMP Last
Ag: INC R0
MOV A,@R0
CJNE A,50H,Nxt
LJMP Ag1
Nxt: JC Ag1
MOV 50H,A
LJMP Ag1
Last: SJMP Last
END

Note: To find the smallest replace JNC by JC

Inputs and outputs:

Inputs: (40) = 04
(41) = 12
(42) = 07
(43) = 16
(44) = 14
Output:
(50) = 16.

DEPT OF EE, B.N.M.I.T. PAGE NO: 22


MICROCONTROLLER LAB MANUAL - 17EEL57

15. Write a program to convert a 2-digit BCD number into binary. Display
the result in the data field.

Concept: The result of addition in 8051 is always hexadecimal. The multiplication


instruction gives hexadecimal result only.

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

Inputs and outputs:

Input: (40) = 23

Output: (50) = 17h, which is the hexadecimal equivalent of input number 23d.

DEPT OF EE, B.N.M.I.T. PAGE NO: 23


MICROCONTROLLER LAB MANUAL - 17EEL57

16. Write a program to convert a 8-bit binary number to BCD number.


Display the result in the data field.

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

MAIN:MOV A,40H ; (A) = no.


MOV 0F0H, #64H ;(B) = 100D
DIV AB ;(A)=100’s digit
MOV 50h,A ;(50) = 100’s digit
MOV A, 0F0h ; (A) = rem of no/100
MOV 0F0H, #0AH ;(B) = 10D
DIV AB ;(A) = tens, (B) = units
SWAP A
ADD A, 0F0h ;(A) =packed tens & units
MOV 51h, A
HERE:SJMP HERE
END

Inputs and output:


Input: (40) = FEh

Output: (50) = 02
(51) = 54

DEPT OF EE, B.N.M.I.T. PAGE NO: 24


MICROCONTROLLER LAB MANUAL - 17EEL57

17. Write a program to generate a square wave of 100Hz on Port 1.

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

Delay calculation: T = 1/f = 10ms. Therefore Ton=Toff=5ms.


Required delay is 5ms. The crystal freq is 11.0592MHz.
Time for 1 machine cycle = 12 / 11.0592M = 1.085µS.
Count = 5ms / 1.085µS = 4608.3 ≈ 4608
Count in TH and TL = 65535 – 4608 +1 = 60928 = EE00H
Timer 1 mode 1 is used for delay generation.
Therefore TMOD:

Gate C / T M1 M0 Gate C/T M1 M0

0 0 0 1 0 0 0 0 =10h
Program :

Rpt: MOV A,#00


LCALL Delay
MOV A,#0FFh
LCALL Delay
LJMP Rpt

Delay: MOV 90h,A ; (P1) = (A)


MOV TMOD, #10h
MOV TH1, #0EEH
MOV TL1,#00
SETB TR1
Ag: JNB TF1,Ag
CLR TF1
CLR TR1
RET

Output:

DEPT OF EE, B.N.M.I.T. PAGE NO: 25


MICROCONTROLLER LAB MANUAL - 17EEL57

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.

Implementation: R0-rp is used as pointer to array.


R2-register is used for pass counter
R3-register is used for comparison counter.

Program: ORG 0H
SJMP MAIN
ORG 30H

MAIN:MOV R2,#04 ;(R2) = n-1


Nxtp: MOV 03, R2 ;(R3) = cmp cntr
MOV R0, #40h ;(R0) = ptr to array
Nxtc: MOV A,@R0
INC R0
MOV 50h,@R0
CJNE A, 50h,Nxt
LJMP Noswap
Nxt: JC Noswap
XCH A,@R0
DEC R0
MOV @R0,A
INC R0
Noswap: DJNZ R3,Nxtc
DJNZ R2,Nxtp
HERE:SJMP HERE

Sample Input and output:


Inputs: Outputs:
( 40 ) = 15 (40) = 11
( 41 ) = 11 (41) = 12
( 42 ) = 14 (42) = 13
( 43 ) = 13 (43) = 14
( 44 ) = 12 (44) = 15

DEPT OF EE, B.N.M.I.T. PAGE NO: 26


MICROCONTROLLER LAB MANUAL - 17EEL57

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:

Expand the icon <Target 1>

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>

DEPT OF EE, B.N.M.I.T. PAGE NO: 27


MICROCONTROLLER LAB MANUAL - 17EEL57

7. Double click on FLASHMAGIC on desktop.

8. On the microcontroller board gave select program mode.

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.

DEPT OF EE, B.N.M.I.T. PAGE NO: 28


MICROCONTROLLER LAB MANUAL - 17EEL57

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.

Logic: Digital 00 corresponds to 0v. Digital FF corresponds to 5v. First output 00 to


DAC and call a delay corresponding to Toff. At the end of the delay output FF to DAC
and call a delay corresponding to Ton. Repeat the above in a loop.

Calculations: Assume F = 1khz  T = 1msec.


Given duty cycle = 0.7; we know that DC = Ton / T
Therefore Ton = 0.7msec and Toff = 0.3 msec.
Ton: n=Ton/Tcry=0.7m/1.085µs=645d; Count in TH &TL=65535-645+1=64891d=FD7B
Toff: n=Ton/Tcry=0.3m/1.085µs=277d; Count in TH &TL=65535-277+1=65259d=FEEB

Interface diagram:

Program:

#include<reg51.h>
void delay1( ),delay2( );
void main( )
{ while(1)
{ P2 = 0x00;
delay1( );
P2 = 0xFF;
delay2( );
}

DEPT OF EE, B.N.M.I.T. PAGE NO: 29


MICROCONTROLLER LAB MANUAL - 17EEL57

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;
}

Expected wave form:

Result: On execution a square wave is obtained as indicated above.

DEPT OF EE, B.N.M.I.T. PAGE NO: 30


MICROCONTROLLER LAB MANUAL - 17EEL57

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.

Program: a) Saw tooth waveform:


#include<reg51.h>
void main( )
{ unsigned char i;
while(1)
{ for(i=0;i<255;i++)
P2 = i;
}
}

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:

DEPT OF EE, B.N.M.I.T. PAGE NO: 31


MICROCONTROLLER LAB MANUAL - 17EEL57

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:

DEPT OF EE, B.N.M.I.T. PAGE NO: 32


MICROCONTROLLER LAB MANUAL - 17EEL57

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.

Logic: Digital 00 corresponds to 0v. Digital FF corresponds to 5v. First output 00 to


DAC and call a delay corresponding to Toff. At the end of the delay output FF to DAC
and call a delay corresponding to Ton. Repeat the above in a loop.

Calculations: Assume F = 1khz  T = 1msec.


Given duty cycle = 0.5; we know that DC = Ton / T
Therefore Ton = Toff = 0.5msec.
Count n=0.5 ms/1.085µs=461d; count in TH0 & TL0= 65535-461+1=65075d=FE33h

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;
}

Expected wave form:

DEPT OF EE, B.N.M.I.T. PAGE NO: 33


MICROCONTROLLER LAB MANUAL - 17EEL57

22. Write a program to control the speed of a DC motor using PWM


technique

Logic: In PWM (Pulse width modulation) technique a square wave is applied as an


input to the motor. The average voltage of square wave is directly proportional to
the pulse width of the square wave. If Ton is large the average voltage is large and
hence speed is more and if Toff is small the average voltage is small and hence
speed is less. The frequency of the square wave is 5Hz and is applied to the motor
through the pin P2.4. The time period of the wave is 1/f = 200ms. The base delay
is selected as 5ms. The count for 200ms is 40.
Ton and Toff is realized as n*5ms.
If switch at P1.6 = 1 then Ton=150ms and Toff = 50ms and the corresponding
count are 30 and 10.
If switch at P1.6 = 0 then Ton=60ms and Toff = 140ms and the corresponding
count are 12 and 28.
Mode 1, Timer 0 is used for 5ms delay.
TMOD
0 0 0 0 0 0 0 1 = 01h

N = delay required / 1.085µs = 5ms / 1.085µs = 4608d


Count in TH and TL = FFFF – n + 1 ≡ 65535 – n +1 = 65535-4608+1 = 60928
i.e, EE00h

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()

DEPT OF EE, B.N.M.I.T. PAGE NO: 34


MICROCONTROLLER LAB MANUAL - 17EEL57

{
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;
}

DEPT OF EE, B.N.M.I.T. PAGE NO: 35


MICROCONTROLLER LAB MANUAL - 17EEL57

23. Write a program to realize anticlockwise rotation from a stepper motor.


The stepper motor should stop when a switch connected to P1.6 is ON.

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:-

Phase P2.3 P2.2 P2.1 P2.0 value


1 0 1 1 1 7
2 1 0 1 1 b
3 1 1 0 1 D
4 1 1 1 0 e

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( );
}
}

DEPT OF EE, B.N.M.I.T. PAGE NO: 36


MICROCONTROLLER LAB MANUAL - 17EEL57

}
}
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.

NOTE: For clockwise rotation the array is to be defined as


arr[]={0x07,0x0b,0x0d,0x0e}; rest all are same as above program.

DEPT OF EE, B.N.M.I.T. PAGE NO: 37


MICROCONTROLLER LAB MANUAL - 17EEL57

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.

DEPT OF EE, B.N.M.I.T. PAGE NO: 38


MICROCONTROLLER LAB MANUAL - 17EEL57

32. Write a program to simulate a movement of lift using elevator


interface.

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

DEPT OF EE, B.N.M.I.T. PAGE NO: 39


MICROCONTROLLER LAB MANUAL - 17EEL57

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;
}

DEPT OF EE, B.N.M.I.T. PAGE NO: 40


MICROCONTROLLER LAB MANUAL - 17EEL57

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++);
}

DEPT OF EE, B.N.M.I.T. PAGE NO: 41


MICROCONTROLLER LAB MANUAL - 17EEL57

33. Write a program to display a message “BNMITEE” on the given LCD


interface.

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:-

µc 8051 LCD interface

P0 D0 – D7

P2.4 RS

P2.5 RW

P2.6 EN

RS A ‘0’ selects command register.


A ‘1’ selects data register.

RW A ‘0’ indicates write operation.


A ‘1’ indicates read operation.

EN Should be a pulse for enabling the LCD

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.

DEPT OF EE, B.N.M.I.T. PAGE NO: 42


MICROCONTROLLER LAB MANUAL - 17EEL57

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++);
}

DEPT OF EE, B.N.M.I.T. PAGE NO: 43


MICROCONTROLLER LAB MANUAL - 17EEL57

34. Write a program demonstrate an A/D conversion using ADC interface.

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

DEPT OF EE, B.N.M.I.T. PAGE NO: 44


MICROCONTROLLER LAB MANUAL - 17EEL57

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++);
}

DEPT OF EE, B.N.M.I.T. PAGE NO: 45


MICROCONTROLLER LAB MANUAL - 17EEL57

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.

DEPT OF EE, B.N.M.I.T. PAGE NO: 46


MICROCONTROLLER LAB MANUAL - 17EEL57

36. Write a program to realize a RTC(Real Time Clock).

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

delay: MOV 89h,#01


MOV 8ch,#4bh
MOV 8ah,#0fdh Subroutine for a delay of 50ms using
SETB TR0 Timer0
wt: JNB TF0,wt
CLR TR0
CLR TF0
RET
END

DEPT OF EE, B.N.M.I.T. PAGE NO: 47


MICROCONTROLLER LAB MANUAL - 17EEL57

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?

DEPT OF EE, B.N.M.I.T. PAGE NO: 48


MICROCONTROLLER LAB MANUAL - 17EEL57

46. What is the advantage of using C for 8051 program development?


47. What are the various data types of 8051 C?
48. What are the ranges of representation with various data types of 8051 C?
49. What is the meaning of the instruction while (1)?
50. How can infinite loops be defined in ALP and 8051 C?
51. What are the different operators in 8051 C?
52. What is the difference between Timer and Counter operation?
53. What is the typical operating frequency of 8051?
54. What is a machine cycle?
55. How many timers are available in 8051?
56. What are the various modes of operation of the timers of 8051?
57. Mention the significance of mode 2 operation of 8051.
58. What are the various SFR’s wrt to timers of 8051?
59. Write the configuration of SMOD and its value to configure timer 1 as timer in mode 2.
60. What is the importance of gate bit in SMOD?
61. What is the use of TCON SFR?
62. When does the Timer overflow flag gets set in the timer operation?
63. What is the difference between serial data transfer and parallel data transfer?
64. What is the difference between simplex, half duplex and full duplex serial
communication?
65. What is the data format in Asynchronous serial communication?
66. What are the logic levels of RS232 standards?
67. Which IC is used as a driver between TTL and RS232 logic levels?
68. What are the various modes of operation of 8051 serial communication?
69. Mention the SFR used for serial communication.
70. What is a baud rate?
71. How the baud rate can be varied?
72. Which timer is used for baud rate setting?
73. How can the baud rate be doubled? Write the necessary code.
74. What is the significance of RI and TI bit in SCON?
75. Why timer 2 is always used in mode 2 for serial communication baud rate setting?
76. What are interrupts?
77. Mention the various interrupts of 8051.
78. What are vectored interrupts?
79. What are the Interrupt handler address/vectored address for the various interrupts in
8051?
80. What is the advantage of using interrupt?
81. What is the default priority of 8051 interrupts?
82. Mention the SFR used with respect to interrupts?
83. IE-SFR has a value 85h. Mention its significance.
84. What is meant by interfacing?
85. Mention the features of a DAC.
86. Mention the features of a ADC.
87. What type of ADC is used in the lab interface?
88. What types of DAC is used in the lab interface?

DEPT OF EE, B.N.M.I.T. PAGE NO: 49


MICROCONTROLLER LAB MANUAL - 17EEL57

89. What is a stepper motor? How is it different from normal motor?


90. 1 complete rotation of the stepper motor requires _______ steps. Assume 1 step = 1.8˚)
91. What is the advantage of LCD over LED display?
92. What is the significance of the various commands of LCD interface?
93. How the direction of rotation of a DC motor can be changes? What is PWM?

DEPT OF EE, B.N.M.I.T. PAGE NO: 50


MICROCONTROLLER LAB MANUAL - 17EEL57

1. Arithmetic Group of instructions

DEPT OF EE, B.N.M.I.T. PAGE NO: 51


MICROCONTROLLER LAB MANUAL - 17EEL57

2. Logical Group of instructions

3. Boolean variable instructions

DEPT OF EE, B.N.M.I.T. PAGE NO: 52


MICROCONTROLLER LAB MANUAL - 17EEL57

4. Data transfer Group of instructions

DEPT OF EE, B.N.M.I.T. PAGE NO: 53


MICROCONTROLLER LAB MANUAL - 17EEL57

5. Program branching instructions

DEPT OF EE, B.N.M.I.T. PAGE NO: 54

You might also like