68HC11 Calculator Code
By Matt Hocking
68HC11 Board:
- The Wytec 68HC11: FOX11TM Trainer
- *Program Name: Multifunctional Calculator with rs232 servo motor control
- * Author: Matt Hocking
- * Date: 13/5/07
- * Version: 2.9
- * Description: Multifunctional Calculator that can also control a servo motor
- *through an rs232 serial com.
- *--------------------------------------------
- ORG $c4
- *** Vector jump table for BUFFALO monitor ***
- JSPI RMB 3
- JPAIE RMB 3
- JPAO RMB 3
- JTOF RMB 3
- JTOC5 RMB 3
- JTOC4 RMB 3
- JTOC3 RMB 3
- JTOC2 RMB 3
- JTOC1 RMB 3
- JTIC3 RMB 3
- JTIC2 RMB 3
- JTIC1 RMB 3
- JRTI RMB 3
- JIRQ RMB 3
- JXIRQ RMB 3
- JSWI RMB 3
- JILLOP RMB 3
- JCOP RMB 3
- JCLM RMB 3
- DDRC EQU $1007
- TMSK1 EQU $1022
- TFLG1 EQU $1023
- TCNT EQU $100E
- TOC1 EQU $1016
- TOC2 EQU $1018
- TOC3 EQU $101A
- TOC4 EQU $101C
- OC1M EQU $100C
- OC1D EQU $100D
- TCTL1 EQU $1020
- TCTL2 EQU $1021
- SCDR EQU $102F
- SCCR1 EQU $102C
- SCCR2 EQU $102D
- SCSR EQU $102E
- BAUD EQU $102B
- adctl equ $30 ; ADC setup
- PERIOD EQU 40000
- *lcd addresses
- DB0: equ 1
- DB1: equ 2
- *
- ONE_MS equ 167 ; for 8 MHz 1000us/6us=167
- FIVE_MS equ 835
- TEN_MS equ 1670
- *SERIAL COMM PORT addresses
- BAUDDAT FCB $30 ; SET BAUD RATE TO 9600BPS
- CONF1 FCB $00
- CONF2 FCB $0C
- crlf equ $0a0d
- PORTF equ $1403
- d40us: equ 15 ; 15x6us= 90 us, 60us ok
- REG_SEL: equ DB0 ; 0=reg, 1=data
- ENABLE: equ DB1
- NOT_REG_SEL: equ $FE
- NOT_ENABLE: equ $FD
- NOT_RESET equ $F7 ; force bit 3 = 0
- OUTSTR EQU $FFC7 ; A BUFFALO routine to write a string to the RS232
- *keypad addresses
- porta: equ 0
- portb: equ $1404
- portc: equ $1403
- portd: equ 8
- ddrd: equ 9
- porte: equ $0a
- REGBLK: equ $1000
- outa equ $ffb8
- STACK: equ $8fff
- one_s: equ 30000 ; 2500 x 8 cycles = 20,000 cycles = 10ms
- org $000
- *keypad vars
- ram: rmb 1
- temp: rmb 1
- key_flag: rmb 1
- * MSG1: rmb 16
- MSG2: rmb 16
- mode rmb 1
- *lcdvars
- temp1: rmb 1
- pfimg: rmb 1
- reset_seq: rmb 1
- disp_ram: rmb 3
- cntchrl1 rmb 1
- cntchrl2 rmb 1
- JSCI RMB 3
- ANSWER RMB 1
- NUMBER RMB 1
- NUMBER2 RMB 1
- ARITH RMB 1
- COUNT RMB 1
- RESULT1 RMB 1
- RESULT2 RMB 1
- RESHUNS RMB 1 ; Hundreds
- RESTENS RMB 1 ; Tens
- RESONES RMB 1 ; Ones
- THOUSANDS RMB 1
- THOUSANDS2 RMB 1
- HUNS RMB 1 ; Hundreds
- HUNS2 RMB 1 ; Hundreds
- HUNS3 RMB 1 ; Hundreds
- TENS RMB 1 ; Tens
- TENS2 RMB 1 ; Tens
- TENS3 RMB 1 ; Tens
- ONES RMB 1 ; Ones
- ONES2 RMB 1 ; Ones
- adcvalue RMB 1
- *
- LCDimg: equ pfimg
- LCD_RSimg: equ pfimg
- LCD_ENimg: equ pfimg
- LCD: equ PORTF
- LCD_RS: equ PORTF
- LCD_EN: equ PORTF
- org $d000
- lds #STACK
- ************************
- *Intialize serial port
- sei
- jsr delay_10ms ; delay 20ms during power up
- jsr delay_10ms
- jsr lcd_ini ; initialize LCD
- STAA SCCR2
- LDAA #%00101100
- STAA SCCR2 ; Enable Transmission and Rx
- LDAA #$7E ; initialize psuedo vector
- STAA $C4 ; for SPI
- LDD #MY_INSTRG
- STD $C5
- *Intialize timer interupt
- LDAA #$7E ; initialize psuedo vector
- STAA $DF ; for timer interupt
- LDD #INT20
- STD $E0
- LDAA #%10000000 ; ENABLED TOC1
- STAA TMSK1
- STAA TFLG1 ; CLEARED FLAG OF TOC1
- LDD TCNT
- ADDD #PERIOD
- STD TOC1 ; TOC WILL INTERRUPT IN 20MS
- ldaa #$ff ; initialize prot d as input
- staa portd
- ldaa #$fe
- staa ddrd
- CLI
- jmp start
- * scan for 4X4 membrane keypad
- * at exit: if a key is down, the carry bit =1 and the accumulator b
- * holds the key number
- * if no key is dwon the carry bit =0
- *
- keybrd: ldx #REGBLK n
- ldab #3
- ldaa #$df ; starting with pd5 is low
- staa temp
- next_row:
- staa portd,x
- nop ; add delay before checking key down
- nop
- nop
- nop
- ldaa porte,x
- anda #$0f ; needs PE0-PE3 only
- cmpa #$0f
- bne keyin ; key input detected
- decb
- cmpb #$ff ; after 4 tests, accu B will be $ff
- beq no_keyin
- ror temp ; check next column
- ldaa temp
- jmp next_row
- no_keyin:
- clc
- rts ; no key input
- keyin: rora
- bcc key_ok
- addb #4
- jmp keyin
- key_ok: sec
- rts
- delayos:
- pshy
- ldy #one_s ; 2500 x 8 = 20,000 cycles = 10ms
- del1.1:
- dey ; 4 cycles
- nop ; 1 cycle
- bne del1.1 ; 3 cycles
- puly
- rts
- nokey:
- jsr delayos
- jmp begin
- start: lds #STACK
- ldx #REGBLK
- ldaa #$ff
- staa portd,x
- ldaa #$fe ; PD0=input
- staa ddrd,x
- ldy #MSG1
- clr cntchrl1
- ldaa #3
- staa cntchrl2
- clr mode
- *
- begin:
- ************************************
- *Retrieve data from adc channel 7
- ldx #$1000 ; locate adc
- ldaa #07 ; select chanel 7
- staa adctl,x
- bset 00,x $80 ; enable adc operation
- wait:
- brclr adctl,x #$80 wait ; wait until conversion is done
- ldd $1034 ; load d register with conversion value
- pshy
- ldy #MSG2.1
- PSHA
- MY_OUTA ;Transmit the adc value out the serial port
- LDAB SCSR ; Is Transmit Register Empty?
- ANDB #%10000000
- BEQ MY_OUTA ; If not, Check again
- STAA SCDR ; Transmit data
- PULA
- tab
- CLRA ;Clear the D Accumulator high byte
- LDX #100 ;Start with the 100s Divide NUMBER by 100
- IDIV ;The result is in IX, the remainder in D
- XGDX ;Now D has the result and IX has the remainder
- ADDB #$30
- STAB 00,y ; RESHUNS And store it
- XGDX ;The remainder in D again
- LDX #10 ;Now for the 10s. Divide the remainder by 10
- IDIV ;Tens are are IX, the units in D
- ADDB #$30
- STAB 02,y ; RESONES ... and store it
- XGDX ;Swap the tens into Acc D ...
- ADDB #$30
- STAB 01,y ; RESTENS ... and store it
- puly
- jsr keybrd
- bcc nokey
- jsr delayos
- bcc nokey
- * key down
- tstb
- bne not_zero
- ldab #$10
- *The following Select Case code is used to convert the keypad number to the
- *charactor that will be displayed on the LCD.
- *It is stored in an array in y and each time a new charactor is inserted
- *into the array y pointer is incremented. If B is pressed on the keypad
- *then y is decremented and the last char is erased.
- not_zero:
- cmpb #16
- bne nzero
- ldaa #'0
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- nzero:
- cmpb #1
- bne none
- ldaa #'1
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- none:
- cmpb #2
- bne ntwo
- ldaa #'2
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- ntwo:
- cmpb #3
- bne nthree
- ldaa #'3
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- nthree:
- cmpb #4
- bne nfour
- ldaa #'4
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- nfour:
- cmpb #5
- bne nfive
- ldaa #'5
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- nfive:
- cmpb #6
- bne nsix
- ldaa #'6
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- nsix:
- cmpb #7
- bne nseven
- ldaa #'7
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- nseven:
- cmpb #8
- bne neight
- ldaa #'8
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- neight:
- cmpb #9
- bne nnine
- ldaa #'9
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- nnine:
- cmpb #10
- bne na
- ldaa #'=
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr CALCULATE
- jsr start1
- na:
- cmpb #11
- bne nb
- ldaa #32
- jsr delayos
- dec cntchrl1
- ldab cntchrl1
- cmpb #255
- bne alright
- inc cntchrl1
- jsr start1
- alright:
- dey
- staa 00,y
- jsr start1
- nb:
- cmpb #12
- bne nc
- ldaa #'+
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- nc:
- cmpb #13
- bne nd
- ldaa #'-
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- nd:
- cmpb #14
- bne ne
- ldaa #'*
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- ne:
- cmpb #15
- bne mnotfive
- ldaa #'/
- jsr delayos
- staa 00,y
- iny
- inc cntchrl1
- jsr start1
- mnotfive:
- jsr begin
- *lcd program
- lcd_ini:
- clra
- staa pfimg
- staa PORTF
- ldx #inidsp1 ; point to init. codes.
- ldaa #1
- staa reset_seq ; need more delay for the first reset seq.
- jsr outins1 ; output codes.
- ldx #inidsp2 ; point to init. codes.
- clr reset_seq
- jsr outins2 ; output codes.
- rts
- inidsp1:
- fcb 2 ; number of instrutions
- fcb $33 ; reset char
- fcb $32 ; reste char
- inidsp2:
- fcb 4 ; number of instrutions
- fcb $28 ; 4bit, 2 line, 5X7 dot
- fcb $06 ; cursor increment, disable display shift
- fcb $0c ; display on, cursor off, no blinking
- fcb $01 ; clear display memory, set cursor to home pos
- outins1:
- pshb ; output instruction command.
- jsr sel_inst
- ldab 0,x
- inx
- onext1: ldaa 0,x
- jsr wrt_pulse ; initiate write pulse.
- inx
- jsr d5ms
- decb
- bne onext1
- pulb
- rts
- *
- outins2:
- pshb ; output instruction command.
- jsr sel_inst
- ldab 0,x
- inx
- onext2: ldaa 0,x
- jsr wrt_pulse ; initiate write pulse.
- inx
- decb
- bne onext2
- jsr d5ms
- pulb
- rts
- *
- delay_10ms:
- nop
- d10ms: pshx
- ldx #TEN_MS
- bsr del1
- pulx
- rts
- d5ms: pshx
- ldx #FIVE_MS
- bsr del1
- pulx
- rts
- del1: dex
- inx
- dex
- bne del1
- rts
- *
- *
- sel_data:
- psha
- ldaa LCD_RSimg
- oraa #REG_SEL
- bra sel_i
- sel_inst:
- psha
- ldaa LCD_RSimg
- anda #NOT_REG_SEL
- sel_i: staa LCD_RSimg
- anda #NOT_RESET ; force bit3=0, no matter what happens
- staa LCD_RS
- pula
- rts
- *
- lcd_line1:
- jsr sel_inst ; select instruction
- ldaa #$80 ; starting address for line1
- bra line3
- lcd_line2:
- jsr sel_inst
- ldaa #$C0 ; starting address for line2
- line3: jsr wrt_pulse
- *
- jsr sel_data
- jsr msg_out
- rts
- * at entry, x must point to the begining of the message,
- * b = number of characters to be sent out
- *
- msg_out:
- ldaa 0,x
- jsr wrt_pulse
- inx
- decb
- bne msg_out
- rts
- *
- * @ enter, a=data to output
- *
- wrt_pulse:
- pshx
- psha ; save it tomporarily.
- anda #$f0 ; mask out 4 low bits.
- staa temp1 ; save nibble value.
- ldaa LCDimg ; get LCD port image.
- anda #$0f ; need low 4 bits.
- oraa temp1 ; add in low 4 bits.
- staa LCDimg ; save it
- anda #NOT_RESET ; force bit3=0, no matter what happens
- staa LCD ; output data
- *
- bsr enable_pulse
- ldaa reset_seq
- beq wrtpls
- jsr d5ms ; delay for reset sequence
- *
- wrtpls: pula
- asla ; move low bits over.
- asla
- asla
- asla
- staa temp1 ; store temporarily.
- *
- ldaa LCDimg ; get LCD port image.
- anda #$0f ; need low 4 bits.
- oraa temp1 ; add in loiw 4 bits.
- staa LCDimg ; save it
- anda #NOT_RESET ; force bit3=0, no matter what happens
- staa LCD ; output data
- *
- bsr enable_pulse
- pulx
- rts
- *
- enable_pulse:
- ldaa LCD_ENimg
- oraa #ENABLE
- staa LCD_ENimg
- anda #NOT_RESET ; force bit3=0, no matter what happens
- staa LCD_EN
- ldaa LCD_ENimg
- anda #NOT_ENABLE
- staa LCD_ENimg
- anda #NOT_RESET ; force bit3=0, no matter what happens
- staa LCD_EN
- pshx
- ldx #d40us
- jsr del1
- pulx
- rts
- *
- start1:
- jsr delay_10ms ; delay 20ms during power up
- jsr delay_10ms
- jsr lcd_ini
- back:
- ; MSG1 for line1, x points to MSG1
- pshx
- ldx #MSG1
- ldab cntchrl1 ; send out 16 characters
- incb
- jsr lcd_line1
- pulx
- pshx
- ldx #MSG2.1 ; MSG2 for line2, x points to MSG2
- ldab #35 ; send out 16 characters
- jsr lcd_line2
- pulx
- jsr delay_10ms
- jsr delay_10ms
- jmp begin
- CALCULATE:
- *First intialize variables
- LDAA #1
- STAA ANSWER
- LDY #MSG1
- LDX #0
- LDAA #0
- STAA ONES
- STAA ONES2
- STAA TENS
- STAA TENS2
- STAA TENS3
- STAA HUNS
- STAA HUNS2
- STAA HUNS3
- STAA RESHUNS
- STAA RESTENS
- STAA RESONES
- STAA THOUSANDS
- CLRB
- FINDENDOFCALC:
- INCB
- INY
- LDAA 00,Y ;Check string for + char
- CMPA #'+
- BNE CONT01
- STAB COUNT
- LDAB #1
- STAB ARITH
- BRA SKIP ; if true branch to skip
- CONT01: ;Check string for - char
- CMPA #'-
- BNE CONT02
- STAB COUNT
- LDAB #2
- STAB ARITH
- BRA SKIP ; if true branch to skip
- CONT02: ;Check string for * char
- CMPA #'*
- BNE CONT03
- STAB COUNT
- LDAB #3
- STAB ARITH
- BRA SKIP ; if true branch to skip
- CONT03: ;Check string for / char
- CMPA #'/
- BNE SKIP
- STAB COUNT
- LDAB #4
- STAB ARITH
- SKIP: ;Find end of string
- CMPA #'=
- BNE FINDENDOFCALC
- DEY
- LDAA 00,Y
- SUBA #48 ;Convert from ASCII to an integer
- STAA ONES ;Extract ones value from the string
- DEY
- LDAA 00,Y
- CMPA #48
- BLT GROUP
- SUBA #48;Convert from ASCII to an integer
- STAA TENS2;Extract tens value from the string
- LDAB #10
- MUL
- STAB TENS;Extract tens value from the string
- DEY
- LDAA 00,Y
- CMPA #48
- BLT GROUP
- SUBA #48;Convert from ASCII to an integer
- STAA HUNS2 ;Extract hundreds value from the string
- LDAB #100
- MUL
- STAB HUNS ;Extract tens value from the string
- DEY
- *Group sub adds the digits together to form a complete integer
- GROUP: LDAA ONES
- LDAB TENS
- ABA
- LDAB HUNS
- ABA
- STAA NUMBER
- LDAA #0
- STAA TENS
- STAA HUNS
- STAA THOUSANDS
- STAA THOUSANDS2
- DEY
- LDAA 00,Y
- SUBA #48 ;Convert from ASCII to an integer
- STAA ONES2;Extract ones value from the string
- LDAB COUNT
- CMPB #1
- BEQ GROP
- DEY
- LDAA 00,Y
- SUBA #48;Convert from ASCII to an integer
- STAA TENS3
- LDAB #10
- MUL
- STAB TENS ;Extract tens value from the string
- LDAB COUNT
- CMPB #2
- BEQ GROP
- DEY
- LDAA 00,Y
- SUBA #48;Convert from ASCII to an integer
- STAA HUNS3;Extract hundreds value from the string
- LDAB #100
- MUL
- STAB HUNS;Extract hundreds value from the string
- *Grop sub adds the digits together to form a complete integer
- GROP: LDAA ONES2
- LDAB TENS
- ABA
- LDAB HUNS
- ABA
- STAA NUMBER2
- LDAA ARITH
- CMPA #1
- BNE MINUS
- ********************************************
- *Addition sub routine
- LDAA ONES ;Add the ones together
- LDAB ONES2
- ABA
- CMPA #9
- BLE NOOVERFLOW;If overflow subtract ten
- SUBA #10
- STAA RESONES ;Store the result
- LDAA TENS2
- LDAB #1 ;Add one to tens
- ABA
- STAA TENS2
- CMPA #9
- BLE NOOVERFLOW1;If overflow subtract ten
- SUBA #10
- STAA TENS2
- LDAA HUNS2
- LDAB #1
- ABA
- STAA HUNS2
- CMPA #9
- BLE NOOVERFLOW1;If overflow subtract ten
- SUBA #10
- STAA HUNS2
- LDAA THOUSANDS
- LDAB #1
- ABA
- STAA THOUSANDS
- BRA NOOVERFLOW1
- NOOVERFLOW:
- STAA RESONES;Store the result
- NOOVERFLOW1:
- LDAA TENS2 ;ADD TENS
- LDAB TENS3
- ABA
- CMPA #9
- BLE NOOVERFLOW2;If overflow subtract ten
- SUBA #10
- STAA RESTENS;Store the result
- LDAA HUNS2
- LDAB #1
- ABA
- STAA HUNS2
- CMPA #9
- BLE NOOVERFLOW3;If overflow subtract ten
- SUBA #10
- STAA HUNS2
- LDAA THOUSANDS
- LDAB #1
- ABA
- STAA THOUSANDS;Store the result
- BRA NOOVERFLOW3
- NOOVERFLOW2:
- STAA RESTENS ;Store the result
- NOOVERFLOW3:
- LDAA HUNS2 ;ADD HUNS
- LDAB HUNS3
- ABA
- CMPA #9
- BLE NOOVERFLOW4;If overflow subtract ten
- SUBA #10
- STAA RESHUNS
- INC THOUSANDS
- BRA NOOVERFLOW5
- NOOVERFLOW4:
- STAA RESHUNS; Store the result
- NOOVERFLOW5:
- MINUS: LDAA ARITH
- CMPA #2
- BNE MULTI
- ********************************************
- *subtracton sub routine
- LDAA ONES2; Subtract ones
- LDAB ONES
- SBA
- STAA RESONES
- BCS BORROW ; if borrow then
- BRA NOTBORROW ; if not borrow then
- BORROW:
- TAB ; transfer result to b
- LDAA #$FF ; load a with 255
- SBA ; subtract result from 255
- ADDA #1
- TAB
- LDAA #10
- SBA
- STAA RESONES;Store the result
- LDAA TENS3
- CMPA #0
- BEQ BORROW2 ; if borrow then
- DEC TENS3
- BRA NOTBORROW2 ; if not borrow then
- BORROW2:
- LDAA #9
- STAA TENS3
- LDAA HUNS3
- SBCA #1
- STAA HUNS3
- NOTBORROW2: ; add carry
- NOTBORROW: ; add carry
- LDAA TENS3
- LDAB TENS2
- SBA
- STAA RESTENS ;Store the result
- BCS BORROW3 ; if borrow then
- BRA NOTBORROW3 ; if not borrow then
- BORROW3:
- TAB ; transfer result to b
- LDAA #$FF ; load a with 255
- SBA ; subtract result from 255
- ADDA #1
- TAB
- LDAA #10
- SBA
- STAA RESTENS ;Store the result
- DEC HUNS3
- NOTBORROW3: ; add carry
- LDAA HUNS3
- LDAB HUNS2
- SBA
- STAA RESHUNS ;Store the result
- MULTI: LDAA ARITH
- CMPA #3
- BEQ NUP
- JMP DIV
- NUP:
- ********************************************
- *multiplication sub routine
- *A * B = A , B
- *= B + (255 * A) + A
- *First multiply the two variables togehter
- *Then the result is in B reg
- *Then Store the multiplier in A reg
- *Then multiply A reg * 255
- *Then add A reg to B reg to get the answer
- LDAA NUMBER
- LDAB NUMBER2
- MUL
- STAA RESULT2
- STAA RESULT1
- CLRA ;Clear the D Accumulator high byte
- LDX #100 ;Start with the 100s Divide NUMBER by 100
- IDIV ;The result is in IX, the remainder in D
- XGDX ;Now D has the result and IX has the remainder
- STAB RESHUNS ;And store it
- XGDX ;The remainder in D again
- LDX #10 ;Now for the 10s. Divide the remainder by 10
- IDIV ;Tens are are IX, the units in D
- STAB RESONES ;... and store it
- XGDX ;Swap the tens into Acc D ...
- STAB RESTENS ;... and store it
- *********************************************
- *A reg * 255
- LDAA RESULT2
- CMPA #0
- BNE GRR
- JMP DIV
- GRR: LDAA #5
- STAA ONES
- STAA TENS
- LDAA #2
- STAA HUNS
- LDAA RESULT2
- CMPA #1
- BEQ GARGAR
- OOHNO: LDAA ONES ;ADD ONES
- LDAB #5
- ABA
- CMPA #9
- BLE NOOVERFLOW6
- SUBA #10
- STAA ONES
- LDAA TENS
- LDAB #1
- ABA
- STAA TENS
- CMPA #9
- BLE NOOVERFLOW7
- SUBA #10
- STAA TENS
- LDAA HUNS
- LDAB #1
- ABA
- STAA HUNS
- CMPA #9
- BLE NOOVERFLOW7
- SUBA #10
- STAA HUNS
- LDAA THOUSANDS
- LDAB #1
- ABA
- STAA THOUSANDS
- BRA NOOVERFLOW7
- NOOVERFLOW6:
- STAA ONES
- NOOVERFLOW7:
- LDAA TENS ;ADD TENS
- LDAB #5
- ABA
- CMPA #9
- BLE NOOVERFLOW8
- SUBA #10
- STAA TENS
- LDAA HUNS
- LDAB #1
- ABA
- STAA HUNS
- CMPA #9
- BLE NOOVERFLOW9
- SUBA #10
- STAA HUNS
- INC THOUSANDS
- BRA NOOVERFLOW9
- NOOVERFLOW8:
- STAA TENS
- NOOVERFLOW9:
- LDAA HUNS ;ADD HUNS
- LDAB #2
- ABA
- CMPA #9
- BLE NOOVERFLOW10
- SUBA #10
- STAA HUNS
- INC THOUSANDS
- BRA NOOVERFLOW11
- NOOVERFLOW10:
- STAA HUNS
- NOOVERFLOW11:
- DEC RESULT2
- LDAA RESULT2
- CMPA #1
- BNE OOHNO
- **********************************************
- GARGAR:
- LDAB RESULT1
- CLRA ;Clear the D Accumulator high byte
- LDX #100 ;Start with the 100s Divide NUMBER by 100
- IDIV ;The result is in IX, the remainder in D
- XGDX ;Now D has the result and IX has the remainder
- STAB HUNS2 ;And store it
- XGDX ;The remainder in D again
- LDX #10 ;Now for the 10s. Divide the remainder by 10
- IDIV ;Tens are are IX, the units in D
- STAB ONES2 ;... and store it
- XGDX ;Swap the tens into Acc D ...
- STAB TENS2 ;... and store it
- **********************************************
- *Add a reg to the above result
- LDAA ONES ;ADD ONES
- LDAB ONES2
- ABA
- CMPA #9
- BLE NOOVERFLOW12
- SUBA #10
- STAA ONES
- LDAA TENS
- LDAB #1
- ABA
- STAA TENS
- CMPA #9
- BLE NOOVERFLOW13
- SUBA #10
- STAA TENS
- LDAA HUNS
- LDAB #1
- ABA
- STAA HUNS
- CMPA #9
- BLE NOOVERFLOW13
- SUBA #10
- STAA HUNS
- LDAA THOUSANDS
- LDAB #1
- ABA
- STAA THOUSANDS
- BRA NOOVERFLOW13
- NOOVERFLOW12:
- STAA ONES
- NOOVERFLOW13:
- LDAA TENS ;ADD TENS
- LDAB TENS2
- ABA
- CMPA #9
- BLE NOOVERFLOW14
- SUBA #10
- STAA TENS
- LDAA HUNS
- LDAB #1
- ABA
- STAA HUNS
- CMPA #9
- BLE NOOVERFLOW15
- SUBA #10
- STAA HUNS
- LDAA THOUSANDS
- LDAB #1
- ABA
- STAA THOUSANDS
- BRA NOOVERFLOW15
- NOOVERFLOW14:
- STAA TENS
- NOOVERFLOW15:
- LDAA HUNS ;ADD HUNS
- LDAB HUNS2
- ABA
- CMPA #9
- BLE NOOVERFLOW16
- SUBA #10
- STAA HUNS
- INC THOUSANDS
- BRA NOOVERFLOW17
- NOOVERFLOW16:
- STAA HUNS
- NOOVERFLOW17:
- **********************************************
- *Add the result from above sub routine to B reg
- LDAA ONES ;ADD ONES
- LDAB RESONES
- ABA
- CMPA #9
- BLE NOOVERFLOW18
- SUBA #10
- STAA RESONES
- LDAA TENS
- LDAB #1
- ABA
- STAA TENS
- CMPA #9
- BLE NOOVERFLOW19
- SUBA #10
- STAA TENS
- LDAA HUNS
- LDAB #1
- ABA
- STAA HUNS
- CMPA #9
- BLE NOOVERFLOW19
- SUBA #10
- STAA HUNS
- LDAA THOUSANDS
- LDAB #1
- ABA
- STAA THOUSANDS
- BRA NOOVERFLOW19
- NOOVERFLOW18:
- STAA RESONES
- NOOVERFLOW19:
- LDAA TENS ;ADD TENS
- LDAB RESTENS
- ABA
- CMPA #9
- BLE NOOVERFLOW20
- SUBA #10
- STAA RESTENS
- LDAA HUNS
- LDAB #1
- ABA
- STAA HUNS
- CMPA #9
- BLE NOOVERFLOW21
- SUBA #10
- STAA HUNS
- LDAA THOUSANDS
- LDAB #1
- ABA
- STAA THOUSANDS
- BRA NOOVERFLOW21
- NOOVERFLOW20:
- STAA RESTENS
- NOOVERFLOW21:
- LDAA HUNS ;ADD HUNS
- LDAB RESHUNS
- ABA
- CMPA #9
- BLE NOOVERFLOW22
- SUBA #10
- STAA RESHUNS
- INC THOUSANDS
- BRA NOOVERFLOW23
- NOOVERFLOW22:
- STAA RESHUNS
- NOOVERFLOW23:
- **********************************************
- *Final result is now in reshuns, restens, resones.
- *Now convert the thousands count to tens of thousands and thousands.
- LDAB THOUSANDS
- CLRA ;Clear the D Accumulator high byte
- LDX #10 ;Start with the 100s Divide NUMBER by 100
- IDIV ;The result is in IX, the remainder in D
- XGDX ;Now D has the result and IX has the remainder
- STAB THOUSANDS2 ; And store it
- XGDX ;Swap the tens into Acc D ...
- STAB THOUSANDS ; ... and store it
- **********************************************
- DIV: LDAA ARITH
- CMPA #4
- BNE NOTDIV
- ********************************************
- *Division sub routine
- *Divid variable one by variable two.
- LDAA NUMBER
- CLRB
- XGDX
- LDAA NUMBER2
- CLRB
- IDIV
- XGDX
- CLRA ;Clear the D Accumulator high byte
- LDX #100 ;Start with the 100s Divide NUMBER by 100
- IDIV ;The result is in IX, the remainder in D
- XGDX ;Now D has the result and IX has the remainder
- STAB RESHUNS ;And store it
- XGDX ;The remainder in D again
- LDX #10 ;Now for the 10s. Divide the remainder by 10
- IDIV ;Tens are are IX, the units in D
- STAB RESONES ;... and store it
- XGDX ;Swap the tens into Acc D ...
- STAB RESTENS ;... and store it
- NOTDIV:
- *The following sub converts the end results from above back into a ASCII
- *String MSG1 so that it can be then displayed on the first line of the LCD.
- LDY #MSG1
- LDAB cntchrl1
- ABY
- LDAA THOUSANDS2
- CMPA #0
- BEQ OVER6
- ADDA #$30 ;Convert the tens of thousands to ascii
- STAA 00,Y
- INY
- INC cntchrl1
- LDAA THOUSANDS
- BRA OVER7
- OVER6: LDAA THOUSANDS
- CMPA #0
- BEQ OVER
- OVER7: ADDA #$30 ;Convert the thousands character to ascii
- STAA 00,Y
- INY
- INC cntchrl1
- LDAA RESHUNS
- BRA OVER2
- OVER: LDAA RESHUNS
- CMPA #0
- BEQ OVER3
- OVER2: ADDA #$30 ;Convert the hundreds character to ascii
- STAA 00,Y
- INY
- INC cntchrl1
- LDAA RESTENS
- BRA OVER4
- OVER3: LDAA RESTENS
- CMPA #0
- BEQ OVER5
- OVER4: ADDA #$30 ;Convert the tens character to ascii
- STAA 00,Y
- INY
- INC cntchrl1
- OVER5: LDAA RESONES
- ADDA #$30 ;Convert the ones character to ascii
- STAA 00,Y
- INY
- INC cntchrl1
- LDX #MSG1
- JSR OUTSTR ;Then print formated to screen.
- RTS
- *Timer interupt routine
- INT20
- PSHA
- PSHB
- PSHX
- PSHY
- LDD TCNT
- ADDD #PERIOD
- STD TOC1 ; set TOC to PERIOD of 20ms
- LDAA #%10000000
- STAA TFLG1 ; reset interupt
- ldaa #$ff
- staa portb
- pshx
- ldx #167 ; 2040 cycles
- delay1ms: ; This sub creates one milli seconds of high time
- dex
- inx
- dex
- bne delay1ms
- pulx
- pshx
- ldaa adcvalue ;adcvalue sets the high time
- cmpa #0
- bne delayvariable
- ldaa #1
- delayvariable: ; This sub creates 0 to 1 ms high time
- abx ; waste 3 cycles
- deca
- bne delayvariable ; loop for 8 to 2040 cycles
- pulx
- ldaa #0
- staa portb
- PULY ; RESTORE REGISTERS
- PULX
- PULB
- PULA
- RTI
- *SPI Interupt Routine
- MY_INSTRG ;Recieve the adc value through the serial port
- PSHA ; save registers
- LDAA SCSR ; Clear interupt flag
- LDAA SCDR ; CHECK FOR START CHARACTER
- STAA adcvalue;load adc value from serial port into var
- PULA ; RESTORE REGISTERS
- RTI
- MSG2.1: FCC " "
- MSG1: FCC " "
- end
Partner's Sites