|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Alternatives to floating point?? |
Posted: Wed Nov 22, 2006 10:02 am |
|
|
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
|
|
Posted: Wed Nov 22, 2006 10:34 am |
|
|
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
|
|
Posted: Wed Nov 22, 2006 10:52 am |
|
|
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
|
|
Posted: Wed Nov 22, 2006 4:05 pm |
|
|
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 |
|
|
|
|
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
|