CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Separate Float Number to 4 Different Digits

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pcmgogo



Joined: 25 Dec 2004
Posts: 9

View user's profile Send private message

Separate Float Number to 4 Different Digits
PostPosted: Sun Nov 09, 2008 3:58 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Nov 09, 2008 4:15 pm     Reply with quote

Quote:
For example, my sensor read 23.6 C. 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.

See these topics on extracting digits:
http://www.ccsinfo.com/forum/viewtopic.php?t=18937
http://www.daniweb.com/forums/printthread.php?t=21658
Ttelmah
Guest







PostPosted: Mon Nov 10, 2008 3:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 14, 2008 10:07 am     Reply with quote

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;
...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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