As for the Print_Dec procedure, it could cause problems if not all variables were fitting into the same RAM bank. The following should be insensitive to variables' location, provided the string parameters are placed in BANK0 or 1. If placed in higher memory banks, one has to set the IRP bit before calling the procedure.
Code: Select all
module Print_Dec_16_Pro
'***********************************************************************************************
'* *
'* PRINT_DEC() by Warren Schroeder - September 23, 2006
'* Compiled with mikroBASIC 5.0.0.1 and tested with 16F876A
'* *
'* PRINT_DEC is a routine that accepts any unsigned number up to 32 bits and converts
'* it to a string of decimal numbers which can also include a user-inserted decimal point.
'* The maximum size of the string is 11 characters: 10 decimal integers + a decimal point.
'* *
'* The result string can be formatted (including an optional decimal point)
'* based on a #'s template and the following justification codes:
'* RJ = Right-Justified with Leading Blank Spaces
'* RJZ = Right-Justified with Leading Zeroes
'* *
'* This is the prototype of the function:
'* Print_Dec(_number as longint,_format as string[11],_justify as byte,_target as string[11])
'* *
'* Examples: *
'* print_dec(123456789, "#####.#####", RJ, mystr)
'* Printed Result in mystr: " 1234.56789" *
'* print_dec(mynumber, "#.######", RJZ, mystr) where mynumber = 64321
'* Printed Result in mystr: "0.064321"
'*
'* 14 Nov 2009
'* Modified to Compile for the mikroElectronika PRO Basic with the assistance of Durango and Janni
'* Janni. Compiled with Basic PRO v3.2 and verified on EasyPIC4 board using 16F88.
'***********************************************************************************************
Const RJ as byte = 1
Const RJZ as byte = 2
'*******************************************************************8
' Need to place this here above implements
sub procedure Print_Dec(dim _number as longint,
dim byref _format as string[11],
dim _justify as byte,
dim byref _target as string[11])
'********************************************************************
implements
sub procedure Print_Dec(dim _number as longint,
dim byref _format as string[11],
dim _justify as byte,
dim byref _target as string[11])
dim tmp0, tmp1, flen, start, ascii as byte
dim doff as short ' ***Dim'ed doff as a short because it is
' given a value of -1 a few lines down. ***
tmp0 = 0
flen = 0
doff = -1
FSR = byte(@_target) ' location for final string
While tmp0 <> 11
INDF = 0
inc(tmp0)
inc(FSR)
Wend
FSR = byte(@_format) ' location of formatting scheme'
While INDF <> 0
If INDF = 46 Then doff = flen End If ' get decimal position
inc(FSR) ' move to next memory position
inc(flen)
Wend
R0 = 32
asm
b32:
end asm
R1 = 10
FSR = byte(@_target) + 10
_number=_number << 1
asm
inloop:
rlf INDF,1
movlw 246
addwf INDF,0
btfsc STATUS,0
movwf INDF
decf FSR,F
decfsz R1,F
goto inloop
decfsz R0,F
goto b32
end asm
start = 12 - flen
tmp1 = 0
FSR = byte(@_target)
While tmp1 < flen
ascii = 48
If tmp1 = doff Then
INDF = 46
dec(start)
Else
FSR = FSR + start
tmp0 = INDF
If tmp0 = 0 Then
If _justify = RJ Then
If tmp1 < doff Then ascii = 32 End If
End If
End If
FSR = FSR - start
INDF = tmp0 + ascii
End If
inc(FSR)
inc(tmp1)
Wend
INDF = 0
end sub
end.