Display word VAR on LCD?
Hello,
Below is a program that I wrote to display variables on an LCD. The program is simple to·solve my problem before adding more code. It has been modified from the LCD program online describing use of the Parallax LCD so it is not all my original programming.
I'm trying to display a word value on the LCD. I understand the LCD_DEC3 subroutine, it displays a byte. I tried to add additional code to add the 1000's digit, but the compiler truncates the last "0" of "1000".
I have a pot on my board which I read and can adjust to give me different values to display.
As the code is written I tried to break up the word variable into MSB and LSB and display each byte individually. That isn't working because the LSB rolls over above 255 back to 0 instead of up to 999 then rolling over to 0. I do understand why it does this, but not sure how to fix it.
Any help to point me in the right direction?
Display a word variable as a string somehow?
Thanks,
Alex
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If at first you don't succeed, then skydiving is not for you.
Below is a program that I wrote to display variables on an LCD. The program is simple to·solve my problem before adding more code. It has been modified from the LCD program online describing use of the Parallax LCD so it is not all my original programming.

I'm trying to display a word value on the LCD. I understand the LCD_DEC3 subroutine, it displays a byte. I tried to add additional code to add the 1000's digit, but the compiler truncates the last "0" of "1000".
I have a pot on my board which I read and can adjust to give me different values to display.
As the code is written I tried to break up the word variable into MSB and LSB and display each byte individually. That isn't working because the LSB rolls over above 255 back to 0 instead of up to 999 then rolling over to 0. I do understand why it does this, but not sure how to fix it.
Any help to point me in the right direction?
Display a word variable as a string somehow?
Thanks,
Alex
' =========================================================================
'
' File...... LCD - MineR2.SXB
' Purpose... Display of variables to the Parallax Serial LCD with SX/B
' Author.... Alex41
' E-mail....
' Started... Feb 01 2009
' Updated... Feb 22 2009
'
' =========================================================================
' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------
'
' runs on SX/B 1.50.01
' R1
' Program displays an output of a value of a pot using RCTIME to an LCD
' R2
' Try to display Word variables
'
'
'
' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------
DEVICE SX48, OSCXT2
FREQ 4_000_000
' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------
LcdTx PIN RC.7 ' LCD serial connection
PotPin PIN RA.0 ' pin for RCTIME
OUTPUT RA
' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------
LcdBaud CON "T19200" ' or T2400, or T9600
LcdBkSpc CON $08 ' move cursor left
LcdRt CON $09 ' move cursor right
LcdLF CON $0A ' move cursor down 1 line
LcdCls CON $0C ' clear LCD (need 5 ms delay)
LcdCR CON $0D ' move pos 0 of next line
LcdBLon CON $11 ' backlight on
LcdBLoff CON $12 ' backlight off
LcdOff CON $15 ' LCD off
LcdOn1 CON $16 ' LCD on; no crsr, no blink
LcdOn2 CON $17 ' LCD on; no crsr, blink on
LcdOn3 CON $18 ' LCD on; crsr on, no blink
LcdOn4 CON $19 ' LCD on; crsr on, blink on
LcdLine1 CON $80 ' move to line 1, column 0
LcdLine2 CON $94 ' move to line 2, column 0
' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------
idx1 VAR Byte ' loop control
char VAR Byte
newChar VAR Byte
pos VAR Byte ' position
counter VAR Byte
tmpB1 VAR Byte ' subroutine work vars
tmpB2 VAR Byte
tmpB3 VAR Byte
tmpB4 VAR Byte
tmpW1 VAR Word
Pottime VAR Word ' var for RCTIME
Potw VAR Word
' =========================================================================
PROGRAM Start
' =========================================================================
' -------------------------------------------------------------------------
' Subroutine Declarations
' -------------------------------------------------------------------------
DELAY_MS SUB 1, 2 ' delay in milliseconds
DELAY_US SUB 1, 2 ' delay in microseconds
LCD_OUT SUB 1 ' byte to LCD
LCD_STR SUB 2 ' string to LCD
LCD_DEC3 SUB 1 ' print using DEC3 format
' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------
Start:
'PLP_A = %00000000 ' pull up unused pins
PLP_B = %00000000
PLP_C = %00000000
PLP_D = %00000000
PLP_E = %00000000
HIGH LcdTx
DELAY_MS 100 ' let LCD initialize
Main:
LCD_OUT LcdBLon ' backlight off
LCD_OUT LcdOn1 ' no cursor or blink
LCD_OUT LcdCls ' clear the LCD
DELAY_MS 250
DO
HIGH potpin ' charge the capacitor
PAUSEUS 300 ' Pause for 300 us
RCTIME Potpin, 1, Pottime ' measure in 2 us units
Pottime = Pottime * 4 ' makes the RCTIME result bigger
LCD_OUT LcdLine1
LCD_STR "Pot - "
LCD_DEC3 Pottime_MSB ' display RCTIME result
LCD_DEC3 Pottime_LSB
DELAY_MS 100 ' delay to make output readable
LOOP
GOTO Main
' -------------------------------------------------------------------------
' Subroutine Code
' -------------------------------------------------------------------------
' Use: DELAY_MS milliseconds
SUB DELAY_MS
IF __PARAMCNT = 1 THEN
tmpW1 = __PARAM1 ' save byte value
ELSE
tmpW1 = __WPARAM12 ' save word value
ENDIF
PAUSE tmpW1
ENDSUB
'--------------------------------------------------------------------------
' Use: DELAY_US microseconds
DELAY_US:
IF __PARAMCNT = 1 THEN
tmpW1 = __PARAM1 ' save byte value
ELSE
tmpW1 = __WPARAM12 ' save word value
ENDIF
PAUSEUS tmpW1
RETURN
' -------------------------------------------------------------------------
' Use: LCD_OUT aByte
' -- "aByte" is single-byte constant or variable
SUB LCD_OUT
SEROUT LcdTx, LcdBaud, __PARAM1
ENDSUB
' -------------------------------------------------------------------------
' Use: LCD_STR [noparse][[/noparse] string | label ]
' -- "string" is an embedded literal string
' -- "label" is DATA statement label for stored z-String
SUB LCD_STR
tmpW1 = __WPARAM12 ' save address
DO
READINC tmpW1, tmpB1
IF tmpB1 = 0 THEN EXIT ' if 0, string complete
LCD_OUT tmpB1
LOOP
ENDSUB
' -------------------------------------------------------------------------
' Use: LCD_DEC3 value
' -- displays byte value in DEC3 format
SUB LCD_DEC3
tmpB2 = __PARAM1 ' copy output value
tmpB2 = tmpB2 / 100 ' get 100's digit
tmpB3 = __REMAINDER ' save 10's & 1's
tmpB1 = "0" + tmpB2 ' make 100's character
LCD_OUT tmpB1 ' print it
tmpB2 = tmpB3 / 10 ' get 10's digit
tmpB3 = __REMAINDER ' save 1's
tmpB1 = "0" + tmpB2 ' make 10's character
LCD_OUT tmpB1 ' print it
tmpB1 = "0" + tmpB3 ' make 1's character
LCD_OUT tmpB1 ' print it
ENDSUB
' =========================================================================
' Program Data
' =========================================================================
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If at first you don't succeed, then skydiving is not for you.

Comments
· SX/B 2.0 has a STR command that will take a WORD variable and create an array of characters that you can send to the LCD.
· I'd suggest downloading SX/B 2 and using the STR command.
You can download it from this thread: http://forums.parallax.com/showthread.php?p=780347
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There is a fine line between arrogance and confidence. Make sure you don't cross it...
·
Thanks for the help.
I've been lurking lately and·seen that·2.0 is out. I guess this will get me to install it. I'll give it a try.
Alex
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If at first you don't succeed, then skydiving is not for you.
' Use: LCD_DEC value {, digits } ' -- prints "value" in DEC format ' -- "digits" MUST be specified with word values ' -- DEC3 or DEC5 format is used if "digits" not specified or invalid SUB LCD_DEC dValue VAR tmpW1 dDigits VAR tmpB1 dIdx VAR tmpB2 dChar VAR __PARAM1 IF __PARAMCNT = 1 THEN ' byte w/o digits dValue = __PARAM1 dDigits = 3 ELSEIF __PARAMCNT = 2 THEN ' byte w/digits dValue = __PARAM1 dDigits = __PARAM2 ELSE ' word w/digits dValue = __WPARAM12 dDigits = __PARAM3 ENDIF IF dDigits = 0 THEN ' validate digits dDigits = 5 ELSEIF dDigits > 5 THEN dDigits = 5 ENDIF STR nStr, dValue ' to decimal string dIdx = 5 - dDigits ' point to first char DO WHILE dIdx < 5 LCD_OUT nStr(dIdx) INC dIdx LOOP ENDSUBThanks for the code! That's a big help.
I see it is for 2.0 by the local variables in the subroutine. I'll install 2.0 and get up to speed on the new features.
Thanks,
Alex
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If at first you don't succeed, then skydiving is not for you.
I just upgraded to 2.0 and used your code from above.
It works perfectly!
Thanks,
Alex
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If at first you don't succeed, then skydiving is not for you.
' Use: LCD_DEC value {, digits} ' -- print value in decimal format ' -- if digits not specified field width automatically adjusted ' -- field is space padded (no leading zeroes) SUB LCD_DEC '{$IFUSED LCD_DEC} dcValue VAR tmpW1 ' value to print dcDigits VAR tmpB3 ' # digits to print dcValue = __WPARAM12 IF __PARAMCNT = 3 THEN dcDigits = __PARAM3 ' get width dcDigits = dcDigits MAX 5 ' make sure it's legal dcDigits = dcDigits MIN 1 ELSE ' adjust field for value IF dcValue > 9999 THEN dcDigits = 5 ELSEIF dcValue > 999 THEN dcDigits = 4 ELSEIF dcValue > 99 THEN dcDigits = 3 ELSEIF dcValue > 9 THEN dcDigits = 2 ELSE dcDigits = 1 ENDIF ENDIF STR nStr, dcValue ' create decimal string dcDigits = 5 - dcDigits ' convert to string index DO UNTIL dcDigits = 5 ' print the value LCD_OUT nStr(dcDigits) INC dcDigits LOOP '{$ENDIF} ENDSUBPost Edited (JonnyMac) : 3/1/2009 8:33:54 PM GMT