You are on page 1of 48

Microcontroller Lab Manual

GOVERNMENT ENGINEERING COLLEGE,


BHUJ

ELECTRIAL DEPARTMENT

Microcontroller Lab Manual


Semester - VI

Name of student:..
Enrollment no.:.

EED, GEC, Bhuj Page 1


Microcontroller Lab Manual

INDEX

Sr.
Description Page No. Date Sign
No.
1 8 Bit Addition and Subtraction

2 Multi-Byte Addition

3 Multiplication of Two Numbers

4 Find the Largest Number in a array of numbers

5 Arranging the array of numbers in ascending order

6 BCD to Hexadecimal conversion

7 Hexadecimal to BCD conversion

8 Hexadecimal to ASCII code conversion

9 Frequency Measurement

10 Data transfer between 8051 microcontroller kit and


IBM PC using serial port.
11 Matrix Keyboard interfacing with 89C51

12 LCD interfacing with 89C51

13 Seven segment Display interfacing with 89C51

14 Interfacing relay and Pushbutton to the 8951


controller
15 C programming of 8051 Microcontroller

EED, GEC, Bhuj Page 2


Microcontroller Lab Manual

FLOWCHART: Addition of two 8 bit data without carry

START

Move the Data1 to Acc. Register

Add Data2 with Acc. Reg and the result


stored in Acc. Reg

Move the Result from Acc. Reg


to Memory Location 30h

Stop

EED, GEC, Bhuj Page 3


Microcontroller Lab Manual

LOWCHART: Addition of two 8 bit data with carry

START

Move the Data1 from


mem.Location 40H to Acc. reg

Add Acc. Reg with 41H add. location


Result stored in Acc. Reg

Move the Result from Acc. Reg


to Memory Location 42H

Move the Data 00 to 43H Mem.Location

NO Check: C=1
?
Yes

Increment 43H Mem. Location

Stop

EED, GEC, Bhuj Page 4


Microcontroller Lab Manual

EX. NO: 1 8 BIT Addition and Subtraction

Aim:

To write and execute an assembly language program to add the given two 8 bit
numbers.

Objectives:
To multiply any two 8 bit numbers using immediate addressing and to store the result

Apparatus Required:

No Name Quantity
1 8051 microcontroller kit & Simulator 1

Algorithm:

Addition without carry

1. Start the Program.


2. Move the Data1 to the accumulator.
3. Add the Data2 with the accumulator, the result stored in accumulator.
4. Move the result which is in accumulator to the 30H address location.
5. Stop the Program.

Addition with carry

1. Start the Program.


2. Move the Data1 from 40H Memory location to A Register.
3. Add the 41H mem.location with the accumulator, the result is in accumulator.
5. Move the result which is in accumulator to the 42H address location.
6. Clear the 43H memory location
7. Check the carry flag, if carry is there, then increment 43H memory location
8. Stop the Program.

EED, GEC, Bhuj Page 5


Microcontroller Lab Manual

Program: Addition of two 8 bit data without carry

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
MOV A, #Data1
ADD A, #Data2
MOV 30H, A
HLT: SJMP HLT
END

INPUT: OUTPUT:
Memor
Data Memory Address Data
y
addres
-- Data1= 30H
s
-- Data2=

Program: Addition of two 8 bit data with carry

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
MOV A,40H
ADD A, 41H
MOV 42H,A
CLR 43H
JNC HLT
INC 43H
HLT: SJMP HLT
END

INPUT: OUTPUT:
Memory
Data Memory Address Data
address
40H 42H Data
41H 43H Carry

EED, GEC, Bhuj Page 6


Microcontroller Lab Manual

Program: Subtract a small number from a large number

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
MOV A, #Data1 ;Data1=
SUBB A, #Data2 ;Data2=
MOV 30H, A
HLT: SJMP HLT
END

INPUT: OUTPUT:
Memory
Data Memory Address Data
address
-- Data1= 30H
-- Data2=

Result:

Thus an assembly language program 8 bit addition and subtraction were written
and executed successfully.

EED, GEC, Bhuj Page 7


Microcontroller Lab Manual

Flowchart: Multi -Byte addition with carry

START

Move the Lower order byte of Data1 to Acc. Reg

Add Acc. Reg with Lower order byte of Data2

Move the Lower order byte result from Acc.


Reg to the Memory location 50H

Move the Higher order byte of Data1 to Acc. Reg

Add Acc. Reg and Higher order byte of Data2 with carry

Move the Higher order byte result from Acc.


Reg to the Memory location 51H

Move the Data 00 to 52H Mem.Location

NO Check: C=1
?
Yes

Increment 52H Mem. Location

Stop

EED, GEC, BHUJ Page 8


Microcontroller Lab Manual

EX. NO: 2 Multi-Byte Addition

Aim:
To write and execute an assembly language program to add two 16 bit numbers.

Objectives:
Get two 16 bit data from the starting locations of 40h and 42h and then add those data and
store in the location of 50H onwards.

Apparatus Required:

No Name Quantity
1 8051 microcontroller kit & Simulator 1

Algorithm:

1. Start the program.

2. Move the first data lower order byte to A register.

3. Add the second data lower order byte with A register.

4. Move the lower order byte result data from A reg to 50h Memory location.

5. Move the first data higher order byte to A register.

6. Add A reg. and Second higher order byte with carry flag.

7. Move the higher order byte result data from A reg to 51H Memory location.

8. Clear 52H Memory location.

9. Check the carry flag; if it is one, increment 52h address location.

10. Stop the program

EED, GEC, BHUJ Page 9


Microcontroller Lab Manual

Program: Multi-Byte Addition.

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
CLR C
MOV A, 40h ;LSB Data1
ADD A, 42h ;LSB Data2
MOV 50H,A
MOV A, 41H ;MSB Data1
ADDC A, 43H ;MSB Data2
MOV 51H,A
CLR 52H
JNC HLT
INC 52H
HLT: SJMP HLT
END

INPUT: OUTPUT:
Memory
Data Memory Address Data
address
40H 50H Lower order byte=
41H 51H Higher order byte=
42H 52H Carry=
43H

RESULT:

Thus an assembly language program is written to add Multi Byte numbers and executed
successfully.

EED, GEC, BHUJ Page 10


Microcontroller Lab Manual

FLOWCHART: Multiplication of two 8 bit numbers.

START

Move the Data1 from


Mem.Location 8250 to B reg

Move the Data2 from


mem.Location 8251 to Acc. reg

Multiply Acc. Reg with B


Reg.

Move the Result from Acc. Reg


to Memory Location 8300h

Move the Result from B. Reg


to Memory Location 8301h

Stop

EED, GEC, BHUJ Page 11


Microcontroller Lab Manual

EX. NO: 3 8 Bit Multiplication

Aim:
To write and execute an assembly language program to multiply the given two 8 Bit
Numbers.

Objectives:
To multiply the give two 8 Bit numbers using MUL command

Apparatus Required:
No Name Quantity
1 8051 microcontroller kit & Simulator 1

Algorithm:

1. Move the multiplicand to accumulator.

2. Move the multiplier to B register (SFR with direct address FOH)

3. Multiply the contents of Accumulator and B register.

4. Store the lower byte result from A register to 8300 memory location

5. Store the higher byte result from B register to 8301 memory location

6. Stop or Halt the program execution.

EED, GEC, BHUJ Page 12


Microcontroller Lab Manual

Program: Two 8 Bit Multiplication

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
MOV DPTR,#8250H
MOVX A, @DPTR
MOV B,A
INC DPTR
MOVX A, @DPTR
MUL AB
MOV DPTR,#8300H
MOVX @DPTR, A
INC DPTR
MOV A,B
MOVX @DPTR,A
HLT: SJMP HLT
END

INPUT: OUTPUT:
Memory
Data Memory Address Data
address
8250H 8300H Lower order byte=
8251H 8300H Higher order byte=

Result:

Thus an assembly language is written to multiply two 8 bit numbers and executed
successfully.

EED, GEC, BHUJ Page 13


Microcontroller Lab Manual

FLOW CHART: Find the Largest number from an array of numbers

START

Move the First data to R1 register

Increment Data Pointer

Move the Next data to Acc register

Subtract R1 Reg from Acc Reg

Yes
Check C=1?
No

Exchange the R1 Data and subtracted Data

No
Check R0 = 0?

Move the Data from R1 Reg to 8300H Mem. Location

Stop

EED, GEC, BHUJ Page 14


Microcontroller Lab Manual

EX. NO: 4 Find the Largest Number in an array

Aim:

To write and execute an assembly language program to add the given array of
8 bit numbers.

Apparatus Required:
No Name Quantity
1 8051 microcontroller kit & Simulator 1

Algorithm:
1. Get first element to R1 register
2. Get the next element in accumulator.
3. Subtract the R1 register from the accumulator.
4. Check the carry flag.
5. If carry is not there, then A register is greater that means 2nd data is larger than
st
1 Data. So exchange the A and B register.
6. If carry is there, then B register is greater that means 1st data is larger than 2nd
Data. So do not exchange the A and B register.
7. Check the R0 register. If R0 is not equal to zero, repeat from the 2nd step else
Move the R1 register to 8300 memory location.

8. Stop the program.

EED, GEC, BHUJ Page 15


Microcontroller Lab Manual

Program: Find the Largest number from an Array.

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
MOV R0,#04H ;Nos. of Data-1
MOV DPTR,#8251H
MOVX A, @DPTR
MOV R1,A
L2: INC DPTR
MOVX A,@DPTR
SUBB A,R1
JC L1
MOVX A,@DPTR
XCH A,R1
L1: DJNZ R0,L2
MOV A,R1
MOV DPTR,#8300h
MOVX @DPTR, A
HLT: SJMP HLT
END

Input: Output:
Memory
Data
address Memory
8251 Data1= Data
address
8300 Largest Number=
8252 Data2=
8253 Data3=
8254 Data4=
8255 Data5=

Result:
Thus an assembly language program is written to find largest number from an array and
executed successfully.

EED, GEC, BHUJ Page 16


Microcontroller Lab Manual

FLOW CHART: Arrange the array of numbers in ascending order

START

Load the R6 register with (length of the array 1)

Load the R7 register with (length of the array Iteration no.)

Load the R0 register with array starting address

Move the first data to R1 register

Increment R0

Move the Next data to Acc register

Subtract R1 Reg from Acc Reg

NO
Check C=1?
Yes

Exchange the R1Data and subtracted Data

No
Check R7 =0?
Yes

No
Check R6=0?

Yes

Stop

EED, GEC, BHUJ Page 20


Microcontroller Lab Manual

EX. NO: 5 Arrange the given 8 bit array in Ascending Order

Aim:

To write and execute the program to sort an array of number using the Bubble Sort
algorithm in ascending order.

Apparatus Required:

No Name Quantity
1 8051 microcontroller kit & Simulator 1

Algorithm:
1. Load the R6 register with (length of the array 1)
2. Load the R7 register with (length of the array Iteration no.)
3. Load the R0 register with array starting address
4. Load R1 register with first data and accumulator with second data
5. Subtract the R1 register from the accumulator.
6. Check the carry flag.
7. If carry is not there, then A register is greater that means 2nd data is larger than 1st
Data. So do not exchange the A and R1 register.
8. If carry is there, then R1 register is greater that means 1st data is larger than 2nd
Data. So exchange the First address location data and second address location data.
9. Decrement and Check the R7 register. If R7 is not equal to zero, repeat from the 4th
step for second and third data processing. Else Decrement R6.

10. Check the R6 register. If R6 is not equal to zero, repeat from the 4th step for
second iteration.

11. stop the program.

EED, GEC, BHUJ Page 21


Microcontroller Lab Manual

Program: Find the Largest number from an Array.

Mem.ad Hex Label Mnemonics - Operend(s) - Comments


dress code(s)
ORG 0000h
MOV R6, #04h ;Counter for LOOP1 =Nos. of elem. - 1
LOOP1: ;Outer Loop - Handles number of passes
MOV R0, #50h ;Point to beginning of array
MOV A, R6 ;Initialize R7
MOV R7, A
LOOP2: ;Inner Loop - Handles each pass.

MOV A, @R0
MOV R1, A
INC R0
MOV A, @R0
SUBB A, R1 ;Subtract the first from the second.
JNC Continue2 ;If no carry is generated the second is
:greater and the numbers are in order with
;respect to each other. Otherwise swap
SwapNumbers:
MOV A, @R0
XCH A, R1
MOV @R0, A
DEC R0
MOV A, R1
MOV @R0, A
INC R0
Continue2:
DJNZ R7, LOOP2
Continue1: DJNZ R6, LOOP1
Here: SJMP Here
END

INPUT OUTPUT
Memory address Data Memory address Data
50H 50H
51H 51H
52H 52H
53H 53H
54H 54H

Result:
Thus an assembly language program was written to arrange 8 bit array of data in
ascending order and it was executed successfully.

EED, GEC, BHUJ Page 22


Microcontroller Lab Manual

FLOW CHART: BCD to Hex conversion

START

Move the Data to Acc. Reg from8300 memory location

Move the MSB of Acc to R1 Reg

Move the LSB of Acc to R2 Reg

Multiply the MSB by 0Ah


And store the result in Acc. reg

ADD the Acc with LSB

Move the HEX output to 8300 Memory location

Stop

EED, GEC, BHUJ Page 24


Microcontroller Lab Manual

EX. NO: 6 BCD TO HEX CONVERSON

Aim:
Write an assembly language program to convert 8 bit two digit BCD number system
into Hexadecimal number system.

Apparatus Required:
No Name Quantity
1 8051 microcontroller kit & Simulator 1

Algorithm:

1. Start the Program


2. Get the data from the 8300 memory location to R5 register. (R5 = 99).
3. Separate the MSB and LSB of R5 register using ANL 0Fh and ANL F0h commands.
4. Move the MSB to Accumulator and LSB to R2 register.( A = 09, R2 = 09)
5. Multiply the MSB with 0Ah Data.(09*0A) = 5A)
6. Add the Accumulator with LSB of the Data. (5A+9 = 63)
7. Move the Accumulator to 8301 memory location.(8301 = 63)
8. Stop the program.

Program: BCD to Hex conversion

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
MOV DPTR,#8300h
MOVX A, @DPTR
MOV R5,A
ANL A, #0F0h
SWAP A
MOV R1,A
MOV A,R5
ANL A, #0Fh
MOV R2,A
MOV A,R1
MOV B, #0Ah
MUL AB
ADD A,R2
INC DPTR
MOVX @DPTR, A
HLT: SJMP HLT
END

EED, GEC, BHUJ Page 25


Microcontroller Lab Manual

INPUT:

Memory address Data


8300 BCD Data=

OUTPUT:

Memory address Data


8301 HEX Data=

Result:

Thus the assembly language program was written to verify the Boolean equation and it was
executed.

EED, GEC, BHUJ Page 26


Microcontroller Lab Manual

FLOW CHART: HEX to BCD conversion

START

Move the Data to Acc. Reg from 8300 memory location

Divide the Acc. Reg by 64h

Move the Data from Acc. Reg to 8301 memory location

Move the Data from B reg to Acc. Reg

Divide the Acc. Reg by 0Ah

Change the Position of MSB and LSB of A Reg

Add A reg with B reg

Move the Data from Acc. Reg to 8302 memory location

Stop

EED, GEC, BHUJ Page 27


Microcontroller Lab Manual

EX. NO: 7 HEX TO BCD CONVERSION

Aim:

To convert the given hexadecimal value to its equivalent BCD conversion.

Apparatus Required:

No Name Quantity
1 8051 microcontroller kit & Simulator 1

Algorithm:
1. Start the program.
2. Move the data from memory location 8300 to Accumulator.(A = FFh)
3. Divide the accumulator with 64h( FF/64 ( A= 2 and B = 37h )
4. Move the accumulator to 8301h ( 8301 = 02)
5. Move the B reg to accumulator (A = 37h)
6. Divide the accumulator with 0Ah (37/0A ( A=05 and B = 05 )
7. Swap the Accumulator.(A = 50)
8. Add the accumulator with B register.(A = 50+5)
9. Move the accumulator to 8302h(8302 = 55)
10. Stop the program.

EED, GEC, BHUJ Page 28


Microcontroller Lab Manual

Program: HEX to BCD conversion

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
MOV DPTR,#8300h
MOVX A, @DPTR
MOV B,#64h
DIV AB
INC DPTR
MOVX @DPTR,A
MOV A,B
MOV B, #0Ah
DIV AB
SWAP A
ADD A,B
INC DPTR
MOVX @DPTR, A
HLT: SJMP HLT
END

INPUT:

Memory address Data


8300 HEX Data=

OUTPUT:

Memory address Data


8301 Higher BCD Data=
8302 Lower BCD Data=

Result
Thus the assembly language program was written to convert the hexadecimal value
into its equivalent BCD value and it was executed successfully.

EED, GEC, BHUJ Page 29


Microcontroller Lab Manual

FLOW CHART: HEX to ASCII conversion

START

Move the Data to Acc. Reg from 8300 memory location

Divide the Acc. Reg by 64h and Add the A Reg with 30h

Move the Data from Acc. Reg to 8301 memory location

Move the Data from B reg to Acc. Reg

Divide the Acc. Reg by 0Ah and Add the A Reg with 30h

Move the Data from Acc. Reg to 8302 memory location

Move the Data from B Reg to A Reg

Add the A Reg with 30h

Move the Data from Acc. Reg to 8303 memory location

Stop

EED, GEC, BHUJ Page 30


Microcontroller Lab Manual

EX. NO: 8 Hexadecimal to ASCII conversion

Aim:

To convert the given hexadecimal value to its equivalent ASCII code.

Apparatus Required:

No Name Quantity
1 8051 microcontroller kit & Simulator 1

Algorithm:
1. Start the program.
2. Move the data from memory location 8300 to Accumulator.(A = FFh)
3. Divide the accumulator with 64h( FF/64 ( A= 2 and B = 37h )
4. Add the accumulator with 30h
5. Move the accumulator to 8301h ( 8301 = 32)
6. Move the B reg to accumulator (A = 37h)
7. Divide the accumulator with 0Ah (37/0A ( A=05 and B = 05 )
8. Add the accumulator with 30h
9. Move the accumulator to 8302h(8302 = 35)
10. Move B reg to Accumulator.
11. Add the accumulator with 30h
12. Move the accumulator to 8303h(8303 = 35)
13. Stop the program.

EED, GEC, BHUJ Page 31


Microcontroller Lab Manual

Program: HEX to ASCII conversion

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
MOV DPTR,#8300h
MOVX A, @DPTR
MOV B,#64h
DIV AB
ORL A, #30h
INC DPTR
MOVX @DPTR,A
MOV A,B
MOV B, #0Ah
DIV AB
ORL A, #30h
INC DPTR
MOVX @DPTR,A
MOV A,B
ORL A, #30H
INC DPTR
MOVX @DPTR, A
HLT:SJMP HLT
END

INPUT:

Memory address Data


8300 HEX Data=

OUTPUT:

Memory address Data


8301 ASCII Data=
8302 ASCII Data=
8303 ASCII Data=

Result
Thus the assembly language program was written to convert the hexadecimal value
into its equivalent ASCII value and it was executed successfully.

EED, GEC, BHUJ Page 32


Microcontroller Lab Manual

Circuit Diagram:

Frequency Measurement

EED, GEC, BHUJ Page 33


Microcontroller Lab Manual

EX. NO: 9 Frequency Measurement

Aim:

Write a program to measure frequency.

Apparatus Required:

No Name Quantity
1 8051 microcontroller kit & Simulator 1
2 Frequency source 1

Algorithm:
1. Set the Timer 0 in mode 1 as a counter.
2. Initialize TH0/TH1timer with zero values.
3. Start timer.
4. Wait for 1 second.
5. Value in TH0/TH1 shows frequency; store that in P0 and P1.
6. Continue from step 2 for continue measurement of frequency.

Program: Timer 0 used as a counter

Memory Hex Label Mnemonics - Operend(s) - Comments


address code(s)
ORG 0000H
MAIN: MOV TMOD,#05H ; TIMER-0,MODE-1, COUNTER
MOV TL0, #00H
MOV TH0, #00H
SETB TR0
ACALL DLY
CLR TR0
MOV P2,TL0
MOV P0,TH0
AJMP MAIN
DLY: MOV R3, #08H
L3: MOV R4, #00H
L2: MOV R5, #00H
L1: DJNZ R5, L1
DJNZ R4, L2
DJNZ R3, L3
RET
END

Result:
The value of measured frequency=

EED, GEC, BHUJ Page 34


Microcontroller Lab Manual

Circuit Diagram:

SERIAL COMMUNICATION

EED, GEC, BHUJ Page 35


Microcontroller Lab Manual

Program -10 SERIAL COMMUNICATION

Aim:
To transfer the data serially between two 8051 microcontroller kit and IBM PC using
serial port (RS232).
Apparatus Required:

No Name Quantity

1 8051 microcontroller kit & Simulator 1


2 RS232C Interfacing kit and wire 1

Theory:

Programming the 8051 to transfer data serially


1. The TMOD register is loaded with the value 20H, indicating the use of timer 1 in mode
2 (8-bit auto-reload) to set the baud rate.
2. The TH1 is loaded with -3H to set the baud rate of 9600 for serial data transfer
3. The SCON register is loaded with the value 50 H, indicating serial mode 1, where an 8-
bit data is framed with start and stop bits.
4. TRI is set to l to start timer l.
5. TI is cleared by the CLR TI instruction.
6. The character byte to be transferred serially is written into the SBUF register.
7. The TI flag bit is monitored with the use of the instruction JNB TI, $ to see if the
character has been transferred completely.
8. To transfer the next character, go to Step 5.

Programming the 8051 to receive date serially


1. The TMOD register is loaded with the value 20H, indicating the use of timer 1 in mode
2 (8-bit auto-reload) to set the baud rate.
2. The TH1 is loaded with -3H to set the baud rate of 9600 for serial data transfer
3. The SCON register is loaded with the value 50 H, indicating serial mode 1, where an 8-
bit data is framed with start and stop bits.
4. TRI is set to l to start timer l.

EED, GEC, BHUJ Page 36


Microcontroller Lab Manual

5. RI is cleared with the CLR RI Instruction.


6. The RI flag bit is monitored with the use of the Instruction JNC RI, $ to see if an
entire character has been received yet.
7. When RI is raised, SBUF has the byte. Its contents are moved into a safe place.
8. To receive the next character, go to Step 5.

Procedure:

1. Connect the 8051 microcontroller kit and IBM PC using RS232 kit.
2. Enter and run the program in 8051 microcontroller kit to transmit GTU serially.
3. Enter and run the program in 8051 microcontroller kit to receive data from PC terminal
program window.
4. Enter and run the program in 8051 microcontroller kit to receive data from PC terminal
program window and sent it to P2 port of 8051 where relay is connected.

Program: Transmitter Program


Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments
ORG 0000H
MOV SCON,#50H
MOV TMOD,#20H
MOV TH1,#-3
SETB TR1

HOME:
MOV A,#'G'
ACALL SEND
MOV A,#'T'
ACALL SEND
MOV A,#'U'
ACALL SEND
AJMP HOME

SEND:
MOV SBUF,A
JNB TI,$
CLR TI
RET

END

EED, GEC, BHUJ Page 37


Microcontroller Lab Manual

Program: Receiver Program


Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments
ORG 0000H
MOV SCON,#50H
MOV TMOD,#20H
MOV TH1,#-3
SETB TR1
HOME:
JNB RI,HOME
CLR RI
MOV P2,SBUF
AJMP HOME
END

Program: Receiver Program-relay interfaced


Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments
ORG 0000H
MOV SCON,#50H
MOV TMOD,#20H
MOV TH1,#-3
SETB TR1
HOME:
JNB RI,HOME
CLR RI
CPL P2.2
AJMP HOME
END

Result:
Thus the assembly language was written to transfer the data serially between micro
controller kit and IBM PC using RS232 cable and it was executed.

EED, GEC, BHUJ Page 38


Microcontroller Lab Manual

Circuit Diagram:

INTERFACING OF 4 X 4 KEYBOARD

EED, GEC, BHUJ Page 39


Microcontroller Lab Manual

Program - 11 INTERFACING OF 4 X 4 KEYBOARD

Aim:
To interface the 4 x 4 matrix keyboard with the 89C51 microcontroller and to verify
the output.

Apparatus Required:

No Name Quantity
1 8051 microcontroller kit 1
2 & Simulator
4x4 matrix keyboard 1

Theory:
In 4*4 keyboard, there are 4 rows and 4 column lines. In the intersection of row and
columns, the keys are wired.

In the board , P2.0-P2.3 are used as row lines and P2.4-P2.7 are used as column
lines.The column lines are read inside the controller through port.

All Column lines are given 0 level and row lines checked for 0 logic. Then All Row
lines are given 0 level and column lines checked for 0 logic. Actual no. of key pressed is (No.
column + 4 * No. row). Then this no. is converted in to seven segment code using look up
table and sent for display.

Procedure:

1. Connections are made as per the diagram shown.


2. Enter the program in the 8051 microcontroller kit & Simulator and execute it.
3. Press any key in the keyboard and execute it again.
4. Verify the result in seven segment LED display.

EED, GEC, BHUJ Page 40


Microcontroller Lab Manual

Program: 4X4 Array Key Board Interfacing

Memory address Hex code(s) Label Mnemonics - Operend(s) Comments


ORG 0000H
ROW0:
MOV p3, #0FH ;C+4R = R1+4R0
JB p3.0, ROW1
MOV R0,#00H
AJMP COLUMN
ROW1:
JB p3.1, ROW2
MOV R0,#01H
AJMP COLUMN
ROW2:
JB p3.2,ROW3
MOV R0,#02H
AJMP COLUMN
ROW3:
JB p3.3, ROW0
MOV R0,#03H
COLUMN:
MOV p3, #0F0H
JB p3.4, COLUMN1
MOV R1,#00H
AJMP DISPLAY
COLUMN1:
JB p3.5, COLUMN2
MOV R1,#01H
AJMP DISPLAY
COLUMN2:
JB p3.6, COLUMN3
MOV R1,#02H
AJMP DISPLAY
COLUMN3:
JB p3.7, ROW0
MOV R1,#03H
DISPLAY:
MOV A, R0
RL A
RL A
ADD A, R1
ACALL CONVERT
MOV P2, A
AJMP ROW0
CONVERT:
INC A
MOVC A, @A+PC

EED, GEC, BHUJ Page 41


Microcontroller Lab Manual

RET
DB 00111111B
DB 00000110B
DB 01011011B
DB 01001111B
DB 01100110B
DB 01101101B
DB 01111101B
DB 00000111B
DB 01111111B
DB 01101111B
DB 01110111B
DB 01111100B
DB 00111001B
DB 01011110B
DB 01111001B
DB 01110001B
END

Result:
Thus the 4 x 4 matrix keyboard was interfaced with 89C51 microcontroller and its output was
verified.

EED, GEC, BHUJ Page 42


Microcontroller Lab Manual

Circuit Diagram:

INTERFACING OF 16X2 CHARACTER LCD DISPLAY

EED, GEC, BHUJ Page 43


Microcontroller Lab Manual

Program -12 INTERFACING OF 16X2 CHARACTER LCD


DISPLAY

Aim:
To interface the LCD display with 89C51 microcontroller and to verify data on LCD
display.

Objectives:
Display Alphabet character in the LCD using micro controller.

Apparatus Required:

No Name Quantity
1 8051 microcontroller kit & Simulator 1
2 LCD display 1

Procedure:

1. Connect the LCD display interface board as required.

2. Enter the program and execute it.

3. The output will be displayed on two LCD display.

Program:
Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments
RSE EQU P1.0
REWR EQU P1.1
ENA EQU P1.2

ORG 0000H
MOV A, #38H
ACALL CONFIG
ACALL DELAY
MOV A, #0EH
ACALL CONFIG
ACALL DELAY
MOV A, #01H
ACALL CONFIG
ACALL DELAY
MOV A, #06H
ACALL CONFIG
AGAIN:
ACALL DELAY
MOV A, #84H
ACALL CONFIG
ACALL DELAY

EED, GEC, BHUJ Page 44


Microcontroller Lab Manual

MOV A, #'G'
ACALL DISPLAY
ACALL DELAY
MOV A, #'T'
ACALL DISPLAY
ACALL DELAY
MOV A, #'U'
ACALL DISPLAY
ACALL DELAY
AJMP AGAIN
CONFIG:
MOV P0, A
CLR RSE
CLR REWR
SETB ENA
ACALL DELAY
CLR ENA
RET
DISPLAY:
MOV P0, A
SETB RSE
CLR REWR
SETB ENA
ACALL DELAY
CLR ENA
RET
DELAY:
MOV R2, #01H
HERE3: MOV R3, #00H
HERE2: MOV R4, #00H
HERE: DJNZ R4, HERE
DJNZ R3, HERE2
DJNZ R2, HERE3
RET
END

Result:
Thus an assembly language program was written to interface LCD display with 89 C 51
micro controller and to verify data on LCD display and it was executed.

EED, GEC, BHUJ Page 45


Microcontroller Lab Manual

Circuit Diagram:

INTERFACING OF SEVEN SEGMENT LED DISPLAY

EED, GEC, BHUJ Page 46


Microcontroller Lab Manual

Program -13 INTERFACING OF SEVEN SEGMENT LED DISPLAY

Aim:
To interface two seven segment LED display in 89C51 microcontroller and to
verify data on LED display.

Objective:
Interface one common cathode type seven segment display with port 1.then write a program
that displays pattern 'E' for 1 second and off for 1 second also, continuously.

Apparatus Required:

No Name Quantity
1 8051 microcontroller kit & Simulator 1
2 Keyboard display interface 1

Procedure:

1. Connect the display interface board as per required.


2. Enter the program.
3. The output would be displayed on two seven segment LED.

Program: Delay loop using loop inside loop

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
MAIN:
MOV P2, #01111001b
ACALL DELAY
MOV P2, #00000000b
ACALL DELAY
AJMP MAIN

DELAY:
MOV R1, #08H ; 1 Second delay loop
LOOP3: MOV R2, #0FFH
LOOP2: MOV R3, #0FFH
LOOP1: DJNZ R3, LOOP1
DJNZ R2, LOOP2
DJNZ R1, LOOP3
RET

EED, GEC, BHUJ Page 47


Microcontroller Lab Manual

END

Program: Delay loop using timer

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
MAIN:
MOV P2, #01111001B
ACALL DELAY
MOV P2, #00000000B
ACALL DELAY
AJMP MAIN

DELAY:
MOV R0,#100D
MOV TMOD,#00000001B
JJ: MOV TH0,#0D8H
MOV TL0,#0F0H
SETB TR0
HH: JNB TF0,HH
CLR TF0
DJNZ R0,JJ
RET
END

Result:
Thus an assembly language program was written to interface two seven segment LED
display with 89 C 51 micro controller and it was executed.

EED, GEC, BHUJ Page 48


Microcontroller Lab Manual

Circuit Diagram:

INTERFACING RELAY AND PUSHBUTTON

EED, GEC, BHUJ Page 49


Microcontroller Lab Manual

Program -14 INTERFACING RELAY AND PUSHBUTTON

Aim:
To interface the pushbutton switch and relay and make relay ON - OFF using
pushbutton switch.

Apparatus Required:

No Name Quantity
1 8051 microcontroller kit & Simulator 1
2 Relay interface board 1

Procedure:

1. Make the connections as per the circuit using interface card.


2. Enter the program and execute.
3. Press the pushbutton and verify the result by seeing the relay ON or OFF.

Program: Transferring data from port to port

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
HOME:
MOV P2,P1
AJMP HOME
END

Program: Odd parity ON

Memory address Hex code(s) Label Mnemonics - Operend(s) - Comments


ORG 0000H
HOME:
MOV A,P1
MOV C,P
MOV P2.1,C
AJMP HOME
END

Result:
Thus the program for interfacing the relay and push button to 8951 microcontroller was
written and executed successfully.

EED, GEC, BHUJ Page 50


Microcontroller Lab Manual

Program -15 C programming of 8051 Microcontroller

Aim:
To study about C programming of 8051 Microcontroller.

Apparatus Required:

No Name Quantity
1 8051 Simulator 1

Programs:

1.Write an 8051 C program to send values 00 FF to port P1.

#include <reg51.h>
void main(void)
{
unsigned char z;
for (z=0;z<=255;z++)
P1=z;
}

2.Write an 8051 C program to send hex values for ASCII characters of 0, 1, 2,


3, 4, 5, A, B, C, and D to port P1.

#include <reg51.h>
void main(void)

{
unsigned char num[] = {'0' , '1' , '2','3' , '4' , '5', 'A' , 'B','C' , 'D' };
unsigned char z;
for(z=0;z<10;z++)
P1=num[z] ;
}

3.Write an 8051 C program to toggle all the bits of P1 continuously.

#include <reg51.h>
void main(void)
{
for (;;)
{
P1=0x55;
P1=0xAA;
}

EED, GEC, BHUJ Page 51


Microcontroller Lab Manual

4.Write an 8051 C program to send values of 4 to +4 to port P1.

#include <reg51.h>
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<=7;z++)
P1=mynum[z];
}

5. Write an 8051 C program to toggle bit D0 of the port P1 (P1.0) 50,000 times.

#include <reg51.h>
sbit MYBIT=P1^0;
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}

Result:

Thus the programs using C language was written and executed successfully.

******

EED, GEC, BHUJ Page 52

You might also like