PDA

View Full Version : value to string



Googfan
08-01-2008, 02:02 AM
How do i convert a value to a string in pbasic 2.5

ie:
··· 1·· ·to "1"
··· 258 to "258"

i would like to display it on an lcd by modifying·my·"Sin to LCD"·code:


' {$STAMP BS2}
' {$PBASIC 2.5}

LineChars··· CON 16' === Number of Characters per Line
'----------------------------------------------
DEBUG "Ready..."
LcdHome····· CON $02 ' move cursor home
LcdCrsrL···· CON $10 ' move cursor left
LcdCrsrR···· CON $14 ' move cursor right
LcdDispL···· CON $18 ' shift chars left
LcdDispR···· CON $1C ' shift chars right
ochar······· VAR Byte
crsrPos····· VAR Byte
char········ VAR Byte
lcdone······ VAR Bit
LcdDirs····· VAR DIRB'.................dirs FOr I/O redirection
LcdBusOut··· VAR OUTB
LcdBusIn···· VAR INB
E··· PIN 1'....Adam Munich is· My PuppetmasterAdam Munich is· My Puppetmaster........................LCD Enable (1 = enabled)
RW·· PIN 2'............................Read/WRITe\
RS·· PIN 3'............................Reg SELECt (1 = char)

' LCD Initialization----------------------------------------------------------------------

DIRL = %11111110
· LcdBusOut = %0011'...................8-Bit mode
· PULSOUT E, 3 : PAUSE 5
· PULSOUT E, 3 : PAUSE 0
· PULSOUT E, 3 : PAUSE 0
· LcdBusOut = %0010'...................4-bit mode
· PULSOUT E, 3
· char = %00101000'....................2-line mode
· GOSUB LCD_Command
· char = %00001101'....................on, no crsr, blink
· GOSUB LCD_Command
· char = %00000110'....................inc crsr, no disp shift
· GOSUB LCD_Command
ochar = 0

'routine---------------------------------------------------------------------------
main:
GOSUB LCD_Clear
DO
SERIN 16, 16468, [char]
IF char = "`" THEN GOTO main'..........Clear?
GOSUB LCD_Write_Char
LOOP

'lcd subprograms--------------------------------------------------------------------------
' -- put command byte in 'char'
LCD_Command:
LOW RS
LcdBusOut = char.HIGHNIB'..............output high nibble
PULSOUT E, 3'..........................strobe the Enable line
LcdBusOut = char.LOWNIB'...............output low nibble
PULSOUT E, 3
HIGH RS
RETURN

' -- put byte to write in 'char'
LCD_Write_Char:
LcdBusOut = char.HIGHNIB'..............output high nibble
PULSOUT E, 3'..........................strobe the Enable line
LcdBusOut = char.LOWNIB'...............output low nibble
PULSOUT E, 3
ochar = ochar+1'.......................coutin
IF ochar > (LineChars-1) THEN'.........Word Wrap?
· IF lcdone = 0 THEN'..................yes
··· char = $C0
··· GOSUB LCD_Command
· ENDIF
· lcdone = 1'..........................no
ENDIF
HIGH RS'...............................Char Mode
RETURN

LCD_Clear:
lcdone = 0'............................reset vars
ochar· = 0
char = $01'............................Clear LCD
GOSUB LCD_Command
PAUSE 5
RETURN

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mixing things...

It can be deadly -- Potassium Carbon Nitrogen [KCN]
It can be fun -----·Potassium Nitrogen Oxogen Carbon Sulfer [KNO3+C+S]
It can be useless - Napkin Butter
wait, what point was I·trying to establish?

·

Googfan
08-01-2008, 02:07 AM
to be more specific i want to display distance measured by a PING))) on the lcd

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mixing things...

It can be deadly -- Potassium Carbon Nitrogen [KCN]
It can be fun -----·Potassium Nitrogen Oxogen Carbon Sulfer [KNO3+C+S]
It can be useless - Napkin Butter
wait, what point was I·trying to establish?

·

Mike Green
08-01-2008, 02:16 AM
Look in the Basic Manual in the section on the DIG operator. This takes a value on the left and a digit position (power of ten) on the right and returns the value of the digit there. Add "0" to that and you have a displayable character.

(4352 DIG 0) + "0" is "2"
(4352 DIG 2) + "0" is "3"
(4352 DIG 4) + "0" is "0"

Googfan
08-01-2008, 02:21 AM
no that takes it literal. I want to convert that '2' into ACSII Character "2"

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mixing things...

It can be deadly -- Potassium Carbon Nitrogen [KCN]
It can be fun -----·Potassium Nitrogen Oxogen Carbon Sulfer [KNO3+C+S]
It can be useless - Napkin Butter
wait, what point was I·trying to establish?

·

Mike Green
08-01-2008, 02:28 AM
That's what it does. Read the manual. Try it.

Googfan
08-01-2008, 02:35 AM
No DIG gives you the value of the digit not the character code. This seems to do what i want but its slow and memory hogging.

char VAR Byte
numb VAR Nib

numb = 563 DIG 2
GOSUB poo
DEBUG char
numb = 563 DIG 1
GOSUB poo
DEBUG char
numb = 563 DIG 0
GOSUB poo
DEBUG char
END

poo:
IF numb = 0 THEN char = $30
IF numb = 1 THEN char = $31
IF numb = 2 THEN char = $32
IF numb = 3 THEN char = $33
IF numb = 4 THEN char = $34
IF numb = 5 THEN char = $35
IF numb = 6 THEN char = $36
IF numb = 7 THEN char = $37
IF numb = 8 THEN char = $38
IF numb = 9 THEN char = $39
RETURN

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mixing things...

It can be deadly -- Potassium Carbon Nitrogen [KCN]
It can be fun -----·Potassium Nitrogen Oxogen Carbon Sulfer [KNO3+C+S]
It can be useless - Napkin Butter
wait, what point was I·trying to establish?

·

PJ Allen
08-01-2008, 02:40 AM
poo:
· char = numb + $30
· RETURN

Googfan
08-01-2008, 02:53 AM
PJ Allen said...
poo:
· char = numb + $30
· RETURN
Perfect Thanks!


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mixing things...

It can be deadly -- Potassium Carbon Nitrogen [KCN]
It can be fun -----·Potassium Nitrogen Oxogen Carbon Sulfer [KNO3+C+S]
It can be useless - Napkin Butter
wait, what point was I·trying to establish?

·