Digital Systems CW1: Calculator

You are viewing an old revision of this post, from 27 July, 2011 @ 3:13. See below for differences between this version and the current revision.

$include (reg66x.inc)

; At the very beginning, jump to 40H...
ORG 0
TIMECOUNT EQU 8h
MBUFFER EQU 10h
KBUFFERA EQU 18h
KBUFFERB EQU 20h
KEY EQU 28h
MODE EQU 30h

DISPLAYA EQU 38H
DISPLAYB EQU 40H
DISPLAYC EQU 48H
DISPLAYD EQU 50H

DISPLAYR EQU 58H
CLEARCALLED EQU 60H
LJMP CLEAR

; ...which is where our code begins.
ORG 40H

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MAIN:             ; Defines the MAIN loop.
ACALL INPUT       ; INPUT subroutine.
ACALL CALCULATE   ; CALCULATE subroutine.
ACALL OUTPUT      ; OUTPUT subroutine.
SJMP MAIN         ; Jump back to the beginning.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Subroutine: CALCULATE
;;;;; Purpose: Prepares calculations for output.
;;;;; MODE: 1 = ADD, 2 = SUB, 3 = MULT
;;;;;       4 = DIV, 9 = RESULT
CALCULATE:
MOV A, MODE
CJNE A, #0, STOREMODE
SJMP NONEOFTHEABOVE

STOREMODE:
CJNE A, #9, DOSTOREMODE
SJMP RESULT
DOSTOREMODE:
MOV MBUFFER, MODE
LJMP CALCULATEDONE

RESULT:
MOV A, MBUFFER
ADDITION:
CJNE A, #1, SUBTRACT
MOV A, KBUFFERA
ADD A, KBUFFERB
MOV R0, A
MOV B, #10
DIV AB
CJNE A, #0, ADDTWODIGITS
MOV DISPLAYA, R0
SJMP CLEANUP
ADDTWODIGITS:
MOV DISPLAYB, A
MOV DISPLAYA, B
SJMP CLEANUP

SUBTRACT:
CJNE A, #2, MULTIPLY
MOV A, KBUFFERA
SUBB A, KBUFFERB
MOV DISPLAYA, A
SJMP CLEANUP

MULTIPLY:
CJNE A, #3, DIVIDE
MOV A, KBUFFERA
MOV B, KBUFFERB
MUL AB
MOV R0, A
MOV B, #10
DIV AB
CJNE A, #0, MULTWODIGITS
MOV DISPLAYA, R0
SJMP CLEANUP
MULTWODIGITS:
MOV DISPLAYB, A
MOV DISPLAYA, B
SJMP CLEANUP

DIVIDE:
CJNE A, #4, CALCULATEDONE
MOV A, KBUFFERA
MOV B, KBUFFERB
DIV AB
MOV DISPLAYA, B
MOV DISPLAYR, #1
MOV DISPLAYC, A
SJMP CLEANUP

NONEOFTHEABOVE:
MOV A, MBUFFER
CJNE A, #0, STOREB
STOREA:
MOV KBUFFERA, KEY
SJMP STOREDONE
STOREB:
MOV KBUFFERB, KEY
STOREDONE:
MOV A, KEY
CJNE A, #0, DISPLAYKEY
SJMP CALCULATEDONE
DISPLAYKEY:
MOV DISPLAYA, KEY
SJMP CALCULATEDONE

CLEANUP:
MOV KBUFFERA, #0
MOV KBUFFERB, #0
MOV MBUFFER, #0
MOV KEY, #0
MOV MODE, #0
MOV A, #0
MOV B, #0
CALCULATEDONE:
RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Subroutine: OUTPUT
;;;;; Purpose: Updates the display.
OUTPUT:
MOV P2, #3
MOV P1, DISPLAYD
ACALL TIMER

MOV P2, #2
MOV P1, DISPLAYC
ACALL TIMER

MOV P2, #1
MOV A, DISPLAYR
CJNE A, #1, DISPLAYBNOW
MOV P1, #0FFh
CPL P2.4
CPL P2.5
SJMP DISPLAYANOW
DISPLAYBNOW:
MOV P1, DISPLAYB

DISPLAYANOW:
ACALL TIMER
MOV P2, #0
MOV P1, DISPLAYA
RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Subroutine: INPUT
;;;;; Purpose: Read buttons pressed on keypad.
INPUT:
MOV MODE, #0
MOV P0, #0FFH
CLR P0.4
ACALL TIMER
MOV A,P0
ANL A, #0FH
CJNE A, #0FH, ROWONECOLONE

SETB P0.4
CLR P0.5
ACALL TIMER
MOV A, P0
ANL A, #0FH
CJNE A, #0FH, ROWTWOCOLONE

SETB P0.5
CLR P0.6
ACALL TIMER
MOV A, P0
ANL A, #0FH
CJNE A, #0FH, ROWTHREECOLONE

SETB P0.6
CLR P0.7
ACALL TIMER
MOV A, P0
ANL A, #0FH
CJNE A, #0FH, ROWFOURCOLONE
RET

ROWONECOLONE:
CJNE A, #14, ROWONECOLTWO
MOV KEY, #1h
RET
ROWONECOLTWO:
CJNE A, #13, ROWONECOLTHREE
MOV KEY, #2h
RET
ROWONECOLTHREE:
CJNE A, #11, ROWONECOLFOUR
MOV KEY, #3h
RET
ROWONECOLFOUR:
MOV MODE, #1 ; mode 1 = ADD
RET

ROWTWOCOLONE:
CJNE A, #14, ROWTWOCOLTWO
MOV KEY, #4h
RET
ROWTWOCOLTWO:
CJNE A, #13, ROWTWOCOLTHREE
MOV KEY, #5h
RET
ROWTWOCOLTHREE:
CJNE A, #11, ROWTWOCOLFOUR
MOV KEY, #6h
RET
ROWTWOCOLFOUR:
MOV MODE, #2 ; mode 1 = SUB
RET

ROWTHREECOLONE:
CJNE A, #14, ROWTHREECOLTWO
MOV KEY, #7h
RET
ROWTHREECOLTWO:
CJNE A, #13, ROWTHREECOLTHREE
MOV KEY, #8h
RET
ROWTHREECOLTHREE:
CJNE A, #11, ROWTHREECOLFOUR
MOV KEY, #9h
RET
ROWTHREECOLFOUR:
MOV MODE, #3 ; mode 1 = MULT
RET

ROWFOURCOLONE:
CJNE A, #14, ROWFOURCOLTWO
MOV CLEARCALLED, #1
ACALL CLEAR
RET
ROWFOURCOLTWO:
CJNE A, #13, ROWFOURCOLTHREE
MOV KEY, #0h
RET
ROWFOURCOLTHREE:
CJNE A, #11, ROWFOURCOLFOUR
MOV MODE, #9 ; mode 9 = EQUALS
RET
ROWFOURCOLFOUR:
CJNE A, #7, NOKEYSCAN
MOV MODE, #4 ; mode 4 = DIVIDE
RET

NOKEYSCAN:
RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Subroutine: CLEAR
;;;;; Purpose: Reset the shit out of it.
CLEAR:
MOV KBUFFERA, #0
MOV KBUFFERB, #0
MOV MBUFFER, #0
MOV DISPLAYA, #0
MOV DISPLAYB, #0
MOV DISPLAYC, #0
MOV DISPLAYD, #0
MOV DISPLAYR, #0
MOV KEY, #0
MOV MODE, #0
MOV A, #0
MOV B, #0
MOV A, CLEARCALLED
CJNE A, #0, CLEARRET
LJMP MAIN
CLEARRET:
MOV A, #0
MOV CLEARCALLED, #0
RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Subroutine: TIMER
;;;;; Purpose: Produce a 58ms-ish delay on ACALL.
TIMER:
MOV R0, #255
TIMELOOPA:
DJNZ R0, TIMELOOPA
TIMEDONE:
RET

END

Post Revisions:

Changes:

There are no differences between the 27 July, 2011 @ 3:13 revision and the current revision. (Maybe only post meta information was changed.)

Leave a comment

You must be logged in to post a comment.