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
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
Add more code...
- - - Updated - - -
a little modification on the function without using global variables and
optimization on channel selection
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/
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
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.
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...
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