Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
04/07/12 15:57
Read: times


 
#187049 - [project]Calculator 4x4 keypad + 2x16LCD - Result
Hi.
I am making a ASM code for calculator and i was trying for few days convert 2 digits from ascii to hex then add them and convert back to ascii but i cant get right result.
Simply i mean if i type 2+3=, for example i was trying many ways. I checked did i not use same register or memory location twice but i can't find miss take. I checked are converters working when i put in Accumulator decimal value it was converted right and displayed.
I am making assembling code in ASM51 and simulating in ISIS Pro.

There is my question could somebody look the code and point me where the error is or tell me i am going right way.


$Mod52
START:

EN EQU P3.7
RS EQU P3.5
RW EQU P3.6
DAT EQU P2
TEMP0 EQU 60H
TEMP1 EQU 61H
INPUTNUM EQU 31H
INPUTKEY EQU 32H
INPUTNUMTWO EQU 33H
FLAG EQU R7
ORG 00H
MOV R3,#00H
MOV R4,#00H
MOV 35H,#00H
cent equ R7
decc equ R6
unit equ R5
LCALL INIT_LCD
;MOV A,#'0'
;LCALL WRITE_TEXT
MOV A,#00H
;MOV R1,#40H
;MOV R0,#45H
;MOV R5,#50H

keyup:
	LCALL Keyboard
	CJNE A,#0FFH,keyup
readkey:
	LCALL Keyboard
	CJNE A,#0FFH,keypressed
	SJMP readkey
keypressed:

	LCALL GetKeyNum
	MOV 40h,A
	;INC R1 
	CJNE A,#'C', ON
CLEAR:	
	LCALL CLEAR_LCD
	LJMP START
ON:	
	;INC R3
	CJNE R3,#02H,SNUM
	LJMP SIG

SNUM:	
	INC R3
	CJNE A,#'+',PLUS
	LJMP SIG
PLUS:
	CJNE A,#'-',MINUS
	LJMP SIG
MINUS:
	CJNE A,#'x',MULTIPLY
	LJMP SIG
MULTIPLY:
	CJNE A,#'/',SUBSTRACT
	LJMP SIG
SUBSTRACT:
	;MOV INPUTNUM,A
	LCALL WRITE_TEXT
	LJMP KEYUP
	
SIG:
	LCALL Keyboard
	CJNE A,#0FFH,SIGN
	SJMP SIG
SIGN:
	MOV INPUTKEY,A
	LCALL GetKeyNum
	
	
	CJNE A,#'C',SIGNN
	SJMP CLEAR
SIGNN:
	CJNE A,#'+',NOTADD
	LCALL WRITE_TEXT
	MOV FLAG,#01H
	LCALL SECNUM
NOTADD:
	CJNE A,#'-',NOTMIN
	LCALL WRITE_TEXT
	MOV FLAG,#02H
	;LCALL MINUS
NOTMIN:
	SJMP SIG

SECNUM:
	LCALL Keyboard
	CJNE A,#0FFH,SECNUM

SECNUMM:
	LCALL Keyboard
	CJNE A,#0FFH,SECNUMMM
	SJMP SECNUMM
SECNUMMM:

	LCALL GetKeyNum
	MOV 41h, A
	;INC R0
	INC R4
	CJNE A,#'C',SECNUMMMM
	SJMP CLEAR
SECNUMMMM:
	CJNE A,#'=',ONEDIGI
	SJMP EQUALLLL
	
	
	
ONEDIGI:
	LCALL WRITE_TEXT
	CJNE A,#'C',NEXT
	SJMP CLEAR
NEXT:
	CJNE R4,#02H,SECNUM
	;SJMP EQUAL
EQUAL:
	LCALL Keyboard
	CJNE A,#0FFH,EQUAL
EQUALL:
	LCALL Keyboard
	CJNE A,#0FFH,EQUALLL
	SJMP EQUALL
EQUALLL:	
	LCALL GetKeyNum
	;MOV INPUTNUMTWO, A
	CJNE A,#'=',EQUAL
EQUALLLL:
	LCALL WRITE_TEXT
	
	LCALL ADITION
	mov a, r5
	add a,#'0'
	;LCALL BIN_DEC
	;lcall hextoascii
	;mov a, unit
	lcall write_text


	
FINISH:	LCALL Keyboard
	CJNE A,#0FFH,FINISH
FINCLR:	LCALL Keyboard
	CJNE A,#0FFH,CLRFIN
	SJMP FINCLR
CLRFIN:	LCALL GetKeyNum
	CJNE A,#'C',FINISH
	LJMP CLEAR


;subrutinesss


ADITION:
	;CLR A
	;CJNE FLAG,#01H,SUBSTRACTION
	MOV A,40h
	LCALL KEYTODEC
	MOV 40h,A
	MOV A,41h
	LCALL KEYTODEC
	
	ADD A,40h
	
	;LCALL BIN_DEC
	LCALL hextoascii
	;MOV 38H,A
	RET

hextoascii:
	mov r7,#30h
	mov r6,#30h
	mov r5,#30h	
	cjne a,#00h,c1_hextobcd 	;//check if number is 0 if not then continue
	ret 	
c1_hextobcd:
	MOV A,38h
	clr c
	mov b,#100	;//divide by 100
	div ab
	orl a,r7
	;LCALL WRITE_TEXT
	mov r7,a	;//save 100th place in R0
	clr c
	mov a,b
	mov b,#10	;//Divide by 10
	div ab
	orl a,r6
	;LCALL WRITE_TEXT
	mov r6,a	;//Save 10th place in R1
	mov a,b
	orl a,r5
	;LCALL WRITE_TEXT
	mov r5,a	;//Save  units place in R2
	ret

BIN_DEC:
	MOV A,#025D
	mov b,#100
	div ab
	add a,#'0'
	mov cent,a
	mov a,b
	mov b,#10
	div ab
	add a,#'0'
	mov decc,a
	mov a,b
	add a,#'0'
	mov unit,a
	ret 

INIT_LCD:
	CLR RS
	MOV DAT,#38h
	SETB EN
	CLR EN
	LCALL WAIT_LCD
	CLR RS
	MOV DAT,#0Eh
	SETB EN
	CLR EN
	LCALL WAIT_LCD
	CLR RS
	MOV DAT,#06h
	SETB EN
	CLR EN
	LCALL WAIT_LCD
	RET

WAIT_LCD:
	MOV DAT, #0FFh
	CLR EN

	CLR RS
	SETB RW
	SETB EN
	NOP

	MOV A, DAT
	CLR EN
	CLR RW

	JB ACC.7, WAIT_LCD
	RET

CLEAR_LCD:
	CLR RW
	CLR RS
	MOV DAT,#01h
	SETB EN
	CLR EN
	LCALL WAIT_LCD
	RET
	
WRITE_TEXT:
	SETB RS
	MOV DAT,A
	SETB EN
	CLR EN
	LCALL WAIT_LCD
	RET


Keyboard:
	MOV P1,#0F0H
	LCALL ShortDelay
	MOV A,P1
	CJNE A,#0F0H,scanrows
	MOV A,#0FFH
	SJMP ret_keyboard
scanrows:
	MOV R0,#4
	MOV R1,#11101111B; p2.4 is first row so we should start [scan] from first.
nextrow:
	MOV A,R1
	RR A
	MOV R1,A
	MOV P1,A
	LCALL ShortDelay
	MOV A,P1
	ANL A,#0F0H
	CJNE A,#0F0H,findcol
	DJNZ R0,nextrow
	MOV A,#0FFH
	SJMP ret_keyboard
findcol:
	MOV R2,#0
nextcol:
	CLR C
	RLC A
	JNC calculate
	INC R2
	SJMP nextcol
calculate:
	CLR C
	MOV A,#4
	SUBB A,R0
	MOV B,#4
	MUL AB
	ADD A,R2 ; r=4(row)+(col)
ret_keyboard:
	RET




GetKeyNum:
	
	MOV DPTR,#key
	MOVC A,@A+DPTR

	RET
key:	DB '7','4','1','C','8','5','2','0','9','6','3','=','/','x','-','+'

KEYTODEC:
	;mov a, 40h
	CJNE A,#'0',ONE
	MOV A,#0d
	RET
ONE:	
	CJNE A,#'1',TWO
	MOV A,#01d
	RET
TWO:	
	CJNE A,#'2',THR
	MOV A,#2d
	RET
THR:	
	CJNE A,#'3',FOR
	MOV A,#3d
	RET
FOR:	
	CJNE A,#'4',FIV
	MOV A,#4d
	RET
FIV:	
	CJNE A,#'5',SIX
	MOV A,#5d
	RET
SIX:	
	CJNE A,#'6',SEV
	MOV A,#6d
	RET
SEV:	
	CJNE A,#'7',EIG
	MOV A,#7d
	RET
EIG:	
	CJNE A,#'8',NIN
	MOV A,#8d
	RET
NIN:	
	CJNE A,#'9',NONE
	MOV A,#9d
	RET
NONE:
	RET

ShortDelay:
	MOV TEMP0,#20
shortloop:
	MOV TEMP1,#20
	DJNZ TEMP1,$
	DJNZ TEMP0,shortloop
	RET
ENDD:
        LCALL CLEAR_LCD
	END
 





Thanks. Lukasz Stanowski

List of 9 messages in thread
TopicAuthorDate
[project]Calculator 4x4 keypad + 2x16LCD - Result            01/01/70 00:00      
   wrong approach            01/01/70 00:00      
      LCD            01/01/70 00:00      
         Initialise it more than one time            01/01/70 00:00      
            Needing To Do Three Times Is A Kludge            01/01/70 00:00      
               Thanks            01/01/70 00:00      
                  Intent Applied; But Verified??            01/01/70 00:00      
                     previous experiences            01/01/70 00:00      
                     ' Will see it again            01/01/70 00:00      

Back to Subject List