Parsing a byte value into three ASCII numeric characters
![John Kauffman](https://forums.parallax.com/uploads/userpics/555/nZBNEPT0B6FMD.jpg)
Parsing a byte value into three ASCII numeric characters
I got some good help on STAMP forum from PeterH and Mark Green with a routine to parse a byte value into thee characters that represent the value in decimal. The code they offered works fine in PBASIC.
·
I tried porting to SX/B (mainly change to one math operation per line). I'm testing with input values of 000, 023, 123 and 255. The result is that the 100's digit comes through fine but the 10's and unit digit is always zero. Any help appreciated.
·
LCD_Byte_Dec3:
' converts byte value into three ASCII characters
' displays those characters as dec numbers
' only good with byte-size input. Needs three temp vars declared
' Temp1 = dividend· Temp2 = divisor·· Temp3 = quotient
' remainder becomes new dividend (Temp1)
·
Temp1 = __PARAM1························ ' save the byte into the dividend
Temp2 = 100······························································ ' set the divisor for first loop
for idx1 = 1 to 3·································· ' run the loop once per digit
··········· Temp3 = Temp1/Temp2·········· ' get quotient by dividend/divisor
··········· Temp1 =__remainder·· ' save remainder for dividend of next loop
··········· Temp3=Temp3+48····························· ' convert quotent to ASCII
··········· lcd_byte Temp3·································· ' print ASCII quotent
··········· Temp2 = Temp2/10················ ' get divisor ready for next loop
next
RETURN
I got some good help on STAMP forum from PeterH and Mark Green with a routine to parse a byte value into thee characters that represent the value in decimal. The code they offered works fine in PBASIC.
·
I tried porting to SX/B (mainly change to one math operation per line). I'm testing with input values of 000, 023, 123 and 255. The result is that the 100's digit comes through fine but the 10's and unit digit is always zero. Any help appreciated.
·
LCD_Byte_Dec3:
' converts byte value into three ASCII characters
' displays those characters as dec numbers
' only good with byte-size input. Needs three temp vars declared
' Temp1 = dividend· Temp2 = divisor·· Temp3 = quotient
' remainder becomes new dividend (Temp1)
·
Temp1 = __PARAM1························ ' save the byte into the dividend
Temp2 = 100······························································ ' set the divisor for first loop
for idx1 = 1 to 3·································· ' run the loop once per digit
··········· Temp3 = Temp1/Temp2·········· ' get quotient by dividend/divisor
··········· Temp1 =__remainder·· ' save remainder for dividend of next loop
··········· Temp3=Temp3+48····························· ' convert quotent to ASCII
··········· lcd_byte Temp3·································· ' print ASCII quotent
··········· Temp2 = Temp2/10················ ' get divisor ready for next loop
next
RETURN
Comments
Update: Added routine to right-justify (with leading spaces) within a 3-column field.
Post Edited (JonnyMac) : 3/30/2008 12:45:43 AM GMT
I haven't read your code yet, but here is what I have after substituting for conflicting variables.
LCD_Byte_Dec3:
' converts byte value into three ASCII characters
' displays those characters as dec numbers
' only good with byte-size input
' remainder becomes new dividend (Temp1)
dividend = __PARAM1 ' save the byte into the dividend
divisor = 100 ' set the divisor for first loop
for idx1 = 1 to 3 ' run the loop once per digit
quotient = dividend/divisor ' get quotient by dividend/divisor
dividend =__remainder ' save remainder for dividend of next loop
quotient=quotient + 48 ' convert quotent to ASCII
lcd_byte quotient ' print ASCII quotent
divisor = divisor/10 ' get divisor ready for next loop
next
RETURN
Maybe the following will help. I just sat down to do this, and it appears to work correctly. I am not saying this is exactly what you want, but I believe it may give you insight as to how to make yours work. ...I hope.
Gary
Post Edited (Sens-a-Dat) : 3/31/2008 6:39:40 AM GMT