Vous êtes sur la page 1sur 64

MICROCONTROLLER

AND
APPLICATIONS

(LAB)
Shri Guru Gobind Singhji Institute Of Engineering And Technology

CONTENTS
SNo. Name of the Experiment Page No. Remark
Experiment-1 (Assembly Language Programs for Addition and Subtraction)
1 a) 8-bit addition for unsigned numbers by using different addressing modes 5
2 b) 8-bit subtraction for unsigned numbers by using different addressing modes 7
3 c) Subtract two 16-bit numbers with out using SUBB instruction 9
4 d) Adding two 3 byte BCD numbers in subroutine 10

Experiment-2 (Assembly Language Programs for Multiplication and Division)


5 a) 8-bit × 8-bit multiplication 12
6 b) 8-bit ÷ 8-bit division 13
7 c) 16-bit × 16-bit multiplication 14
8 d) To set alternate bits in first half addressable memory and set 1 bit then reset 16
2 bits then set three bits and so on in remaining half bit addressable memory

Experiment-3 (Assembly Language Programs on array of Hexadecimal


numbers)
10 a)Sorting n-hexadecimal number in Ascending order 19
11 b) Sorting n-hexadecimal number in Descending order 20
12 c) To find largest number in an array of n-hexadecimal numbers 21
13 d) To find smallest number in an array of n-hexadecimal numbers 22
14 e) To find number of EVEN and ODD in an array of n-hexadecimal numbers 23

Experiment-4 (Assembly Language Programs)


15 a) To compute Z = (A + B) × C × (D ÷ E) 25
16 b) To find roots of given quadratic equation (x2+10x+9) 27
17 c) To Convert a given BCD number to its ASCII equivalent code (in ALP & C) 29
Experiment-5 (Interfacing 8 LEDs to 8051)
17 a) Program to blink each LED one by one 32
18 b) Program to display binary number from 0-to-F on LEDs 34

Experiment-6 (Interfacing 7-Segment Display to 8051)


19 a) Program to display moving E.C.E. 36

Experiment-7 (Interfacing Switches and 7-Segment Display to 8051)


20 a) Program to detect Switch pressed and display it 39

Experiment-8 ( Stepper Motor Interfacing)


21 a) Program to rotate stepper motor 600 CW and 450 CCW if input pin is at logic 41
1 otherwise 220 CCW

Experiment-9 (Interfacing Hexadecimal Matrix keyboard to 8051)


22 a) Program to detect key pressed and display it 44

Microcontroller And Applications Lab 2


Shri Guru Gobind Singhji Institute Of Engineering And Technology

Experiment-14 ( LCD Interfacing to 8051)


30 a) Program to display on LCD 63

Microcontroller And Applications Lab 3


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 1

a) 8-bit addition for unsigned numbers by using different addressing modes

Type 1- Register Addressing Mode:

ORG 2000H
START:
MOV A, #20H ; Move 02H to A
MOV B, #10H ; Move 10H to B
ADD A, B ; Add A B
RET
END

INPUT: A - #20H, B- #10H OUTPUT: A - #30H

Type 2- Direct Addressing Mode:

Microcontroller And Applications Lab 4


Shri Guru Gobind Singhji Institute Of Engineering And Technology

ORG 2000H
START:
MOV A, #10H ; Move 10H to A
MOV B, #11H ; Move 11H to B
ADD A, 0F0H ; Add contents in A and 0F0H (B)
RET
END

Type 3- Indirect Addressing Mode:

ORG 2000H
START:
MOV A, #01H ; Move 01H to A
MOV R1, #0F0H ; Move the Address of register B (0F0H) to R1
MOV B, #05H ; Move 05H to B
ADD A, @R1 ; Add A and the content in the Address pointed by R1, i.e., B
RET
END

INPUT: A - #01H, B - #05H OUTPUT: A - #06H

Type 4- Immediate Addressing Mode:

ORG 2000H
START:
MOV A, #03H ; Move 03H to A
ADD A, #02H ; Add A and the immediate value 02H
RET
END

INPUT: A - #03H, Immediate value - #02H OUTPUT: A - #05H

Microcontroller And Applications Lab 5


Shri Guru Gobind Singhji Institute Of Engineering And Technology

a) 8-bit subtraction for unsigned numbers by using different addressing modes

Type 1- Register Addressing Mode:

ORG 2000H
START:
MOV A, #20H ; Move 20H to A
MOV B, #10H ; Move 10H to B
SUBB A, B ; Subtract B from A
RET
END

INPUT: A – #20H, B – #10H OUTPUT: A - #10H

Type 2- Direct Addressing Mode:

Microcontroller And Applications Lab 6


Shri Guru Gobind Singhji Institute Of Engineering And Technology

ORG 2000H
START:
MOV A, #11H ; Move 11H to A
MOV B, #10H ; Move 10H to A
SUBB A, 0F0H ; Subtract contents in A and 0F0H (B)
RET
END

INPUT: A - #11H, B - #10H OUTPUT: A - #01H

Type 3- Indirect Addressing Mode:

ORG 2000H
START:
MOV A, #05H ; Move 05H to A
MOV R1, #0F0H ; Move the Address of register B (0F0H) to R1
MOV B, #01H ; Move 01H to B
SUBB A, @R1 ; Subtract A and the content in the Address, pointed by R1, i.e., B
RET
END

INPUT: A - #05H, B - #01H OUTPUT: A - #04H

Type 4- Immediate Addressing Mode:

ORG 2000H
START:
MOV A, #03H ; Move 03H to A
SUBB A, #02H ; Subtract immediate value 02H from A
RET
END

INPUT: A - #03H, Immediate value - #02H OUTPUT: A - #01H

Microcontroller And Applications Lab 7


Shri Guru Gobind Singhji Institute Of Engineering And Technology

c) Subtract two 16-bit numbers with out using SUBB instruction

ORG 2000H
START:
MOV A, 23H ; Move lower byte of 2nd number to A
MOV B, 21H ; Move lower byte of 1st number to B
CPL A ; Compliment A
INC A ; Increment A, Now number in A is 2’s compliment of the original
MOV R0, A ; Move the contents in A to R0
MOV A, 22H ; Move the higher byte of 2nd number to A
CPL A ; Compliment A
ADDC A, #00H ; Add A, #00h with carry
MOV R1, A ; Move the contents in A to R1
CLR C ; Clear Carry
MOV A, R0 ; Move the lower byte of the 2’s compliment to A

Microcontroller And Applications Lab 8


Shri Guru Gobind Singhji Institute Of Engineering And Technology
ADD A, B ; Add A and lower byte of the 2nd number
MOV R4, A ; Move the lower byte result to R4
MOV A, R1 ; Move the higher byte of the 2’s compliment to A
MOV B, 20H ; Move the higher byte of the 1st number to B
ADDC A, B ; Add A, B with carry
MOV R3, A ; Move the higher byte result to R3
RET
END

INPUT: No1 – 5683, No2 - 12AA


1st Number: 2nd Number:
20H - #56H 22H - #12H
21H - #83H 23H - #AAH

OUTPUT: 43D9
R3 - #43H R4 - #D9

d) Adding two 3 byte BCD numbers

ORG 2000H
START:
MOV A, 22H ; Move the lower byte of 1st BCD number to A
MOV B, 25H ; Move the lower byte of 2nd BCD number to B
ADD A, B ; Add A B
DA A ; Decimal adjust A
MOV R3, A ; Move the result to R3
MOV A, 21H ; Move the middle byte of 1st BCD number to A
MOV B, 24H ; Move the middle byte of 2nd BCD number to B
ADDC A, B ; Add with carry A and B
DA A ; Decimal adjust A
MOV R2, A ; Move the result to R2

Microcontroller And Applications Lab 9


Shri Guru Gobind Singhji Institute Of Engineering And Technology
MOV A, 20H ; Move the higher byte of 1st BCD number to A
MOV B, 23H ; Move the higher byte of 2nd BCD number to B
ADDC A, B ; Add with carry A and B
DA A ; Decimal adjust A
MOV R1, A ; Move the result to R1
JNC NC ; If no carry jump to NC
JC CA ; If carry exist jump to CA
NC: MOV R0, #00H ; Move 00H to R0 if no carry
JMP HERE ; Jump HERE
CA: MOV RO, #01H ; Move 01H to R0 if carry exist
JMP HERE ; Jump HERE
HERE: RET
END

INPUT:
1st Number- 979899, 2nd Number- 878889
20H - #97H 23H - #87H
21H - #98H 24H - #88H
22H - #99H 25H - #89H

OUTPUT: 01858788 H
R0 – #01H
R1 – #85H
R2 - #87h
R3 - #88H

Microcontroller And Applications Lab 10


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 2

a) 8-bit × 8-bit multiplication

ORG 2000H
START:
MOV A, #12H ; Move 12H to A
MOV B, #23H ; Move 23H to B
MUL A B ; Multiply A B
MOV R0, B ; Move the lower byte of the result to R0
MOV R1, A ; Move the higher byte of the result to R1
RET
END

INPUT: A - #12H, B - #23H OUTPUT: R0 - #02H, R1 - #76H

Microcontroller And Applications Lab 11


Shri Guru Gobind Singhji Institute Of Engineering And Technology

b) 8-bit ÷ 8-bit division

Type 1 – Dividing Larger number by smaller number:

ORG 2000H
START:
MOV A, #A2H ; Move A2H to A
MOV B, #06H ; Move 06H to B
DIV A B ; Divide A B, quotient is Stored in A and Remainder is stored in B
RET
END

INPUT: A - #A2H, B - #06H OUTPUT: A – #1BH (Quotient), B – #00H (Remainder)

Microcontroller And Applications Lab 12


Shri Guru Gobind Singhji Institute Of Engineering And Technology
Type 2 – Dividing Smaller number by Larger number:

ORG 2000H
START;
MOV A, #02H ; Move 02H to A
MOV B, #09H ; Move 09H to A
DIV A B ; Divide A B, quotient is Stored in A and Remainder is stored in B
RET
END

INPUT: A - #02H, B - #06H OUTPUT: A – #00H (Quotient), B – #02H (Remainder)

Type 3 – Dividing any number by ZERO:

ORG 2000H
START:
MOV A, #A1H ; Move A1H to A
MOV B, #00H ; Move 00H to A
DIV A B ; Divide A B, quotient is Stored in A and Remainder is stored in B
RET
END

INPUT: A - #A1H, B - #00H OUTPUT: A - #FFH (Quotient), B - #A1H (Remainder)

c) 16-bit × 16-bit multiplication

ORG 2000H
START:
MOV A, 31H ; Move lower byte of 1st number to A
MOV B, 33H ; Move lower byte of 2nd number to B
MUL A B ; Multiply A B
MOV R0, B ; Move higher byte of the result to R0
MOV 13H, A ; Move the lower byte of the result to 13H or R3 of Bank 2
MOV A, 33H ; Move the lower byte of 2nd number to A
MOV B, 30H ; Move higher byte of the 1st number to B
MUL A B ; Multiply A B
MOV R2, B ; Move the higher byte result to R2
MOV R3, A ; Move the lower byte result to R3
MOV A, 32H ; Move the higher byte of 2nd number to A

Microcontroller And Applications Lab 13


Shri Guru Gobind Singhji Institute Of Engineering And Technology
MOV B, 31H ; Move the lower byte of 1st number to B
MUL A B ; Multiply A B
MOV R4, B ; Move the higher byte result to R4
MOV R5, A ; Move the lower byte result to R5
MOV A, 32H ; Move the higher byte of 2nd number to A
MOV B, 30H ; Move the higher byte of 1st number to B
MUL A B ; Multiply A B
MOV R6, B ; Move the higher byte result to R6
MOV R7, A ; Move the lower byte result to R7
MOV A, R0 ; Move the higher byte value in R0 to A
MOV B, R3 ; Move the lower byte value in R3 to B
ADD A, B ; Add A, B
MOV B, R5 ; Move the contents in R5 to B
ADDC A, B ; Add A, B with carry
MOV 12H, A ; Move the result to 12H or R2 of Bank 2
MOV A, R2 ; Move the contents in R2 to A
MOV B, R4 ; Move contents in R4 to B
ADD A, B ; Add A, B
MOV B, R7 ; Move contents in R7 to B
ADDC A, B ; Add A, B with carry
MOV 11H, A ; Store the result in 11H or R1 of bank 2
MOV A, R6 ; Move R6 contents to A
ADDC A, #00H ; Add 00H to A with carry
MOV 10H, A ; Move the result to 10H or R0 of bank2
RET
END

INPUT: 1st number- 1234, 2nd number-1122


30H - #12H 32H – #11H
31H - #34H 33H - #22H

OUTPUT: 0137DEE8
Bank 2:
R0/10H - #01H
R1/11H - #37H
R2/12H - #DEH
R3/13H - #E8H

Microcontroller And Applications Lab 14


Shri Guru Gobind Singhji Institute Of Engineering And Technology
12 34
11 22
----------------------
06 E8
02 64
03 74
01 32
----------------------
01 37 DE E8
----------------------

d) To set alternate bits in first half addressable memory and set 1 bit then reset 2 bits then set
three bits and so on in remaining half bit addressable memory

MOV R6, #08H ;counter of 8

MOV R0,#1FH ;addr of ram location just before bit-addr's 1st location

AGAIN:INC R0

MOV @R0,#0AAH ;AAh is the data for alternate set-reset

DJNZ R6,AGAIN

MOV 28H,#39H

MOV 29H,#7CH

MOV 2AH,#0E0H

MOV 2BH,#0FH

Microcontroller And Applications Lab 15


Shri Guru Gobind Singhji Institute Of Engineering And Technology
MOV 2CH,#0F0H

MOV 2DH,#1FH

MOV 2EH,#80H

MOV 2FH,#0FFH

SJMP $

OUTPUT:
RAM AREA
20H=AAH 28H=39H
21H=AAH 29H=7CH
' 2AH=E0H
' 2BH=0FH
' 2CH=F0H
' 2DH=1FH
' 2EH=80H
27H=AAH 2FH=FFH

PROGRAM TO SET THE BIT ADDRESABLE RAM

MOV A,#0FFH ;SET ALL BITS OF ACC.


MOV R0,#20H ;MOVE THE STARTING LOCATION IN R0
MOV R7,#0FH ;MOVE THE NO OF BIT ADDRESSABLE LOCATION
UP: MOV @R0,A ;CLEAR THE CONTENT OF LOCATION POINTED BY RO
INC R0 ;INCREMENT REGISTER R0
DJNZ R7,UP ;DECREMENT & CHECK WHETHER R7=0
LOOP: SJMP LOOP

OUTPUT:
RAM AREA
20H=FFH
21H=FFH
'
'
'
'
2FH=FFH

Microcontroller And Applications Lab 16


Shri Guru Gobind Singhji Institute Of Engineering And Technology

PROGRAM TO RESET THE BIT ADDRESABLE RAM


MOV A,#00H ;CLEAR ACC.
MOV R0,#20H ;MOVE THE STARTING LOCATION IN R0
MOV R7,#0FH ;MOVE THE NO OF BIT ADDRESSABLE LOCATION
UP: MOV @R0,A ;CLEAR THE CONTENT OF LOCATION POINTED BY RO
INC R0 ;INCREMENT REGISTER R0
DJNZ R7,UP ;DECREMENT & CHECK WHETHER R7=0
LOOP: SJMP LOOP:

OUTPUT:
;RAM AREA
20H=00H
21H=00H
'
'
'
'
2FH=00H

Microcontroller And Applications Lab 17


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 3

a) Sorting n-hexadecimal number in ascending order

ORG 2000H
START:
MOV R0, #04H ; Initialize the counter for outer loop by moving 04H to R0

LOOP1: MOV R1, #20H ; initialize the starting index of the array by moving 20H to R1
MOV A, R0 ; Move the contents in R0 to A
MOV R2, A ; Move the contents in A to R2 so as initialize the inner loop counter
LOOP2: MOV A,@R1 ; Move the contents in the location pointed by R1, Ex: 20h
MOV R3, A ; Move contents in A to R3
INC R1 ; Increment R1 for next number
MOV A,@R1 ; Move the contents in the location pointed by R1
SUBB A, R3 ; Subtract contents in A and R3

Microcontroller And Applications Lab 18


Shri Guru Gobind Singhji Institute Of Engineering And Technology
JNC CONTINUE ; If carry is not generated by the above instruction jump to CONTINUE, i.e., contents
;in A>R3 (2nd number is greater than the 1st)

SWAP: MOV A,@R1 ; Move the contents in the location pointed by R1 to A


XCH A, R3 ; Exchange the contents in A and R3
MOV @R1, A ; Move the contents in A to the location pointed by R1
DEC R1 ; Decrement R1
MOV A, R3 ; Move the contents in R3 to A
MOV @R1, A ; Move the contents in A to the location pointed by R1
INC R1 ; Increment R1 so as to return to the default value.

CONTINUE:
DJNZ R2, LOOP2 ; (inner loop) Decrement R2, if not zero jump to loop2
DJNZ R0, LOOP1 ; (outer Loop) Decrement R0, if not zero jump to loop1

RET
END

INPUT: OUTPUT:
20H - #24H 20H - #02H
21H - #02H 21H - #05H
22H - #05H 22H - #12H
23H - #58H 23H - #24H
24H - #12H 24H - #58H

b) Sorting n-hexadecimal number in descending order

ORG 2000H
START:
MOV R0, #04H ; Initialize the counter for outer loop by moving 04H to R0

LOOP1: MOV R1, #20H ; Initialize the starting index of the array by moving 20H to R1
MOV A, R0 ; Move the contents in R0 to A
MOV R2, A ; Move the contents in A to R2 so as initialize the inner loop counter
LOOP2: MOV A,@R1 ; Move the contents in the location pointed by R1, Ex: 20h
MOV R3, A ; Move contents in A to R3
INC R1 ; Increment R1 for next number
MOV A,@R1 ; Move the contents in the location pointed by R1
SUBB A, R3 ; Subtract contents in A and R3

Microcontroller And Applications Lab 19


Shri Guru Gobind Singhji Institute Of Engineering And Technology
JC CONTINUE ; If carry is generated by the above instruction jump to CONTINUE, i.e., contents in
;A>R3 (2nd number is greater than the 1st)

SWAP: MOV A,@R1 ; Move the contents in the location pointed by R1 to A


XCH A, R3 ; Exchange the contents in A and R3
MOV @R1, A ; Move the contents in A to the location pointed by R1
DEC R1 ; Decrement R1
MOV A, R3 ; Move the contents in R3 to A
MOV @R1, A ; Move the contents in A to the location pointed by R1
INC R1 ; Increment R1 so as to return to the default value.

CONTINUE:
DJNZ R2, LOOP2 ; (inner loop) Decrement R2, if not zero jump to loop2
DJNZ R0, LOOP1 ; (outer Loop) Decrement R0, if not zero jump to loop1

RET
END

INPUT: OUTPUT:
20H - #02H 20H - #58H
21H - #05H 21H - #12H
22H - #12H 21H - #05H
23H - #01H 23H - #02H
24H - #58H 24H - #01H

c) To find largest number in an array of n-hexadecimal numbers

ORG 0000H ;Originate RAM address with 0000H


START:
MOV R0,#00H ;Initialise starting index of an array by loading 00H into R0
MOV R1,#05 ;Initialise R1=5(n=5) as a counter
MOV B,#0 ;Initialise reg. B=00,for comparision of two no.(other is to loaded in
accumulator)
AGAIN:MOV A,@R0 ;Load number (stored at location of RAM from 0000H-0004H) in accumulator
CJNE A,B,LOOP ;Compare the two numbers( one in A & other in B) and jump to desired
loction if not equal
LOOP: JC LOOP1 ;Jump if there is carry in above operation
MOV B,A ;Move LARGER no. to B
INC R0 ;Increament RAM address(Jump to the next location)

Microcontroller And Applications Lab 20


Shri Guru Gobind Singhji Institute Of Engineering And Technology
DJNZ R1,AGAIN ;Decreament counter and repeat loop if =/=0
SJMP NEXT ;Take a short jump to NEXT
LOOP1: INC R0; ;if there is carry in above operation,then B is greater then increament
address and decreament counter and again repeat loop if counter=/=0
DJNZ R1,AGAIN
NEXT: MOV A,B ;Move the largest no. found out to A
MOV 20H,A ;Store the no.to 20H location of RAM

END

INPUT: OUTPUT:
00H - #42H 20H - #90H
01H - #67H
02H - #90H
03H - #20H
04H - #01H

d) To find smallest number in an array of n-hexadecimal numbers

ORG 0000H ;Originate RAM address with 0000H


START:
MOV R0,#00H ;Initialise starting index of an array by loading 00H into R0
MOV R1,#05 ;Initialise R1=5(n=5) as a counter
MOV B,#0 ;Initialise reg. B=00,for comparision of two no.(other is to loaded in
accumulator)
AGAIN:MOV A,@R0 ;Load number (stored at location of RAM from 0000H-0004H) in accumulator
CJNE A,B,LOOP ;Compare the two numbers( one in A & other in B) and jump to desired
loction if not equal
LOOP: JNC LOOP1 ;Jump if there is no carry in above operation

MOV B,A ;Move smaller no. to B

Microcontroller And Applications Lab 21


Shri Guru Gobind Singhji Institute Of Engineering And Technology

INC R0 ;Increament RAM address(Jump to the next location)


DJNZ R1,AGAIN ;Decreament counter and repeat loop if =/=0
SJMP NEXT ;Take a short jump to NEXT
LOOP1: INC R0; ;if there is no carry in above operation,then B is smaller, then increament
address and decreament counter and again repeat loop if counter=/=0
DJNZ R1,AGAIN
NEXT: MOV A,B ;Move the smallest no. found out to A
MOV 20H,A ;Store the no.to 20H location of RAM

END

INPUT: OUTPUT:
00H - #42H 20H - #01H
01H - #67H
02H - #90H
03H - #20H
04H - #01H

e) ALP to find number of EVEN and ODD numbers in an array of n-hexadecimal numbers

MOV R2,#00H ; intialize odd counter

MOV R3,#00H ; initialize even counter

MOV R1,#30H ;initialize memory pointer

MOV R5,#06H ;initialize counter

BACK: MOV A,@R1 ;get number from memory

RRC A ;check carry

JNC L1 ;jump if no carry(even)

INC R2 ; increment odd counter

Microcontroller And Applications Lab 22


Shri Guru Gobind Singhji Institute Of Engineering And Technology
SJMP NEXT ;unconditional jump

L1: INC R3 ;increment even counter

NEXT:INC R1 ;increment memory pointer

DJNZ R5,BACK ;decrement counter and jump if not zero

L3: SJMP L3 ;end

OUTPUT:

R1=30H

DB 30H : 12 H 24H 23H 57H 13H

Number of ODD numbers R2: 3

Number of EVEN numbers R3: 2

Microcontroller And Applications Lab 23


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 4

a) To compute Z = (A + B) × C × (D ÷ E) (A=26H, B=35H, C=05H, D=40H, E=08H)

ORG 2000H
START:
MOV A, #26H ; (A+B); Move 26H to A
MOV B, #33H ; Move 35H to B
ADD A, B ; Add A, B (A+B)
MOV R0, A ; Move the result to R0

MOV A, #40H ; (D/E); Move 04H to A


MOV B, #08H ; Move 08H to B
DIV A B ; Divide A, B (D/E)

Microcontroller And Applications Lab 24


Shri Guru Gobind Singhji Institute Of Engineering And Technology
MOV R1, A ; Move the result to R1

MOV A, R0 ; ((A+B)*C); Move addition result in R0 to A


MOV B, #05H ; Move 05H to B (C=05h)
MUL A B ; Multiply A, B
MOV R3, A ; Move the lower byte to R3
MOV R2, B ; Move the higher byte to R2

: MOV B, R1 ;(A+B)*C*(D/E); Move the Division result to B


MUL A B ; Multiply the lower byte value in A with B
MOV R4, B ; Move the higher byte value to R4
MOV R5, A ; Move the lower byte value to R5
MOV A, R2 ; Move the higher byte value in R2 to A
MOV B, R1 ; Move the division result to B
MUL A B ; Multiply A B
MOV R6, B ; Move the higher byte result to R6
MOV R7, A ; Move the lower byte result to R7
MOV A, R4 ; Move the higher byte value stored in R4 to A
MOV B, R7 ; Move the lower byte value stored in R7 to B
ADD A, B ; Add A, B
MOV R1, A ; Move the result to R1
MOV A, R5 ; Move the lower byte value in R5 to A
MOV R2, A ; Move the value in A to R2
MOV A, R6 ; Move the higher byte result in R6 to A
ADDC A, #00H ; Add the content in A with 00H
MOV R0, A ; Move the result to R0
RET
END

INPUT:
A=26H
B=35H
C-05H
D=40H
E=08H
OUTPUT: 0000E8
R0 – 00
R1 – 00
R2 – E8

Microcontroller And Applications Lab 25


Shri Guru Gobind Singhji Institute Of Engineering And Technology

b) To find the roots of the quadratic equation: x2+10x+9=0

MOV A,21H ; b value is in 21H

MOV B,A ; b value in B

MUL AB ; A=b^2

MOV 23H,A ; 23H =b^2

MOV A,20H ; a value is in 20H

MOV B,22H ; c value is in 22H

MUL AB ; A=ac

Microcontroller And Applications Lab 26


Shri Guru Gobind Singhji Institute Of Engineering And Technology
MOV B,#04H ; 04H in B

MUL AB ; A=4ac

MOV 24H,A ; 24H=4ac

CLR C

MOV A,23H ; A=b^2

SUBB A,24H ; A=b^2-4ac

MOV R0,A ; R0=b^2-4ac

MOV R1,#00H ; to find square root of (b^2-4ac)

BACK: MOV A,R1

MOV B,A

MUL AB

MOV B,R0

CJNE A,B,GO

SJMP AHEAD

GO: INC R1

SJMP BACK

AHEAD:MOV A,R1 ; R1=sqrt(b^2-4ac)

CLR C

SUBB A,21H ; A=-b+sqrt(b^2-4ac)

MOV B,20H

DIV AB

MOV B,#02H

DIV AB ; 1st root=A=(-b+sqrt(b^2-4ac))/2a

MOV 30H,A ; 30h:1st root stored

MOV A,R1

ADD A,21H

MOV B,20H

DIV AB

MOV B,#02H

Microcontroller And Applications Lab 27


Shri Guru Gobind Singhji Institute Of Engineering And Technology
DIV AB ; A=(b+sqrt(b^2-4ac))/2a=-(2nd root)

MOV B,A ; B=-(2nd root)

CLR A

CLR C

SUBB A,B ; A=2nd root=(-b-sqrt(b^2-4ac))/2a

MOV 31H,A ; 31h:2nd root stored

SJMP $

INPUT:- OUTPUT:-

20H: 01 30H: FF(-1)


21H: 0A 31H: F7 (-9)
22H: 09

C) ALP and C-language program to Convert a given BCD number to its ASCII equivalent code

ALP:-

MOV DPTR,#1000H ;16 BIT LOCN EXTERNAL RAM

MOV A,#255 ;GIVEN BCD NO.

MOV B,#100

DIV AB

ORL A,#30H ;GIVES ASCII CODE OF 2 I.E. 32H

MOVX @DPTR,A ;STORE CODE FOR 2 AT 1000H EXTERNAL RAM

Microcontroller And Applications Lab 28


Shri Guru Gobind Singhji Institute Of Engineering And Technology
INC DPTR

MOV A,B

MOV B,#10

DIV AB

ORL A,#30H ;GIVES CODE FOR 5

MOVX @DPTR,A ;ABOVE STORED AT 1001H

INC DPTR

MOV A,B

ORL A,#30H ;GIVES CODE FOR THE UNIT'S PLACE 5

MOVX @DPTR,A ;STORE THE ABOVE CODE AT 1002H

HERE: SJMP HERE

INPUT:- OUTPUT:- (EXTERNAL RAM)

ACC = 255(BCD) 1001H = 32H (ASCII CODE OF 2)


1002H = 35H (ASCII CODE OF 5)
1003H = 35H (ASCII CODE OF 5)

C code

#include <Intel\8051.h>

/* variable declaration in internal RAM and external RAM */

xdata char *ptr;

bdata char bcd_no;

idata char temp;

void main ()
{

Microcontroller And Applications Lab 29


Shri Guru Gobind Singhji Institute Of Engineering And Technology
bcd_no = 255;
ptr = 0x1001;

*ptr = (bcd_no / 100) + 0x30;

temp = bcd_no % 100;

ptr++;

*ptr = (temp / 10) + 0x30;

ptr++;

*ptr = (temp % 10) + 0x30;

while(1)
{
}
}

INPUT:- OUTPUT:- (EXTERNAL RAM)

ACC = 255(BCD) 1001H = 32H (ASCII CODE OF 2)


1002H = 35H (ASCII CODE OF 5)
1003H = 35H (ASCII CODE OF 5)

Microcontroller And Applications Lab 30


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 5

a.) Program to blink each LED one by one

ALP:-

ORG 2000H
MOV R1 , #07H ;Count of 07 stored in R1
MOV A , #00H ;Move 00 in Accumulator
MOV P1 , A ;Move 00 in port 1 to make it a output port
MOV A , #FEH ;Move FE in accumulator
UP1 : MOV P1 , A ;move to port 1
ACALL DELAY ;call delay of 0.5 sec
ACALL DELAY ;call delay of 0.5 sec

Microcontroller And Applications Lab 31


Shri Guru Gobind Singhji Institute Of Engineering And Technology
SETB C ;Set carry bit
RLC A ;Rotate data in accumulator to left
DJNZ R1 , UP ;Decrement counter
RET

DELAY: MOV R2 , #07H ;Take count of 7 in register 2


HERE1: MOV R3 , #0FFH ;Take count of 255 in register 3
HERE2: MOV R0 , #0FFH ;Take count of 255 in register 0
HERE3: DJNZ R0 , HERE3 ;decrement r0 till zero
DJNZ R3 , HERE2 ;decrement r3 till zero
DJNZ R2 , HERE1 ;decrement r2 till zero
RET

NOTE: The LED’s used are in COMMON ANODE mode. Hence when 0 is sent to output,LED glows.

C-code

#include< Intel\8051.h >

#define LED P1

void delay(void);

main()
{
unsigned int c;

unsigned char i;

while(1)
{

Microcontroller And Applications Lab 32


Shri Guru Gobind Singhji Institute Of Engineering And Technology
i = 0x01 ;
for(c=0;c<8;c++)
{
LED = i ;
delay();

i = i << 1;
}
}
}

void delay(void)
{

unsigned char c,d;

for(c=0;c<=200;c++)
{
for(d=0;d<=200;d++)
{
}
}
}

b.) Program to display binary number from 0-to-F on LEDs

ORG 2000H
MOV A,#00H ;move 00h in accumulator
MOV P1,A ;move 00h to port 1 to make it an output port
MOV R0,#10H ;store number of counts
UP: MOV A,#00H ;move starting number to accumulator
MOV P1,A ;move this to port 1
ADD A,#01H ;increment data in accumulator by 1
DJNZ R0,UP ;decrement reg 0 till zero
RET

Microcontroller And Applications Lab 33


Shri Guru Gobind Singhji Institute Of Engineering And Technology

NOTE: The LEDs used are in common cathode mode. The LEDs are connected to lower nibble of port 1.

C-code

#include< Intel\8051.h >

#define LED P1

void delay(void);

main()
{
unsigned int c;

unsigned char i;

while(1)
{
i = 0x01 ;
for(c=0;c<15;c++)
{
LED = i ;
delay();
i ++;
}
}
}

void delay(void)
{

unsigned char c,d;

for(c=0;c<=200;c++)
{
for(d=0;d<=200;d++)
{
}
}
}

Microcontroller And Applications Lab 34


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 6

a) Program to display moving ECE on seven segment display

#include<Intel\8051.h>
#include<stdio.h>

void delay(unsigned char);


void display (unsigned char x,unsigned char y,unsigned char z,unsigned char w);

void main ()
{
P1=0x00;
P3=0x00;

while(1)
{
display(0x1c,0xfd,0xfd,0xfd); //displaying E_ _ _

Microcontroller And Applications Lab 35


Shri Guru Gobind Singhji Institute Of Engineering And Technology
delay(5);

display(0x9c,0x1c,0xfd,0xfd); //displaying EC _ _

delay(5);

display(0x1c,0x9c,0x1c,0xfd); //displaying ECE _

delay(5);

display(0xfd,0x1c,0x9c,0x1c); //displaying _ ECE

delay(5);

display(0xfd,0xfd,0x1c,0x9c); //displaying _ _ EC

delay(5);

display(0xfd,0xfd,0xfd,0x1c); //displaying _ _ _ E

delay(5);

display(0xfd,0xfd,0xfd,0xfd); //displaying _ _ _ _

delay(5);

}
}

void display(unsigned char a, unsigned char b,unsigned char c,unsigned char d)


{
P1=a;

P3=0x80; //selecting first seven segment display

delay(2);

P1=b;

P3=0x40; //selecting second seven segment display

delay(2);

P1=c;

P3=0x20; //selecting third seven segment display

Microcontroller And Applications Lab 36


Shri Guru Gobind Singhji Institute Of Engineering And Technology
delay(2);

P1=d;

P3=0x10; //selecting fourth seven segment display

delay(2);

void delay(unsigned char c)


{
unsigned char d;

for(c=0;c<=200;c++)
{
for(d=0;d<=200;d++)
{
}
}
}

Microcontroller And Applications Lab 37


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 7

a) Interface 8 Switches & 1 Seven Segment display with the controller & write the C program
for displaying the number of switch pressed.

#include<Intel\8052.h>

#define IN P1 //initialization
#define OUT P3

void main()
{
IN =0xFF; // p1 as input
OUT =0x00; //p3 as output
while(1)
{

switch(IN) // wait for input


{
case 0xFE:
OUT =0xC0; // check for the switch pressed and display its number

Microcontroller And Applications Lab 38


Shri Guru Gobind Singhji Institute Of Engineering And Technology
break;
case 0xFD:
OUT =0xF9;
break;
case 0xFB:
OUT =0xA4;
break;
case 0xF7:
OUT =0xB0;
break;
case 0xEF:
OUT =0x99;
break;
case 0xDF:
OUT =0x92;
break;
case 0xBF:
OUT =0x82;
break;
case 0x7F:
OUT =0xF8;
break;
default:
OUT =0xFF;
break;
}
}
}

Microcontroller And Applications Lab 39


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 8

a) Interface Stepper motor to 8051 and write C Program to rotate stepper motor 600 CW and 220
CCW if input pin P3.1 is at logic 1 otherwise 450 CCW

#include<reg51.h>
#include<standard.h>
#include<stdio.h>
BIT IN P3^1 //switch connected to port 3.1
Void cw(int); //function for rotating stepper motor in clockwise manner
Void ccw(int); //function for rotating stepper motor in counterclockwise manner
Void delayms(int);

Void main(void)
{
P0=0x00; //configure port zero as an output port
IN=0x01; //configure p3.1 as input
While(1) // continous
{
If(IN==0x01) // if switch is 1

Microcontroller And Applications Lab 40


Shri Guru Gobind Singhji Institute Of Engineering And Technology
{
Cw(60); // rotate stepper motor 60deg clockwise
Ccw(22); // rotate stepper motor 22deg conuterclockwise
}
Else //if switch is zero
{ ccw(45); } //rotate stepper motor 22deg conuterclockwise
}}
Void cw(int c1)
{
Unsigned int i,j;
J= (240/360)*c1; // calculation for converting angle to hex value
For(i=0;i<j;i++) //rotating until hex value
{
P0=0x03;
Delayms(1);
P0=0x09;
Delayms(1);
P0=0x0C;
Delayms(1);
P0=0x06;
Delayms(1);
P0=0x03;
Delayms(1);
}}
Void ccw(int c1)
{
Unsigned int i,j;
J=(240/360)*c1; // calculation for converting angle to hex value
For(i=0;i<j;i++) //rotating until hex value
{
P0=0x03;
Delayms(1);
P0=0x06;
Delayms(1);
P0=0x0C;
Delayms(1);
P0=0x09;
Delayms(1);
P0=0x03;
Delayms(1);
}}
Void delayms(int c)

{unsigned int I,j


for(i=0;i<1275;i++)
for(j=0;j<c;j++);
}

Microcontroller And Applications Lab 41


Shri Guru Gobind Singhji Institute Of Engineering And Technology

Microcontroller And Applications Lab 42


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 9

AIM: C-PROGRAM FOR INTERFACING KEYPAD TO 8051 AND DISPLAY THE KEY
PRESSED ON SEVEN SEGMENT DISPLAY

#include <Intel\8051.h>

void delay(unsigned int a)


{
unsigned int i,j;
for(i=0;i<a;i++) ;delay of 'a'ms
{
for(j=0;j<1275;j++)
{
}
}
}

Microcontroller And Applications Lab 43


Shri Guru Gobind Singhji Institute Of Engineering And Technology
void main ()
{
unsigned char col;
while(1)
{
do
{
do
{
P2=0xf0; //connect P2.7 through P2.4 to col(i/p) and connect
// P2.0 through P2.3 to row (o/p)
col=P2&0xf0; // mask off lower nibble
} while(col==0xf0);

delay(50); //for key debouncing


col=P2&0xf0;
}while(col==0xf0);

if(col==0xf0) //if key is released


{
}
else
{
P2=0xfe; //if key form row 0 is pressed
col=P2&0xf0;
switch(col)
{
case 0xe0:
P1=0x39; //value to be sent to seven seg display for displaying 'C'
break;
case 0xd0:
P1=0x74 //value to be sent to seven seg display for displaying '8'
break;
case 0xb0:
P1=0x66; //value to be sent to seven seg display for displaying '4'
break;
case 0x70:
P1=0x3f; //value to be sent to seven seg display for displaying '0'
break;
}

P2=0xfd; //if key form row 1 is pressed


col=P2&0xf0;

switch(col)
{
case 0xe0:
P1=0x5e; //value to be sent to seven seg display for displaying 'd'
break;
case 0xd0:
P1=0x6f; //value to be sent to seven seg display for displaying '9'
break;
case 0xb0:
P1=0x7d; //value to be sent to seven seg display for displaying '5'
break;
case 0x70:
P1=0x06; //value to be sent to seven seg display for displaying '1'
break;
}

P2=0xfb; //if key form row 2 is pressed

Microcontroller And Applications Lab 44


Shri Guru Gobind Singhji Institute Of Engineering And Technology
col=P2&0xf0;
switch(col)
{
case 0xe0:
P1=0x79; //value to be sent to seven seg display for displaying 'E'
break;
case 0xd0:
P1=0x77; //value to be sent to seven seg display for displaying 'A'
break;
case 0xb0:
P1=0x7d; //value to be sent to seven seg display displaying '6'
break;
case 0x70:
P1=0x5b; //value to be sent to seven seg display for displaying '2'
break;
}

P2=0xf7; //if key form row 3 is pressed


col=P2&0xf0;
switch(col)
{
case 0xe0:
P1=0x71; //value to be sent to seven seg display for displaying 'F'
break;
case 0xd0:
P1=0x7c; //value to be sent to seven seg display for displaying 'b'
break;
case 0xb0:
P1=0x47; //value to be sent to seven seg display for displaying '7'
break;
case 0x70:
P1=0x4f; //value to be sent to seven seg display for displaying '3'
break;
}
}
delay(50); //key release debouncing(for pressing of different
// keys one after the other)
}
}

Microcontroller And Applications Lab 45


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 10

a) Interface ADC 0809 to the 8051 microcontroller & write the C program for it.
Theory:
ADC 0809 allows us to monitor up to 8 analog channels using a single chip. It has 8-bit
data bus. The 8 analog input channels are multiplexed and selected according to three address pins A,
B, C. Vref(+) & Vref set the reference voltage. The step size depends upon the voltage applied to the
Vref(+).
SC: Start Conversion
EOC: End of Conversion
ALE: Address latch enable
OE: Output Enable
CLK: 640 KHz along with the Vcc & GND.

ADC Analog Channel Selection:

Microcontroller And Applications Lab 46


Shri Guru Gobind Singhji Institute Of Engineering And Technology
Selected Analog C B A
Channel
IN0 0 0 0
IN1 0 0 1
IN2 0 1 0
IN3 0 1 1
IN4 1 0 0
IN5 1 0 1
IN6 1 1 0
IN7 1 1 1

Vref(+) relation to Vin range :


Vref(+) Vin Step Size(mV)
Not connected 0–5 5/256=19.53
4.0 0-4 4/256=15.62
3.0 0–3 3/256=11.71
2.56 0 – 2.56 2.56/256=10
2.0 0–2 2/256=7.81
1.0 0–1 1/256=3.90

Program in C:

#include<reg51.h> //including header files

sbit ALE = P2^4; //initialization


sbit OE = P2^5;
sbit SC = P2^6;
sbit EOC = P2^7;
sbit A = P2^0;
sbit B = P2^1;
sbit C = P2^2;
sfr MYDATA = P1;

void delayms(unsigned char);

void main()

Microcontroller And Applications Lab 47


Shri Guru Gobind Singhji Institute Of Engineering And Technology
{
unsigned char value;
MYDATA = 0xFF; //make p1 an input
EOC = 1; //make eoc an input
ALE = 0; //clear ale
OE = 0; //clear oe
SC = 0; //clear sc
while(1)
{
C = 0; //clear c
B = 0; //clear b
A = 1; // set a select channel 1
delayms(1);
ALE = 1;
delayms(1);
SC = 1;
delayms(1);
ALE = 0;
SC = 0; //start conversion
while(EOC==1); //wait for data conversion
while(EOC==0);
OE = 1; //enable read
delayms(1);
value = MYDATA; //get data
OE = 0; //disable read
}
}

void delayms(unsigned char c)


{
unsigned char i,j;
for(i=0;i<=c;i++)
for(j=0;j<=1275;j++);
}

Microcontroller And Applications Lab 48


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 11

a) Interface DAC 0808 to the 8051 microcontroller & write the program for generating the
sine wave.

Microcontroller And Applications Lab 49


Shri Guru Gobind Singhji Institute Of Engineering And Technology

Microcontroller And Applications Lab 50


Shri Guru Gobind Singhji Institute Of Engineering And Technology

a) Program to generate sinusoidal Wave

Microcontroller And Applications Lab 51


Shri Guru Gobind Singhji Institute Of Engineering And Technology

Generating a SINE wave: To Generate a sine wave


Vout =5V+(5*sin ʘ)
Angle ʘ Sin ʘ Vout =5V+(5*sin Voltage*25.6
(degrees) ʘ)
0 0 5 128
30 0.5 7.5 192
60 0.866 9.33 238
90 1.0 10 255
120 0.866 9.33 238
150 0.5 7.5 192
180 0 5 128
210 -0.5 2.5 64
240 -0.866 0.699 17
270 -1.0 0 0
300 -0.866 0.699 17
330 -0.5 2.5 64
360 0 5 128

#include<reg51.h>

sfr DACDATA = P1;

Void main ()
{
Unsigned char value[12] = {128,192,238,255,238,192,128,64,17,0,17,64,128};
Unsigned char x;
While(1)
{
for(x=0;x<13:x++)
{
DACDATA = value[x]; // passing values for conversion one by one
}
}
}

Microcontroller And Applications Lab 52


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 12

Microcontroller And Applications Lab 53


Shri Guru Gobind Singhji Institute Of Engineering And Technology

a) Program to generate Square Wave with 25% Duty Cycle using Timers

#include "Intel\8051.h"
#include "delay.h"
#include"intrins.h"

BIT OUT P3.5

main()
{
while(1)
{
LED = 0x01 ;
delay25ms();
LED = 0x00;
delay25ms();
delay25ms();
delay25ms();
}
}

/********************* HEADER FILE “delay.h” **************************/

void delay50ms(void)
{
TMOD=0x01;
TL0=0x0B0;
TH0=0x3C;
TR0=1;
while(!TF0);
TR0=0;
TF0=0;
}

void delay25ms(void)
{
TMOD=0x01;
TL0=0x58;
TH0=0x9E;
TR0=1;
while(!TF0);
TR0=0;
TF0=0;
}

void delayS(unsigned int c)


{
unsigned char i=1;
for(;i<=(c*10);i++)
{
delay50ms();
delay50ms();
}
}

Microcontroller And Applications Lab 54


Shri Guru Gobind Singhji Institute Of Engineering And Technology

b) Program to generate Square Wave using Timer Interrupt

#include "Intel\8051.h"
#include "delay.h"
#include"intrins.h"

BIT OUT P3.5

void Timer0_int(void) interrupt 1


{
LED = ~LED;
}

main()
{

IE=0x82;
TMOD=0x09;
TL0=0x58;
TH0=0x9E;
TR0=1;
while(1)
{
}

Microcontroller And Applications Lab 55


Shri Guru Gobind Singhji Institute Of Engineering And Technology

EXPERIMENT NO. 13

Microcontroller And Applications Lab 56


Shri Guru Gobind Singhji Institute Of Engineering And Technology

a) Program to transmit data from 1000H memory location on P1.5 serially with start and stop bits

#include<Intel\8051.h>

bit out = P1^5;


void delay(void);
xdata char *f;

main()
{
unsigned int c;
unsigned char i;
P1 = 0x00;
f = 0x1000
*f = 0x0ab;
while(1)
{
i=f;
out = 0x01; // start bit
delay();
for(c=0;c<8;c++)
{
out = (i & (1<<c)) ;
delay();
}
out = 0x00; //stop bit
delay();
}
}

void delay(void)
{
unsigned char c,d;
for(c=0;c<=200;c++)
{
for(d=0;d<=200;d++)
{
}
}
}

Microcontroller And Applications Lab 57


Shri Guru Gobind Singhji Institute Of Engineering And Technology

b) Program to receive data and store at 1000H memory location from P1.5 serially with start and stop bits

#include<Intel\8051.h>

bit out = P1^5;


void delay(void);
xdata char *f;

main()
{
unsigned int c;
unsigned char i;
P1 = 0xff;

while(1)
{

temp = in;
delay();
while(temp==1)
{
for(c=0;c<8;c++)
{
if(in)
{
i |= (1<<c);
}
delay();
}
temp = in;
delay();
if(temp==0)
{
f=i;
}
}
}
}

void delay(void)
{
unsigned char c,d;
for(c=0;c<=200;c++)
{
for(d=0;d<=200;d++)
{
}
}
}

Microcontroller And Applications Lab 58


Shri Guru Gobind Singhji Institute Of Engineering And Technology

c)Program to transmit and receive character, string, integer and floating point number using serial
communication of 8051

#include<Intel\8051.h>
#include"uart.h"

main()
{
char *str1;
str1=UART_Read();
InitUART();
UART_str("welcome to SGGS\n\r");
UART_Int(1234);
UART_str("\n\r");
UART_str(str1);
UART_Float(56.78);
while(1);
{
}
}

/*********************************Header File “uart.h”*********************************/

void InitUART();
void UART_Tx(char);
char UART_Rx();
void UART_str(char *);
void UART_Int(unsigned int);
void UART_Float(float);
char buff;
void InitUART()
{
SCON=0x50;
TMOD=0x20;
TH1=TL1=-3;
TR1=1;
}

void UART_Tx(char c)
{
SBUF=c;
while(!TI);
TI=0;
}

char UART_Rx()
{
while(!RI);
RI=0;
return SBUF;
}

Microcontroller And Applications Lab 59


Shri Guru Gobind Singhji Institute Of Engineering And Technology

void UART_str(char *s)


{
while(*s)
{
UART_Tx(*s++);
}
}

void UART_Int(unsigned int n)


{
unsigned char a[5]={0,0,0,0,0};
char i=0;
if(n==0)
{
UART_Tx('0');
return;
}
else
{
while(n>0)
{
a[i++]=(n%10)+48;
n=n/10;
}
for(--i;i>=0;i--)
{
UART_Tx(a[i]);
}
}
}

void UART_Float(float f)
{
float x;
int y;
y=f;
UART_Int(y);
UART_Tx('.');
x=(f-y)*100;
y=x;
UART_Int(y);
}

char *UART_Read()
{
char *str;
while(1)
{
while(!RI);
if(RI==1)
{
*str++=SBUF;
RI=0;
}
else
{

Microcontroller And Applications Lab 60


Shri Guru Gobind Singhji Institute Of Engineering And Technology
*str=0;
return(str); } } }

EXPERIMENT NO. 14

Microcontroller And Applications Lab 61


Shri Guru Gobind Singhji Institute Of Engineering And Technology

a) Program to display string, integer and floating point value on LCD

#include<reg51.h>
#include"delays.h"

#define LCD P0
#define LINE1 0x80
#define LINE2 0xC0

sbit rs=P0^0;
sbit rw=P0^1;
sbit en=P0^2;
bit flag;
void InitLCD();
void CmdLCD(char);
void DisplayLCD(char);
void WriteLCD(char);
void StrLcd(char *);
void IntLCD(unsigned int);
void FloatLCD(float);

void InitLCD()
{
CmdLCD(0x28);
CmdLCD(0x0e);
CmdLCD(0x01);
CmdLCD(0x06);
}

void CmdLCD(char c)
{
flag=0;
WriteLCD(c);
}

void DisplayLCD(char c)
{
flag=1;
WriteLCD(c);
}

void StrLCD(char *s)


{
while(*s)
DisplayLCD(*s++);
}

Microcontroller And Applications Lab 62


Shri Guru Gobind Singhji Institute Of Engineering And Technology

void WriteLCD(char c)
{
LCD=(c&0xf0);
if(flag==1)
rs=1;

en=1;
DelayMS(1);
en=0;

LCD=((c&0x0f)<<4);
if(flag==1)
rs=1;

en=1;
DelayMS(1);
en=0;

DelayMS(2);
}

void intlcd( unsigned int n)


{
char x[5]={0},i=0;
if(n==0)
{
displaylcd('0');
return;
}
else
{
while(n>0)
{
x[i++]=n%10+48;
n=n/10;
}
for(--i;i>=0;i--)
displaylcd(x[i]);
}
}

void fltlcd( float f)


{
unsigned int x;
float y;
x=f;
intlcd(x);
displaylcd('.');
y=(f-x)*100;
x=y;
intlcd(x);
}

Microcontroller And Applications Lab 63


Shri Guru Gobind Singhji Institute Of Engineering And Technology

main()
{
InitLCD();
CmdLCD(LINE1);
DisplayLCD('v');
CmdLCD(LINE2);

StrLCD("LCD 4 BIT");

while(1)
{
}

Microcontroller And Applications Lab 64

Vous aimerez peut-être aussi