You are on page 1of 57

Microcontroller Lab [2016-17]

A. PROGRAMMING IN 8051

1. DATA TRANSFER

(1.1) ASSEMBLY LANGUAGE PROGRAM TO MOVE A BLOCK OF DATA.

Write an assembly language program to transfer n =10 bytes of data from location
20h to location 50h (without overlap).

$ INCLUDE (REG51.INC)
ORG 0000H
MOV R7,#0AH ; initialize counter by 10 (dec)
MOV R0,#20H ; get initial source location
MOV DPTR,#50H ; get initial destination location
BACK: MOV A,@R0 ; get first content in accumulator
MOVX @DPTR,A ; move acc value to external location
INC R0 ; increment source location
INC DPTR ; increment destination location
DJNZ R7, BACK ; decrement R7, if not zero then jump to back
HERE: SJMP HERE ; endless loop
END

RESULT:

BEFORE EXECUTION: 10 locations X: 20h are filled up with data.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 1


Microcontroller Lab [2016-17]

AFTER EXECUTION: 10 locations X:50h are filled up with data from 20h.

Algorithm

1. Initialize registers to hold count data & also the source & destination addresses.
2. Get data from source location into accumulator and transfer to the destination
location.
3. Decrement the count register and repeat step 2 till count is zero.

Note: For data transfer with overlap start transferring data from the last location of
source array to the last location of the destination array.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 2


Microcontroller Lab [2016-17]

(1.2) ASSEMBLY LANGUAGE PROGRAM TO EXCHANGE A BLOCK OF


DATA.

Write an assembly language program to exchange n = 5 bytes of data at location


0020h and at location 0050h.

$ INCLUDE (REG51.INC)

ORG 0000H

MOV R7,#0AH ; initialize counter by 10(Decimal)

MOV R0,#20H ; get initial source location

MOV DPTR,#50H ; get initial destination location

NEXT: MOV A,@R0 ; get first content into accumulator

PUSH 0E0H ; push A register to stack

MOV A,@DPTR ; from external location move it to accumulator

MOV @R0,A ; from acc move it to the address pointed by r0

POP 0E0H ; pop A register from stack

MOV @DPTR,A ; move it to external location

INC R0 ; increment source location

INC DPTR ; increment destination location

DJNZ R7,NEXT ; decrease r7, if not zero move to next

HERE: SJMP HERE ; endless loop


END

RESULT:

BEFORE EXECUTION: 5 locations at X:0020h & X:0050h are filled up with data.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 3


Microcontroller Lab [2016-17]

AFTER EXECUTION: The data at X:20h & X:50h are exchanged.

Algorithm

1. Initialize registers to hold count data (array size) & also the source & destination
addresses.
2. Get data from source location into accumulator and save in a register.
3. Get data from the destination location into accumulator.
4. Exchange the data at the two memory locations.
5. Decrement the count register and repeat from step 2 to 4 till count is zero.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 4


Microcontroller Lab [2016-17]

(1.3) ASSEMBLY LANGUAGE PROGRAM TO SORT NUMBERS (BUBBLE


SORT PROGRAM)

Write an assembly language program to sort an array of n= 5 bytes of data in


Descending order stored from location 9000h.(use bubble sort algorithm)

$ INCLUDE (REG51.INC)

ORG 0000H

MOV R0,#05 ; count n-1 -ARRAY SIZE-n- Pass Counter

L1: MOV DPTR, #9000h ; array stored from address 9000h

MOV A,R0 ; initialize exchange counter

MOV R1,A

L2: MOVX A, @DPTR ; get number from array

MOV B, A ; & store in b

INC DPTR

MOVX A, @DPTR ; next number in the array

CLR C ; reset borrow flag

MOV R2, A ; store in r2

SUBB A, B ; 2nd - 1st no.—no compare instruction

JC NOEXCHG ; JNC - for ascending order

MOV A,B ; exhange the 2 noes in the array

MOVX @DPTR,A

DEC DPL ; DEC DPTR-instruction not ptresent

MOV A,R2

MOVX @DPTR,A

INC DPTR

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 5


Microcontroller Lab [2016-17]

NOEXCHG: DJNZ R1,L2 ; decrement compare counter

DJNZ R0,L1 ;decrement pass counter

HERE: SJMP HERE ; endless loop


END

RESULT:

BEFORE EXECUTION: Unsorted Array at 9000h

AFTER EXECUTION: Sorted Array (Descending order) at 9000h

Algorithm

1. Store the elements of the array from the address 9000h


2. Initialize a pass counter with array size-1 count (for number of passes).
3. Load compare counter with pass counter contents & initialize DPTR to point to
the start address of the array (here 9000h).
4. Store the current and the next array elements pointed by DPTR in registers B and
r2 respectively.
5. Subtract the next element from the current element.
6. If the carry flag is set (for ascending order) then exchange the 2 numbers in the
array.
7. Decrement the compare counter and repeat through step 4 until the counter
becomes 0.
8. Decrement the pass counter and repeat through step 3 until the counter becomes 0.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 6


Microcontroller Lab [2016-17]

(1.4) ASSEMBLY LANGUAGE PROGRAM TO FIND LARGEST ELEMENT IN


AN ARRAY.

Write an assembly language program to find the largest element in a given string of
n = 6 bytes at location 4000h. Store the largest element at location 4062h.

$ INCLUDE (REG51.INC)

ORG 0000H

MOV R3,#05 ; length of the array

MOV DPTR,#4000H ; starting address of the array

MOVX A,@DPTR

MOV R1,A

NEXT: INC DPTR

MOVX A,@DPTR

CLR C ; reset borrow flag

MOV R2,A ; next number in the array

SUBB A,R1 ; other num - previous largest no.

JC SKIP ; JNC for smallest element

MOV A,R2 ; update larger number in R1

MOV R1,A

SKIP: DJNZ R3,NEXT

MOV DPL,#62H ; location of the result-4062h

MOV A,R1 ; largest number

MOVX @DPTR,A ; store at #4062h

HERE: SJMP HERE ; endless loop


END

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 7


Microcontroller Lab [2016-17]

RESULT:

BEFORE EXECUTION:

AFTER EXECUTION: Location 4062 has the largest element.

Algorithm

1. Store the elements of the array from the address 4000h


2. Store the length of the array in r3 and set it as counter.
3. DPTR is loaded with starting address of the array.
4. Store the first number of the array in r1 (r1 is assigned to hold the largest
number).
5. Increment DPTR.
6. Subtract the number pointed by DPTR from the contents of r1 (to compare
whether the next array element is larger than the one in r1).
7. If the element pointed by DPTR is larger then load the larger number into r1.
8. Decrement the counter and repeat steps through 5 until the counter becomes 0.
9. Store the largest number in r1 in address 4062h

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 8


Microcontroller Lab [2016-17]

2. ARITHMETIC INSTRUCTIONS

(2.1) ASSEMBLY LANGUAGE PROGRAM ILLUSTRATING 16 BIT ADDITION

$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#22H
CLR C ; make the carry bit to 0
MOV A,#3CH ; load the low byte into A
ADD A,#0DFH ; add the low byte, now A=1B, cy=1
MOVX @R0,A ; load low byte of sum in R0 ie., at 22h
DEC R0 ; address is decremented to 21h
MOV A,#0BAH ; load the high byte into A
ADDC A,#04H ; add the high bytes with carry i.e, 04+0BA+1=BF
MOVX @R0,A ; at 21h the value BF is moved
CLR A ; clear the accumulator value
RLC A ; to obtain the carry bit value from the accumulator DEC
R0 ; address is decremented to 20h
MOVX @R0,A ; the carry bit value is loaded to 20h
HERE: SJMP HERE ; endless loop
END

AFTER EXECUTION:

BA3CH + 04DFH = BF1B

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 9


Microcontroller Lab [2016-17]

(2.2) ASSEMBLY LANGUAGE PROGRAM ILLUSTRATING 16 BIT


SUBTRACTION

$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#22H
CLR C ; make the carry bit to 0
MOV A,#58H ; load the low byte into A
SUBB A,#53H ; sub the low byte, now A=05, cy=0
MOV @R0,A ; load low byte of diff in R0 ie., at 22h
DEC R0 ; address is decremented to 21h
MOV A,#22H ; load the high byte into A
SUBB A,#48H ; sub the high bytes with carry 22-48-0=DA
MOV @R0,A ; at 21h the value DA is moved
CLR A ; clear the accumulator value
RLC A ; to obtain the carry bit value from the accumulator
DEC R0 ; address is decremented to 20h
MOV @R0,A ; the carry bit value is loaded to 20h
HERE: SJMP HERE ; endless loop
END

AFTER EXECUTION:
2258 – 4853 = 01 DA 05

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 10


Microcontroller Lab [2016-17]

(2.3) ASSEMBLY LANGUAGE PROGRAM ILLUSTRATING 16 BIT


MULTIPLICATION

$ INCLUDE (REG51.INC)
ORG 0000H
MOV R6,#62H ; load the higher byte of first value into R6
MOV R7,#30H ; load the lower byte of first value into R7
MOV R4,#43H ; load the higher byte of second value into R4
MOV R5,#2EH ; load the lower byte of second value into R5
; multiply R5 by R7
MOV A,R5 ; move the R5 into the accumulator
MOV B,R7 ; move R7 into B
MUL AB ; multiply the two values
MOV R2,B ; move B (high-byte) into R2
MOV R3,A ; move A (low-byte) into R3
; multiply R5 by R6
MOV A,R5 ; move R5 back into the accumulator
MOV B,R6 ; move R6 into B
MUL AB ; multiply the two values
ADD A,R2 ; add the low-byte into the value already in R2
MOV R2,A ; move the resulting value back into R2
MOV A,B ; move the high-byte into the accumulator
ADDC A,#00H ; add zero (plus the carry, if any)
MOV R1,A ; move the resulting answer into R1
MOV A,#00H ; load the accumulator with zero
ADDC A,#00H ; add zero (plus the carry, if any)
MOV R0,A ; move the resulting answer to R0
; multiply R4 by R7
MOV A,R4 ; move R4 into the accumulator
MOV B,R7 ; move R7 into B
MUL AB ; multiply the two values
ADD A,R2 ; add the low-byte into the value already in R2

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 11


Microcontroller Lab [2016-17]

MOV R2,A ; move the resulting value back into R2


MOV A,B ; move the high-byte into the accumulator
ADDC A,R1 ; add the current value of R1 (plus any carry)
MOV R1,A ; move the resulting answer into R1
MOV A,#00H ; load the accumulator with zero
ADDC A,R0 ; add the current value of R0 (plus any carry)
MOV R0,A ; move the resulting answer to R1
; multiply R4 by R6
MOV A,R4 ; move R4 back into the accumulator
MOV B,R6 ; move R6 into B
MUL AB ; multiply the two values
ADD A,R1 ; add the low-byte into the value already in R1
MOV R1,A ; move the resulting value back into R1
MOV A,B ; move the high-byte into the accumulator
ADDC A,R0 ; add it to the value already in R0 (plus any carry)
MOV R0,A ; move the resulting answer back to R0
HERE: SJMP HERE ; endless loop
END ; answer is now in R0, R1, R2 and R3

AFTER EXECUTION:

6230 HX 432E H= 19C434A0 H

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 12


Microcontroller Lab [2016-17]

(2.4) ASSEMBLY LANGUAGE PROGRAM ILLUSTRATING 16 BIT DIVISION

$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#49H ; lower byte of dividend
MOV R1,#00H ; higher byte of dividend
MOV R2,#10H ; lower byte of divisor
MOV R3,#00H ; higher byte of divisor
CLR C ; clear carry initially
MOV R4,#00H ; clear R4 working variable initially
MOV R5,#00H ; clear R5 working variable initially
MOV B,#00H ; clear B since B will count the number of left-shifted bits
DIV1: INC B ; increment counter for each left shift
MOV A,R2 ; move the current divisor low byte into the accumulator
RLC A ; shift low-byte left, rotate through carry to
; apply highest bit to high-byte
MOV R2,A ; save the updated divisor low-byte
MOV A,R3 ; move the current divisor high byte into the accumulator
RLC A ; shift high-byte left high, rotating in carry from low-byte
MOV R3,A ; save the updated divisor high-byte
JNC DIV1 ; repeat until carry flag is set from high-byte
; shift right the divisor
DIV2: MOV A,R3 ; move high-byte of divisor into accumulator
RRC A ; rotate high-byte of divisor right and into carry
MOV R3,A ; save updated value of high-byte of divisor
MOV A,R2 ; move low-byte of divisor into accumulator
RRC A ; rotate low-byte of divisor right, with carry from high-byte
MOV R2,A ; save updated value of low-byte of divisor
CLR C ; clear carry, not needed
MOV 07H,R1 ; make a safe copy of the dividend high-byte
MOV 06H,R0 ; make a safe copy of the dividend low-byte
MOV A,R0 ; move low-byte of dividend into accumulator

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 13


Microcontroller Lab [2016-17]

SUBB A,R2 ; dividend – shifted divisor = result bit


MOV R0,A ; save updated dividend
MOV A,R1 ; move high-byte of dividend into accumulator
SUBB A,R3 ; subtract high-byte of divisor (all together 16-bit sub)
MOV R1,A ; save updated high-byte back in high-byte of divisor
JNC DIV3 ; if carry flag is not set, result is 1, if not then 0
MOV R1,07H ; save copy of divisor to undo subtraction
MOV R0,06H
DIV3: CPL C ; invert carry, so it can be directly copied into result
MOV A,R4
RLC A ; shift carry flag into temporary result
MOV R4,A
MOV A,R5
RLC A
MOV R5,A
DJNZ B,DIV2 ; now count backwards and repeat until B is 0
MOV R3,05H ; move result to R3/R2
MOV R2,04H ; move result to R3/R2
HERE: SJMP HERE ; endless loop
END

AFTER EXECUTION:
0049 h = dividend and 0010 h = divisor
Then, R0=09h, R1=00h, R2=04h and R3=00h

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 14


Microcontroller Lab [2016-17]

(2.5) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE SQUARE


OPERATION

$ INCLUDE (REG51.INC)
ORG 0000H
MOV R6,#62H ; load the higher byte into R6
MOV R7,#30H ; load the lower byte into R7
MOV A,R6
MOV R4,A ; copy the contents from R6 to R4
MOV A,R7
MOV R5,A ; copy the contents from R7 to R5 then multiply R5 by R7
MOV A,R5 ; move the R5 into the accumulator
MOV B,R7 ; move R7 into B
MUL AB ; multiply the two values
MOV R2,B ; move B (high-byte) into R2
MOV R3,A ; move A (low-byte) into R3 then multiply R5 by R6
MOV A,R5 ; move R5 back into the accumulator
MOV B,R6 ; move R6 into B
MUL AB ; multiply the two values
ADD A,R2 ; add the low-byte into the value already in R2
MOV R2,A ; move the resulting value back into R2
MOV A,B ; move the high-byte into the accumulator
ADDC A,#00H ; add zero (plus the carry, if any)
MOV R1,A ; move the resulting answer into R1
MOV A,#00H ; load the accumulator with zero
ADDC A,#00H ; add zero (plus the carry, if any)
MOV R0,A ; move the resulting answer to R0 then multiply R4 by R7
MOV A,R4 ; move R4 into the accumulator
MOV B,R7 ; move R7 into B
MUL AB ; multiply the two values
ADD A,R2 ; add the low-byte into the value already in R2
MOV R2,A ; move the resulting value back into R2

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 15


Microcontroller Lab [2016-17]

MOV A,B ; move the high-byte into the accumulator


ADDC A,R1 ; add the current value of R1 (plus any carry)
MOV R1,A ; move the resulting answer into R1
MOV A,#00H ; load the accumulator with zero
ADDC A,R0 ; add the current value of R0 (plus any carry)
MOV R0,A ; move the resulting answer to R0 then multiply R4 by R6
MOV A,R4 ; move R4 back into the accumulator
MOV B,R6 ; move R6 into B
MUL AB ; multiply the two values
ADD A,R1 ; add the low-byte into the value already in R1
MOV R1,A ; move the resulting value back into R1
MOV A,B ; move the high-byte into the accumulator
ADDC A,R0 ; add it to the value already in R0 (plus any carry)
MOV R0,A ; move the resulting answer back to R0
HERE: SJMP HERE ; endless loop
END ; answer is now in R0, R1, R2 and R3

AFTER EXECUTION: 6230 H X 6230 H= 25A8C900 H

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 16


Microcontroller Lab [2016-17]

(2.6) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE CUBE


OPERATION

$ INCLUDE (REG51.INC)
ORG 0000H
MOV DPTR,#9000H ; source location is chosen to be 9000h
MOVX A,@DPTR ; the content at 9000h is moved to accumulator
MOV B,A ; accumulator content is moved to B
PUSH B ; push B register to stack
MUL AB ; multiply the content of A and B
POP B ; pop B register from stack
MUL AB ; multiply the content of A and B
MOV R0,A ; accumulator content is moved to R0
INC DPTR ; address is 9001h
MOV A,B ; B register content is moved to accumulator
MOVX @DPTR,A ; from accumulator to 9001h
INC DPTR ; address is 9002h
MOV A,R0 ; R0 register content is moved to accumulator
MOVX @DPTR,A ; from accumulator to 9002h
HERE: SJMP HERE ; endless loop
END

BEFORE EXECUTION: AFTER EXECUTION:


0F X 0F X 0F = 0D 2F

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 17


Microcontroller Lab [2016-17]

3. COUNTERS

(3.1) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE HEX UP/DOWN


COUNTER

$ INCLUDE (REG51.INC)
ORG 0000H
MOV A,#00H
BACK: ACALL DELAY
INC A
CJNE A,#FFH,LOOP
SJMP DOWN
LOOP: SJMP BACK
DOWN: ACALL DELAY
DEC A
CJNE A,#00H,DOWN
SJMP BACK
DELAY: MOV R1,#0FFH
DEC1: MOV R2,#0FFH
DEC2: MOV R3,#0FFH
DJNZ R3,$
DJNZ R2,DEC2
DJNZ R1,DEC1
RET
HERE: SJMP HERE ; endless loop
END

AFTER EXECUTION:

RESULTS IS INCREMENTED FROM 00 T0 FF AND DECREMENT FROM FF TO


00

Algorithm:

1. Move 00 to A register
2. Call the delay subroutine for 1 second, in delay program move FFH to registers
r1, r2 and r3, loop and decrement until 0.
3. Increment A register(decrement for down counter)

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 18


Microcontroller Lab [2016-17]

(3.2) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE BCD UP/DOWN


COUNTER

$ INCLUDE (REG51.INC)
ORG 0000H
MOV A,#00
BACK: ACALL DELAY
ADD A,#01H
DA A
CJNE A,#25H,LOOP
SJMP DOWN
LOOP: SJMP BACK
DOWN: ACALL DELAY
ADD A,#99H
DA A
CJNE A,#00H,DOWN
SJMP BACK
DELAY: MOV R1,#0FFH
DEC1: MOV R2,#0FFH
DEC2: MOV R3,#0FFH
DJNZ R3,$
DJNZ R2,DEC2
DJNZ R1,DEC1
RET
HERE: SJMP HERE ; endless loop
END

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 19


Microcontroller Lab [2016-17]

AFTER EXECUTION:

IN WATCH WINDOW 00 01 02........25 24 23........00

Algorithm:
1. Move 00 to A register
2. Call the delay subroutine for 1 second (in delay program move FFH to registers
r1, r2 and r3, loop and decrement until 0).
3. Increment A register(add 99h for down counter)
4. Decimal adjust accumulator for the BCD up/down counter.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 20


Microcontroller Lab [2016-17]

4. BOOLEAN & LOGICAL INSTRUCTIONS

(4.1) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE BIT


MANIPULATIONS
$ INCLUDE (REG51.INC)
ORG 0000H
MOV DPTR,#8000H
MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
CLR C
SUBB A,R0
JZ EQUAL
JNC BIG
SETB 78H
SJMP STOP
BIG: SETB 7FH
SJMP STOP
EQUAL: CLR 77H
CLR 7FH
STOP: NOP
HERE: SJMP HERE ; endless loop
END

RESULT:
BEFORE EXECUTION: X:08000h = 45 & X:8001 = 35

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 21


Microcontroller Lab [2016-17]

AFTER EXECUTION: D:02FH =01

2) Before Execution: X:08000h = 25 & X:8001 = 35


After Executuion: D:02FH =80

3) Before Execution: X:08000h = 45 & X:8001 = 45


After Executuion: D:02FH =00

Algorithm:
1. Store the elements of the array from the address 4000h & Move the first number
in r0 and the second number in register A respectively
2. Clear carry flag and subtract the two numbers, if the carry flag is 0(if the nos are
equal), Clear both LSB & MSB of bit addressable memory location 2Fh
3. If the carry bit is set then Set MSB of 2F(7FH), else LSB of data RAM 2F (bit
address 78H).

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 22


Microcontroller Lab [2016-17]

(4.2) ASSEMBLY PROGRAM ILLUSTRATING LOGICAL INSTRUCTIONS


(BYTE LEVEL)

$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#20H
MOV A,@R0 ; on chip data ram-donot use movx
MOV R1,A
INC R0
MOV A,@R0 ; A -num1
INC R0 ; R0 points to num2
CJNE R1,#0,CKOR
ANL A,@R0
SJMP LAST
CKOR: CJNE R1,#01,CKXOR
ORL A,@R0
SJMP LAST
CKXOR: CJNE R1,#02,OTHER
XRL A,@R0
SJMP LAST
OTHER: CLR A
LAST: INC R0
MOV @R0,A ; store result
HERE: SJMP HERE ; endless loop
END

RESULT:
BEFORE EXECUTION: D:020H =00, 21=0f, 22 = 12

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 23


Microcontroller Lab [2016-17]

AFTER EXECUTION: D:023H = 02

2)Before Execution: D:020H =01, 21=0f, 22 = 12


After Execution D:023H = 1F

3)Before Execution: D:020H =02, 21=0f, 22 = 12


After Execution D:023H = 1D

4)Before Execution: D:020H =34, 21=0f, 22 = 12


After Execution D:023H = 00

Algorithm:

1. Point to the data RAM register 20h and store the condition x.
2. Point to 21h and 22h and move the first number to A register.
3. Compare the contents of r1 and perform the operations accordingly.
4. The result will be stored in 23H register.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 24


Microcontroller Lab [2016-17]

5. CONDITIONAL CALL & RETURN

(5.1) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE HEX TO ASCII


CONVERSION
This program also illustrates conditional branching (JNC), call and return
instructions.

$ INCLUDE (REG51.INC)
ORG 0000H
MOV R1,#50H
MOV A,@R1 ; get hexadecimal data byte from RAM location 50h
MOV R2,A ; Store in R2
ANL A,#0FH ; Get the lower nibble
ACALL ASCII ; Convert to ASCII
INC R1
MOV @R1,A ; Store the lower digit's ASCII code
MOV A,R2 ; Get back the number
SWAP A ; Swap nibbles in A
ANL A,#0FH ; Get the upper BCD digit
ACALL ASCII
INC R1
MOV @R1,A ; Store the upper digit's ASCII code
ASCII: MOV R4,A ; Store A
CLR C
SUBB A,#0AH ; Check if digit >=0A
MOV A,R4
JNC SKIP
SJMP SKIP1
SKIP: ADD A,#07H ; Add 07 if >09
SKIP1: ADD A,#30H ; Else add only 30h for 0-9
RET

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 25


Microcontroller Lab [2016-17]

BEFORE EXECUTION:

AFTER EXECUTION:

Algorithm :

Converts the hexadecimal byte in A into two ASCII characters.


1. Move the hexadecimal data to be converted to accumulator.
2. Get the lower nibble & call ASCII routine
3. Store the converted ASCII value
4. Get the higher nibble & call ASCII routine
5. Store the converted ASCII value
ASCII subroutine
1. If digit greater than 09,(for A-F) add 07h & 30h
2. Else (i.e., for 0-9) add only 30h
3. return

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 26


Microcontroller Lab [2016-17]

6. CODE CONVERSION

(6.1) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE DECIMAL TO


HEX CONVERSION

$ INCLUDE (REG51.INC)
ORG 0000H
MOV DPTR,#40H ; 2-digit decimal number to be converted is given at 40h
MOVX A,@DPTR
ANL A,#0F0H ; obtain upper decimal digit
SWAP A ; bring to the units place
MOV B,#0AH
MUL AB ; MULTIPLY tens digit with #0A-toget tens in hex
MOV R1,A ; temporarily store the converted tens value
MOVX A,@DPTR ; get the decimal number again
ANL A,#0FH ; obtain the units digit
ADD A,R1 ; add to the converted tens value
INC DPTR ; increment data address
MOVX @DPTR,A ; converted hexadecimal number in next location
HERE: SJMP HERE ; endless loop
END

RESULT:

BEFORE EXECUTION- X:0040H = 45 (Decimal/BCD)

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 27


Microcontroller Lab [2016-17]

AFTER EXECUTION: X:0041h = 2D (hex value)

Algorithm

1. Move the decimal data to be converted from external memory 40h to


accumulator.

2. AND A reg with 0f0h and obtain the upper MSB of the decimal digit and swap
the LSB and MSB of accumulator to bring the same to units place.

3. Move 0ah to B register and multiply with A reg to convert to hex value, store the
converted tens value in r1

4. Get the LSB of the decimal number and add to the converted tens value

5. point to the next memory location and store the result (hexadecimal).

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 28


Microcontroller Lab [2016-17]

(6.2) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE HEX TO


DECIMAL CONVERSION

$ INCLUDE (REG51.INC)
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR ; Get hex number
MOV B,#10
DIV AB ; divide by 10 (0AH)
INC DPTR
XCH A,B
MOVX @DPTR,A ; Store the remainder (in B) In units place
XCH A,B
MOV B,#10 ; Divide the quotient in A by 10
DIV AB
INC DPTR
XCH A,B
MOVX @DPTR,A ; Store the remainder (in B) In tens place
XCH A,B
INC DPTR
MOVX @DPTR,A ; Store the quotient (in A) in hundreds place
HERE: SJMP HERE ; endless loop
END

RESULT:

BEFORE EXECUTION: 9000H – FF (HEX NUMBER)

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 29


Microcontroller Lab [2016-17]

AFTER EXECUTION: 9001 to 9003 – unpacked BCD number (decimal)- 5,5,2 (i.e.,
255 stored Lower digit first)

Algorithm

1. Move the hex data to be converted to accumulator.

2. Move 10 to B register and divide with A reg to convert to ascii value

3. Store the converted LSB value in r7

4. Repeat the step 2 to obtain the converted MSB value

5. Store the same in r6

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 30


Microcontroller Lab [2016-17]

(6.3) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE BCD TO ASCII


CONVERSION

$ INCLUDE (REG51.INC)
ORG 0000H
MOV R1,#50H
MOV A,@R1 ; get BCD data byte from RAM location 50h
MOV R2,A ; Store in R2
ANL A,#0FH ; Get the lower nibble
ORL A,#30H ; Add/or with 30h i.e., 0-9 converted to 30-39h
MOV R1,#52H
MOV @R1,A ; Store the lower digit's ASCII code
MOV A,R2 ; Get back the number
SWAP A ; Swap nibbles in A
ANL A,#0FH ; Get the upper BCD digit
ORL A,#30H ; Convert to ASCII
DEC R1
MOV @R1,A ; Store the upper digit's ASCII code
HERE: SJMP HERE ; endless loop
END

RESULTS:

BEFORE EXECUTION:

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 31


Microcontroller Lab [2016-17]

AFTER EXECUTION:

Algorithm :

Converts the BCD byte in A into two ASCII characters.

1. Move the BCD data to be converted to accumulator.

2. Get the lower nibble(BCD digit) & ADD (or ORL) with 30h

3. Store the converted ASCII value

4. Get the higher nibble(tens BCD digit) & ADD (or ORL) with 30h

5. Store the converted ASCII value

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 32


Microcontroller Lab [2016-17]

(6.4) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE ASCII TO HEX


CONVERSION

$ INCLUDE (REG51.INC)
ORG 0000H

MOV R1,#50H

MOV A,@R1 ; get ascii byte from RAM location 50h

CLR C

SUBB A,#41H

MOV A,@R1

JC SKIP

CLR C

SUBB A,#07H

SKIP: CLR C

SUBB A,#30H

INC R1

MOV @R1,A ; Store the hex code

HERE: SJMP HERE ; endless loop


END

RESULTS:

BEFORE EXECUTION:

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 33


Microcontroller Lab [2016-17]

AFTER EXECUTION:

Note: For this program the input data should be only in the range 30h-39h & 41h to 46h.

Algorithm :

Converts the ASCII characters into hexadecimal number.

1. Move the ASCII character to be converted to accumulator.

2. If character is greater than 41h,(for A-F), then subtract 07h & 30h

3. Else (i.e., for 0-9) subtract only 30h

4. Store the converted hexadecimal number.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 34


Microcontroller Lab [2016-17]

(6.5) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE DECIMAL TO


ASCII CONVERSION

$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#20H
MOV R1,#30H
MOV R2,#5

REPEAT: MOV A,@R0


ORL A,#30H
MOV @R1,A
INC R0
INC R1
DJNZ R2,REPEAT
HERE: SJMP HERE ; endless loop
END

RESULTS:
BEFORE EXECUTION:

AFTER EXECUTION:

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 35


Microcontroller Lab [2016-17]

(6.6) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE ASCII TO


DECIMAL CONVERSION

$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#20H
MOV R1,#30H
MOV R2,#5
REPEAT: MOV A,@R0
XRL A,#30H
MOV @R1,A
INC R0
INC R1
DJNZ R2,REPEAT
HERE: SJMP HERE ; endless loop
END

RESULTS:
BEFORE EXECUTION:

AFTER EXECUTION:

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 36


Microcontroller Lab [2016-17]

7. PROGRAMS USING SERIAL PORT


(7.1) PROGRAM ILLUSTRATING TIMER DELAY

$ INCLUDE (REG51.INC)
ORG 0H ; Reset Vector
SJMP 30H
ORG 0BH ; TF0 vector
SJMP ISR
ORG 30H
MOV A,#00
MOV R0,#0
MOV R1,#0
MOV TMOD,#02H ; 00000010-Run timer0 in mode 2
MOV TH0,#118 ; Set up timer 0 to overflow in 0.05msec
MOV IE,#82H ; %10000010 – Enable timer0 interrupt
SETB TCON.4 ; Start the timer0
DLY: SJMP DLY
ISR: CLR TCON.4 ; Disable timer0
INC R1 ; r1*r2 = 100*200 = 20000 * 0.05msec = 1sec
CJNE R1,#100,SKIP
MOV R1,#00
INC R0
CJNE R0,#200,SKIP
MOV R0,#00H
INC A
SKIP: SETB TCON.4 ; Enable Timer
RETI ; Return from interrupt subroutine
HERE:SJMP HERE ; endless loop
END

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 37


Microcontroller Lab [2016-17]

RESULT: Accumulator A is incremented in binary from 00, 01,02…09,0A, 0B, …, 0F,


10, 11, …FF every 1 second (for 33MHz clock setting & every 3 seconds for
11.0598MHz)

Algorithm:

1. Set up timer0 in mode 2 operation


2. Load TH1 with 118 to generate an interrupt every 0.05msec.
3. Reset registers a, r1 & r0.
4. Repeat step 4 continuously
5. On interrupt; ISR at 000B loaction goes to step 6
6. disable timer0
7. Update r1 & r0
8. Check if 20000 interrupts (=1 sec) over. Yes –increment accumulator a.
9. enable timer & return from ISR.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 38


Microcontroller Lab [2016-17]

(7.2) ASSEMBLY LANGUAGE PROGRAM TO ILLUSTRATE SERIAL ASCII


DATA TRANSMISSION

PROGRAM TO GENERATE DELAY, PROGRAMS USING SERIAL PORT AND


ON-CHIP TIMER
SERIAL DATA TRANSMISSION

Program illustrating serial ascii data transmission (data-yE). Note-to use result of
this program, after selecting DEBUG session in the main menu use

View-> serial window #1. On running & halting the program, the data is seen in the
serial window.

Conduct an experiment to configure 8051 microcontroller to transmit characters


(yE) to a PC using the serial port and display on the serial window.

$ INCLUDE (REG51.INC)
ORG 0H
SJMP 30H
ORG 30H
MOV TMOD,#20H ; timer 1; mode 2
MOV TH1,#-3 ; -3=FD loaded into TH1 for 9600 baud, 11.0592MHz.
MOV SCON,#50H ; 8-bit, 1 stop bit, REN enabled
SETB TR1 ; Start timer 1
AGAIN:MOV A,#‘y‘ ; transfer ―y‖
ACALL TRANS
MOV A,#‘E‘ ; transfer ―E‖
ACALL TRANS
NEXT:SJMP NEXT
TRANS: MOV SBUF,A ; load SBUF
DLY:JNB TI,DLY ; Wait for last bit to transfer
CLR TI ; get ready for next byte
RET
HERE: SJMP HERE ; endless loop
END

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 39


Microcontroller Lab [2016-17]

RESULT: yE is printed on the serial window each time the program is executed.

Theory: In serial transmission as opposed to parallel transmission, one bit at a time is


transmitted. In serial asynchronous transmission, the data consists of a Start bit (high),
followed by 8 bits of data to be transmitted and finally the stop bit. The byte character to
be transmitted is written into the SBUF register. It transmits the start bit. The 8-bit
character is transferred one bit at a time. The stop bit is transferred. After the
transmission, the TI flag = 1 indicating the completion of transmission. Hence in the
subroutine wait until TI is set. Later clear the TI flag and continue with transmission of
the next byte by writing into the SBUF register. (The program can also be written in
interrupt mode). The speed of the serial transmission is set by the baud rate which is done
with the help of timer 1. (Refer Ayala). Timer1 must be programmed in mode 2 (that is,
8-bit, auto reload).

Baud rate Calculation: Crystal freq/ (12*32) = (11.0592MHz)/(12*32) = 28800.

Serial communication circuitry divides the machine cycle frequency(11.0592MHz)/(12)


by 32 before it is being used by the timer to set the baud rate.

To get 9600, 28800/3 is obtained by loading timer1 with -3 (i.e., FF – 3 = FD) for further
clock division. For 2400 baud rate, 28800/12 => -12 = F4 in TH1.

Algorithm:

1. Initialize timer 1 to operate in mode 2 by loading TMOD register.


2. Load TH1 with -3 to obtain 9600 baud.
3. Initialize the asynchronous serial communication transmission (SCON) register.
4. Start timer1 to generate the baud rate clock.
5. Transmit the characters ―y‖ & ―E‖ by writing into the SBUF register and waiting
for the TI flag.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 40


Microcontroller Lab [2016-17]

B. INTERFACING

1. WAVEFORM GENERATION USING DUAL DAC

8 CRO
0 Dual
5 P0 DAC Xout

1 P1
μC
(1.1) PROGRAM FOR SQUARE WAVE GENERATION

#include "c:\ride\inc\51\reg51.h"
sbit Amp = P3^3; /* Port line to change amplitude */
sbit Fre = P3^2; /* Port line to change frequency */

void delay(unsigned int x) /* delay routine */


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

main()
{
unsigned char on = 0x7f,off=0x00;
unsigned int fre = 100;

while(1)
{
if(!Amp) /* if user choice is to change amplitude */
{
while(!Amp); /* wait for key release */
on+=0x08; /* Increase the amplitude */

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 41


Microcontroller Lab [2016-17]

}
if(!Fre) /* if user choice is to change frequency */
{
if(fre > 1000) /* if frequency exceeds 1000 reset to default */
fre = 100;

while(!Fre); /* wait for key release */


fre += 50;
} /* Increase the frequency */

P0=on; /* write amplitude to port */


P1=on;
delay(fre);
P0 = off; /* clear port */
P1 = off;
delay(fre);
}
}

Algorithm for Square wave generation

 Let initial, amplitude of the square wave be 2.5v (7F) and frequency count 100.
 Output the values 00h (0ff) and 7fh (on) Values through P0.
 If amplitude key is pressed then increase the voltage in steps of 0.15v(8).
 If the frequency key is pressed increment the count in steps of 50. If the count
exceeds 1000 reset it back to 100.
 Every time amplitude and frequency changes output the value thro P0 and note
the waveform on CRO.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 42


Microcontroller Lab [2016-17]

(1.2) PROGRAM FOR TRIANGULAR WAVE GENERATION

#include "c:\ride\inc\51\reg51.h"

main()
{
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while(1)
{
for(i=0;i<0xff;i++)
{ /* Generate ON pulse */
P1 = i;
P0 = i;
}
for(i=0xfe;i>0x00;i--) /* Generate OFF pulse */
{
P0 = i;
P1 = i;
}
}
}

Algorithm for Triangular wave generation

 Output the initial value 00 through P0.


 Increment it in steps of 1 until a count value of FFh (5V) is reached. Every time
repeat step 1.
 Decrement it in steps of 1 until a zero value is reached and repeat step 1.

(1.3) PROGRAM FOR RAMP WAVEFORM GENERATION

#include "c:\ride\inc\51\reg51.h"
main ()
{
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 43


Microcontroller Lab [2016-17]

{
for (i=0;i<0xff;i++) /* Generate ON pulse */
{
P1 = i;
P0 = i;
}
}
}

Algorithm for Ramp wave generation

 Output the initial value 00 through P0.


 Increment it in steps of 1 until a count value of FFh (5V) is reached. Every
time repeat step 1.
 Repeat step 1 & 2 continuously.

(1.4) PROGRAM FOR SINE WAVE GENERATION

#include "c:\ride\inc\51\reg51.h"
main()
{
static int a[13]={128,192,238,255,238,192,128,64,17,0,17,64,128};
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)
{
for(i=0;i<13;i++) /* Output different values */
{ P0 = a[i]; }
}
}

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 44


Microcontroller Lab [2016-17]

Algorithm for Sine wave

 Compute different step values (θ = 20o,15o…) of sine using the equation


V= 2.5V +2.5Vsinθ. . Output the values thro P0.
 More the steps smoother will be sine wave.
 E.g.: θ = 0o
V= 2.5V +2.5Vsinθ = 2.5V
The value sent to DAC is 25.6X5V= 128.

2. STEPPER MOTOR
• Stepper motor unlike DC motor rotates in steps.
• Stepper motor has 4 coils which forms the stator and a central rotor.
• Rotation depends on excitation of stator coils.
step coil A coil B coil C coil D
1 0 0 0 1
2 1 0 0 0
3 0 1 0 0
4 0 0 0 1
Anyone of these values forms the initial value. To get 360o revolution 200 steps are
required.

Step angle= 360o /200 = 1.8o. (difference between 2 teeth).

Algorithm for Stepper Motor

• Configure P0 as output.
• Apply the initial excitation of 11 to motor coils through P0.
• For clockwise motion -Rotate right once the excitation and repeat step 2.
• For anticlockwise motion -Rotate left once the excitation and repeat step 2.

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 45


Microcontroller Lab [2016-17]

PS

PS

8051µC

Stepper
Motor Stepper
P0
Motor
FRC 26pin Interface
Card
Cable

Program for stepper motor interface

#include "c:\ride\inc\51\reg51.h"

void delay (unsigned int x) /* Delay Routine */

for(;x>0;x--);

return; }

Main ( )

unsigned char Val, i;

P0=0x00;

Val = 0x11;

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

P0 = Val;

Val = Val<<1; /* Val= Val>>1; for clockwise direction*/

delay (500);

}}

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 46


Microcontroller Lab [2016-17]

3. 4X4 HEXADECIMAL KEYBOARD INTERFACE

Algorithm for Keyboard Interface

• Configure P1 as output port to scan the rows and P0 as input port to read the
column values.
• First select the last row by grounding that row. Scan the columns of entire row if a
key is pressed in that row then one of the column reads ‗0‘.
• If now key is pressed in the selected row all 1‘s is returned. So scan next row.
Repeat the action until all rows are scanned.

// PROGRAM FOR 4X4 HEXADECIMAL KEYPAD INTERFACE

#include "c:\ride\inc\51\reg51.h"

sbit row0=P1^0;

sbit row1=P1^1;

sbit row2=P1^2;

sbit row3=P1^3;

sbit col0=P2^0;

sbit col1=P2^1;

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 47


Microcontroller Lab [2016-17]

sbit col2=P2^2;

sbit col3=P2^3;

void get_key();

void scan();

void delayms(unsigned char);

unsigned char r,c,key,flag,temp,temp1,temp2;

code unsigned char lut [4][4]


={0x3f,0x6,0x56,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0xf7,0x7c,0x39,0x5e,0xfb,0xf1};

void main()

P1=0x00;

P2=0xff;

while(1)

delayms(100);

get_key();

if(flag)

P0=lut[r][c];

P2=0x0f;

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 48


Microcontroller Lab [2016-17]

void get_key() row2=1;

{ row3=0;}

flag =0; scan();

for(r=0;r<4;r++) if(flag!=0)

{if(r=0) break;}}}}

{ void scan()

row0=0; {temp=P2;

row1=1; temp=temp & 0x0f;

row2=1; if(temp!=0x0f)

row3=1; {flag=1;

else if(r=1) if(! 010)

{row0=1; C=0;

row1=0; else if(! col1)

row2=1; C=1;

row3=1; else if(! clo2)

else if(r==2) C=2;

{row0=1; else if(! col 3)

row1=1; C=3;

row2=0; }}

row3=1; void delayms(unsigned char k)

else if(!r==3) {

{ unsigned char j;

row0=1; for(j=0;j<k*2;k++)

row1=1; }

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 49


Microcontroller Lab [2016-17]

4. DC INTERFACE

Algorithm for DC motor interface


• Configure P0,P1 as output port and P3 as input port.
• Let initially the motor rotate with half speed count 7fh.
• If ―INR‖ button is pressed reduce the count because the speed is inversely
proportional to count.
• If ―DEC‖ button is pressed increase the count.

PS
PS

8051µC

DC Motor
DC
P0 Interface Motor
Card
FRC 26pin

Cable

P3.2(inr)

P3.3(dec)
Program for DC motor

#include "c:\ride\inc\51\reg51.h"

sbit inr= P3^2; //speed increment switch

sbit dcr= P3^3; //speed decrement switch

main()

unsigned char i=0x80;

P0 = 0x7f; /*Run the motor at half speed.*/

while (1)

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 50


Microcontroller Lab [2016-17]

if (!inr)

while (!inr);

if(i>10)

i=i-10; //increase the DC motor speed

if(!dcr)

while(!dcr);

if(i<0xf0)

i=i+10; //decrease the DC motor speed

P0=i;

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 51


Microcontroller Lab [2016-17]

5. CALCULATOR USING KEYBOARD AND 7SEGMENT DISPLAY

Algorithm

• Read the numbers n1 and n2 from keyboard and display them on seven segment.
• Read the operand from the keypad if key pressed is B (+), C(-),D(*),E(/) then
respective operation is performed.
• Result is displayed on 2 digit seven segment display.
• If any time the key pressed value returned as 10h then clear the LCD.

PS
PS

8051µC
Keypad

P0

FRC 26pin

Cable
7 Seg

Display

#include "c:\ride\inc\51\reg51.h"
void DispChar(unsigned char ch);
void ClrLED();
unsigned char getkey();
unsigned char getnum();
unsigned char getOp();
sbit Clk = P3^4; /* Clock line for 7 segment display */
sbit Dat = P0^0; /* Data line for 7 segment display */
main()
{ unsigned char tmp=0x0ff,n1=0,n2,Op,Res;

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 52


Microcontroller Lab [2016-17]

unsigned char NumTab[10] = { 0x0c0,0x0f9,0x0a4,0xb0, 0x99,0x92, 0x82,0x0f8,


0x80,0x90 };
unsigned char OpTab[4] = { 0x88,0x0Bf,0xc8,0x0a1};
bit Neg=0;
ClrLED(); /* Clear 7 segment display */
while(1)
{
Neg = 0; /* Negative flag */
n1=getnum(); /* Get 1st number */
Op = getOp() - 0x0B; /* Get Opcode. 0x0b is keycode of '+'(see keyboard schematics)*/
n2=getnum(); /* Get 2nd number */
while(getkey()!=0x13); /* wait for '=' key */
ClrLED();
switch(Op) /* Perform corresponding operation */
{
case 0: Res = n1 + n2;
break;
case 1: if(n2>n1) /* check for negativity */
{ Neg = 1;
Res = n2 - n1;
break; }
Res = n1 - n2;
break;
case 2: Res = n1 * n2;
break;
case 3: Res = n1 / n2;
break; }
DispChar(NumTab[Res%10]); /* Display number */
DispChar(NumTab[Res/10]);
if(Neg) /* if negative result display '-' */
DispChar(0x0Bf); }}

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 53


Microcontroller Lab [2016-17]

void DispChar(unsigned char ch) /* Routine to display char on 7 segment */


{
unsigned char i,tmp;
P0=0x00;
for(i=0;i<8;i++) /* for all bits */
{ tmp = ch & 0x80;
if(tmp) /* write data depending on MSB */
Dat = 1;
else
Dat = 0;
Clk = 0; /* Give Clk Pulse for synchronization */
Clk = 1;
ch = ch << 1; }} /* Get next bit */
void ClrLED()
{
unsigned char i;
for(i=0;i<4;i++)
DispChar(0x0ff); } /* 0xff for clear segment */
unsigned char getkey()
{
unsigned char i,j,indx,t;
P2 = 0x00; /* P2 as Output port */
P0 = 0x0ff;
indx = 0x00; /* Index for storing the first value of scanline */
while(1)
{
for(i=1;i<=4;i<<=1) /* for 4 scanlines */
{
P2 = 0x0f & ~i; /* write data to scanline */
t = P0; /* Read readlines connected to P0*/
t = ~t;

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 54


Microcontroller Lab [2016-17]

if(t>0) /* If key press is true */


{
for(j=0;j<=7;j++) /* Check for 8 lines */
{ t >>=1;
if(t==0) /* if get pressed key*/
{
return(indx+j); /* Return index of the key pressed */
} } }
indx += 8;
} }} /* If no key pressed increment index */
unsigned char getnum() /* Method for getting number */
{
unsigned char tmp;
while(1)
{
tmp = getkey();
if(tmp < 0x0a || tmp==0x10) /* if pressed key is number, return */
return(tmp);
}}
unsigned char getOp() /* Method for getting Operator */
{
unsigned char tmp;
while(1)
{
tmp = getkey();
if((tmp > 0x0a && tmp <0x0f)|| tmp==0x10) /* if pressed key is a Operator, return */
return(tmp); } }

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 55


Microcontroller Lab [2016-17]

6. ELEVATOR CONTROL

Algorithm for elevator interface


• Read the floor request through input port P1.
• If the current floor and requested floor are the same no change light up the
corresponding LED through P0.
• If the requested floor greaterthan current moving up of the lift is indicated by
glowing of LED‘s from current floor to the requested.
• If the requested floor lesserthan current moving down of the lift is indicated by
glowing of LED‘s from current floor to the requested.

PS
PS

8051µC

Elevator
interface
P0

FRC 26pin

Cable

#include "c:\ride\inc\51\reg51.h"
void delay(unsigned int);
main()
{
unsigned char Flr[9] = {0xff,0x00,0x03,0xff,0x06,0xff,0xff,0xff,0x09};
unsigned char FClr[9] = {0xff,0x0E0,0x0D3,0xff,0x0B6,0xff,0xff,0xff,0x79};
unsigned char ReqFlr,CurFlr = 0x01,i,j;
P0 = 0x00;
P0 = 0x0f0;
while(1)
{

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 56


Microcontroller Lab [2016-17]

P1 = 0x0f;
ReqFlr = P1 | 0x0f0;
while(ReqFlr == 0x0ff)
ReqFlr = P1 | 0x0f0; /* Read Request Floor from P1 */
ReqFlr = ~ReqFlr;
if(CurFlr == ReqFlr) /* If Request floor is equal to Current Floor */
{
P0 = FClr[CurFlr]; /* Clear Floor Indicator */
continue; } /* Go up to read again */
else if(CurFlr > ReqFlr) /* If Current floor is > request floor */
{
i = Flr[CurFlr] - Flr[ReqFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];
for(;i>0;i--) /*Move the indicator down */
{ delay(25000);
}}
else /* If Current floor is < request floor */
{
i = Flr[ReqFlr] - Flr[CurFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];
for(;i>0;i--) /*Move the indicator Up */
{ P0 = 0x0f0 | j;
j++;
delay(25000);
} }
CurFlr = ReqFlr; /* Update Current floor */
P0 = FClr[CurFlr]; /* Clear the indicator */
}}
void delay(unsigned int x)
{
for(;x>0;x--); }

ML Dept., Dayananda Sagar College of Engineering, Bengaluru Page 57

You might also like