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

Alternatives to floating point??

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








Alternatives to floating point??
PostPosted: Wed Nov 22, 2006 10:02 am     Reply with quote

Hi,

I'm trying to make a precision voltage measurement device that I can calibrate using a software "scale" factor. Ultimately, I'd like to take an A/D reading, apply the scale factor, and display the result on an LCD. To calibrate the scale factor, I plan to have a couple of pushbuttons that increment and decrement the value.

I've been playing around with some code, and while it works as expected, it is also clear that the floating point math is going to eat me alive in terms of available code space. I only have a small test program, yet I've used over 50% of the available ROM (PIC16F628).

Here is what I'm doing:

Code:


float fVscale = 1.0;
float fVolts = 12.0;
float fDisplayVolts;

fDisplayVolts = (fVolts * fVscale);
printf("Vport Voltage: %2.2f\r", fDisplayVolts);



Is there a way to do this using exclusively integer math, and then display the result in a pseudo float format? I'm vaguely aware of a technique like that, but have never needed it before.

Compiler is 3.249

Thanks,

Tony
Ttelmah
Guest







PostPosted: Wed Nov 22, 2006 10:34 am     Reply with quote

Yes.
The compiler supports an output format '%w', which outputs an integer, with a decimal point inserted a set number of 'places' up the value.
Ideally, the 'volts' would start out as an integer (read from the ADC).
So if (for instance), the ADC is running on a 0-5v input, and you wanted a 'volts' value, then you could have:
Code:

int16 ADC_IN;
int32 volts;
int32 scaledvolts;

ADC_IN=read_adc();
//At this point, 1024 counts would equal 5v (you can never actually get
//1024 counts, but this is the scaling from the ADC).
//Now we want to display 5.000 for 1024.
volts=((int32)ADC_IN*4883)/100;
//Key here is that the multiplication _must_ occur first, so that the value
//is a large integer and little is lost.
printf("%5.3w",volts);
//Now you have a value of '5000' for 5v. If you wanted to 'scale' this by
//10%, then again multiply first, so:
scaledvolts=(volts*11)/10;
printf("%5.3w",scaledvolts);
//will give 5.500 (for the impossible full scale value).


Best Wishes
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Nov 22, 2006 10:52 am     Reply with quote

Quote:

I've been playing around with some code, and while it works as expected, it is also clear that the floating point math is going to eat me alive in terms of available code space. I only have a small test program, yet I've used over 50% of the available ROM (PIC16F628).

This is a recurrent issue in this forum.

If Ttelmahīs proposal it isnīt enough to fit your code in your target processor,
you have an alternative packed in an 8 PIN device: uM-FPU from Micromega Corp.
It is a 32-bit floating point coprocessor that can be interfaced with microcontrollers
using I2C or SPI.
It can do most of the arithmetic operations either using float or long integers with the
capabilities of cross conversion between them.

http://www.micromegacorp.com/umfpu-v2.html
http://www.micromegacorp.com/picmicro.html

Humberto
Guest








PostPosted: Wed Nov 22, 2006 4:05 pm     Reply with quote

Ttelmah,

Thanks for the tip, that is exactly what I was looking for! A quick bit of coding, and it's working as I expected!

Humberto, yes I know similar things have been discussed before, but sometimes fishing through the archives for just the right solution can be a bit daunting!

Tony
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