; Conversion times:
; The whole routine requires 228 clock cycles maximum (converting $FF), and 79 clock cycles minimum (converting $00).
; At 4 MHz the times are 56.75 microseconds resp. 17.75 microseconds.
; Definitions:
; Registers
.DEF rmp = R16 ; used as multi-purpose register
; AVR type: Tested for type AT90S8515, only required for stack setting, routines work fine with other AT90S-types also
.NOLIST
.INCLUDE "8515def.inc"
.LIST
; Start of test program
; Just writes a number to R1 and starts the conversion routine, for test purposes only
.CSEG
.ORG $0000
rjmp main
main:
[COLOR="#FF0000"]ldi rmp,HIGH(RAMEND) ; Set the stack
out SPH,rmp
ldi rmp,LOW(RAMEND)
out SPL,rmp[/COLOR]
ldi rmp,$FF ; Convert $FF
mov R1,rmp
rcall fpconv8 ; call the conversion routine
no_end: ; unlimited loop, when done
rjmp no_end
; Conversion routine wrapper, calls the different conversion steps
fpconv8:
rcall fpconv8m ; multiplicate by 502
rcall fpconv8r ; round and divide by 256
rcall fpconv8a ; convert to ASCII string
ldi rmp,'.' ; set decimal char
mov R6,rmp
ret ; all done
; Subroutine multiplication by 502
fpconv8m:
clr R4 ; set the multiplicant to 502
ldi rmp,$01
mov R3,rmp
ldi rmp,$F6
mov R2,rmp
clr R7 ; clear the result
clr R6
clr R5
fpconv8m1:
or R1,R1 ; check if the number is all zeros
brne fpconv8m2 ; still one's, go on convert
ret ; ready, return back
fpconv8m2:
lsr R1 ; shift number to the right (div by 2)
brcc fpconv8m3 ; if the lowest bit was 0, then skip adding
add R5,R2 ; add the number in R6:R5:R4:R3 to the result
adc R6,R3
adc R7,R4
fpconv8m3:
lsl R2 ; multiply R4:R3:R2 by 2
rol R3
rol R4
rjmp fpconv8m1 ; repeat for next bit
; Round the value in R7:R6 with the value in bit 7 of R5
fpconv8r:
clr rmp ; put zero to rmp
lsl R5 ; rotate bit 7 to carry
adc R6,rmp ; add LSB with carry
adc R7,rmp ; add MSB with carry
mov R2,R7 ; copy the value to R2:R1 (divide by 256)
mov R1,R6
ret
; Convert the word in R2:R1 to an ASCII string in R5:R6:R7:R8
fpconv8a:
clr R4 ; Set the decimal divider value to 100
ldi rmp,100
mov R3,rmp
rcall fpconv8d ; get ASCII digit by repeated subtraction
mov R5,rmp ; set hundreds string char
ldi rmp,10 ; Set the decimal divider value to 10
mov R3,rmp
rcall fpconv8d ; get the next ASCII digit
mov R7,rmp ; set tens string char
ldi rmp,'0' ; convert the rest to an ASCII char
add rmp,R1
mov R8,rmp ; set ones string char
ret