CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Floating point problems w/PIC16F

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
fredmatic
Guest







Floating point problems w/PIC16F
PostPosted: Sat Jan 03, 2009 10:38 am     Reply with quote

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







PostPosted: Sat Jan 03, 2009 1:13 pm     Reply with quote

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
PostPosted: Tue Jan 06, 2009 2:38 pm     Reply with quote

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







PostPosted: Wed Jan 07, 2009 3:52 am     Reply with quote

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

View user's profile Send private message

Post subject: Floating point problems w/PIC16F
PostPosted: Wed Jan 07, 2009 7:37 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 07, 2009 9:44 am     Reply with quote

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!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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