Best ways to display leading zeros with LCDOUT?
Hello, sometimes it is needed to display defined number of zeros in front of variable, when using LCDOUT. So say when you have to display 15, you display 15, but when you have to display 7, you display 07
In case of 2 digit variable it is quite easy:
Code:
IF X>=10 then Y=X DIG 1
ELSE
Y=0
ENDIF
X= X DIG 0
LCDOUT $FE, $C0, #Y, #X
But what if I need to display 1-2-3-4 leading zeros?
Add that many extra variables and handling routines?
Re: Best ways to display leading zeros with LCDOUT?
I create a buffer for the transmission. For example, I'm using a Nextion where if I wanted to display RPM, I name the Nextion Number object "rpm". To send it, ASCii characters "rpm.val=1234" then $FF, $FF, $FF. I create a NexBuf[16] VAR BYTE to hold each character byte.
Here is my Send_RPM subroutine:
Code:
Send_RPM: ;Send Current RPM Value to Nextion
tRPM = RPM ;Create a Temporary Variable We Can Modify
ARRAYWRITE NexBuf,["rpm.val="] ;Start Nextion Command Sequence, Occupies NexBuf[0] >> [7]
IF tRPM > 1000 THEN ;If RPM is 4 Digits
b0 = tRPM / 1000 ;Get the Xxxx Value
NexBuf[8] = b0 + $30 ;Convert it to ASCii
tRPM = tRPM - (b0 * 1000) ;Subtract the Xxxx Value from tRPM
IF tRPM < 100 THEN ;2nd Digit = 0
NexBuf[9] = $30 ;If New tRPM < 100, Add Zero Fill (ASCii)
IF tRPM < 10 THEN ;3rd Digit = 0
NexBuf[10] = $30 ;If tRPM < 10, Add Zero Fill (ASCii)
ENDIF ;IF tRPM < 10
ENDIF ;IF tRPM < 100
ELSE ;tRPM < 1000
NexBuf[8] = $30 ;If tRPM < 1000, Xxxx = Zero (ASCii)
ENDIF ;IF tRPM > 1000
IF tRPM > 100 THEN ;If RPM is 3 Digits
b0 = tRPM / 100 ;Get the xXxx Value
NexBuf[9] = b0 + $30 ;Convert it to ASCii
tRPM = tRPM - (b0 * 100) ;Subtract the xXxx Value from tRPM
IF tRPM < 10 THEN ;3rd Digit = 0
NexBuf[10] = $30 ;If tRPM < 10, Add Zero Fill (ASCii)
ENDIF ;IF tRPM < 10
ELSE ;tRPM < 100
NexBuf[9] = $30 ;If tRPM < 100, it's Zero (ASCii)
ENDIF ;IF tRPM > 100
IF tRPM > 10 THEN ;If RPM is 2 Digits
b0 = tRPM / 10 ;Get the xxXx Value
NexBuf[10] = b0 + $30 ;Convert it to ASCii
tRPM = tRPM - (b0 * 10) ;Subtract the xxXx Value from tRPM
ELSE ;tRPM < 10
NexBuf[10] = $30 ;If tRPM < 10, it's Zero (ASCii)
ENDIF ;IF tRPM > 10
NexBuf[11] = tRPM + $30 ;Convert xxxX Value to ASCii & Add to Buffer
FOR b0 = 12 TO 14 ;Generate "End of Message" Sequence: $FF, $FF, $FF
NexBuf[b0] = $FF ;Load it to Buffer
NEXT b0 ;FOR b0 = 12 TO 14
NexBuf[15] = 0 ;Tells Us To Turn UART TX INT Off
RotX = 0 ;NexFlags.RotX -- Clear RotX Flag
GOTO Start_TX ;No RETURN Needed, Using GOTO Command
I'm using the UART TX Interrupt to keep feeding NexBuf through the pipeline (not LCDOOUT). But I hope this at least gives you some ideas.
Re: Best ways to display leading zeros with LCDOUT?
Thanks, your code give me another idea - just create an array, write variable to via DIG, and read and display array in sequence, that should work imo...
Re: Best ways to display leading zeros with LCDOUT?
Why not use the DEC modifier on a word variable?
For example if wVar=123 then DEC5 will display 00123 since it is padding zeros.
Ioannis
Re: Best ways to display leading zeros with LCDOUT?
Thanks, haven't thought about that :)