Need to convert user input to multiple word value
gianallen
Posts: 16
Hi,
Along with a separate PIC chip, I am using a BS2pe to accumulate total flow from a flow meter. The BS2pe's role is to calculate an ongoing accumulation of the total pulse count into 2 words, giving a maximum value of 2^32 using Tracy Allen's double precision math routines (http://emesystems.com/BS2math6.htm).
Here's the issue that my feeble brain just can't seem to figure out:
I want a user in the field to be able to set an initial value for the count, as the flow meters we're working with often already have some initial total flow when we install our equipment. I can't figure out how to translate a large number that might be entered by a user (up to 8 digits) into a binary value that spans two 16-bit words.
For instance, if a user enters a value of "12345678", I need that to be stored in two words as $BC $614E. (Note that the user could also enter a small value (even 0) as well)
I have gone down several paths trying to solve this, but haven't gotten there. All I have so far is the user input stored in sequential bytes which I can then convert to decimal values for processing (of course, I could just as easily store the user input as a sequence of bytes in the scratchpad):
char0 VAR Byte
char1 VAR Byte
.
.
.
char7 VAR Byte
SERIN 16,$54,[STR char0\8\CR] <--- if user enter <8 digits, char0(ix) will be set to 0. Can be used to determine number of chars entered by user.
{Convert char0-char7 from ASCII to decimal before processing}
{Move decimal values into 2 words: the part where I can't figure out what to do...}
Any ideas how to get these digits into two words? Thank you.
-Gian
Along with a separate PIC chip, I am using a BS2pe to accumulate total flow from a flow meter. The BS2pe's role is to calculate an ongoing accumulation of the total pulse count into 2 words, giving a maximum value of 2^32 using Tracy Allen's double precision math routines (http://emesystems.com/BS2math6.htm).
Here's the issue that my feeble brain just can't seem to figure out:
I want a user in the field to be able to set an initial value for the count, as the flow meters we're working with often already have some initial total flow when we install our equipment. I can't figure out how to translate a large number that might be entered by a user (up to 8 digits) into a binary value that spans two 16-bit words.
For instance, if a user enters a value of "12345678", I need that to be stored in two words as $BC $614E. (Note that the user could also enter a small value (even 0) as well)
I have gone down several paths trying to solve this, but haven't gotten there. All I have so far is the user input stored in sequential bytes which I can then convert to decimal values for processing (of course, I could just as easily store the user input as a sequence of bytes in the scratchpad):
char0 VAR Byte
char1 VAR Byte
.
.
.
char7 VAR Byte
SERIN 16,$54,[STR char0\8\CR] <--- if user enter <8 digits, char0(ix) will be set to 0. Can be used to determine number of chars entered by user.
{Convert char0-char7 from ASCII to decimal before processing}
{Move decimal values into 2 words: the part where I can't figure out what to do...}
Any ideas how to get these digits into two words? Thank you.
-Gian
Comments
Data entry and accumulation can be done in line with fewer variables and without first PUTting the data in scratchpad RAM.
Much more elegant than my clumsy solution, as usual. After we tried this in the field today, it was immediately apparent 8 characters were not going to be enough to cover the existing flow totals on some previously installed flow meters. For instance, a flow meter that's been in the field for some time has already accumulated 570 acre-feet of flow. Given the meter they're using, that translates to 399,323,920 counts! I've expanded the algorithm to accept up to 10 characters, with a warning that the maximum value that can be entered is 4,294,967,295. I tried the algorithm up to that 32 bit max value and it appears to work. In this case it doesn't make sense to allow for values greater than 32-bits since the rest of the BS2pe accumulation program is using a 32-bit value, as is the server we send it to.