View previous topic :: View next topic |
Author |
Message |
soonc
Joined: 03 Dec 2013 Posts: 215
|
PIC24 and printf float |
Posted: Sat Jun 06, 2015 8:39 pm |
|
|
I've just started using the PIC24FJ128GA306 with Compiler V5.046.
Does not seem to handle float the same way as for PIC18
Am I forgetting to include some special library or what ?
Code: |
#include <24FJ128GA306.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOJTAG //JTAG disabled
#FUSES CKSFSM //Clock Switching is enabled, fail Safe clock monitor is enabled
#device ICD=TRUE
#device ICSP=2
#device adc=12
#use delay(crystal=20000000)
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#pin_select U1TX = PIN_F5
#pin_select U1RX = PIN_F4
#use rs232(baud=115200, xmit=PIN_F5, rcv=PIN_F4)
void main()
{
float fV;
fV = 3.123;
printf("Hello Float\r\n");
printf("float=%1.3f",fV);
while(1){;}
}
|
output:
Hello Float
f
I've also tried using sprintf() to a buffer and it still does not output the float value. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Sun Jun 07, 2015 12:56 am |
|
|
You are making a common mistake. It _should_ work, but in C, the number in front of the decimal in a printf statement, is the 'total field width' (how many characters to print), while the value after the decimal is the digits to follow the decimal. The total width includes the decimal, so to print 3.123, you actually need 5.3....
Now I say 'it should work', since the library is meant to automatically 'overflow' and add extra characters as needed. However I'd suspect that in the case of having a total width that is impossible (less than the decimal digits), the internal maths is failing to handle the overflow..... |
|
|
soonc
Joined: 03 Dec 2013 Posts: 215
|
Must be something else |
Posted: Sun Jun 07, 2015 6:29 am |
|
|
Ttelmah wrote: | You are making a common mistake. It _should_ work, but in C, the number in front of the decimal in a printf statement, is the 'total field width' (how many characters to print), while the value after the decimal is the digits to follow the decimal. The total width includes the decimal, so to print 3.123, you actually need 5.3....
Now I say 'it should work', since the library is meant to automatically 'overflow' and add extra characters as needed. However I'd suspect that in the case of having a total width that is impossible (less than the decimal digits), the internal maths is failing to handle the overflow..... |
Thanks for the heads up on formatting. !
However seems not to be the issue here.
I changed the code to:
printf("float=%5.3f",fV);
Same problem.
Using the PIC18 it works even with my bad formatting !
This should work also:
printf("float=%f",fV);
But is does not.
I reported this as a problem to CCS. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Jun 07, 2015 6:50 am |
|
|
What is this?
It's a bit unusual and it should be causing an error.
I bet the program is not stopping and "falling off the end"
Try while(1); to see if it makes any difference. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Sun Jun 07, 2015 7:14 am |
|
|
It could be falling off the end, however too many characters are involved. You only 'lose' what is in the FIFO, and I think this PIC has a 4 character FIFO. It looks like eleven characters are lost....
I'm starting to wonder if there is a problem with 5.046. I've not bothered to 'upgrade' to this, but a quick scan shows several threads in the last few days with people having odd problems. I'll try loading it (keeping it separate from my older versions), and see if it is the problem. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Sun Jun 07, 2015 7:45 am |
|
|
Try without DEBUG enabled.
It prints perfectly without this. I think it is trying to load the wrong DEBUG executive. It compiles OK, if I use MPLAB, and tell it to use DEBUG mode (which then uses the MPLAB debug code), or if I run in the CCS IDE, and select 'DEBUG' from here (which uses the CCS code). |
|
|
soonc
Joined: 03 Dec 2013 Posts: 215
|
That fixed it for now |
Posted: Sun Jun 07, 2015 8:13 am |
|
|
Ttelmah wrote: | Try without DEBUG enabled.
It prints perfectly without this. I think it is trying to load the wrong DEBUG executive. It compiles OK, if I use MPLAB, and tell it to use DEBUG mode (which then uses the MPLAB debug code), or if I run in the CCS IDE, and select 'DEBUG' from here (which uses the CCS code). |
Thanks that nailed it.
Not much help as I'll need the debugger when the real code gets loaded.
Look like your idea that it may be loading the wrong DEBUG exe may be the real problem.
I tried installing V5.04 and it did the same thing.
Not sure how to tell the IDE to use the correct DEBUG exe.
I did report the float problem to CCS but I'll update that bug report.
Thanks for all the help. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Sun Jun 07, 2015 1:56 pm |
|
|
What I did was got rid of the ICD=TRUE line.
Instead enable debugging from the IDE. |
|
|
soonc
Joined: 03 Dec 2013 Posts: 215
|
I tried that |
Posted: Sun Jun 07, 2015 2:15 pm |
|
|
Ttelmah wrote: | What I did was got rid of the ICD=TRUE line.
Instead enable debugging from the IDE. |
I compiled without the ICD=TRUE and then enabled the debugger which prompted me to allow a re-compile to generate debugging code.
That did not work. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Jun 07, 2015 4:20 pm |
|
|
I tried a few other things using 5.046 CCS IDE and ICDU64.
The below program works as expected as long as I comment out the #ICD=TRUE;
It prints Hello Float then float=3.123 and stops.
If I add the ICD line it has issues.
Code: | include <24FJ128GA306.h>
//#device ICD=TRUE
#device ICSP=2
#device adc=12
#use delay(crystal=20000000)
#pin_select U1TX = PIN_F5
#pin_select U1RX = PIN_F4
#use rs232(baud=115200, xmit=PIN_F5, rcv=PIN_F4)
void main()
{
float fV;
fV = 3.123;
printf("Hello Float\r\n");
printf("float=%1.3f",fV);
while(1);
}
|
_________________ Google and Forum Search are some of your best tools!!!! |
|
|
soonc
Joined: 03 Dec 2013 Posts: 215
|
Thanks |
Posted: Sun Jun 07, 2015 4:37 pm |
|
|
dyeatman wrote: | I tried a few other things using 5.046 CCS IDE and ICDU64.
The below program works as expected as long as I comment out the #ICD=TRUE;
It prints Hello Float then float=3.123 and stops.
If I add the ICD line it has issues. |
Yes as I mentioned earlier I programmed the chip without #ICD=TRUE in the code and programmed the chip and it worked.
Clearly there is something else wrong.
It's not practical to develop code without being able to debug it.
Tomorrow I hope CCS addresses the issue. I'm ready to start using the PIC24 and this kind of puts a damper on things !
Thanks for the help. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Mon Jun 08, 2015 12:55 am |
|
|
Use an old copy of MPLAB 8.92 (as a person who hates MPLAB-X..). Set it to compile for DEBUG (the default unless you turn it off). Compile.
It debugs merrily. |
|
|
soonc
Joined: 03 Dec 2013 Posts: 215
|
H'mm |
Posted: Mon Jun 08, 2015 5:23 am |
|
|
Ttelmah wrote: | Use an old copy of MPLAB 8.92 (as a person who hates MPLAB-X..). Set it to compile for DEBUG (the default unless you turn it off). Compile.
It debugs merrily. |
I don't want to move to MPLAB any version. I've tried it, and found it to be a disjointed mess, but thanks for the suggestion.
I also do not like the new UI in CCS V5 IDE.
At least I can still configure it for the old style menu scheme and keyboard control which is much more productive for me.
Thanks for all the help let's see what CCS does today. |
|
|
soonc
Joined: 03 Dec 2013 Posts: 215
|
The fix |
Posted: Mon Jun 08, 2015 8:31 am |
|
|
Called CCS tech. Support.
He suggested to try increasing the stack size.
#stack (256)
That fixed the problem. I can now use ICD=TRUE and debug.
Also in my original posting for this thread I said I was new to the PIC24
I asked if there was anything special or what am I doing wrong.
I was not aware there are different types of float, just thought I'd post my new found information here.
float // 32 bit original
float48 // 48 bit for PIC24
float64 // 64 bit for PIC24
Thanks for all the help. |
|
|
|