View previous topic :: View next topic |
Author |
Message |
fredmatic Guest
|
Floating point problems w/PIC16F |
Posted: Sat Jan 03, 2009 10:38 am |
|
|
I wrote a small piece of code for the PIC16F690 to use it as a sweep frequency generator. I have a few lines of code using floating point, which at the end I re-cast the float into an integer for a timer.
Code: |
void main()
{
unsigned char count;
unsigned int icount;
long lcount,ltemp;
float32 fTemp1,fTemp2,fTemp,fTbase,fcount;
.
.
.
for(count=0;count<69;count++)
{
fTemp = 1/fcount;
fTbase = fTemp/.0000005;
fcount = fTbase / 2.0;
gTimeArray[count] = (unsigned int)fcount;
icount += 250;
} |
In simulation mode, under the MPLAB IDE, every time I hit the first floating point calculation, the PIC code jumps into the 16F690 header file. If I comment out the lines of floating point code, it works fine.
Has anyone run into this problem? Any suggestions for corrections?... or am I destined to do it all in integer?
thanx in advance..... mark |
|
|
Ttelmah Guest
|
|
Posted: Sat Jan 03, 2009 1:13 pm |
|
|
You are probably actually triggering a maths overflow, which isn't being handled.
fcount, contains a random value. Possibly zero. 1/fcount, then gives an invalid result.
What is fcount meant to hold?. Initialise it.
Initialise icount.
As a general comment, multiply by 2000000, rather than dividing by 0.0000005, and multply by 0.5, rather than dividing by 2. Division takes significantly more time than multiplication.
Best Wishes |
|
|
fredmatic Guest
|
Floating point problems w/PIC16F |
Posted: Tue Jan 06, 2009 2:38 pm |
|
|
I initialized the fcount to 1000, and the problem still exists. I step thru the program with the ICD2 and when I hit the first floating point line, the ICD2's next step is at the " #include <16F690.h> " line in the "main.h" hearder file the CCS compiler initially generated. So I have no idea what could be causing this. |
|
|
Ttelmah Guest
|
|
Posted: Wed Jan 07, 2009 3:52 am |
|
|
Try running it in something like the MPLAB simulator, rather than the ICD2. I'd suspect you are hitting an ICD problem of some sort, but 'prove it' first.
Best Wishes |
|
|
andyfraser
Joined: 04 May 2004 Posts: 47 Location: UK
|
Post subject: Floating point problems w/PIC16F |
Posted: Wed Jan 07, 2009 7:37 am |
|
|
Hi,
I have seen a similar thing when debugging with ICD2. When you single step over a compiler library function the debugger jumps to the corresponding include file. Try setting the breakpoint to the line after the one you want to stop on then look at the value in fTemp.
Andy |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Jan 07, 2009 9:44 am |
|
|
This is because you are executing a CCS library function which you don't have the code for so you can't step through it in C.
Switch to assembler mode at this point and you will see what I mean!
When you have stepped through the assembler and it returns back to your main code you can switch back to C.
If you continue to step through while in C mode it will still work and return you to the correct point but it will just look messy.
Or you can just but a break point after the routines and run it! |
|
|
|