I'm working on a PIC project, and my brain has almost exploded! I'm hoping that by writing down my problem, that the answer will come to me, but please feel free to help!

Background (mostly irrelevant to an outsider): I'm storing a number in a variable (say r, an unsigned integer) which represents the ratio of a pulley system. The ratio itself is a real number and unlikely to be an integer, but I need to store it as an integer for storage reasons (not relevant to this discussion). When used, the integer will be divided by a constant (probably 4000) and the result will be used via a cast to a float.

In order for the user to set the value of the integer (using select and increment buttons), I have to first display it using 3 single variables (d1, d10, d100) that represent each digit up to 1000, then the thousands (and possible tens of thousands) as another variable (d1000).

For example, r = 32768 is comprised of:

d1000 = 32
d100 = 7
d10 = 6
d1 = 8

Getting d1000 is easy, it's just r/1000. For d100 I considered (r-(1000*d1000))/100 which is OK, but it gets messy after that. I'm guessing there is a way of doing this with remainders, but my brain is too hot.

[Later] Well, it didn't work, I'm still stuck. Anyone care to help?

Nigel

PS I'm over 50, this is not homework!

The problem in your approach is that your mind is stuck to thinking from left to right. Try thinking from right to left.

That is, compute d1, then d10, then d100. You also don't need the digit you've just stored anymore, so you're free to drop them. To give you an example, let's compute d1.

``````d1 = r % 10 /* Thinking from right to left, we first store d1 */
r /= 10 /* Now we drop the last digit of r, as we don't need it anymore */

/* By the time we've got all of d1, d10 and d100, r will be equal to r / 1000 */``````

I hope this is what you were asking and that it solves your problem. Good luck!

Creeps got your back, Nigel. while(variable > 0), peel those bad digits right on off.

I think the most underrated operator in C is the "%", the modulo operator. It is so versatile and can be used for tasks as the ones you descirbe. Especially in embedded programming where you would like to work with integers.

Basically, given two positive numbers, x%n (x mod n) represents the remainder, on division of x by n. For instance, the expression "85 mod 10" would evaluate to 5 because after dividing 85 by 10 the subtraction of 80 from 85 leaves 5, while "80 mod 10" would evaluate to 0 because the division of 80 by 10 leaves no remainder.

Why this operator is not introduced in elementary level algebra, I have no idea. The first time I came across it was in my university level studies of C.

Thanks for all the help. I thought it would be good to share my code, even though C experts may scoff! Note that the number in question is referred to as a "function" and it is an unsigned int: rpmFunc

``````void setRfn(void)         // Set RPM/Tach Function, store to EEPROM
{
byte digit[5] ;         // array of bytes (unsigned short int) to hold 5 digits of function
unsigned int tempFn ;   // temporary store of function

rpmFunc = 12345 ;  // DEBUG for testing
tempFn = rpmFunc ;      // store function in temporary variable

for (i = 0; i < 5 ; i++)
{ // loop through function extracting digits to array, LSD first
digit[i] = tempFn % 10 ;
tempFn = tempFn / 10 ; // to C experts: I know I can shorten this, but this is more readable IMO
}

while (!SRFn_Ena)
{ // loop until SRDiv_Ena switch deactivated

for (i = 0; i < 5 ; i++)
{
digitLCD5XY(digit[4-i], 24+ i*6, 2) ; // display each digit on LCD
}

// insert button pressed code here

rpmFunc = 0 ; // button pressed, so rebuild the function from array
for (i = 0; i < 5 ; i++)
{ // loop through array building digits to rpmFunc
digitLCD5XY(digit[4-i], 24+ i*6, 2) ;
rpmFunc = 10*rpmFunc + digit[4-i] ;
}

...``````

Hmmm, got another problem now :(