I have made a project with a pic16f84 there i have a variable called TEMP and it can be 0 to 100 (it is a temperature). If the variable for example is 74, is it possible to split the numbers to 7 and 4 or how can i do to show the temperature on the LCD-Display??? I have made the program in CC5X C-Compiler
Just remember to set lsd and msd to 0 before testing for them by calling the seperate function, Id call it as a gosub so execution continues from where it's called from. Msd will contain the variables multiple of 10(example 9 = 90) and lsd the variables 0-9 component.
char Hund, Tens, Ones;
Hund = 0;
Tens = 0;
Ones = 0; //Reset our variables
if (Temp == 100) //if it is 100 then ...
Hund++; //increase Hund to 1
else
{ //and end... or, if it wasn't 100 then...
while (Temp > 9) //keep looping as long as Temp is bigger then 9
{
Temp = Temp - 10; //subtract 10 from temp ...
Tens++; //and add one to Tens
}
Ones = Temp; //when done the 'ones' remain in temp.
}
The contents of Temp are destroyed by this code...
Code like this is better then using multiplations and divisions because a pic is much better at simply adding and subtracting
it only works up too 100 like you said, bigger values will output incorrect results
Aye that's why I used variable as the main holder and temp is well a temporary storage. Temp will always be 0 at the end of a call to seperate
Nice code though, much more optimized then what I posted. Also I should have assigned the value to temp outside of the seperation loop, as it is atm any value over 10 will be stuck in a continous loop since temp is assigned on each call.