|
|
View previous topic :: View next topic |
Author |
Message |
pcmgogo
Joined: 25 Dec 2004 Posts: 9
|
Separate Float Number to 4 Different Digits |
Posted: Sun Nov 09, 2008 3:58 pm |
|
|
hi all,
I am programming a digital clock and temperature display with seven segment displays.
I checked the forum for my problem but I could not find exact result from forum.
My circuit has DS18B20 sensor for reading temperature.
My variable is float and which name is temperature, here is function,
Code: | void write_temp()
{
float temperature;
int8 n1,n2,n3,n4;
temperature=ds1820_read();
... |
For example, my sensor read 23.6 C and give me a float number which is stored temperature for this result. I use it on LCD with printf function but how could I separate this value to n1,n2,n3,n4 variables as unsigned integer ? I want to get this value to n1=2, n2=3, n3=6, n4=0
then I will send this n1,n2,n3 and n4 to seven segment displays for displaying.
I am waiting your help. Thanks lot.
My compiler version is 4.030 and my PIC is Pic16F628A. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah Guest
|
|
Posted: Mon Nov 10, 2008 3:17 am |
|
|
As a further comment, avoid 'float', if you can!....
Floating point arithmetic, is quite bulky, and slow. In the example given, if you are only ever going to display one decimal place, and the number is only going to a four digit display (999.9 max), then it is much quicker, and smaller, to declare 'temperature' as a signed int16, and put the temperature value *10 into this (so it stores an integer number of tenths of a degree).
Even better (since you are reading this from a sensor. that does not return a 'float', but a simple binary count, in 0.5 degree steps), just multiply this by 5, and never get involved with 'float' at all.....
For the printf outputs, CCS, has a %w format, that allows you to treat an integer value as a 'scaled' number this way, which makes doing the 'printf' part easy.
Best Wishes |
|
|
pcmgogo
Joined: 25 Dec 2004 Posts: 9
|
|
Posted: Fri Nov 14, 2008 10:07 am |
|
|
Thanks for your interests. I solved my problem. I forgot (float) addition before multiplication in my code.
Code: | void write_temp()
{
signed int16 sic;
float temperature;
temperature=ds1820_read();
sic=[b](float)[/b]temperature*100;
...
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|