Continue to Site

# [PIC]How do I use Float in xc8 compilor ?

Status
Not open for further replies.

#### Ivan-Holm

##### Member level 5
the code works fine with out float to show the int number of the ADC, but when I want to use float it works for a little bit time, and then Crash the Program and the micro P stop.
the code print the voltage i Volt, with to decimal after. on a 4 x 20 ascii Display. and have hit the wall :bang: to come op what to do. even #include<float.h> the Pic is an Pic18f242
define
Code:
//*************Analog*****************
#define analog1 ADCON0 = 0x85;
#define analog2 ADCON0 = 0x8d;
#define analog3 ADCON0 = 0x95;
#define analog4 ADCON0 = 0x9d;
//*******************Global variabler***********
char lcdtext[16];
int line,L;
float volt;
int An;
float vdd10bit = 4.75/1023;

Code:
 //************ analog read ***
{
if(ch==1)
{
analog1;
}
else if(ch==2)
{
analog2;
}
else if(ch==3)
{
analog3;
}
else if(ch==4)
{
analog4;
}
// int An;

//         for (An=0; An<1; An++)
//         {
{
blaa_on;
}

return (float)volt;
}

There are no much to tell you from these code fragments here.
1) The vdd10bit should be declared with const modifier (probably optimizer will handle multiplication better).
2) function calculates a float and then converts it to int and return int

- - - Updated - - -

a little modification on the function without using global variables and
optimization on channel selection

Code:
float AnalogRead(short int ch){
const float vdd10bit = 4.75/1023;
float value = 0;
if(ch>0 && ch<=4){
ADCON0 = 0x85 + ((ch-1) << 3);
{
?????;
}

}
return value;
}

Ivan-Holm

### Ivan-Holm

Points: 2
the float data type is supported by the xc8 compiler you just declare a variable a float and the compiler will allocate storage for it etc
no need to #include <float.h> unless you want to determine the precision, maximum/minimum values, etc, see
https://www.cplusplus.com/reference/cfloat/

Ivan-Holm

### Ivan-Holm

Points: 2
what does the const do ? xenos

horace1: yes the float works also in practice, but after some time it stops completely and the display just just blink with the cursor, after some time.
If I do not use float , it works fine, but then I can not show the volt with decimal, so something happens, maybe some buffer that the the Pic use got can't be emty.

I will paste the code, but it has nothing whit to do with the conversion. and some comment is en danish

horace1: yes the float works also in practice, but after some time it stops completely and the display just just blink with the cursor, after some time.
If I do not use float , it works fine, but then I can not show the volt with decimal, so something happens, maybe some buffer that the the Pic use got can't be emty.
danish
float calculations take far longer than integer - in particular on devices without floating point coprocessors such as the PIC18 it will be orders of magnitude greater
you are probably getting some problems with the delays due to the longer calculation time

Ivan-Holm

### Ivan-Holm

Points: 2
The "const" means that the value of the variable does not change. If the value is visible during compilation, the compiler will use it directly in stead of loading it from some memory.

I asked for the code, because I had a suspicion that you call this function from within a tight time limited section (like an interrupt service routine) and that's why the processor time (after some point of execution) is not enough to handle all the requests and stops responding.

Althow you can use float arithmetic with no problem (except the significant processing time increment), you can use decimals to print, without float arithmentic.
e.g. use as a unity value the 1/100 of a volt. Then if the ADC is 202, it means that you have 2,02 volts.

Ivan-Holm

### Ivan-Holm

Points: 2
the Const helped it does not stop now. Thanks for the advice

also check that the printf (I think the implementation of ftoa) is buggy in the XC8 standard library (at least on some versions of XC), so try to implement your own float printing function...

I use the sprintf funktion not printf, I like to add it to an string array to chop the Ascii in to the display write loop.

It makes no real difference whether you use sprintf or printf or any other of the 'printf' family of functions as they all use the same core functions.
If it is working then that is good, but I will almost bet that you have used a constant format string with whichever function you are using . (In my experience I have only ever seen a couple of instances where people have used generated format string on the printf family of functions.) My issue with these functions is that they are big (in terms of their RAM and ROM memory footprint) and slow (at run-time the format string must be parsed each time) and that microcontrollers are generally memory limited and have a slow(ish) processor.
The printf family of functions all parse the format string and then select an appropriate function from a suite (that must all be in ROM so it can handle any valid format instruction) that will do the actual conversion to a string and then concatenating that with the other parts to generate the final string. As the format string is almost always constant, you can save a lot of time and memory by simply calling the appropriate function yourself and do the string concatenation yourself as well.
Just me on my soap-box!
Susan

Status
Not open for further replies.