August 6th, 2011
Include math.h for C32
I spent the past 3 hours debugging my code that was absolutely fine. So just a quick note for everybody who’s working on pic32′s, if you’re using any math functions you have to include math.h; it is not enough to include the peripheral library (plib.h). The compiler won’t complain but you will get garbage out of your math functions.
And if you’re wondering what I was doing with all that math is just computing the temperature for a thermistor using the Steinhart–Hart equation.



August 6th, 2011 at 12:33 pm
I believe the problem is that the compiler does not have built-in definition for “math.h” functions.
With GCC compiler, the compilation gives a warning such as:
“incompatible implicit declaration of built-in function XXX”
Without a prototype, the compiler assumes the unknown function returns an “int” and each parameter is an “int”, so if you write “a = sqrt(2)” without including “math.h” the compiler assumes “int sqrt(int)” instead of “float sqrt(float)”. Since the internal representation of “int” and “float” in the cpu registers are very different, you got garbage as output.
Increasing the warning level (and examining the warnings) is always a good idea; the compiler could have given a warning such as:
“implicit declaration of function XXX”
when forgetting to include function prototypes.
August 6th, 2011 at 12:40 pm
Its just that I though including plib.h(Peripheral Library) automatically includes everything else that comes with the compiler (including math.h) which is a wrong assumption. Nevertheless the compiler(C32) gave no warning on this one.