|
|
View previous topic :: View next topic |
Author |
Message |
Hellowik
Joined: 04 Jun 2016 Posts: 3
|
Float assignation on PIC18F452 |
Posted: Sat Jun 04, 2016 10:19 am |
|
|
Hello
As i was working on a school project i came across something tricky.
I use PIC 18F452 and MPLAB V8.92 and of course CCS Compiler.
I was trying to use some basic math operations (multiplication) and the result given was completely absurd. I found out there was a problem with my float assignation. Here's how it happens: i declare a float variable, let's say toto, I initialize it to 0.0. I then enter my main.c, in which i give it the value of 20.3 for ex, but then, this gem appears
I tried to see if this was not just a byte order inversion or something like that, but I didn't find anything suspicious.
Have you guys any idea ? If you need any further informations or if this is not clear enough, don't hesitate.
Thank you guys ! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Sat Jun 04, 2016 11:29 am |
|
|
You have the float type set incorrectly.
MPLAB can display floats stored as Microchip float32, or as IEEE float32.
The former format was developed by Microchip before IEEE floats appeared to be the most efficient way to store float numbers in the PIC16/18. It is this format that CCS uses (for the PIC16/18).
20.299999 stores in Microchip float32 as 66662283hex.
66662283hex displays as IEEE float32 as 2.716952E23. This is what you are seeing.
Select the properties of your watch.
Select Format.
Change to MCHP float. |
|
|
Hellowik
Joined: 04 Jun 2016 Posts: 3
|
|
Posted: Sat Jun 04, 2016 3:38 pm |
|
|
Thanks a lot, we tried to change to MCHP float, but didn't tried the Low to High byte order, and that was the answer.
Thanks again |
|
|
Hellowik
Joined: 04 Jun 2016 Posts: 3
|
|
Posted: Tue Jun 07, 2016 3:05 am |
|
|
Hello again !
It seems the problem is not completly solved. Let me explain.
I am receiving 4 bits from an other card, which, by a union I convert into a float named CurrentSpeed The float has the value expected when displayed in IEEEFloat. However, when I try to myltiply that float by then the result is not what was expected. I tried showing it in the different MCHP float formats but it won't work.
Meanwhile, when I declare my variable toto, initialized to 0.0, declared as float too, the mutlplication works fine...
I feel the difference is that toto is showed as an MCHP FLoat and CurrentSpeed is an IEEE but does that mean you can't multiply IEEE floats ? That seems strange.
Thanks |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Tue Jun 07, 2016 4:02 am |
|
|
Hellowik wrote: |
It seems the problem is not completly solved.
|
Yes, the problem, as you described it, is completely solved: the debugger was interpreting the Microchip format 32 bit float as an IEEE754 float32. What you are now describing is a different, but related problem.
Quote: |
I am receiving 4 bits from an other card, which, by a union I convert into a float named CurrentSpeed The float has the value expected when displayed in IEEEFloat. However, when I try to myltiply that float by then the result is not what was expected. I tried showing it in the different MCHP float formats but it won't work.
|
No, it won't work. The float you are getting from your other device is most likely an IEEE754 float32. AS you now know, CCS C code cannot understand that format. You have to convert the IEEE float to the Microchip format. CCS provides code to do that: float f_IEEEtoPIC(int32 f) and the reverse (in case you need to send anything back in IEEE format), int32 f_PICtoIEEE(float f). You have to get the four bytes in the right order, which you can do in the union. These routines are in ieeefloat.c. |
|
|
|
|
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
|