Design of Experiment

Excess 5 to BCD

CPE108L
3Q1920
Excess 5 to Binary Coded Decimal

This Project was made using EdSim51DI like the other Laboratory Experiments. You may download the tool in their site here.


The setup of the project is different from the default. So first, we need to setup the Dynamic Interface of the EdSim51DI by clicking the DI button located near the LEDs.


This will be the default settings of the EdSim51DI.

Default settings

This will be the new settings of the EdSim51DI.

New settings


Code:
;Excess-5 to BCD
ORG 0000H	;Initialize start address

MAIN:
CALL CLEAR	;call label to clear registers

;set seven-segment display (SSD)
MOV 30H, #11000000B 	;store 0 to display in SSD
MOV 31H, #11111001B 	;store 1 to display in SSD
MOV 32H, #10100100B 	;store 2 to display in SSD
MOV 33H, #10110000B 	;store 3 to display in SSD
MOV 34H, #10011001B 	;store 4 to display in SSD
MOV 35H, #10010010B 	;store 5 to display in SSD
MOV 36H, #10000010B 	;store 6 to display in SSD
MOV 37H, #11111000B 	;store 7 to display in SSD
MOV 38H, #10000000B 	;store 8 to display in SSD
MOV 39H, #10010000B		;store 9 to display in SSD

;set Liquid Crystal Display (LCD) to display 'ERROR'
MOV 3AH, #'E'	;store E to display in LCD
MOV 3BH, #'R'	;store R to display in LCD
MOV 3CH, #'R'	;store R to display in LCD
MOV 3DH, #'O'	;store O to display in LCD
MOV 3EH, #'R'	;store R to display in LCD
MOV 3FH, #0		;end of data

READINP:
	CALL DELAY		;time delay
	CALL CLEAR		;clear the registers
	MOV A, P2		;store input from switches into register A
	CPL A			;inverts the content of register A
	
	
	
	CALL SUB5		;call SUB5 to subtract 5 from the input
	CALL CHECK		;call check to validate input

OUTLED: ;Output to LEDs
	CALL CLEARLCD	;clear display on LCD
	CPL A			;inverts the content of register A
	MOV B, A		;store contents of reg B to reg B
	MOV P1, B		;display the content of B into the LEDs
	
	JMP READINP		;Detect input again

SUB5: ;Subtract 5 from input
	MOV R0, #05		;sets R0 to 5

SUBLOOP:	;loop for subtraction
	DEC A				;Decrement 1 from reg A
	DJNZ R0, SUBLOOP	;Loop 5 times to subtract 5 from A
	RET

CHECK: ;CHECK input if within BCD range (0-9 in Decimal)
C9:
	CJNE A, #09H, C8	;continue to next line if reg A == #09H, else, jump to C8
	MOV P3, 39H			;Display '9' (stored in 39H) on the SSD
	RET
C8:
	CJNE A, #08H, C7	;continue to next line if reg A == #08H, else, jump to C7
	MOV P3, 38H			;Display '8' (stored in 39H) on the SSD
	RET
C7:
	CJNE A, #07H, C6	;continue to next line if reg A == #07H, else, jump to C6
	MOV P3, 37H			;Display '7' (stored in 39H) on the SSD
	RET
C6:
	CJNE A, #06H, C5	;continue to next line if reg A == #06H, else, jump to C5
	MOV P3, 36H			;Display '6' (stored in 39H) on the SSD
	RET
C5:
	CJNE A, #05H, C4	;continue to next line if reg A == #05H, else, jump to C4
	MOV P3, 35H			;Display '5' (stored in 39H) on the SSD
	RET
C4:
	CJNE A, #04H, C3	;continue to next line if reg A == #04H, else, jump to C3
	MOV P3, 34H			;Display '4' (stored in 39H) on the SSD
	RET
C3:
	CJNE A, #03H, C2	;continue to next line if reg A == #03H, else, jump to C2
	MOV P3, 33H			;Display '3' (stored in 39H) on the SSD
	RET
C2:
	CJNE A, #02H, C1	;continue to next line if reg A == #02H, else, jump to C1
	MOV P3, 32H			;Display '2' (stored in 39H) on the SSD
	RET
C1:
	CJNE A, #01H, C0	;continue to next line if reg A == #01H, else, jump to C0
	MOV P3, 31H			;Display '1' (stored in 39H) on the SSD
	RET
C0:
	CJNE A, #00H, LCD		;continue to next line if reg A == #09H, else, jump to LCD
	MOV P3, 30H				;Display '0' (stored in 39H) on the SSD
	RET

LCD: ;Display "ERROR" on LCD

; initialise the display
; see instruction set for details
CLR P1.3		; clear RS - indicates that instructions are being sent to the module
; function set	
	CLR P1.7		; |
	CLR P1.6		; |
	SETB P1.5		; |
	CLR P1.4		; | high nibble set

	SETB P1.2		; |
	CLR P1.2		; | negative edge on E

	CALL DELAY		; wait for BF to clear	
; function set sent for first time - tells module to go into 4-bit mode

	SETB P1.2		; |
	CLR P1.2		; | negative edge on E
; same function set high nibble sent a second time

	SETB P1.7		; low nibble set (only P1.7 needed to be changed)
	SETB P1.2		; |
	CLR P1.2		; | negative edge on E

; function set low nibble sent
	CALL DELAY		; wait for BF to clear


; entry mode set
; set to increment with no shift
	CLR P1.7		; |
	CLR P1.6		; |
	CLR P1.5		; |
	CLR P1.4		; | high nibble set

	SETB P1.2		; |
	CLR P1.2		; | negative edge on E

	SETB P1.6		; |
	SETB P1.5		; |low nibble set

	SETB P1.2		; |
	CLR P1.2		; | negative edge on E

	CALL DELAY		; wait for BF to clear


; display on/off control
; the display is turned on, the cursor is turned on and blinking is turned on
	CLR P1.7		; |
	CLR P1.6		; |
	CLR P1.5		; |
	CLR P1.4		; | high nibble set

	SETB P1.2		; |
	CLR P1.2		; | negative edge on E

	SETB P1.7		; |
	SETB P1.6		; |
	SETB P1.5		; |
	SETB P1.4		; | low nibble set

	SETB P1.2		; |
	CLR P1.2		; | negative edge on E

	CALL DELAY		; wait for BF to clear


; send data
	SETB P1.3		; clear RS - indicates that data is being sent to module
	MOV R1, #3AH	; data to be sent to LCD is stored in 8051 RAM, starting at location 3AH
LOOP:
	MOV A, @R1		; move data pointed to by R1 to A
	JZ FINISH		; if A is 0, then end of data has been reached - jump out of loop
	CALL SENDCHAR	; send data in A to LCD module
	INC R1			; point to next piece of data
	JMP LOOP		; repeat

FINISH:
	JMP READINP

SENDCHAR:
	MOV C, ACC.7		; |
	MOV P1.7, C			; |
	MOV C, ACC.6		; |
	MOV P1.6, C			; |
	MOV C, ACC.5		; |
	MOV P1.5, C			; |
	MOV C, ACC.4		; |
	MOV P1.4, C			; | high nibble set

	SETB P1.2			; |
	CLR P1.2			; | negative edge on E

	MOV C, ACC.3		; |
	MOV P1.7, C			; |
	MOV C, ACC.2		; |
	MOV P1.6, C			; |
	MOV C, ACC.1		; |
	MOV P1.5, C			; |
	MOV C, ACC.0		; |
	MOV P1.4, C			; | low nibble set

	SETB P1.2			; |
	CLR P1.2			; | negative edge on E
	
	MOV R0, #50
	CALL DELAY			; wait for BF to clear



CLEAR: ;set registers to zero
	SETB P0.3		; | enable display 3
	SETB P0.4		; | of SSD (leftmost)

	MOV A, #0H		;clear content of reg A
	MOV B, #0H		;clear content of reg B
	MOV R0, #0H		;clear content of R0
	RET

CLEARLCD:
	MOV P1, #00H	;clear display of LCD
	RET

DELAY:
	MOV R0, #50H		;set delay to 50H
	DJNZ R0, $			;While R0 != 0, subtract 1 from R0
	RET

ENDOFCODE:
	END


Documentation: Click here to download the file