You are on page 1of 67

Microcontrollers Lab Manual 06ESL47

Exp1
1a.1) Write an a ALP to Move a Block of Data within the Internal RAM:
;30H(RAM location) to 50H(RAM location)

ORG 0000H
MOV R3,#05H ;R3 IS THE COUNTER

MOV 30H,#01H ;load the 30H location by 01


MOV 31H,#02H ;load the 31H location by 02
MOV 32H,#03H ;load the 32H location by 03
MOV 33H,#04H ;load the 33H location by 04
MOV 34H,#05H ;load the 34H location by 05

MOV R0,#30H ;block source address on 30h

MOV R1,#50H ;Destination address 50h

;following routing takes 5 numbers


;starting from 30h location
;send to the 50h location
AGAIN:
MOV A,@R0
MOV @R1,A
INC R0
INC R1
DJNZ R3,AGAIN

SJMP $ ;termination of the program


END

RESULT:
After execution:

Or

Dept. of Electronics & Communication Engg., R.I.T, Hassan 1


Microcontrollers Lab Manual 06ESL47

1a.2) Program For Block Data Transfer from External RAM to Internal RAM:
;Enter The Elements From Location 0500h(Ext.RAM)
ORG 0000H
START1:MOV DPTR,#0500H ;DATA POINTER POINTED TO EXTERNAL MEMORY 0500H
MOV R1,#30H ;R1 POINTING TO INTERNAL RAM 030H
MOV R2,#5 ;R2 LOADED WITH NO. OF ELEMENTS IN THE ARRAY
START:
MOVX A,@DPTR
MOV @R1,A
INC DPTR
INC R1
DJNZ R2,START
SJMP START1
END

RESULT:
Before Execution: After Execution:

1b) Write an ALP to Exchange Data between Internal RAM Locations:

ORG 0000H
MOV R5,#05H ;R5 IS THE COUNTER

MOV 30H,#01H ;load the 30H location by 01


MOV 31H,#02H ;load the 31H location by 02
MOV 32H,#03H ;load the 32H location by 03
MOV 33H,#04H ;load the 33H location by 04
MOV 34H,#05H ;load the 34H location by 05

MOV 50H,#11H ;load the 50H location by 11


MOV 51H,#22H ;load the 51H location by 22
MOV 52H,#33H ;load the 52H location by 33
MOV 53H,#44H ;load the 53H location by 44
MOV 54H,#55H ;load the 54H location by 55

MOV R0,#30H ;block source address on 30h

MOV R1,#50H ;Destination address 50h

;following routing exchange the data from 30h location to 50h location

AGAIN:
MOV A,@R1 ;take data from 50h location
MOV R6,A ;store temp. on R6
MOV A,@R0 ;take data from 30h location
MOV @R1,A ;send it to 50h location
MOV A,R6 ;take stored temp. data
MOV @R0,A ;send to 30h location

Dept. of Electronics & Communication Engg., R.I.T, Hassan 2


Microcontrollers Lab Manual 06ESL47

INC R0 ;increase the memory location 30h


INC R1 ;increase the memory location 30h
DJNZ R5,AGAIN ;decrement the counter and jump up to it not get zero
SJMP $ ;termination of the program

END

RESULT:
Before Execution:

After Execution:

1c.1) Write an ALP to Find the Largest Element in an Array Stored in the
Internal RAM:
;Program For Finding the Largest Element In A Given Array In The
Internal RAM
;finding largest number in given three number
;largest number display on Port 0

ORG 0000H
MOV R0,#02H ;Enter first no.
MOV R1,#09H ;Enter second no.
MOV R2,#0AH ;Enter third no.

;First finding largest no. in reg.R0 & R1

MOV A,R0
SUBB A,R1
JC R1_large

MOV A,R0
SUBB A,R2
JC R2_large
MOV P0,R0 ; R0 is large
SJMP Exit

R1_large:
MOV A,R1
SUBB A,R2
JC R2_large

MOV P0,R1 ;R1 is large


SJMP Exit

Dept. of Electronics & Communication Engg., R.I.T, Hassan 3


Microcontrollers Lab Manual 06ESL47

R2_large:
MOV P0,R2 ;R2 is large
Exit:
HERE: SJMP HERE ;terminate the program
END

RESULT:
Before Execution: After Execution:

1c.2) Write an ALP to Find the Largest Element in an Array Stored in the
External RAM:

ORG 0000H
MOV DPTR,#0100H
MOVX A,@DPTR
MOV R3,A
DEC R3
INC DPTR
BACK: MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
MOV R1,A
CJNE A,00H,SKIP
SJMP HERE
SKIP: JMP HERE
MOV A,R0
MOVX @DPTR,A
DEC DPL
MOV A,R1
MOVX @DPTR,A
INC DPTR
HERE: DJNZ R3,BACK
MOVX A,@DPTR
SJMP $
END
RESULT:
After Execution:

Dept. of Electronics & Communication Engg., R.I.T, Hassan 4


Microcontrollers Lab Manual 06ESL47

1d) Write an ALP to Find the Smallest Element in an Array Stored in the
Internal RAM:
;Program For Finding the Smallest Element In A Given Array In The
Internal RAM
;finding Smallest number in given three number
;Smallest number display on Port 0

ORG 0000H
MOV R0,#02H ;Enter first no.
MOV R1,#09H ;Enter second no.
MOV R2,#0AH ;Enter third no.

;First finding largest no. in reg.R0 & R1

MOV A,R0
SUBB A,R1
JNC R1_large

MOV A,R0
SUBB A,R2
JNC R2_large
MOV P0,R0 ; R0 is large
SJMP Exit

R1_large:
MOV A,R1
SUBB A,R2
JNC R2_large

MOV P0,R1 ;R1 is large


SJMP Exit
R2_large:
MOV P0,R2 ;R2 is large

Exit:
HERE: SJMP HERE ;terminate the program

END

RESULT:
Before Execution: After Execution:

Dept. of Electronics & Communication Engg., R.I.T, Hassan 5


Microcontrollers Lab Manual 06ESL47

1e) Write an ALP to Sort an Array stored in the Internal RAM:


Write an a ALP for Sorting the Elements in Ascending Order in a
Given Array in the Internal RAM
ORG 0000H
MOV R3,#04H
DEC R3

L3: MOV A,R3


MOV R2,A
MOV DPTR,#000CH

L1: MOVX A,@DPTR


MOV R0,A
INC DPTR
MOVX A,@DPTR
MOV R1,A
CJNE A,00H,L2
SJMP SKIP

L2: JNC SKIP


MOV A,R0
MOVX @DPTR,A
DEC DPL
MOV A,R1
MOVX @DPTR,A
INC DPTR

SKIP: DJNZ R2,L1


DJNZ R3,L3
SJMP $
END

RESULT:
Before Execution:

After Execution:

Dept. of Electronics & Communication Engg., R.I.T, Hassan 6


Microcontrollers Lab Manual 06ESL47

Exp-2
2a.1) Write an ALP for Adding Two 16-Bit Data, We need to be concerned
With the Propagation of a Carry from Lower Byte to the Higher Byte.

; The Instruction Addc(Add With Carry) Is Used On Such Occasions.

ORG 0000H
LJMP RESET_VECTOR
ORG 0030H

RESET_VECTOR:

MOV SP,#5FH ; ASSIGN 32 BYTES FOR STACK 60H TO 7FH


MOV IE,#00H ; DISABLE ALL INTERRUPTS

MOV A,#0E7H ;LOAD THE LOW BYTE NOW A=E7H


ADD A,#8DH ;ADD THE LOW BYTE NOW A=74H & CY=1

MOV R6,A ;SAVE THE LOW BYTE OF THE SUM IN R6


MOV A,#3CH ;LOAD THE HIGH BYTE IN A
ADDC A,#3BH ;ADD WITH THE CARRY
;3B+3C+1=78H
MOV R7,A ;SAVE THE HIGH BYTE OF THE SUM

MOV P0,R6 ;DISPLAY LOWERBYTE ANSWER ON THE PORT 0


MOV P1,R7 ;DISPLAY UPPERBYTE ANSWER ON THE PORT 1
SJMP $

END

RESULT:
Before Execution:

After execution:

Dept. of Electronics & Communication Engg., R.I.T, Hassan 7


Microcontrollers Lab Manual 06ESL47

2a.2) Write an ALP for Adding Two 16-Bit Data:

ORG 0000H
MOV R0, #41H
MOV R1, #51H
MOV A, @R1
ADD A, @R0
MOV R2,A
MOV R0, #40H
MOV R1, #50H
MOV A,@R1
ADDC A,@R0
MOV R3,A
JNC L1
CLR A
RLC A
MOV R4,A
L1: LCALL 0003H
END

RESULT:
Before Execution:

After Execution:

2b.1) Write an ALP for Subtracting the Two 16-Bit Data; we need to be
concerned With the Propagation of a Barrow from Lower Byte to the
Higher Byte:

;The Instruction Subb(Sub With Barrow) Is Used On Such Occasions.

ORG 0000H
LJMP RESET_VECTOR
ORG 0030H
RESET_VECTOR:

MOV SP,#5FH ; ASSIGN 32 BYTES FOR STACK 60H TO 7FH


MOV IE,#00H ; DISABLE ALL INTERRUPTS

MOV A,#0E7H ;LOAD THE LOW BYTE NOW A=E7H


SUBB A,#8DH ;ADD THE LOW BYTE NOW A=74H & CY=1

MOV R6,A ;SAVE THE LOW BYTE OF THE SUM IN R6


MOV A,#3CH ;LOAD THE HIGH BYTE IN A
SUBB A,#3BH ;ADD WITH THE CARRY

Dept. of Electronics & Communication Engg., R.I.T, Hassan 8


Microcontrollers Lab Manual 06ESL47

;3B+3C+1=78H
MOV R7,A ;SAVE THE HIGH BYTE OF THE SUM

MOV P0,R6 ;DISPLAY LOWERBYTE ANSWER ON THE PORT 0


MOV P1,R7 ;DISPLAY UPPERBYTE ANSWER ON THE PORT 1
SJMP $
END

RESULT:
After execution:

2b.2) Write an ALP to Find the Subtracting of Two 16-Bit Data:

ORG 0000H
CLR C
MOV R0, #41H
MOV R1, #51H
MOV A,@R0
SUBB A, @R1
MOV R2,A
MOV R0,#40H
MOV R1,#50H
MOV A,@R0
SUBB A,@R1
MOV R3,A
JNC L1
CLR A
RLC A
MOV R4,A
L1: SJMP L1
END

RESULT:
After execution:

Dept. of Electronics & Communication Engg., R.I.T, Hassan 9


Microcontrollers Lab Manual 06ESL47

2c.1) Write an ALP to Find the Multiplication of the Two Numbers:


;FIRST NUMBER ON PORT 0 AND SECOND NUMBER ON THE PORT 1
;ANSWER ON THE PORT 2
ORG 0000H
MOV A,#05H ;A=05H
MOV P0,A
MOV B,#0AH ;B=0AH
MOV P1,B
MUL AB ;A=A*B
MOV P2,A
MOV P3,B
SJMP $
END ;TERMINATE THE PROGRAM

RESULT:
After Execution:

2c.2) Write an ALP to Find the Multiplication of Two 16 Bit Numbers:


ORG 0000H
MOV A,#14H
MOV B,#75H
MUL AB
MOV R0,A
MOV R1,B
MOV A,#0B2H
MOV B,#75H
MUL AB
ADD A,R1
JNC END1
INC B
END1: MOV R1,A
MOV R2,B
MOV A,#14H
MOV B,#0E0H
MUL AB
MOV R3,A
MOV R4,B
MOV A,#0B2H
MOV B,#0E0H
MUL AB
ADD A, R4
JNC END2
INC B

Dept. of Electronics & Communication Engg., R.I.T, Hassan 10


Microcontrollers Lab Manual 06ESL47

END2: MOV R4,A


MOV R5,B
MOV A,R1
ADD A,R3
MOV R1,A
JNC END3
INC R2
END3: MOV A,R2
ADD A,R4
MOV R2,A
JNC END4
INC R5
END4: MOV A,R5
MOV R3,A
MOV R4,#00H
MOV R5,#00H
END

RESULT:
After Execution:

2d) Write an ALP to Find the Division of the Two Numbers:


;FIRST NUMBER ON PORT 0 AND SECOND NUMBER ON THE PORT 1
;ANSWER ON THE PORT 2 AND REMENDER ON PORT 3

ORG 0000H
MOV A,#08H ;A=08H
MOV P0,A
MOV B,#03H ;B=03H
MOV P1,B
DIV AB ;A=A/B
MOV P2,A
MOV P3,B ;REMENDER ON PORT 3
SJMP $
END ;TERMINATE THE PROGRAM

RESULT:
After execution:

Dept. of Electronics & Communication Engg., R.I.T, Hassan 11


Microcontrollers Lab Manual 06ESL47

2e.1) Write an ALP to Find Squaring of an 8 Bit Numbers:


;ENTER THE NUMBER IN THE NUMBER SQUARE
;ANSWER IS ON THE PORT 0
;NOTE ENTER THE NUMBER IN THE RANGE OF 00 TO 0FH

NUMBER_SQU EQU 05H ;ENTER NUMBER HERE (IN PLACE OF 05)

ORG 0000H

MOV A,#NUMBER_SQU ;LOAD THE NUMBER IN ACCUMULATOR


MOV B,A ;B = A
MUL AB ;A=A*B
MOV P0,A ;DISPLAYING THE ANSWER ON THE PORT 0
SJMP $ ;TERMINATE THE PROGRAM

END

RESULT:
Before execution: After Execution:
P0= 0xFFH P1= 0x19H

2e.2) Write an ALP to Find Squaring of a 16 Bit Numbers:

ORG 0000H
MOV A,#17H
MOV B,#2AH
MUL AB
MOV R0,A
MOV R1,B
MOV A,#2AH
MOV B,#2AH
MUL AB
ADD A,R1
JNC END1
INC B
END1: MOV R1,A
MOV R2,B
MOV A,#17H
MOV B,#2AH
MUL AB
MOV R3,A
MOV R4,B
MOV A,#17H
MOV B,#17H
MUL AB
ADD A,R4
JNC END2
INC B
END2: MOV R4,A
MOV R5,B
MOV A,R1
ADD A,R3
MOV R1,A
JNC END3
INC R2

Dept. of Electronics & Communication Engg., R.I.T, Hassan 12


Microcontrollers Lab Manual 06ESL47

END3: MOV A,R2


ADD A,R4
MOV R2,A
JNC END4
INC R5
END4: MOV A,R5
MOV R3,A
MOV R4,#00H
MOV R5,#00H
END

RESULT:
After Execution:

2f.1) Write an ALP to Find the Cube of a Numbers:


;ENTER THE NUMBER IN THE NUMBER SQUARE PLACE
;ANSWER IS ON THE PORT 0
;NOTE ENTER THE NUMBER IN THE RANGE OF O0 TO 06H

NUMBER_SQU EQU 02H ;ENTER NUMBER HERE (IN PLACE OF 05)

ORG 0000H

MOV A,#NUMBER_SQU ;LOAD THE NUMBER IN ACCUMULATOR


MOV B,A ;B = A
MUL AB ;A=A*B
MOV B,#NUMBER_SQU
MUL AB ;A=A*B*B
MOV P0,A ;DISPLAYING THE ANSWER ON THE PORT 0
SJMP $ ;TERMINATE THE PROGRAM

END

RESULT:
Before Execution: After Execution:
P0= 0xFFH P0= 0x08H

Dept. of Electronics & Communication Engg., R.I.T, Hassan 13


Microcontrollers Lab Manual 06ESL47

2f.2) Write an ALP to Find the Cube of a Numbers:

ORG 0000h
MOV A,#0ffh
MOV R7,A
MOV B,A
MUL AB
MOV R0,A
MOV R1,B
MOV A,R7
MOV B,R0
MUL AB
MOV 42h,A
MOV R2,B
MOV A,R7
MOV B,R1
MUL AB
ADD A,R2
MOV 41h,A
MOV 40h,B
END

RESULT:
After execution:

Dept. of Electronics & Communication Engg., R.I.T, Hassan 14


Microcontrollers Lab Manual 06ESL47

Exp3
3a) Write an ALP to perform the operation of Hex-Up Counter:

ORG 0000H
MOV P1,#00H ;P1=00h
BACK: ACALL DELAY ;SUBROUTINE CALL
INC P1 ;P1=P1+1
MOV A,P1 ;A=P1
JNZ BACK ;JUMP TO BACK IF NON ZERO
SJMP HERE ;UNCONDITIONAL JUMP
DELAY:MOV R1,#0FFH ;R1=0FFh
DEC1: MOV R2,#0FFH ;R2=0FFh
DEC2: MOV R3,#0FFH ;R3=0FFh
DJNZ R3,$ ;DECREMENT R3 BY 1 AND JUMP TO NEXT IF NON ZERO
DJNZ R2,DEC2 ;DECREMENT R2 BY 1 AND IF NON ZERO JUMP TO DEC2
DJNZ R1,DEC1 ;DECREMENT R1 BY 1 AND IF NON ZERO JUMP TO DEC1
RET
HERE:
END

RESULT:
Before execution: After Execution:
P1= FFH P1= 00H
01H
..
FFH

3b) Write an ALP to perform the operation of Hex-Down Counter:

ORG 0000H
MOV P1,#0FFH ;P1=FFh
BACK: ACALL DELAY ;SUBROUTINE CALL
DEC P1 ;P1=P1-1
MOV A,P1 ;A=P1
JNZ BACK ;JUMP TO BACK IF NON ZERO
SJMP HERE ;UNCONDITIONAL JUMP
DELAY:MOV R1,#0FFH ;R1=0FFh
DEC1: MOV R2,#0FFH ;R2=0FFh
DEC2: MOV R3,#0FFH ;R3=0FFh
DJNZ R3,$ ;DECREMENT R3 BY 1 AND JUMP TO NEXT IF NON ZERO
DJNZ R2,DEC2 ;DECREMENT R2 BY 1 AND IF NON ZERO JUMP TO DEC2
DJNZ R1,DEC1 ;DECREMENT R1 BY 1 AND IF NON ZERO JUMP TO DEC1
RET
HERE:
END

RESULT:
Before Execution: After Execution:
P1= FFH P1= FFH
FEH
..
00H

Dept. of Electronics & Communication Engg., R.I.T, Hassan 15


Microcontrollers Lab Manual 06ESL47

3c) Write an ALP to perform the operation of BCD-Up Counter:

ORG 0000H
MOV A,#00H ;A= 00h
BACK: ACALL DELAY ;SUBROUTINE CALL
ADD A,#01H ;A= A+01
DA A ;DECIMAL ADJUST
MOV P1,A
JNZ BACK ;JUMP TO BACK IF NON ZERO
HERE: SJMP HERE ;UNCONDITIONAL JUMP
DELAY:MOV R1,#0FFH ;R1 =0FFh
DEC2: MOV R2,#0FFH ;R2= 0FFh
DEC1: MOV R3,#0FFH ;R3= 0FFh
NEXT: DJNZ R3,NEXT ;DECREMENT R3 BY 1 AND JUMP TO NEXT IF NON ZERO
DJNZ R2,DEC1 ;DECREMENT R2 BY 1 AND IF NON ZERO JUMP TO DEC2
DJNZ R1,DEC2 ;DECREMENT R1 BY 1 AND IF NON ZERO JUMP TO DEC2
RET ;RETURN TO SUBROUTIEN
END

RESULT:
Before Execution: After Execution:
P1= FFH P1= 00H
01H
..
99H

3d) Write an ALP to perform the operation of BCD-Down Counter:

ORG 0000H
MOV A,#99H ;A=99h
BACK: ACALL DELAY ;SUBROUTINE CALL
ADD A,#01H ;A= A+01
DA A ;DECIMAL ADJUST
MOV P1,A
JNZ BACK ;JUMP TO BACK IF NON ZERO
HERE: SJMP HERE ;UNCONDITIONAL JUMP
DELAY:MOV R1,#0FFH ;R1= 0FFh
DEC2: MOV R2,#0FFH ;R2= 0FFh
DEC1: MOV R3,#0FFH ;R3= 0FFh
NEXT: DJNZ R3,NEXT ;DECREMENT R3 BY 1 AND JUMP TO NEXT IF NON ZERO
DJNZ R2,DEC1 ;DECREMENT R2 BY 1 AND IF NON ZERO JUMP TO DEC2
DJNZ R1,DEC2 ;DECREMENT R1 BY 1 AND IF NON ZERO JUMP TO DEC2
RET ;RETURN TO SUBROUTIEN
END

RESULT:
Before Execution: After Execution:
P1= FFH P1= 99H
98H
..
00H

Dept. of Electronics & Communication Engg., R.I.T, Hassan 16


Microcontrollers Lab Manual 06ESL47

3e) Write an ALP to find Numbers of 1's and 0's:

ORG 0000H
MOV R1,#00H ;R1=#00h
MOV R2,#00H ;R2=#00h
MOV R7,#08H ;R7=#08h
CLR C ;CLEAR CARRY
MOV A,40H ;A=40h
AGAIN:RLC A ;ROTATE LEFT CLOCKWISE A
JNC NEXT
INC R1 ;R1=R1+1
DJNZ R7,AGAIN ;DECREMENT BY 1 AND JUMP IF NOT EQUAL 0
SJMP NEW
NEXT: INC R2
NEW:
END

RESULT:
Before Execution: After Execution:

3f) Write an ALP to find whether the give Number ODD or EVEN Number:

ORG 0000H
MOV A,51H
CLR C
MOV R0,#00H
RRC A
JNC NEXT
INC R0
SJMP HERE
NEXT: MOV R0,#00H
HERE:
END

RESULT:
Before Execution: After Execution:

Dept. of Electronics & Communication Engg., R.I.T, Hassan 17


Microcontrollers Lab Manual 06ESL47

3g) Write an ALP to generate FABONACCI Series upto Nth Term:

ORG 0000H
MOV R0,#10H
MOV R1,#20H
MOV @R1,#00H
INC R1
MOV @R1,#1
BACK: MOV A,@R1
DEC R1
ADD A,@R1
INC R1
INC R1
MOV @R1,A
CJNE @R1,#08H,BACK
HERE: SJMP HERE
END

RESULT:
After execution:

D:0x20= 00 01 01 02 03 05 08

3h) Write an ALP to find SQURE ROOT of the given Numbers and Store the
Result in R2:

ORG 0000H
MOV R1,#00
BACK: MOV A,R1
MOV B,A
MUL AB
CJNE A,#64,NEXT
SJMP LAST
NEXT: INC R1
SJMP BACK
LAST: MOV A,R1
MOV R2,A
HERE: SJMP HERE
END

RESULT:
After execution:

Dept. of Electronics & Communication Engg., R.I.T, Hassan 18


Microcontrollers Lab Manual 06ESL47

Exp-4
4a) Write an ALP to perform the Boolean Bit Manipulation operation:

ORG 0000H
JB P1.0, CLRLOOP
SETLOOP: SETB C
MOV P2.0,C
SJMP CMPLOOP
CLRLOOP: CLR C
MOV P2.0,C
SJMP CMPLOOP
CMPLOOP: MOV C,P1.7
CPL C
MOV P2.7,C
END

RESULT:
Before Execution: After Execution:

4b.1) Write an ALP to perform the ALL LOGICAL OPERATION:

4b.1a) Write an ALP to perform the operation of AND:

AND_INPUT_1 BIT P3.2


AND_INPUT_2 BIT P3.3

AND_OUTPUT BIT P3.7

ORG 0000H
LJMP RESET_VECTOR

ORG 0030H
RESET_VECTOR:
MOV IE,#00H ; DISABLE ALL INTERRUPTS
MOV SP,#5FH ; ASSIGN STACK POINTER

MOV P3,#8CH ; CLEAR PORT 3


LOOP:
MOV C,AND_INPUT_1
ANL C,AND_INPUT_2 ; AND OPERATION
MOV AND_OUTPUT,C
SJMP LOOP
END

Dept. of Electronics & Communication Engg., R.I.T, Hassan 19


Microcontrollers Lab Manual 06ESL47

RESULT:
Before Execution: After Execution:
P3=0Xff P3= 0X8C

P3.2 P3.3 P3.7


0 0 0
0 1 0
1 0 0
1 1 1

4b.1b) Write an ALP to perform the OR:

OR_INPUT_1 BIT P3.2


OR_INPUT_2 BIT P3.3

OR_OUTPUT BIT P3.7

ORG 0000H
LJMP RESET_VECTOR

ORG 0030H
RESET_VECTOR:
MOV IE,#00H ; DISABLE ALL INTERRUPTS

MOV SP,#5FH ; ASSIGN STACK POINTER


MOV P3,#8CH ; CLEAR PORT 3
LOOP:
MOV C,OR_INPUT_1
ORL C,OR_INPUT_2 ; OR OPERATION
MOV OR_OUTPUT,C
SJMP LOOP
END

RESULT:
Before Execution: After Execution:
P3=0Xff P3= 0X8C

P3.2 P3.3 P3.7


0 0 0
0 1 1
1 0 1
1 1 1

Dept. of Electronics & Communication Engg., R.I.T, Hassan 20


Microcontrollers Lab Manual 06ESL47

4b.1c) Write an ALP to perform the operation of NOT:

NOT_INPUT BIT P3.2


NOT_OUTPUT BIT P3.7

ORG 0000H
LJMP RESET_VECTOR
ORG 0030H
RESET_VECTOR:
MOV IE,#00H ; DISABLE ALL INTERRUPTS
MOV SP,#5FH ; ASSIGN STACK POINTER
MOV P3,#00H ; CLEAR PORT 3

Back MOV C,NOT_INPUT


CPL C ; NOT OPERATION
MOV NOT_OUTPUT,C
SJMP Back
END

RESULT:
Before Execution: After Execution:
P3=0Xff P3= 0X80
P3.2 P3.7
0 1
1 0
4b.1d) Write an ALP to perform the operation of NAND:

NAND_INPUT_1 BIT P3.2


NAND_INPUT_2 BIT P3.3
NAND_OUTPUT BIT P3.7
ORG 0000H
LJMP RESET_VECTOR
ORG 0030H
RESET_VECTOR:
MOV IE,#00H ; DISABLE ALL INTERRUPTS
MOV SP,#5FH ; ASSIGN STACK POINTER
MOV P3,#8CH ; CLEAR PORT 3
LOOP:
MOV C,NAND_INPUT_1
ANL C,NAND_INPUT_2 ; AND OPERATION
CPL C ; NOT OPERATION
MOV NAND_OUTPUT,C
SJMP LOOP
END

RESULT:
Before Execution: After Execution:
P3=0Xff P3= 0X0C

P3.2 P3.3 P3.7


0 0 1
0 1 1
1 0 1
1 1 0

Dept. of Electronics & Communication Engg., R.I.T, Hassan 21


Microcontrollers Lab Manual 06ESL47

4b.1e) Write an ALP to perform the operation of NOR:

NOR_INPUT_1 BIT P3.2


NOR_INPUT_2 BIT P3.3
NOR_OUTPUT BIT P3.7

ORG 0000H
LJMP RESET_VECTOR

ORG 0030H
RESET_VECTOR:
MOV IE,#00H ; DISABLE ALL INTERRUPTS
MOV SP,#5FH ; ASSIGN STACK POINTER

MOV P3,#8CH ; CLEAR PORT 3


LOOP:
MOV C,NOR_INPUT_1
ORL C,NOR_INPUT_2 ; OR OPERATION
CPL C ; NOT OPERATION
MOV NOR_OUTPUT,C
SJMP LOOP
END

RESULT:
Before Execution: After Execution:
P3=0Xff P3= 0X0C

P3.2 P3.3 P3.7


0 0 1
0 1 0
1 0 0
1 1 0

4b.1f) Write an ALP to perform the operation of XOR:

XOR_INPUT_1 BIT P3.2


XOR_INPUT_2 BIT P3.3
XOR_OUTPUT BIT P3.7

TEMP_BIT BIT 20H.0

ORG 0000H
LJMP RESET_VECTOR

ORG 0030H
RESET_VECTOR:
MOV IE,#00H ; DISABLE ALL INTERRUPTS
MOV SP,#5FH ; ASSIGN STACK POINTER
MOV P3,#8CH ; CLEAR PORT 3
LOOP:
MOV C,XOR_INPUT_1
CPL C
ANL C,XOR_INPUT_2 ; XOR_INPUT_1 BAR & XOR_INPUT_2
MOV TEMP_BIT,C

Dept. of Electronics & Communication Engg., R.I.T, Hassan 22


Microcontrollers Lab Manual 06ESL47

MOV C,XOR_INPUT_2
CPL C
ANL C,XOR_INPUT_1 ; XOR_INPUT_2 BAR & XOR_INPUT_1
ORL C,TEMP_BIT ; XOR OPREATION
MOV XOR_OUTPUT,C
SJMP LOOP
END

RESULT:
Before Execution: After Execution:
P3=0Xff P3= 0X0C

P3.2 P3.3 P3.7


0 0 0
0 1 1
1 0 1
1 1 0
4b.1g) Write an ALP to perform the operation of XNOR:

XNOR_INPUT_1 BIT P3.2


XNOR_INPUT_2 BIT P3.3
XNOR_OUTPUT BIT P3.7

TEMP_BIT BIT 20H.0


ORG 0000H
LJMP RESET_VECTOR
ORG 0030H
RESET_VECTOR:
MOV IE,#00H ; DISABLE ALL INTERRUPTS
MOV SP,#5FH ; ASSIGN STACK POINTER
MOV P3,#8CH ; CLEAR PORT 3
LOOP:
MOV C,XNOR_INPUT_1
CPL C
ANL C,XNOR_INPUT_2 ; XNOR_INPUT_1 BAR & XNOR_INPUT_2
MOV TEMP_BIT,C ; TEMP STORAGE
MOV C,XNOR_INPUT_2
CPL C
ANL C,XNOR_INPUT_1 ; XNOR_INPUT_2 BAR & XNOR_INPUT_1
ORL C,TEMP_BIT ; XOR OPERATION
CPL C ; XNOR OPERATION
MOV XNOR_OUTPUT,C
SJMP LOOP
END
RESULT:
Before Execution: After Execution:
P3=0Xff P3= 0X8C

P3.2 P3.3 P3.7


0 0 1
0 1 0
1 0 0
1 1 1

Dept. of Electronics & Communication Engg., R.I.T, Hassan 23


Microcontrollers Lab Manual 06ESL47

4b.2) Write an ALP to perform the LOGICAL OPERATION:

ORG 0000H
MOV R0,#34h
MOV A,R0
ANL A,#0Fh
MOV P0,A
MOV A,00h
ORL A,#00h
ORL A,#0Fh
MOV P1,A
MOV A,R0
CPL A
MOV P2,A
MOV A,R0
RR A
RR A
RR A
RR A
MOV P3,A
MOV A,R0
RL A
RL A
RL A
RL A
MOV R1,A
SJMP $
END

RESULT:
After Execution:

Dept. of Electronics & Communication Engg., R.I.T, Hassan 24


Microcontrollers Lab Manual 06ESL47

4c.1) Write an ALP to study the call and return instruction:


; Wring the some delay between on-off of the port
; Using Acall and Lcall instruction

ORG 0000h

BACK: MOV P0,#55H


MOV P1,#55H
MOV P2,#55H
MOV P3,#55H
ACALL DELAY

MOV P0,#0AAH
MOV P1,#0AAH
MOV P2,#0AAH
MOV P3,#0AAH

LCALL DELAY
SJMP BACK
;**************************
DELAY:
MOV 30H,#0FFH
BACK_1:MOV 31H,#00H
DJNZ 31H,$
DJNZ 30H,BACK_1
RET
;**************************
SJMP $
END

RESULT:
Before Execution: After Execution:

P0=0xff P0= 0xAA,0x55


P1=0xff P1= 0xAA,0x55
P2=0xff P2= 0xAA,0x55
P3=0xff P3= 0xAA,0x55

4c.2) Write an ALP to study the call and return instruction:

ORG 0000H
JB P1.0,LOOP1
LOOP2:MOV A,#0FFH
MOV P2,A
ACALL DELAY
MOV A,#00h
MOV P2,A
ACALL DELAY
SJMP LOOP2
LOOP1:MOV A,#78h
MOV P2,A
ACALL DELAY
MOV A,#0D3h
MOV P2,A
ACALL DELAY
SJMP LOOP1

Dept. of Electronics & Communication Engg., R.I.T, Hassan 25


Microcontrollers Lab Manual 06ESL47

DELAY:MOV R0,#0FFh
HERE2:DJNZ R1,HERE1
HERE1:DJNZ R1,HERE2
RET
END

RESULT:
Before Execution: After Execution:

P2=0xff P2= 0x78,0xD3

Dept. of Electronics & Communication Engg., R.I.T, Hassan 26


Microcontrollers Lab Manual 06ESL47

Exp5
5a) Write an ALP to Converting the BCD DATA TO ASCII CONVERTION:

ORG 0000H
MOV R0,#30h
MOV A,@R0
MOV R1,A
ANL A,#0Fh
ADD A,#30h
MOV P0, A
INC R0
MOV @R0,A
MOV A,R1
ANL A,#0F0h
SWAP A
ADD A,#30H
INC R0
MOV @R0,A
MOV P1, A
HERE: SJMP HERE
END

RESULT:
Before Execution: After Execution:
30h: 06 30h: 36 30

5b) Write an ALP ASCII TO DECIMAL CONVERTION:

;NUMBER FROM R0 IS FIREST CONVERTED INTO BCD & THEN ASCII

ORG 00000H
MOV R0,#35h ;LOAD THE R0 WITH ASCII
MOV A,R0

;CONVERTING THES NUMBER INTO BCD FORM AND THEN DECIMAL


CLR C
SUBB A,#30H
MOV P0,A ;DIPLAY THE BCD NUMBER ON PORT 0
DA A ;DECIMAL ADJESTNENT(CONVERT IN TO DECIMAL)
MOV P1,A ;DISPLAY THE DECIMAL NUMBER ON PORT 1

SJMP $
END ;TERMINATE THE PROGRAM

RESULT:
ASCII value 35h
Before Execution: After Execution:

P0=0Xff P0= 0X05


P1= 0x05

Dept. of Electronics & Communication Engg., R.I.T, Hassan 27


Microcontrollers Lab Manual 06ESL47

5c) Write an ALP DECIMAL TO ASCII CONVERTION:

;NUMBER FROM R0 IS FIRST CONVERTED INTO BCD & THEN ASCII

ORG 0000H

MOV R0,#35 ;LOAD THE R0 WITH DECIMAL NUMBER


MOV A,R0

ANL A,#0FH
MOV P0,A ;DISPLAY LOWER BCD ON PORT 0
ORL A,#30H ;CONVETING IT TO ASCII
MOV P1,A ;DISPLAY LOWER BCD NUMBER IN ASCII FORM ON PORT 1

MOV A,R0
ANL A,#0F0H
SWAP A
MOV P2,A ;DISPLAY UPPER NUMBER ON PORT 2
ORL A,#30H ;CONVETING IT TO ASCII
MOV P3,A ;DISPLAY UPPER BCD NUMBER IN ASCII FORM ON PORT 3

END

RESULT:

DECIMAL value 35h


Before Execution: After Execution:
P0=0Xff P0= 0X03
P1= 0x33
P2= 0x02
P3= 0x32

5d) Write an ALP for Converting the DECIMAL DATA TO HEX CONVERTION:

ORG 0000H
LJMP MAIN

ORG 0020H
MAIN:
MOV R7,#253 ;TAKE A SIMPLE DECIMAL NO
MOV A,R7
MOV B,#10H ;DIVIDE BY 16
DIV AB
MOV P1,A ;STORE THE FIRST COUNT
MOV A,B
MOV B,#10H
DIV AB
MOV P2,B ;STORE THE SECOND COUNT
END

RESULT:
DECIMAL value 253
Before Execution: After Execution:
P0=0Xff P1= 0X0F
P2= 0x0D

Dept. of Electronics & Communication Engg., R.I.T, Hassan 28


Microcontrollers Lab Manual 06ESL47

5e) Write an ALP for Converting the HEX DATA TO DECIMAL:

ORG 0000H
LJMP MAIN

ORG 0020H
MAIN:
MOV R7,#0AAH ;TAKE A SIMPLE HEX NO
MOV A,R7
MOV B,#10 ;DIVIDE BY TEN
DIV AB
MOV P1,A ;STORE THE FIRST COUNT
MOV A,B
MOV B,#10
DIV AB
MOV P2,B
END
RESULT:
HEX value 0AAH
Before Execution: After Execution:

P0=0Xff P1= 0X11


P2= 0x00

Dept. of Electronics & Communication Engg., R.I.T, Hassan 29


Microcontrollers Lab Manual 06ESL47

Exp-6
6a) Write an ALP to generate Delay:
;PORT ON_OFF WITH SOME DELAY

ORG 0000H

START:MOV P0,#0AAH
MOV P1,#0AAH
MOV P2,#0AAH
MOV P3,#0AAH
ACALL DELAY ;DELAY
MOV P0,#55H
MOV P1,#55H
MOV P2,#55H
MOV P3,#55H
ACALL DELAY ;DELAY
SJMP START
;DELAY ROUTINES 1
;DELAY:
; MOV R0,#0FFH
;BACK:MOV R1,#0FFH
; DJNZ R1,$
; DJNZ R0,BACK
; RET
;################################
;DELAY ROUTINES 2
;################################

DELAY:
MOV R3,#0FFH
D3: MOV R4,#0FFH
D2: MOV R5,#0FFH
D1: DJNZ R5,D1
DJNZ R4,D2
DJNZ R3,D3
RET

END

RESULT:
Before Execution: After Execution:

P0=0xff P0= 0xAA,0x55


P1=0xff P1= 0xAA,0x55
P2=0xff P2= 0xAA,0x55
P3=0xff P3= 0xAA,0x55

Dept. of Electronics & Communication Engg., R.I.T, Hassan 30


Microcontrollers Lab Manual 06ESL47

6b) Write an ALP for ON CHIP TIMER:


;Write a program using Timer0 to create a 10khz square wave on P1.0

MOV TMOD,#02H ;8-bit auto-reload mode


MOV TH0,#-3 ;9600 baud rate

SETB TR0 ;start timer0

LOOP: JNB TF0, LOOP ;wait for overflow

CLR TF0 ;clear timer0 overflow flag


CPL P1.0 ;toggle port bit
SJMP LOOP ;repeat

END

RESULT:
Before Execution: After Execution:

P1=0xff P1= after executing P1.0 is toggle.


ie. P1= 0xFF and 0xFE

6c) Write an ALP for SERIAL DATA RECIPTION:


;PLEASE READ THE MANUAL FOR HARDWARE DETAILS

ORG 0000H ;START AT LOCATION 0000H


MOV TMOD,#20H ;TIMER1,MODE 2(AUTO RELOAD)
MOV TH1,#-3 ;4800 BAUD
MOV SCON,#50H ;8-BIT 1 STOP,REN ENABLED
SETB TR1 ;START TIMER 1
HERE: JNB RI,HERE ;WAIT FOR CHAR TO COME IN
MOV A,SBUF ;SAVE INCOMING BYTE IN A
MOV SBUF,A ;SEND TO PORT 1
MOV P2,A
CLR RI ;GET READY TO RECEIVING NEXT BYTE
SJMP HERE ;KEEP IN LOOPING

END ;TERMINAYE THE RPOGRAM

RESULT :
Steps to read the result:
a) After executing run go to view option
b) Select serial window #01
c) Type any character on keyboard
d) Observe the same character on serial window
e) It shows the program receiving data through serial port

Dept. of Electronics & Communication Engg., R.I.T, Hassan 31


Microcontrollers Lab Manual 06ESL47

6d) Write an ALP for SERIALLY DATA TRANSMITION:


;PLEASE READ THE MANUAL FOR HARDWARE DETAILS

ORG 0000H

MOV TMOD,#20H ;TIMER1,MODE2


MOV TH1,#-3 ;9600 BAUD RATE
MOV SCON,#50H ;8 BIT,1 STOP,REN
SETB TR1 ;START TIMER

MOV A,#"G" ;LATEER M TO BE TRANSFERD


ACALL TRANS
MOV A,#"O" ;LATEER E TO BE TRANSFERD
ACALL TRANS
MOV A,#"O" ;LATEER G TO BE TRANSFERD
ACALL TRANS
MOV A,#"D" ;LATEER A TO BE TRANSFERD
ACALL TRANS
MOV A,#" " ;LATEER C TO BE TRANSFERD
ACALL TRANS
MOV A,#"D" ;LATEER O TO BE TRANSFERD
ACALL TRANS
MOV A,#"A" ;LATEER R TO BE TRANSFERD
ACALL TRANS
MOV A,#"Y" ;LATEER E TO BE TRANSFERD
ACALL TRANS

AGAIN:SJMP AGAIN ;KEEP LOOPING

;SERIALLY DATA TRANSFER SUBROUTINE


TRANS:
MOV SBUF,A
HERE: JNB TI,HERE

CLR TI
RET

END

RESULT:
Steps to read the result
a) After executing run, go to view option
b) Select serial window #01
c) Observe the message “GOOD DAY” in serial window
d) It shows the program transmitting data through serial port

Dept. of Electronics & Communication Engg., R.I.T, Hassan 32


Microcontrollers Lab Manual 06ESL47

EXP-7:

Write C Program to interface 8051 chip to interfacing modules to develop


single chip solutions.

7a.1) simple calculator using 6 digit seven segment display and hex
keyboard interface to 8051.

ORG 0000H
LJMP RESET_VECTOR

ORG 0030H
RESET_VECTOR:
MOV SP,#5FH ; ASSIGN 32 BYTES FOR
STACK 60H TO 7FH
MOV IE,#00H ; DISABLE ALL INTERRUPTS

MOV R0,#7FH ;--


CLR A ; |
CLR_RAM_LOOP: ; | CLEAR RAM ROUTINE
MOV @R0,A ; | 00H TO 7FH
DJNZ R0,CLR_RAM_LOOP ;--

MOV 35H + 0, #00H


MOV 35H + 1, #00H
MOV 35H + 2, #0H
MOV 35H + 3, #0H
MOV 35H + 4, #0H
MOV 35H + 5, #0H

LOOP_AGAIN:
LCALL DSPL_DATA
; LJMP LOOP_AGAIN

LCALL CHK_KEYPRESS
MOV 33H, R7
CJNE R7,#00,CHK_X_KEY
LJMP LOOP_AGAIN
CHK_X_KEY:
CJNE R7,#'X',CHK_KEY
LJMP LOOP_AGAIN
CHK_KEY:
LCALL CHK_FNCT_KEY
CHK_NUM1:
JB 20H.0, CHK_FNCT
JB 21H.1, NO_FNCT_NOW
LCALL CLEAR_ALL
MOV A, 33H
ANL A, #0FH
MOV 30H, A
SETB 20H.0
LJMP LOOP_AGAIN
CHK_FNCT:
JB 20H.7, CHK_NUM2
JNB 21H.1, NO_FNCT_NOW
SETB 20H.7
LJMP LOOP_AGAIN

Dept. of Electronics & Communication Engg., R.I.T, Hassan 33


Microcontrollers Lab Manual 06ESL47

CHK_NUM2:
JB 20H.1, INVALID_FNCT
JB 21H.1, NO_FNCT_NOW
MOV A, 33H
ANL A, #0FH
MOV 31H, A
SETB 20H.1
LCALL DO_FNCT
LJMP LOOP_AGAIN

INVALID_FNCT:
LCALL CLEAR_ALL
NO_FNCT_NOW:
LJMP LOOP_AGAIN

;-------------------------------------------------------------------------
---
;DO_FNCT:
;-------------------------------------------------------------------------
---
DO_FNCT:
JNB 20H.3, CHK_S
MOV A, 30H
ADD A, 31H
MOV 32H, A
SETB 20H.2
RET
CHK_S:
JNB 20H.4, CHK_M
MOV A, 30H
SUBB A, 31H
JNC XXX
SETB 21H.3
CPL A
INC A
XXX:
MOV 32H, A
SETB 20H.2
RET
CHK_M:
JNB 20H.5, CHK_D
MOV A, 30H
MOV B, 31H
MUL AB
MOV 32H, A
SETB 20H.2
RET
CHK_D:
JNB 20H.6, ERROR
MOV A, 31H
JZ ERROR
MOV A, 30H
MOV B, 31H
DIV AB
MOV 32H, A
SETB 20H.2
RET

Dept. of Electronics & Communication Engg., R.I.T, Hassan 34


Microcontrollers Lab Manual 06ESL47

ERROR:
SETB 21H.2
RET

;-------------------------------------------------------------------------
---
;CLEAR_ALL
;-------------------------------------------------------------------------
---
CLEAR_ALL:
CLR 20H.7
CLR 20H.3
CLR 20H.4
CLR 20H.5
CLR 20H.6
CLR 20H.0
CLR 20H.1
CLR 20H.2
CLR 21H.2
CLR 21H.3
MOV 30H, #00H
MOV 31H, #00H
MOV 32H, #00H
RET
;-------------------------------------------------------------------------
---
;CHK_FNCT_KEY
;-------------------------------------------------------------------------
---
CHK_FNCT_KEY:
SETB 21H.1

CJNE A, #'+', CHK_MINUS


SETB 20H.3
MOV 34H, #73H ; P
LJMP END_CHK_FNCT_KEY
CHK_MINUS:
CJNE A, #'-', CHK_MUL
SETB 20H.4
MOV 34H, #40H ; -
LJMP END_CHK_FNCT_KEY
CHK_MUL:
CJNE A, #'*', CHK_DIV
SETB 20H.5
MOV 34H, #55H ; M
LJMP END_CHK_FNCT_KEY
CHK_DIV:
CJNE A, #'/', NO_FNCT_KEY
SETB 20H.6
MOV 34H, #5EH ; d
LJMP END_CHK_FNCT_KEY

NO_FNCT_KEY:
CLR 21H.1
END_CHK_FNCT_KEY:
RET

Dept. of Electronics & Communication Engg., R.I.T, Hassan 35


Microcontrollers Lab Manual 06ESL47

;-------------------------------------------------------------------------
---
;DSPL_DATA
;-------------------------------------------------------------------------
---
DSPL_DATA:

LCALL UPDATE_DSPL

SETB P2.0
SETB P2.1
SETB P2.2
SETB P2.3
SETB P2.4
SETB P2.5

MOV P1, 35H + 5


CLR P2.0
LCALL DSPL_DELAY
SETB P2.0

MOV P1, 35H + 4


CLR P2.1
LCALL DSPL_DELAY
SETB P2.1

MOV P1, 35H+3


CLR P2.2
LCALL DSPL_DELAY
SETB P2.2

MOV P1, 35H + 2


CLR P2.3
LCALL DSPL_DELAY
SETB P2.3

MOV P1, 35H + 1


CLR P2.4
LCALL DSPL_DELAY
SETB P2.4

MOV P1, 35H + 0


CLR P2.5
LCALL DSPL_DELAY
SETB P2.5

RET

;-------------------------------------------------------------------------
---
;UPDATE_DSPL:
;-------------------------------------------------------------------------
---
UPDATE_DSPL:
JB 21H.2, SHOW_ERROR
MOV 35H+3,#48H

Dept. of Electronics & Communication Engg., R.I.T, Hassan 36


Microcontrollers Lab Manual 06ESL47

MOV DPTR, #HEX_SEVEN_SEG


JNB 20H.0, NO_NUM1
MOV A, 30H
MOVC A, @A+DPTR
MOV 35H+0, A
LJMP CHK_N2_F
NO_NUM1:
MOV 35H+0, #00H

CHK_N2_F:
JNB 20H.1, NO_NUM2
MOV A, 31H
MOVC A, @A+DPTR
MOV 35H+2, A
LJMP CHK_RESULT
NO_NUM2:
MOV 35H+2, #00H
CHK_RESULT:
JNB 20H.2, NO_RESULT

MOV A, 32H
MOV B, #0AH
DIV AB
MOVC A, @A+DPTR
JNB 21H.3, PLUS
MOV A, #40H
PLUS:
MOV 35H+4, A
MOV A,B
MOVC A, @A+DPTR
MOV 35H+5, A
LJMP CHK_FUN
NO_RESULT:
MOV 35H+4, #00H
MOV 35H+5, #00H
CHK_FUN:
JNB 20H.7, NO_FUN
MOV 35H+1, 34H
LJMP END_UPDATE_DSPL
NO_FUN:
MOV 35H+1, #00H
END_UPDATE_DSPL:
RET

SHOW_ERROR:
MOV 35H+0, #79H
MOV 35H+1, #50H
MOV 35H+2, #50H
MOV 35H+3, #5CH
MOV 35H+4, #50H
MOV 35H+5, #00H
RET

;-------------------------------------------------------------------------
---
; CHK_KEYPRESS ROUTINE

Dept. of Electronics & Communication Engg., R.I.T, Hassan 37


Microcontrollers Lab Manual 06ESL47

; PORT USED :: HIGHER NIBBLE = RS, LOWER NIBBLE = CS


; RETURNS :: R7 = KEY PRESSED / 0 = INVALID KEY OR NO KEY PRESSED
;-------------------------------------------------------------------------
---
CHK_KEYPRESS:
MOV P0,#0FH
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,KEY_PRESSED ; CHK KEY CLOSEURE
LJMP NO_KEY_PRESSED

KEY_PRESSED:
JB 21H.0,KEY_STUCK
LCALL DEBOUNCE_DELAY
LCALL DEBOUNCE_DELAY

MOV A,P0
ANL A,#0FH
CJNE A,#0FH,KEYPRESS_CONFIRMED ; CONFIRM KEY CLOSURE
LJMP NO_KEY_PRESSED

KEYPRESS_CONFIRMED:
SETB 21H.0
LCALL DEBOUNCE_DELAY

MOV P0,#0EFH ; CHK R 0


MOV A,P0
ANL A,#0FH
CJNE A,#0FH,SCN_R_0 ; KEY_FOUND IN R 0

MOV P0,#0DFH ; CHK R 1


MOV A,P0
ANL A,#0FH
CJNE A,#0FH,SCN_R_1 ; KEY_FOUND IN R 1

MOV P0,#0BFH ; CHK R 2


MOV A,P0
ANL A,#0FH
CJNE A,#0FH,SCN_R_2 ; KEY_FOUND IN R 2

MOV P0,#07FH ; CHK R 3


MOV A,P0
ANL A,#0FH
CJNE A,#0FH,SCN_R_3 ; KEY_FOUND IN R 3
LJMP NO_KEY_PRESSED

SCN_R_0:
MOV DPTR,#KEY_R_0
LJMP SCN_C
SCN_R_1:
MOV DPTR,#KEY_R_1
LJMP SCN_C
SCN_R_2:
MOV DPTR,#KEY_R_2
LJMP SCN_C
SCN_R_3:
MOV DPTR,#KEY_R_3

Dept. of Electronics & Communication Engg., R.I.T, Hassan 38


Microcontrollers Lab Manual 06ESL47

SCN_C:
RRC A
JNC KEY_FOUND
INC DPTR
LJMP SCN_C
KEY_FOUND:
CLR A
MOVC A,@A+DPTR
MOV R7,A
LJMP END_KEY_PRESS

NO_KEY_PRESSED:
CLR 21H.0
KEY_STUCK:
MOV R7,#00H
END_KEY_PRESS:
RET

;-------------------------------------------------------------------------
---
; DELAY ROUTINES...... APROX 10ms
;-------------------------------------------------------------------------
---
DEBOUNCE_DELAY:
MOV 40H,#4EH ; FOR THIS DELAY BUZZER REMAINS
ON
DB_LOOP2: MOV 41H,#20H
DJNZ 41H,$ ; LOOP UNTIL CNTR_1MsB = 00H
DJNZ 40H,DB_LOOP2 ; LOOP UNTIL CNTR_1MsA = 00H
RET

;-------------------------------------------------------------------------
---
; LOOKUP TABLE FOR KYB RETURN CODES
;-------------------------------------------------------------------------
---
KEY_R_0: DB "1","4","7","X"
KEY_R_1: DB "2","5","8","0"
KEY_R_2: DB "3","6","9","X"
KEY_R_3: DB "/","*","-","+"

;-------------------------------------------------------------------------
---
; DELAY ROUTINES......
;-------------------------------------------------------------------------
---
DSPL_DELAY:
MOV 40H,#03H ; FOR THIS DELAY BUZZER REMAINS ON
D_LOOP2: MOV 41H,#0FFH
DJNZ 41H,$ ; LOOP UNTIL CNTR_1MsB = 00H
DJNZ 40H,D_LOOP2 ; LOOP UNTIL CNTR_1MsA = 00H
RET

;-------------------------------------------------------------------------
---
; LOOKUP TABLE FOR BCD TO 7-SEGMENT DSPL

Dept. of Electronics & Communication Engg., R.I.T, Hassan 39


Microcontrollers Lab Manual 06ESL47

;-------------------------------------------------------------------------
---
HEX_SEVEN_SEG:DB 3FH, 06H, 5BH, 4FH, 66H, 6DH, 7DH, 07H, 7FH,
6FH, 077H, 7CH, 39H, 5EH, 79H, 71H
;DIGIT 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B,
C, D, E, F

END

7a.2) Write a C program for calculator LCD:

#include <reg51.H>

#define KEY P0 //P0.0-P0.3=ROW AND P0.4-P0.7=COL


#define lcd_databus P1
sbit lcd_rs = (P2 ^ 0);
sbit lcd_rw = (P2 ^ 1);
sbit lcd_en = (P2 ^ 2);

void lcd_init();
void lcd_command(unsigned char command);
void lcd_data(unsigned char d);

void msdelay(unsigned int value);

void lcd1(unsigned char );


void lcd2(unsigned char );
void chk_func_key();
void message_display();
void find_result();

unsigned char keyscan();


unsigned char no_1,func_key,no_2,equal_sign,result;
unsigned char colloc,rowloc;
unsigned char keypad[4][4]= {'1','2','3','/',
'4','5','6','*',
'7','8','9','-',
'a','0','=','+'};
void main()
{
lcd_init();
while(1)
{

message_display();

lcd_command(0xc6);// Select curcur position of LCD(DDRAM Address)


msdelay(1);
/////////////////////////////////////////////
//enter first number
/////////////////////////////////////////////
no_1 = keyscan();
lcd1(no_1);
no_1 -= 0x30;
//////////////////////////////////////////////
//select operation

Dept. of Electronics & Communication Engg., R.I.T, Hassan 40


Microcontrollers Lab Manual 06ESL47

//////////////////////////////////////////////
chk_func_key();
//////////////////////////////////////////////
//enter second no
//////////////////////////////////////////////
no_2 = keyscan();
lcd1(no_2);
no_2 -= 0x30;
//////////////////////////////////////////////
//enter = sign
//////////////////////////////////////////////
equal_sign=keyscan();
lcd1(equal_sign);
//////////////////////////////////////////////
//find result and display it on lcd display
//////////////////////////////////////////////
find_result();
//////////////////////////////////////////
//TO CLEAR THE DISPLAY PRESS K13 key
//////////////////////////////////////////
do
{
msdelay(10); //call delay
colloc = KEY; //see if key is pressed
colloc &= 0xf0; //mask unused bits
}while(colloc ==0xf0);//check for key pressed
/////////////////////////////////////////////
//clear lcd display
/////////////////////////////////////////////
lcd_command(0x01); // Clear Display
msdelay(1);
/////////////////////////////////////////////
}
}

///////////////////////////////////////////////
//data display on lcd
///////////////////////////////////////////////
void lcd1(unsigned char x)
{
lcd_data(x);
}
///////////////////////////////////////////////
//answer display on lcd
///////////////////////////////////////////////
void lcd2(unsigned char x)
{
lcd_command(0xCA); // Select Line 1 of LCD (DDRAM Address)
msdelay(1);
lcd_data(x);
}
////////////////////////////////////////////////
//delay ruting
////////////////////////////////////////////////
void msdelay(unsigned int value)
{
unsigned int x,y;

Dept. of Electronics & Communication Engg., R.I.T, Hassan 41


Microcontrollers Lab Manual 06ESL47

for (x=0;x<1275;x++)
for (y=0;y<value;y++);
}
////////////////////////////////////////////////
//lcd initiliation ruting
/////////////////////////////////////////////////
void lcd_init()
{
lcd_command(0x38);// 16x2 Chars., 5x7 matrix, 8-bit data
msdelay(1);

lcd_command(0x14); // Shift Display


msdelay(1);

lcd_command(0x0C); // Display on/off


msdelay(1);

lcd_command(0x06); // Entry Mode


msdelay(1);

lcd_command(0x01); // Clear Display


msdelay(1);

// LCD Initialization over

lcd_command(0x82);// Select Line 1 of LCD (DDRAM Address)


msdelay(1);
}
//////////////////////////////////////////
//lcd command routing
//////////////////////////////////////////
void lcd_command(unsigned char command)
{
unsigned char i;
lcd_rs = 0;
lcd_rw = 0;
lcd_databus = command;
lcd_en = 1;
i++;i--;
lcd_en = 0;
}
//////////////////////////////////////////
//lcd data ruting
//////////////////////////////////////////
void lcd_data(unsigned char d)
{
unsigned char i;

lcd_rs = 1;
lcd_rw = 0;
lcd_databus = d;
lcd_en = 1;
i++;
i--;
lcd_en = 0;
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 42


Microcontrollers Lab Manual 06ESL47

////////////////////////////////////////////////
// find which perticular key is pressed
////////////////////////////////////////////////
unsigned char keyscan()
{
// unsigned char colloc,rowloc;
KEY =0XFF; //make p0 as input port

do
{
KEY =0xf0; //ground all rows
colloc =KEY; //read the coloumn
colloc &=0xf0; //mask used bit
}
while(colloc !=0xF0); //check until all key realeased
do
{
do
{
msdelay(10); //call delay
colloc =KEY; //see if key is pressed
colloc &=0xf0; //mask unused bits
}while(colloc ==0xf0);//check for key pressed
msdelay(10); //call delay
colloc =KEY; //see if key is pressed
colloc &=0xf0; //mask unused bits
}while(colloc ==0xf0);//check for key pressed

while(1)
{
KEY =0xfe; //ground row 0
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =0; //save row
break; //exit while loop
}
KEY =0xfd; //ground row 1
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =1; //save row
break; //exit while loop
}
KEY =0xfb; //ground row 2
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =2; //save row
break; //exit while loop
}
KEY =0XF7;
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits

Dept. of Electronics & Communication Engg., R.I.T, Hassan 43


Microcontrollers Lab Manual 06ESL47

rowloc =3; //save row


break; //exit while loop
}
//CHECK COLOUMN AND SEND DATA TO SERIAL PORT
if(colloc ==0xe0)

return (keypad[rowloc][0]);
else if(colloc ==0xd0)

return (keypad[rowloc][1]);
else if(colloc ==0xb0)

return (keypad[rowloc][2]);
else if(colloc ==0x70)

return (keypad[rowloc][3]);
}

////////////////////////////////////////////
//check key pressed is operation key or not
///////////////////////////////////////////
void chk_func_key()
{
while(1)
{
func_key = keyscan();
if(func_key=='/')
{
lcd1(func_key);
break;
}
if(func_key=='*')
{
lcd1(func_key);
break;
}
if(func_key=='-')
{
lcd1(func_key);
break;
}
if(func_key=='+')
{
lcd1(func_key);
break;
}
}
}
/////////////////////////////////////
//calculator nane display on LCD
/////////////////////////////////////
void message_display()
{
lcd_command(0x84); // Select Line 1 of LCD (DDRAM Address)
msdelay(1);

lcd_data('C');

Dept. of Electronics & Communication Engg., R.I.T, Hassan 44


Microcontrollers Lab Manual 06ESL47

msdelay(1);
lcd_data('a');
msdelay(1);
lcd_data('l');
msdelay(1);
lcd_data('c');
msdelay(1);
lcd_data('u');
msdelay(1);
lcd_data('l');
msdelay(1);
lcd_data('a');
msdelay(1);
lcd_data('t');
msdelay(1);
lcd_data('o');
msdelay(1);
lcd_data('r');
msdelay(1);
}
////////////////////////////////////////////////
//result calculation
///////////////////////////////////////////////
void find_result()
{
if(func_key=='+')
{
result=no_1+no_2;
result=result+0x30;
lcd2(result);
}

if(func_key=='-')
{
result=no_1-no_2;
result=result+0x30;
lcd2(result);
}

if(func_key=='*')
{
result=no_1 * no_2;
result=result+0x30;
lcd2(result);
}

if(func_key=='/')
{
result = no_1 / no_2;
result=result+0x30;
lcd2(result);
}
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 45


Microcontrollers Lab Manual 06ESL47

7b) Interface 4*4 matrix keyboard to 8051 and write a program to display
the key pressed value by using 7-segment LED:

#include <reg52.H>

#define KEY P0 //P0.0-P0.3=ROW AND P0.4-P0.7=COL

void msdelay(unsigned int value);


//void sertx (unsigned char);
void sevenseg (unsigned char );

unsigned char keypad[4][4]= {0x06,0x5b,0x4f,0x5e,


0x66,0x6d,0x7d,0x55,
0x07,0x7f,0x6f,0x40,
0x77,0x3f,0x7c,0x73};
void main()
{
unsigned char colloc,rowloc;
// TMOD =0X20; //TIMER 1,MODE 2
// TH1 =-3; //9600 BAUD RATE
// SCON =0X50; //8-BIT ,1STOP BIT
// TR1 =1; //START TIMER
//KEYBOARD ROUTINE.THIS SENDS THE ASCII CODE FOR PRESSED KEY
//TO SERIAL PORT
KEY =0XFF; //make p0 as input port
while(1)
{
do
{
KEY =0xf0; //ground all rows
colloc =KEY; //read the coloumn
colloc &=0xf0; //mask used bit
}
while(colloc !=0xF0); //check until all key realeased
do
{
do
{
msdelay(10); //call delay
colloc =KEY; //see if key is pressed
colloc &=0xf0; //mask unused bits
}while(colloc ==0xf0);//check for key pressed
msdelay(10); //call delay
colloc =KEY; //see if key is pressed
colloc &=0xf0; //mask unused bits
}while(colloc ==0xf0);//check for key pressed

while(1)
{
KEY =0xfe; //ground row 0
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =0; //save row
break; //exit while loop
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 46


Microcontrollers Lab Manual 06ESL47

KEY =0xfd; //ground row 1


colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =1; //save row
break; //exit while loop
}
KEY =0xfb; //ground row 2
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =2; //save row
break; //exit while loop
}
KEY =0XF7;
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
rowloc =3; //save row
break; //exit while loop
}
//CHECK COLOUMN AND SEND DATA TO SERIAL PORT
if(colloc ==0xe0)
//sertx(keypad[rowloc][0]);
sevenseg(keypad[rowloc][0]);
else if(colloc ==0xd0)
//sertx(keypad[rowloc][1]);
sevenseg(keypad[rowloc][1]);
else if(colloc ==0xb0)
//sertx(keypad[rowloc][2]);
sevenseg(keypad[rowloc][2]);
else if(colloc ==0x70)
//sertx(keypad[rowloc][3]);
sevenseg(keypad[rowloc][3]);
}
}
//void sertx(unsigned char x)
//{
// SBUF =x;
// while(TI==0);
// TI=0;
//}

void sevenseg(unsigned char x)


{
P2=0;
P1=x;
}

void msdelay(unsigned int value)


{
unsigned int x,y;
for (x=0;x<1275;x++)
for (y=0;y<value;y++);
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 47


Microcontrollers Lab Manual 06ESL47

7c) Write a C program for display message on lcd display:

#include <reg51.h>
#define ldata P1
sbit rs = P2^0;
sbit rw = P2^1;
sbit en = P2^2;
sbit busy = P1^7;
void MSDelay(unsigned int);
void lcdcmd(unsigned char );
void lcddata(unsigned char );
void lcdready();
void main()
{
unsigned char
lcd_command[]={0x38,0x0e,0x01,0x06,0x80};
unsigned char lcd_message[]="Have a Nice Day";
unsigned char c,d;
for(c=0;c<5;c++)
{
lcdcmd(lcd_command[c]);
}
for(d=0;d<15;d++)
{
lcddata(lcd_message[d]);
MSDelay(30);
}
while(1)
{
}
}
void lcdcmd(unsigned char value)
{
ldata=value; //put the value on the pins
rs=0;
rw=0;
en=1;
MSDelay(1);
en=0;
return;
}
void lcddata(unsigned char value)
{
ldata = value; // put the value on the pin
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
return;
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 48


Microcontrollers Lab Manual 06ESL47

7d) Write a C program for KEYCODE_SERIAL_PORT hyper terminal:

#include <reg51.h>

#define KEY P0 //P0.0-P0.3=ROW AND P0.4-P0.7=COL

void msdelay(unsigned int value);


void sertx (unsigned char);

unsigned char keypad[4][4]= {'1','2','3','/',


'4','5','6','*',
'7','8','9','-',
'a','0','b','+'};
void main()
{
unsigned char colloc,rowloc;
TMOD =0X20; //TIMER 1,MODE 2
TH1 =-3; //9600 BAUD RATE
SCON =0X50; //8-BIT ,1STOP BIT
TR1 =1; //START TIMER
//KEYBOARD ROUTINE.THIS SENDS THE ASCII CODE FOR PRESSED KEY
//TO SERIAL PORT
KEY =0XFF; //make p0 as input port
while(1)
{
do
{
KEY =0xf0; //ground all rows
colloc =KEY; //read the coloumn
colloc &=0xf0; //mask used bit
}
while(colloc !=0xF0); //check until all key realeased
do
{
do
{
msdelay(10); //call delay
colloc =KEY; //see if key is pressed
colloc &=0xf0; //mask unused bits
}while(colloc ==0xf0); //check for key pressed
msdelay(10); //call delay
colloc =KEY; //see if key is pressed
colloc &=0xf0; //mask unused bits
}while(colloc ==0xf0); //check for key pressed

while(1)
{
KEY =0xfe; //ground row 0
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc = 0; //save row
break; //exit while loop
}
KEY =0xfd; //ground row 1
colloc =KEY; //read coloumn

Dept. of Electronics & Communication Engg., R.I.T, Hassan 49


Microcontrollers Lab Manual 06ESL47

colloc &=0xf0; //mask unused bits


if(colloc !=0xf0) //coloumn detected
{
rowloc = 1; //save row
break; //exit while loop
}
KEY =0xfb; //ground row 2
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc = 2; //save row
break; //exit while loop
}
KEY =0XF7;
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
rowloc = 3; //save row
break; //exit while loop
}
//CHECK COLOUMN AND SEND DATA TO SERIAL PORT
if(colloc ==0xe0)
sertx(keypad[rowloc][0]);
else if(colloc ==0xd0)
sertx(keypad[rowloc][1]);
else if(colloc ==0xb0)
sertx(keypad[rowloc][2]);
else if(colloc ==0x70)
sertx(keypad[rowloc][3]);
}
}
void sertx(unsigned char x)
{
SBUF =x;
while(TI==0);
TI=0;
}
void msdelay(unsigned int value)
{
unsigned int x,y;
for (x=0;x<1275;x++)
for (y=0;y<value;y++);
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 50


Microcontrollers Lab Manual 06ESL47

Exp8

Write a C program for External ADC and Temperature control interface to


8051:

8a) Write an ADC_LCD Program:

#include<reg51.h>
#include<intrins.h>

#define adc_databus P1
#define lcd_databus P3

sbit adc_soc = (P2 ^ 5);


sbit adc_ale = (P2 ^ 3);

sbit adc_a0 = (P2 ^ 0);


sbit adc_a1 = (P2 ^ 1);
sbit adc_a2 = (P2 ^ 2);

sbit lcd_rs = (P0 ^ 0);


sbit lcd_rw = (P0 ^ 1);
sbit lcd_en = (P0 ^ 2);

unsigned char read_adc();


void delay_5ms();
void delay_200us();
void lcd_init();
void lcd_command(unsigned char command);
void lcd_data(unsigned char d);
void display_data(unsigned char d);

unsigned char adc_data;

void main(void)
{
lcd_init();

lcd_data('C');
delay_200us();
lcd_data('h');
delay_200us();
lcd_data('a');
delay_200us();
lcd_data('n');
delay_200us();
lcd_data('n');
delay_200us();
lcd_data('e');
delay_200us();
lcd_data('l');
delay_200us();
lcd_data('0');
delay_200us();
lcd_data(':');
delay_200us();
lcd_data(' ');

Dept. of Electronics & Communication Engg., R.I.T, Hassan 51


Microcontrollers Lab Manual 06ESL47

delay_200us();

// select channel 0
adc_a0 = 0;
adc_a1 = 0;
adc_a2 = 0;

while(1)
{
adc_data = read_adc();
display_data(adc_data);

}
}

unsigned char read_adc()


{
unsigned char channel_data;

// send start of conversion pulse


adc_ale = 0;
adc_soc = 0;
delay_5ms();

adc_ale = 1;
adc_soc = 1;
delay_5ms();

adc_ale = 0;
adc_soc = 0;
delay_5ms();

delay_5ms();
delay_5ms();

// read data
channel_data = adc_databus;
return channel_data;
}

void display_data(unsigned char d)


{
unsigned char temp;

lcd_command(0x8a);
delay_200us();

temp = d / 100;
d = d - (temp * 100);
temp = temp | 0x30;
lcd_data(temp);
delay_200us();

temp = d / 10;
d = d - (temp * 10);

Dept. of Electronics & Communication Engg., R.I.T, Hassan 52


Microcontrollers Lab Manual 06ESL47

temp = temp | 0x30;


lcd_data(temp);
delay_200us();

temp = d;
temp = temp | 0x30;
lcd_data(temp);
delay_200us();

void delay_200us()
{
unsigned char delay=100;

for(delay; delay > 0; --delay) {}


}

void delay_5ms()
{
unsigned int delay;

for(delay = 2500; delay > 0; --delay) { }


}

void lcd_init()
{
// LCD Initialisation

delay_5ms(); // 20 ms Power on Delay


delay_5ms();
delay_5ms();
delay_5ms();

lcd_command(0x38); // Function Set


delay_5ms();

lcd_command(0x38); // Function Set


delay_5ms();

lcd_command(0x38); // 16x2 Chars., 5x7


matrix, 8-bit data
delay_200us();

lcd_command(0x14); // Shift Display


delay_200us();

lcd_command(0x0C); // Display on/off


delay_200us();

lcd_command(0x06); // Entry Mode


delay_200us();

lcd_command(0x01); // Clear Display


delay_5ms();

// LCD Initialization over

Dept. of Electronics & Communication Engg., R.I.T, Hassan 53


Microcontrollers Lab Manual 06ESL47

lcd_command(0x80); // Select Line 1 of LCD


(DDRAM Address)
delay_200us();
}

void lcd_command(unsigned char command)


{
unsigned char i;
lcd_rs = 0;
lcd_rw = 0;
lcd_databus = command;
lcd_en = 1;
i++;i--;
lcd_en = 0;
}

void lcd_data(unsigned char d)


{
unsigned char i;

lcd_rs = 1;
lcd_rw = 0;
lcd_databus = d;
lcd_en = 1;
i++;
i--;
lcd_en = 0;
}

8b) Write an ADC Temperature Program:

#include<reg51.h>
#include<intrins.h>

#define adc_databus P1
#define lcd_databus P3

sbit adc_soc = (P2 ^ 5);


sbit adc_ale = (P2 ^ 3);

sbit adc_a0 = (P2 ^ 0);


sbit adc_a1 = (P2 ^ 1);
sbit adc_a2 = (P2 ^ 2);

sbit lcd_rs = (P0 ^ 0);


sbit lcd_rw = (P0 ^ 1);
sbit lcd_en = (P0 ^ 2);

unsigned char read_adc();


void delay_5ms();
void delay_200us();
void lcd_init();
void lcd_command(unsigned char command);
void lcd_data(unsigned char d);
void display_data(unsigned char d);

Dept. of Electronics & Communication Engg., R.I.T, Hassan 54


Microcontrollers Lab Manual 06ESL47

unsigned char adc_data;

void main(void)
{
lcd_init();

lcd_data('C');
delay_200us();
lcd_data('h');
delay_200us();
lcd_data('a');
delay_200us();
lcd_data('n');
delay_200us();
lcd_data('n');
delay_200us();
lcd_data('e');
delay_200us();
lcd_data('l');
delay_200us();
lcd_data('5');
delay_200us();
lcd_data(':');
delay_200us();
lcd_data(' ');
delay_200us();

// select channel 5
adc_a0 = 1;
adc_a1 = 0;
adc_a2 = 1;

while(1)
{
adc_data = read_adc();
display_data(adc_data);

}
}

unsigned char read_adc()


{
unsigned char channel_data;

// send start of conversion pulse


adc_ale = 0;
adc_soc = 0;

delay_5ms();
adc_ale = 1;
adc_soc = 1;
delay_5ms();

adc_ale =0;

Dept. of Electronics & Communication Engg., R.I.T, Hassan 55


Microcontrollers Lab Manual 06ESL47

adc_soc = 0;
delay_5ms();

delay_5ms();
delay_5ms();

// read data
channel_data = adc_databus;
return channel_data;
}

void display_data(unsigned char d)


{
unsigned char temp;

lcd_command(0x8a);
delay_200us();

temp = d / 100;
d = d - (temp * 100);
temp = temp | 0x30;
lcd_data(temp);
delay_200us();

temp = d / 10;
d = d - (temp * 10);
temp = temp | 0x30;
lcd_data(temp);
delay_200us();

temp = d;
temp = temp | 0x30;
lcd_data(temp);
delay_200us();

void delay_200us()
{
unsigned char delay=100;

for(delay; delay > 0; --delay) {}


}

void delay_5ms()
{
unsigned int delay;

for(delay = 2500; delay > 0; --delay) { }


}

void lcd_init()
{
// LCD Initialisation

delay_5ms(); // 20 ms Power on Delay

Dept. of Electronics & Communication Engg., R.I.T, Hassan 56


Microcontrollers Lab Manual 06ESL47

delay_5ms();
delay_5ms();
delay_5ms();

lcd_command(0x38); // Function Set


delay_5ms();

lcd_command(0x38); // Function Set


delay_5ms();

lcd_command(0x38); // 16x2 Chars., 5x7 matrix, 8-


bit data
delay_200us();

lcd_command(0x14); // Shift Display


delay_200us();

lcd_command(0x0C); // Display on/off


delay_200us();

lcd_command(0x06); // Entry Mode


delay_200us();

lcd_command(0x01); // Clear Display


delay_5ms();

// LCD Initialization over

lcd_command(0x80); // Select Line 1 of LCD (DDRAM


Address)
delay_200us();
}

void lcd_command(unsigned char command)


{
unsigned char i;
lcd_rs = 0;
lcd_rw = 0;
lcd_databus = command;
lcd_en = 1;
i++;i--;
lcd_en = 0;
}

void lcd_data(unsigned char d)


{
unsigned char i;

lcd_rs = 1;
lcd_rw = 0;
lcd_databus = d;
lcd_en = 1;
i++;
i--;
lcd_en = 0;
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 57


Microcontrollers Lab Manual 06ESL47

Exp-9
Generate different waveforms Sine, Square, Triangular, Ramp etc. Using DAC
Interface to 8051.

9a) Write a DAC Sine wave Program:

#include<reg51.h>
void MSDelay(unsigned int itime);
void main()
{
unsigned char
WAVEVALUE[12]={128,192,238,255,238,192,128,64,17,0,17,64};

unsigned char x;
while(1)
{
for(x=0;x<12;x++)
{
P1=WAVEVALUE[x];
MSDelay(1);
}
}
}

void MSDelay(unsigned int itime)


{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<25;j++);
}

9b) Write a Negative Ramp Program:

#include <reg51.h>
void MSDelay(unsigned int);
void main (void)
{
P0=0x40;
P1=00;

for(;;)
{
P1--;
MSDelay(1);
}
}

void MSDelay(unsigned int itime)


{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<20;j++);
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 58


Microcontrollers Lab Manual 06ESL47

9c) Write an Positive Ramp Program:

#include <reg51.h>
void MSDelay(unsigned int);
void main (void)
{
P0=0x40;
P1=00;

for(;;)
{
MSDelay(1);
P1++;

}
}

void MSDelay(unsigned int itime)


{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<20;j++);
}

9d) Write a Square Wave Program:

//toggle P1 forever with some delay in between "ON" and "OFF"


# include<reg51.h>
void MSDelay(unsigned int);
void main(void)
{
P0=0X20;
for(;;)
{
P1=0x00;
MSDelay(1);
P1=0xFF;
MSDelay(1);
}
}

void MSDelay(unsigned int itime)


{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<25;j++);
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 59


Microcontrollers Lab Manual 06ESL47

9e) Write a Triangular Wave Program:

#include <reg51.h>
void MSDelay(unsigned int);
void main(void)

{
unsigned char count;
P0=0x80;

P1=00;
while(1)
{
do
{
count=P1+ 0x01;
P1=count;
MSDelay(1);
}while(count<=0XFE);

do
{
count=P1 - 0x01;
P1=count;
MSDelay(1);
}while(count>=0X01);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<20;j++);
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 60


Microcontrollers Lab Manual 06ESL47

Exp10

Stepper and DC Motor control interface to 8051.

10a) Write a DC Motor Program:

#include<reg51.h>
sbit mtr_1 =P0^0;
sbit mtr_2 =P0^1;

sbit pwm_control =P2^6;

sbit key2_REV = P1^2;


sbit key1_FRW = P1^3;
sbit speed_dec = P1^4;
sbit speed_inc = P1^5;

void main(void)
{
unsigned int count,value=500;
pwm_control =0;
mtr_1 =0;
mtr_2 =1;
while(1)
{
if(key1_FRW == 0)
{
mtr_1 =0;
mtr_2 =1;
// pwm_control = 1;
}
// key1_FRW = 1;

if(key2_REV == 0)
{
mtr_1 =1;
mtr_2 =0;
// pwm_control = 1;
}

if(speed_dec == 0 )
{
value -= 35;
if(value <= 35)
{
value = 36;//
}
}
if(speed_inc ==0 )
{
value += 25;
if(value >= 1000)
{
value = 976;
}
}
pwm_control=1;

Dept. of Electronics & Communication Engg., R.I.T, Hassan 61


Microcontrollers Lab Manual 06ESL47

for(count=0;count<=value ;count++);

pwm_control=0;
for(count=0;count<=500;count++);
}
while(1);
}

10b) Write a Stepper Motor Program

/*-----------------------------------------------------------------------
Designer: i2c logic

Module Name: stepper motor speed and direction control

Describition : Interface stepper motor to control direction and speed by


accepting key pressed.

Procedure:
1. Connect port J3 of SDM card to 26 pin connector of MEGA-EM-P02.
2.Connect the power supply to both microcontroller and SDM card.
Note - SDM card require separate +12V power supply.without power
supply motor is not rotating Also remove the jumper J1 on microcontroller
board.
3. Write the program and create hex file.Connect the serial cable to
serial port. Then apply poer supply to boards.Download the program trough
flah magic.
4. Use the key1 and key2 on the SDM card to rotate the motor.If you press
key1 to rotate the motor clockwise and if press key2 it rotate in
anticlockwise. key3 & key4 ars used for speed control.By pressing key3
speed goes on incremating and pressing key4 motor speed decresing.
5. see it by practicaly.
------------------------------------------------------------------------*/
CLOCKWISE:
#include<reg51.h>
#define DATA_BUS P0
void delay_msec(unsigned int );
void main()
{
unsigned char full_step_clock[4] = {0x28,0x24,0x14,0x18};
unsigned char i;
while(1)

for(i = 0;i <= 3; i++)


{
DATA_BUS = full_step_clock[i];
delay_msec(50);
}
}

void delay_msec(unsigned int count)


{
unsigned int r;
TMOD = 0X01;
TH0 = 0XF8;
TL0 = 0XCD;
TR0 = 1;

Dept. of Electronics & Communication Engg., R.I.T, Hassan 62


Microcontrollers Lab Manual 06ESL47

for(r = 0; r < count; r++)


{
while(!TF0);
TF0 = 0;
TH0 = 0XF8;
TL0 = 0XCD;
}
TR0 = 0;
}

ANTI CLOCKWISE:
#include<reg51.h>
#define DATA_BUS P0
void delay_msec(unsigned int );
void main()
{
unsigned char full_step_clock[4] = {0x18,0x14,0x24,0x28};
unsigned char i;
while(1)

for(i = 0;i <= 3; i++)


{
DATA_BUS = full_step_clock[i];
delay_msec(50);
}
}
void delay_msec(unsigned int count)
{
unsigned int r;
TMOD = 0X01;
TH0 = 0XF8;
TL0 = 0XCD;
TR0 = 1;
for(r = 0; r < count; r++)
{
while(!TF0);
TF0 = 0;
TH0 = 0XF8;
TL0 = 0XCD;
}
TR0 = 0;
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 63


Microcontrollers Lab Manual 06ESL47

Exp-11

Write a program to implement an Elevator interface to 8051:

#include<reg51.h>
sbit FLOOR1_KEY =P1^3;
sbit FLOOR2_KEY =P1^2;
sbit FLOOR3_KEY =P1^1;
sbit FLOOR4_KEY =P1^0;

sbit CLR_FLOOR1_KEY = P2^7;


sbit CLR_FLOOR2_KEY = P2^6;
sbit CLR_FLOOR3_KEY = P2^5;
sbit CLR_FLOOR4_KEY = P2^4;

bit membit;

sbit LED_A=P2^0;
sbit LED_B=P2^1;
sbit LED_C=P2^2;
sbit LED_D=P2^3;

void delay(unsigned char);


void UPDATE_DISPLAY();
void GOTO_FLOOR1();
void GOTO_FLOOR2();
void GOTO_FLOOR3();
void GOTO_FLOOR4();
void CHECK_KEY_PRESS();
void END_GOTO_FLOOR1();
void main(void)
{
P2=0x0f; //port2 upper 4 bit are used as output
P1=0xff; //p1 used as input
P3=0xff;

//P2=0x00;
delay(10);
delay(10);
P2=0xff;
P3=0x00;
P2=P3|0xf0;
while(1)
{
CHECK_KEY_PRESS();
}
}

void CHECK_KEY_PRESS()
{
if(FLOOR1_KEY==0)
{
GOTO_FLOOR1();
}
else
{
if(FLOOR2_KEY==0)

Dept. of Electronics & Communication Engg., R.I.T, Hassan 64


Microcontrollers Lab Manual 06ESL47

{
GOTO_FLOOR2();
}
else
{
if(FLOOR3_KEY==0)
{
GOTO_FLOOR3();
}
else
{
if(FLOOR4_KEY==0)
{
GOTO_FLOOR4();
}
else
{

}
}
}
}

}
void delay(unsigned char value)
{
unsigned int x,y;
for(x=0;x<1275;x++)
for(y=0;y<value;y++);
}

void GOTO_FLOOR1()
{
while(P3!=0x00)
{
P3--;
delay(20);
P2=0xf0|P3;
}

while(FLOOR1_KEY==0)
{
CLR_FLOOR1_KEY=0;
delay(10);
}
CLR_FLOOR1_KEY=1;
delay(10);
delay(10);
FLOOR1_KEY=1;
}

void GOTO_FLOOR2()
{
while(P3!=0x03)
{
if(P3>0x03)

Dept. of Electronics & Communication Engg., R.I.T, Hassan 65


Microcontrollers Lab Manual 06ESL47

{
P3--;
P2=0xf0|P3;
delay(20);
}
else
{
P3++;
P2=0xf0|P3;
delay(20);
}
}

CLR_FLOOR2_KEY=0;
delay(10);
delay(10);

CLR_FLOOR2_KEY=1;
delay(10);
delay(10);

void GOTO_FLOOR3()
{
while(P3!=0x06)
{
if(P3>0x06)
{
P3--;
P2=0xf0|P3;
delay(20);
}
else
{
P3++;
P2=0xf0|P3;
delay(20);
}
}
CLR_FLOOR3_KEY=0;
delay(10);
delay(10);

CLR_FLOOR3_KEY=1;
delay(10);
delay(10);
}

void GOTO_FLOOR4()
{
while(P3!=0x09)
{
P3++;
P2=0xf0|P3;
delay(20);
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 66


Microcontrollers Lab Manual 06ESL47

CLR_FLOOR4_KEY=0;
delay(10);
delay(10);

CLR_FLOOR4_KEY=1;
delay(10);
delay(10);
}

Dept. of Electronics & Communication Engg., R.I.T, Hassan 67