View Full Version : Pulling out digits from a value...?

02-08-2007, 02:48 AM
I'm working on a program where I need the value of each digit of a variable. Basically the VAR DIG(X) COMMAND in the BS2. I looked around in the manual and I couldnt find anything that caught my eye. I need to get the value of each digit so I can output the value to a 7-segment display. I'm currently using the max7219 and I can communicate with it just fine, but my problem is breaking down the data I need to display.

And another question I have is can you declare variables at the bit level? as far as I can see you can just break the memory up down to the byte level. So lets say I wanted to make a boolean flag or whatnot, do you need to make flag a byte? or can you just declare it as a bit, and if so how?

David B
02-08-2007, 03:42 AM
If it's possible, it might be easier to store your variable in the form of the several individual digits as needed by the display.

Sometimes it's easier to update the many individual values, in order to have a simple display method, rather than to maintain a single variable that's easy to update but can be time-consuming or difficult to break down into the parts for display.

Mike Green
02-08-2007, 03:52 AM
The Propeller has a very rich set of bit-wise Boolean operators that let you do all kinds of things, but no direct support of storage less than a byte. The main reason is that the Stamps are so limited in storage that direct bit access is very important, but the Propeller has a relatively huge data storage space and that wasn't necessary. It's easier to just use a byte for a Boolean flag with zero for false and non-zero for true.

To access the digits of a value, just use multiplication (*) and modulus (//) by powers of 10. To get digits 3 through 0 of v, use "(v / 1000) // 10", "(v / 100) // 10", "(v / 10) // 10", and "v // 10". Clearly, with 32 bits available, you can extend this to 10 digits at least. If you want to have something more like the DIG operator, you can set up a table of long constants in a DAT section with the powers of ten and access this like "(v / table[ i]) // 10". The table would look like:

table long 1, 10, 100, 1000, 10_000, 100_000, 1_000_000, 10_000_000
long 100_000_000, 1_000_000_000

You could even declare a DIG function like:

PRI DIG(a,b)
return (a / table[ i]) // 10

Post Edited (Mike Green) : 2/7/2007 8:56:31 PM GMT

02-08-2007, 05:17 AM
Mike, Thanks! that was exactally what I was looking for. Now I'll put our *ahhem* your knowledge to good use!

Mike Green
02-08-2007, 05:22 AM
That's the wonderful thing about knowledge. I have it, then I give it to you, and we both have it! (And we can both give it to others!)